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