The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 15 | */ |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 16 | |
| 17 | #include "SysUtil.h" |
| 18 | |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | #include <errno.h> |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 22 | #include <limits.h> |
Yabin Cui | a029c9a | 2016-02-18 11:32:10 -0800 | [diff] [blame] | 23 | #include <stdint.h> |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | #include <string> |
| 34 | #include <vector> |
Tianjie Xu | 7477814 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 35 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 36 | #include <android-base/file.h> |
| 37 | #include <android-base/logging.h> |
| 38 | #include <android-base/strings.h> |
| 39 | #include <android-base/unique_fd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | |
Elliott Hughes | f267dee | 2015-06-23 12:31:02 -0700 | [diff] [blame] | 41 | static bool sysMapFD(int fd, MemMapping* pMap) { |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 42 | CHECK(pMap != nullptr); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 43 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 44 | struct stat sb; |
| 45 | if (fstat(fd, &sb) == -1) { |
| 46 | PLOG(ERROR) << "fstat(" << fd << ") failed"; |
| 47 | return false; |
| 48 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 50 | void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 51 | if (memPtr == MAP_FAILED) { |
| 52 | PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed"; |
| 53 | return false; |
| 54 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 55 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 56 | pMap->addr = static_cast<unsigned char*>(memPtr); |
| 57 | pMap->length = sb.st_size; |
| 58 | pMap->ranges.push_back({ memPtr, static_cast<size_t>(sb.st_size) }); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 59 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 60 | return true; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 63 | // A "block map" which looks like this (from uncrypt/uncrypt.cpp): |
| 64 | // |
| 65 | // /dev/block/platform/msm_sdcc.1/by-name/userdata # block device |
| 66 | // 49652 4096 # file size in bytes, block size |
| 67 | // 3 # count of block ranges |
| 68 | // 1000 1008 # block range 0 |
| 69 | // 2100 2102 # ... block range 1 |
| 70 | // 30 33 # ... block range 2 |
| 71 | // |
| 72 | // Each block range represents a half-open interval; the line "30 33" |
| 73 | // reprents the blocks [30, 31, 32]. |
| 74 | static int sysMapBlockFile(const char* filename, MemMapping* pMap) { |
| 75 | CHECK(pMap != nullptr); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 76 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 77 | std::string content; |
| 78 | if (!android::base::ReadFileToString(filename, &content)) { |
| 79 | PLOG(ERROR) << "Failed to read " << filename; |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n"); |
| 84 | if (lines.size() < 4) { |
| 85 | LOG(ERROR) << "Block map file is too short: " << lines.size(); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | size_t size; |
| 90 | unsigned int blksize; |
| 91 | if (sscanf(lines[1].c_str(), "%zu %u", &size, &blksize) != 2) { |
| 92 | LOG(ERROR) << "Failed to parse file size and block size: " << lines[1]; |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | size_t range_count; |
| 97 | if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) { |
| 98 | LOG(ERROR) << "Failed to parse block map header: " << lines[2]; |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | size_t blocks; |
| 103 | if (blksize != 0) { |
| 104 | blocks = ((size - 1) / blksize) + 1; |
| 105 | } |
| 106 | if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 || |
| 107 | lines.size() != 3 + range_count) { |
| 108 | LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize |
| 109 | << ", range_count " << range_count << ", lines " << lines.size(); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | // Reserve enough contiguous address space for the whole file. |
| 114 | void* reserve = mmap64(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); |
| 115 | if (reserve == MAP_FAILED) { |
| 116 | PLOG(ERROR) << "failed to reserve address space"; |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | const std::string& block_dev = lines[0]; |
| 121 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY))); |
| 122 | if (fd == -1) { |
| 123 | PLOG(ERROR) << "failed to open block device " << block_dev; |
| 124 | munmap(reserve, blocks * blksize); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | pMap->ranges.resize(range_count); |
| 129 | |
| 130 | unsigned char* next = static_cast<unsigned char*>(reserve); |
| 131 | size_t remaining_size = blocks * blksize; |
| 132 | bool success = true; |
| 133 | for (size_t i = 0; i < range_count; ++i) { |
| 134 | const std::string& line = lines[i + 3]; |
| 135 | |
| 136 | size_t start, end; |
| 137 | if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) { |
| 138 | LOG(ERROR) << "failed to parse range " << i << " in block map: " << line; |
| 139 | success = false; |
| 140 | break; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 141 | } |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 142 | size_t length = (end - start) * blksize; |
| 143 | if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) { |
| 144 | LOG(ERROR) << "unexpected range in block map: " << start << " " << end; |
| 145 | success = false; |
| 146 | break; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 149 | void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, |
| 150 | static_cast<off64_t>(start) * blksize); |
| 151 | if (addr == MAP_FAILED) { |
| 152 | PLOG(ERROR) << "failed to map block " << i; |
| 153 | success = false; |
| 154 | break; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 155 | } |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 156 | pMap->ranges[i].addr = addr; |
| 157 | pMap->ranges[i].length = length; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 158 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 159 | next += length; |
| 160 | remaining_size -= length; |
| 161 | } |
| 162 | if (success && remaining_size != 0) { |
| 163 | LOG(ERROR) << "ranges in block map are invalid: remaining_size = " << remaining_size; |
| 164 | success = false; |
| 165 | } |
| 166 | if (!success) { |
| 167 | munmap(reserve, blocks * blksize); |
| 168 | return -1; |
| 169 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 170 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 171 | pMap->addr = static_cast<unsigned char*>(reserve); |
| 172 | pMap->length = size; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 173 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 174 | LOG(INFO) << "mmapped " << range_count << " ranges"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 175 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 176 | return 0; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 179 | int sysMapFile(const char* fn, MemMapping* pMap) { |
| 180 | if (fn == nullptr || pMap == nullptr) { |
| 181 | LOG(ERROR) << "Invalid argument(s)"; |
| 182 | return -1; |
| 183 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 184 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 185 | *pMap = {}; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 186 | |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 187 | if (fn[0] == '@') { |
| 188 | if (sysMapBlockFile(fn + 1, pMap) != 0) { |
| 189 | LOG(ERROR) << "Map of '" << fn << "' failed"; |
| 190 | return -1; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 191 | } |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 192 | } else { |
| 193 | // This is a regular file. |
| 194 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn, O_RDONLY))); |
| 195 | if (fd == -1) { |
| 196 | PLOG(ERROR) << "Unable to open '" << fn << "'"; |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | if (!sysMapFD(fd, pMap)) { |
| 201 | LOG(ERROR) << "Map of '" << fn << "' failed"; |
| 202 | return -1; |
| 203 | } |
| 204 | } |
| 205 | return 0; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 206 | } |
| 207 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 208 | /* |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 209 | * Release a memory mapping. |
| 210 | */ |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 211 | void sysReleaseMap(MemMapping* pMap) { |
| 212 | std::for_each(pMap->ranges.cbegin(), pMap->ranges.cend(), [](const MappedRange& range) { |
| 213 | if (munmap(range.addr, range.length) == -1) { |
| 214 | PLOG(ERROR) << "munmap(" << range.addr << ", " << range.length << ") failed"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 215 | } |
Tao Bao | 9c05a82 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 216 | }); |
| 217 | pMap->ranges.clear(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 218 | } |