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> |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
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> |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 27 | #include <limits> |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 28 | #include <string> |
| 29 | #include <vector> |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 30 | |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 31 | #include <android-base/file.h> |
| 32 | #include <android-base/logging.h> |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 33 | #include <android-base/properties.h> |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 34 | #include <android-base/strings.h> |
| 35 | #include <android-base/unique_fd.h> |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 36 | #include <cutils/android_reboot.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 38 | BlockMapData BlockMapData::ParseBlockMapFile(const std::string& block_map_path) { |
| 39 | std::string content; |
| 40 | if (!android::base::ReadFileToString(block_map_path, &content)) { |
Tianjie Xu | e521861 | 2019-06-25 13:59:39 -0700 | [diff] [blame] | 41 | PLOG(ERROR) << "Failed to read " << block_map_path; |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 42 | return {}; |
| 43 | } |
| 44 | |
| 45 | std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n"); |
| 46 | if (lines.size() < 4) { |
| 47 | LOG(ERROR) << "Block map file is too short: " << lines.size(); |
| 48 | return {}; |
| 49 | } |
| 50 | |
| 51 | const std::string& block_dev = lines[0]; |
| 52 | |
| 53 | uint64_t file_size; |
| 54 | uint32_t blksize; |
| 55 | if (sscanf(lines[1].c_str(), "%" SCNu64 "%" SCNu32, &file_size, &blksize) != 2) { |
| 56 | LOG(ERROR) << "Failed to parse file size and block size: " << lines[1]; |
| 57 | return {}; |
| 58 | } |
| 59 | |
| 60 | if (file_size == 0 || blksize == 0) { |
| 61 | LOG(ERROR) << "Invalid size in block map file: size " << file_size << ", blksize " << blksize; |
| 62 | return {}; |
| 63 | } |
| 64 | |
| 65 | size_t range_count; |
| 66 | if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) { |
| 67 | LOG(ERROR) << "Failed to parse block map header: " << lines[2]; |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | uint64_t blocks = ((file_size - 1) / blksize) + 1; |
| 72 | if (blocks > std::numeric_limits<uint32_t>::max() || range_count == 0 || |
| 73 | lines.size() != 3 + range_count) { |
| 74 | LOG(ERROR) << "Invalid data in block map file: size " << file_size << ", blksize " << blksize |
| 75 | << ", range_count " << range_count << ", lines " << lines.size(); |
| 76 | return {}; |
| 77 | } |
| 78 | |
| 79 | RangeSet ranges; |
| 80 | uint64_t remaining_blocks = blocks; |
| 81 | for (size_t i = 0; i < range_count; ++i) { |
| 82 | const std::string& line = lines[i + 3]; |
| 83 | uint64_t start, end; |
| 84 | if (sscanf(line.c_str(), "%" SCNu64 "%" SCNu64, &start, &end) != 2) { |
| 85 | LOG(ERROR) << "failed to parse range " << i << ": " << line; |
| 86 | return {}; |
| 87 | } |
| 88 | uint64_t range_blocks = end - start; |
| 89 | if (end <= start || range_blocks > remaining_blocks) { |
| 90 | LOG(ERROR) << "Invalid range: " << start << " " << end; |
| 91 | return {}; |
| 92 | } |
| 93 | ranges.PushBack({ start, end }); |
| 94 | remaining_blocks -= range_blocks; |
| 95 | } |
| 96 | |
xunchang | 311e6ca | 2019-03-22 08:54:35 -0700 | [diff] [blame] | 97 | if (remaining_blocks != 0) { |
| 98 | LOG(ERROR) << "Invalid ranges: remaining blocks " << remaining_blocks; |
| 99 | return {}; |
| 100 | } |
| 101 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 102 | return BlockMapData(block_dev, file_size, blksize, std::move(ranges)); |
| 103 | } |
| 104 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 105 | bool MemMapping::MapFD(int fd) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 106 | struct stat sb; |
| 107 | if (fstat(fd, &sb) == -1) { |
| 108 | PLOG(ERROR) << "fstat(" << fd << ") failed"; |
| 109 | return false; |
| 110 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 111 | |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 112 | void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 113 | if (memPtr == MAP_FAILED) { |
| 114 | PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed"; |
| 115 | return false; |
| 116 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 117 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 118 | addr = static_cast<unsigned char*>(memPtr); |
| 119 | length = sb.st_size; |
| 120 | ranges_.clear(); |
| 121 | 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] | 122 | |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 123 | return true; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 126 | bool MemMapping::MapBlockFile(const std::string& filename) { |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 127 | auto block_map_data = BlockMapData::ParseBlockMapFile(filename); |
| 128 | if (!block_map_data) { |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 129 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 130 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 131 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 132 | if (block_map_data.file_size() > std::numeric_limits<size_t>::max()) { |
| 133 | LOG(ERROR) << "File size is too large for mmap " << block_map_data.file_size(); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 134 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 135 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 136 | |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 137 | // Reserve enough contiguous address space for the whole file. |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 138 | uint32_t blksize = block_map_data.block_size(); |
| 139 | uint64_t blocks = ((block_map_data.file_size() - 1) / blksize) + 1; |
Tao Bao | c13d2ec | 2017-10-10 10:56:09 -0700 | [diff] [blame] | 140 | 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] | 141 | if (reserve == MAP_FAILED) { |
| 142 | PLOG(ERROR) << "failed to reserve address space"; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 143 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 144 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 145 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 146 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_map_data.path().c_str(), O_RDONLY))); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 147 | if (fd == -1) { |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 148 | PLOG(ERROR) << "failed to open block device " << block_map_data.path(); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 149 | munmap(reserve, blocks * blksize); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 150 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 151 | } |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 152 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 153 | ranges_.clear(); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 154 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 155 | auto next = static_cast<unsigned char*>(reserve); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 156 | size_t remaining_size = blocks * blksize; |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 157 | for (const auto& [start, end] : block_map_data.block_ranges()) { |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 158 | size_t range_size = (end - start) * blksize; |
Tao Bao | c13d2ec | 2017-10-10 10:56:09 -0700 | [diff] [blame] | 159 | void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, |
| 160 | static_cast<off_t>(start) * blksize); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 161 | if (range_start == MAP_FAILED) { |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 162 | PLOG(ERROR) << "failed to map range " << start << ": " << end; |
| 163 | munmap(reserve, blocks * blksize); |
| 164 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 165 | } |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 166 | ranges_.emplace_back(MappedRange{ range_start, range_size }); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 167 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 168 | next += range_size; |
| 169 | remaining_size -= range_size; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 170 | } |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 171 | if (remaining_size != 0) { |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 172 | LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 173 | munmap(reserve, blocks * blksize); |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 174 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 177 | addr = static_cast<unsigned char*>(reserve); |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 178 | length = block_map_data.file_size(); |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 179 | |
xunchang | 625c588 | 2019-03-22 09:33:49 -0700 | [diff] [blame] | 180 | LOG(INFO) << "mmapped " << block_map_data.block_ranges().size() << " ranges"; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 181 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 182 | return true; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 185 | bool MemMapping::MapFile(const std::string& fn) { |
| 186 | if (fn.empty()) { |
| 187 | LOG(ERROR) << "Empty filename"; |
| 188 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 191 | if (fn[0] == '@') { |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 192 | // Block map file "@/cache/recovery/block.map". |
| 193 | if (!MapBlockFile(fn.substr(1))) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 194 | LOG(ERROR) << "Map of '" << fn << "' failed"; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 195 | return false; |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 196 | } |
| 197 | } else { |
| 198 | // This is a regular file. |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 199 | 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] | 200 | if (fd == -1) { |
| 201 | PLOG(ERROR) << "Unable to open '" << fn << "'"; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 202 | return false; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 205 | if (!MapFD(fd)) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 206 | LOG(ERROR) << "Map of '" << fn << "' failed"; |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 207 | return false; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 208 | } |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 209 | } |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 210 | return true; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 213 | MemMapping::~MemMapping() { |
| 214 | for (const auto& range : ranges_) { |
Tao Bao | c3292f3 | 2016-11-04 10:52:13 -0700 | [diff] [blame] | 215 | if (munmap(range.addr, range.length) == -1) { |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 216 | PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 217 | } |
Tao Bao | b656a15 | 2017-04-18 23:54:29 -0700 | [diff] [blame] | 218 | }; |
| 219 | ranges_.clear(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 220 | } |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 221 | |
Tianjie Xu | 00c4aba | 2020-03-13 14:25:02 -0700 | [diff] [blame^] | 222 | void Reboot(std::string_view target) { |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 223 | std::string cmd = "reboot," + std::string(target); |
| 224 | // Honor the quiescent mode if applicable. |
| 225 | if (target != "bootloader" && target != "fastboot" && |
| 226 | android::base::GetBoolProperty("ro.boot.quiescent", false)) { |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 227 | cmd += ",quiescent"; |
| 228 | } |
Tianjie Xu | 00c4aba | 2020-03-13 14:25:02 -0700 | [diff] [blame^] | 229 | if (!android::base::SetProperty(ANDROID_RB_PROPERTY, cmd)) { |
| 230 | LOG(FATAL) << "Reboot failed"; |
| 231 | } |
| 232 | |
| 233 | while (true) pause(); |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 234 | } |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 235 | |
Mark Salyzyn | 488cc05 | 2019-05-20 10:36:16 -0700 | [diff] [blame] | 236 | bool Shutdown(std::string_view target) { |
| 237 | std::string cmd = "shutdown," + std::string(target); |
| 238 | return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd); |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 241 | std::vector<char*> StringVectorToNullTerminatedArray(const std::vector<std::string>& args) { |
| 242 | std::vector<char*> result(args.size()); |
| 243 | std::transform(args.cbegin(), args.cend(), result.begin(), |
| 244 | [](const std::string& arg) { return const_cast<char*>(arg.c_str()); }); |
| 245 | result.push_back(nullptr); |
| 246 | return result; |
| 247 | } |