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