blob: 8366fa0ac5da1e326ff00579c4a95b4942b1ea05 [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)) {
41 LOG(ERROR) << "Failed to read " << block_map_path;
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
97 return BlockMapData(block_dev, file_size, blksize, std::move(ranges));
98}
99
Tao Baob656a152017-04-18 23:54:29 -0700100bool MemMapping::MapFD(int fd) {
Tao Baoc3292f32016-11-04 10:52:13 -0700101 struct stat sb;
102 if (fstat(fd, &sb) == -1) {
103 PLOG(ERROR) << "fstat(" << fd << ") failed";
104 return false;
105 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800106
Tao Baoc3292f32016-11-04 10:52:13 -0700107 void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
108 if (memPtr == MAP_FAILED) {
109 PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed";
110 return false;
111 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112
Tao Baob656a152017-04-18 23:54:29 -0700113 addr = static_cast<unsigned char*>(memPtr);
114 length = sb.st_size;
115 ranges_.clear();
116 ranges_.emplace_back(MappedRange{ memPtr, static_cast<size_t>(sb.st_size) });
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117
Tao Baoc3292f32016-11-04 10:52:13 -0700118 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800119}
120
Tao Baob656a152017-04-18 23:54:29 -0700121bool MemMapping::MapBlockFile(const std::string& filename) {
xunchang625c5882019-03-22 09:33:49 -0700122 auto block_map_data = BlockMapData::ParseBlockMapFile(filename);
123 if (!block_map_data) {
Tao Baob656a152017-04-18 23:54:29 -0700124 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700125 }
Doug Zongker99916f02014-01-13 14:16:58 -0800126
xunchang625c5882019-03-22 09:33:49 -0700127 if (block_map_data.file_size() > std::numeric_limits<size_t>::max()) {
128 LOG(ERROR) << "File size is too large for mmap " << block_map_data.file_size();
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
Tao Baoc3292f32016-11-04 10:52:13 -0700132 // Reserve enough contiguous address space for the whole file.
xunchang625c5882019-03-22 09:33:49 -0700133 uint32_t blksize = block_map_data.block_size();
134 uint64_t blocks = ((block_map_data.file_size() - 1) / blksize) + 1;
Tao Baoc13d2ec2017-10-10 10:56:09 -0700135 void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
Tao Baoc3292f32016-11-04 10:52:13 -0700136 if (reserve == MAP_FAILED) {
137 PLOG(ERROR) << "failed to reserve address space";
Tao Baob656a152017-04-18 23:54:29 -0700138 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700139 }
Doug Zongker99916f02014-01-13 14:16:58 -0800140
xunchang625c5882019-03-22 09:33:49 -0700141 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_map_data.path().c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700142 if (fd == -1) {
xunchang625c5882019-03-22 09:33:49 -0700143 PLOG(ERROR) << "failed to open block device " << block_map_data.path();
Tao Baoc3292f32016-11-04 10:52:13 -0700144 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700145 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700146 }
Doug Zongker99916f02014-01-13 14:16:58 -0800147
Tao Baob656a152017-04-18 23:54:29 -0700148 ranges_.clear();
Tao Baoc3292f32016-11-04 10:52:13 -0700149
xunchang625c5882019-03-22 09:33:49 -0700150 auto next = static_cast<unsigned char*>(reserve);
Tao Baoc3292f32016-11-04 10:52:13 -0700151 size_t remaining_size = blocks * blksize;
xunchang625c5882019-03-22 09:33:49 -0700152 for (const auto& [start, end] : block_map_data.block_ranges()) {
Tao Baob656a152017-04-18 23:54:29 -0700153 size_t range_size = (end - start) * blksize;
Tao Baoc13d2ec2017-10-10 10:56:09 -0700154 void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
155 static_cast<off_t>(start) * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700156 if (range_start == MAP_FAILED) {
xunchang625c5882019-03-22 09:33:49 -0700157 PLOG(ERROR) << "failed to map range " << start << ": " << end;
158 munmap(reserve, blocks * blksize);
159 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700160 }
Tao Baob656a152017-04-18 23:54:29 -0700161 ranges_.emplace_back(MappedRange{ range_start, range_size });
Tao Baoc3292f32016-11-04 10:52:13 -0700162
Tao Baob656a152017-04-18 23:54:29 -0700163 next += range_size;
164 remaining_size -= range_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700165 }
xunchang625c5882019-03-22 09:33:49 -0700166 if (remaining_size != 0) {
Tao Baob656a152017-04-18 23:54:29 -0700167 LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700168 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700169 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700170 }
171
Tao Baob656a152017-04-18 23:54:29 -0700172 addr = static_cast<unsigned char*>(reserve);
xunchang625c5882019-03-22 09:33:49 -0700173 length = block_map_data.file_size();
Tao Baoc3292f32016-11-04 10:52:13 -0700174
xunchang625c5882019-03-22 09:33:49 -0700175 LOG(INFO) << "mmapped " << block_map_data.block_ranges().size() << " ranges";
Tao Baoc3292f32016-11-04 10:52:13 -0700176
Tao Baob656a152017-04-18 23:54:29 -0700177 return true;
Tao Baoc3292f32016-11-04 10:52:13 -0700178}
179
Tao Baob656a152017-04-18 23:54:29 -0700180bool MemMapping::MapFile(const std::string& fn) {
181 if (fn.empty()) {
182 LOG(ERROR) << "Empty filename";
183 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700184 }
185
Tao Baoc3292f32016-11-04 10:52:13 -0700186 if (fn[0] == '@') {
Tao Baob656a152017-04-18 23:54:29 -0700187 // Block map file "@/cache/recovery/block.map".
188 if (!MapBlockFile(fn.substr(1))) {
Tao Baoc3292f32016-11-04 10:52:13 -0700189 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700190 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700191 }
192 } else {
193 // This is a regular file.
Tao Baob656a152017-04-18 23:54:29 -0700194 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700195 if (fd == -1) {
196 PLOG(ERROR) << "Unable to open '" << fn << "'";
Tao Baob656a152017-04-18 23:54:29 -0700197 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800198 }
199
Tao Baob656a152017-04-18 23:54:29 -0700200 if (!MapFD(fd)) {
Tao Baoc3292f32016-11-04 10:52:13 -0700201 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700202 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800203 }
Tao Baoc3292f32016-11-04 10:52:13 -0700204 }
Tao Baob656a152017-04-18 23:54:29 -0700205 return true;
Doug Zongker99916f02014-01-13 14:16:58 -0800206}
207
Tao Baob656a152017-04-18 23:54:29 -0700208MemMapping::~MemMapping() {
209 for (const auto& range : ranges_) {
Tao Baoc3292f32016-11-04 10:52:13 -0700210 if (munmap(range.addr, range.length) == -1) {
Tao Baob656a152017-04-18 23:54:29 -0700211 PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212 }
Tao Baob656a152017-04-18 23:54:29 -0700213 };
214 ranges_.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215}
Tao Bao2c526392018-05-03 23:01:13 -0700216
217bool reboot(const std::string& command) {
218 std::string cmd = command;
219 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
220 cmd += ",quiescent";
221 }
222 return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd);
223}
Tao Bao1700cc42018-07-16 22:09:59 -0700224
225std::vector<char*> StringVectorToNullTerminatedArray(const std::vector<std::string>& args) {
226 std::vector<char*> result(args.size());
227 std::transform(args.cbegin(), args.cend(), result.begin(),
228 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
229 result.push_back(nullptr);
230 return result;
231}