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