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