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