blob: b3ead973687b6d5c93d37462c0ea6743d4cc4780 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 *
Tao Baoc3292f32016-11-04 10:52:13 -07004 * 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 Projectc24a8e62009-03-03 19:28:42 -080015 */
Tao Baoc3292f32016-11-04 10:52:13 -070016
Tao Bao17054c02018-05-03 22:41:23 -070017#include "otautil/sysutil.h"
Tao Baoc3292f32016-11-04 10:52:13 -070018
Tao Baob9bffdf2017-10-11 12:14:38 -070019#include <errno.h> // TEMP_FAILURE_RETRY
Doug Zongker99916f02014-01-13 14:16:58 -080020#include <fcntl.h>
xunchang625c5882019-03-22 09:33:49 -070021#include <inttypes.h>
Elliott Hughesf267dee2015-06-23 12:31:02 -070022#include <sys/mman.h>
23#include <sys/stat.h>
24#include <sys/types.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025
Tao Bao1700cc42018-07-16 22:09:59 -070026#include <algorithm>
xunchang625c5882019-03-22 09:33:49 -070027#include <limits>
Tao Baoc3292f32016-11-04 10:52:13 -070028#include <string>
29#include <vector>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070030
Tao Baoc3292f32016-11-04 10:52:13 -070031#include <android-base/file.h>
32#include <android-base/logging.h>
Tao Bao2c526392018-05-03 23:01:13 -070033#include <android-base/properties.h>
Tao Baoc3292f32016-11-04 10:52:13 -070034#include <android-base/strings.h>
35#include <android-base/unique_fd.h>
Tao Bao2c526392018-05-03 23:01:13 -070036#include <cutils/android_reboot.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
xunchang625c5882019-03-22 09:33:49 -070038BlockMapData BlockMapData::ParseBlockMapFile(const std::string& block_map_path) {
39 std::string content;
40 if (!android::base::ReadFileToString(block_map_path, &content)) {
Tianjie Xue5218612019-06-25 13:59:39 -070041 PLOG(ERROR) << "Failed to read " << block_map_path;
xunchang625c5882019-03-22 09:33:49 -070042 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
xunchang311e6ca2019-03-22 08:54:35 -070097 if (remaining_blocks != 0) {
98 LOG(ERROR) << "Invalid ranges: remaining blocks " << remaining_blocks;
99 return {};
100 }
101
xunchang625c5882019-03-22 09:33:49 -0700102 return BlockMapData(block_dev, file_size, blksize, std::move(ranges));
103}
104
Tao Baob656a152017-04-18 23:54:29 -0700105bool MemMapping::MapFD(int fd) {
Tao Baoc3292f32016-11-04 10:52:13 -0700106 struct stat sb;
107 if (fstat(fd, &sb) == -1) {
108 PLOG(ERROR) << "fstat(" << fd << ") failed";
109 return false;
110 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800111
Tao Baoc3292f32016-11-04 10:52:13 -0700112 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 Projectc24a8e62009-03-03 19:28:42 -0800117
Tao Baob656a152017-04-18 23:54:29 -0700118 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 Projectc24a8e62009-03-03 19:28:42 -0800122
Tao Baoc3292f32016-11-04 10:52:13 -0700123 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124}
125
Tao Baob656a152017-04-18 23:54:29 -0700126bool MemMapping::MapBlockFile(const std::string& filename) {
xunchang625c5882019-03-22 09:33:49 -0700127 auto block_map_data = BlockMapData::ParseBlockMapFile(filename);
128 if (!block_map_data) {
Tao Baob656a152017-04-18 23:54:29 -0700129 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700130 }
Doug Zongker99916f02014-01-13 14:16:58 -0800131
xunchang625c5882019-03-22 09:33:49 -0700132 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 Baob656a152017-04-18 23:54:29 -0700134 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700135 }
Doug Zongker99916f02014-01-13 14:16:58 -0800136
Tao Baoc3292f32016-11-04 10:52:13 -0700137 // Reserve enough contiguous address space for the whole file.
xunchang625c5882019-03-22 09:33:49 -0700138 uint32_t blksize = block_map_data.block_size();
139 uint64_t blocks = ((block_map_data.file_size() - 1) / blksize) + 1;
Tao Baoc13d2ec2017-10-10 10:56:09 -0700140 void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
Tao Baoc3292f32016-11-04 10:52:13 -0700141 if (reserve == MAP_FAILED) {
142 PLOG(ERROR) << "failed to reserve address space";
Tao Baob656a152017-04-18 23:54:29 -0700143 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700144 }
Doug Zongker99916f02014-01-13 14:16:58 -0800145
xunchang625c5882019-03-22 09:33:49 -0700146 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_map_data.path().c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700147 if (fd == -1) {
xunchang625c5882019-03-22 09:33:49 -0700148 PLOG(ERROR) << "failed to open block device " << block_map_data.path();
Tao Baoc3292f32016-11-04 10:52:13 -0700149 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700150 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700151 }
Doug Zongker99916f02014-01-13 14:16:58 -0800152
Tao Baob656a152017-04-18 23:54:29 -0700153 ranges_.clear();
Tao Baoc3292f32016-11-04 10:52:13 -0700154
xunchang625c5882019-03-22 09:33:49 -0700155 auto next = static_cast<unsigned char*>(reserve);
Tao Baoc3292f32016-11-04 10:52:13 -0700156 size_t remaining_size = blocks * blksize;
xunchang625c5882019-03-22 09:33:49 -0700157 for (const auto& [start, end] : block_map_data.block_ranges()) {
Tao Baob656a152017-04-18 23:54:29 -0700158 size_t range_size = (end - start) * blksize;
Tao Baoc13d2ec2017-10-10 10:56:09 -0700159 void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
160 static_cast<off_t>(start) * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700161 if (range_start == MAP_FAILED) {
xunchang625c5882019-03-22 09:33:49 -0700162 PLOG(ERROR) << "failed to map range " << start << ": " << end;
163 munmap(reserve, blocks * blksize);
164 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700165 }
Tao Baob656a152017-04-18 23:54:29 -0700166 ranges_.emplace_back(MappedRange{ range_start, range_size });
Tao Baoc3292f32016-11-04 10:52:13 -0700167
Tao Baob656a152017-04-18 23:54:29 -0700168 next += range_size;
169 remaining_size -= range_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700170 }
xunchang625c5882019-03-22 09:33:49 -0700171 if (remaining_size != 0) {
Tao Baob656a152017-04-18 23:54:29 -0700172 LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700173 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700174 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700175 }
176
Tao Baob656a152017-04-18 23:54:29 -0700177 addr = static_cast<unsigned char*>(reserve);
xunchang625c5882019-03-22 09:33:49 -0700178 length = block_map_data.file_size();
Tao Baoc3292f32016-11-04 10:52:13 -0700179
xunchang625c5882019-03-22 09:33:49 -0700180 LOG(INFO) << "mmapped " << block_map_data.block_ranges().size() << " ranges";
Tao Baoc3292f32016-11-04 10:52:13 -0700181
Tao Baob656a152017-04-18 23:54:29 -0700182 return true;
Tao Baoc3292f32016-11-04 10:52:13 -0700183}
184
Tao Baob656a152017-04-18 23:54:29 -0700185bool MemMapping::MapFile(const std::string& fn) {
186 if (fn.empty()) {
187 LOG(ERROR) << "Empty filename";
188 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700189 }
190
Tao Baoc3292f32016-11-04 10:52:13 -0700191 if (fn[0] == '@') {
Tao Baob656a152017-04-18 23:54:29 -0700192 // Block map file "@/cache/recovery/block.map".
193 if (!MapBlockFile(fn.substr(1))) {
Tao Baoc3292f32016-11-04 10:52:13 -0700194 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700195 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700196 }
197 } else {
198 // This is a regular file.
Tao Baob656a152017-04-18 23:54:29 -0700199 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700200 if (fd == -1) {
201 PLOG(ERROR) << "Unable to open '" << fn << "'";
Tao Baob656a152017-04-18 23:54:29 -0700202 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800203 }
204
Tao Baob656a152017-04-18 23:54:29 -0700205 if (!MapFD(fd)) {
Tao Baoc3292f32016-11-04 10:52:13 -0700206 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700207 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800208 }
Tao Baoc3292f32016-11-04 10:52:13 -0700209 }
Tao Baob656a152017-04-18 23:54:29 -0700210 return true;
Doug Zongker99916f02014-01-13 14:16:58 -0800211}
212
Tao Baob656a152017-04-18 23:54:29 -0700213MemMapping::~MemMapping() {
214 for (const auto& range : ranges_) {
Tao Baoc3292f32016-11-04 10:52:13 -0700215 if (munmap(range.addr, range.length) == -1) {
Tao Baob656a152017-04-18 23:54:29 -0700216 PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 }
Tao Baob656a152017-04-18 23:54:29 -0700218 };
219 ranges_.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220}
Tao Bao2c526392018-05-03 23:01:13 -0700221
Tianjie Xu00c4aba2020-03-13 14:25:02 -0700222void Reboot(std::string_view target) {
Tao Bao782dcc12019-04-29 11:23:16 -0700223 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 Bao2c526392018-05-03 23:01:13 -0700227 cmd += ",quiescent";
228 }
Tianjie Xu00c4aba2020-03-13 14:25:02 -0700229 if (!android::base::SetProperty(ANDROID_RB_PROPERTY, cmd)) {
230 LOG(FATAL) << "Reboot failed";
231 }
232
233 while (true) pause();
Tao Bao2c526392018-05-03 23:01:13 -0700234}
Tao Bao1700cc42018-07-16 22:09:59 -0700235
Mark Salyzyn488cc052019-05-20 10:36:16 -0700236bool Shutdown(std::string_view target) {
237 std::string cmd = "shutdown," + std::string(target);
238 return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd);
Tao Bao782dcc12019-04-29 11:23:16 -0700239}
240
Tao Bao1700cc42018-07-16 22:09:59 -0700241std::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}