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