blob: d8969a0bb9d07e542024aacb6ce5348ad464b145 [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>
Tao Baob656a152017-04-18 23:54:29 -070021#include <stdint.h> // SIZE_MAX
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>
Tao Baoc3292f32016-11-04 10:52:13 -070027#include <string>
28#include <vector>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070029
Tao Baoc3292f32016-11-04 10:52:13 -070030#include <android-base/file.h>
31#include <android-base/logging.h>
Tao Bao2c526392018-05-03 23:01:13 -070032#include <android-base/properties.h>
Tao Baoc3292f32016-11-04 10:52:13 -070033#include <android-base/strings.h>
34#include <android-base/unique_fd.h>
Tao Bao2c526392018-05-03 23:01:13 -070035#include <cutils/android_reboot.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Tao Baob656a152017-04-18 23:54:29 -070037bool MemMapping::MapFD(int fd) {
Tao Baoc3292f32016-11-04 10:52:13 -070038 struct stat sb;
39 if (fstat(fd, &sb) == -1) {
40 PLOG(ERROR) << "fstat(" << fd << ") failed";
41 return false;
42 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Tao Baoc3292f32016-11-04 10:52:13 -070044 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 Projectc24a8e62009-03-03 19:28:42 -080049
Tao Baob656a152017-04-18 23:54:29 -070050 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 Projectc24a8e62009-03-03 19:28:42 -080054
Tao Baoc3292f32016-11-04 10:52:13 -070055 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056}
57
Tao Baoc3292f32016-11-04 10:52:13 -070058// A "block map" which looks like this (from uncrypt/uncrypt.cpp):
59//
Tao Baob656a152017-04-18 23:54:29 -070060// /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 Baoc3292f32016-11-04 10:52:13 -070066//
Tao Baob656a152017-04-18 23:54:29 -070067// Each block range represents a half-open interval; the line "30 33" reprents the blocks
68// [30, 31, 32].
69bool MemMapping::MapBlockFile(const std::string& filename) {
Tao Baoc3292f32016-11-04 10:52:13 -070070 std::string content;
71 if (!android::base::ReadFileToString(filename, &content)) {
72 PLOG(ERROR) << "Failed to read " << filename;
Tao Baob656a152017-04-18 23:54:29 -070073 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070074 }
Doug Zongker99916f02014-01-13 14:16:58 -080075
Tao Baoc3292f32016-11-04 10:52:13 -070076 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 Baob656a152017-04-18 23:54:29 -070079 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070080 }
Doug Zongker99916f02014-01-13 14:16:58 -080081
Tao Baoc3292f32016-11-04 10:52:13 -070082 size_t size;
Tao Baob656a152017-04-18 23:54:29 -070083 size_t blksize;
84 if (sscanf(lines[1].c_str(), "%zu %zu", &size, &blksize) != 2) {
Tao Baoc3292f32016-11-04 10:52:13 -070085 LOG(ERROR) << "Failed to parse file size and block size: " << lines[1];
Tao Baob656a152017-04-18 23:54:29 -070086 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070087 }
Doug Zongker99916f02014-01-13 14:16:58 -080088
Tao Baoc3292f32016-11-04 10:52:13 -070089 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 Baob656a152017-04-18 23:54:29 -070092 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070093 }
Doug Zongker99916f02014-01-13 14:16:58 -080094
Tao Baoc3292f32016-11-04 10:52:13 -070095 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 Baob656a152017-04-18 23:54:29 -0700103 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700104 }
Doug Zongker99916f02014-01-13 14:16:58 -0800105
Tao Baoc3292f32016-11-04 10:52:13 -0700106 // Reserve enough contiguous address space for the whole file.
Tao Baoc13d2ec2017-10-10 10:56:09 -0700107 void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
Tao Baoc3292f32016-11-04 10:52:13 -0700108 if (reserve == MAP_FAILED) {
109 PLOG(ERROR) << "failed to reserve address space";
Tao Baob656a152017-04-18 23:54:29 -0700110 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700111 }
Doug Zongker99916f02014-01-13 14:16:58 -0800112
Tao Baoc3292f32016-11-04 10:52:13 -0700113 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 Baob656a152017-04-18 23:54:29 -0700118 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700119 }
Doug Zongker99916f02014-01-13 14:16:58 -0800120
Tao Baob656a152017-04-18 23:54:29 -0700121 ranges_.clear();
Tao Baoc3292f32016-11-04 10:52:13 -0700122
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 Baob656a152017-04-18 23:54:29 -0700131 LOG(ERROR) << "failed to parse range " << i << ": " << line;
Yabin Cui4f2df162016-02-18 11:32:10 -0800132 success = false;
Tao Baoc3292f32016-11-04 10:52:13 -0700133 break;
Yabin Cui4f2df162016-02-18 11:32:10 -0800134 }
Tao Baob656a152017-04-18 23:54:29 -0700135 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 Baoc3292f32016-11-04 10:52:13 -0700138 success = false;
139 break;
140 }
141
Tao Baoc13d2ec2017-10-10 10:56:09 -0700142 void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
143 static_cast<off_t>(start) * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700144 if (range_start == MAP_FAILED) {
145 PLOG(ERROR) << "failed to map range " << i << ": " << line;
Tao Baoc3292f32016-11-04 10:52:13 -0700146 success = false;
147 break;
148 }
Tao Baob656a152017-04-18 23:54:29 -0700149 ranges_.emplace_back(MappedRange{ range_start, range_size });
Tao Baoc3292f32016-11-04 10:52:13 -0700150
Tao Baob656a152017-04-18 23:54:29 -0700151 next += range_size;
152 remaining_size -= range_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700153 }
154 if (success && remaining_size != 0) {
Tao Baob656a152017-04-18 23:54:29 -0700155 LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700156 success = false;
157 }
158 if (!success) {
159 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700160 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700161 }
162
Tao Baob656a152017-04-18 23:54:29 -0700163 addr = static_cast<unsigned char*>(reserve);
164 length = size;
Tao Baoc3292f32016-11-04 10:52:13 -0700165
166 LOG(INFO) << "mmapped " << range_count << " ranges";
167
Tao Baob656a152017-04-18 23:54:29 -0700168 return true;
Tao Baoc3292f32016-11-04 10:52:13 -0700169}
170
Tao Baob656a152017-04-18 23:54:29 -0700171bool MemMapping::MapFile(const std::string& fn) {
172 if (fn.empty()) {
173 LOG(ERROR) << "Empty filename";
174 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700175 }
176
Tao Baoc3292f32016-11-04 10:52:13 -0700177 if (fn[0] == '@') {
Tao Baob656a152017-04-18 23:54:29 -0700178 // Block map file "@/cache/recovery/block.map".
179 if (!MapBlockFile(fn.substr(1))) {
Tao Baoc3292f32016-11-04 10:52:13 -0700180 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700181 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700182 }
183 } else {
184 // This is a regular file.
Tao Baob656a152017-04-18 23:54:29 -0700185 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700186 if (fd == -1) {
187 PLOG(ERROR) << "Unable to open '" << fn << "'";
Tao Baob656a152017-04-18 23:54:29 -0700188 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800189 }
190
Tao Baob656a152017-04-18 23:54:29 -0700191 if (!MapFD(fd)) {
Tao Baoc3292f32016-11-04 10:52:13 -0700192 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700193 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800194 }
Tao Baoc3292f32016-11-04 10:52:13 -0700195 }
Tao Baob656a152017-04-18 23:54:29 -0700196 return true;
Doug Zongker99916f02014-01-13 14:16:58 -0800197}
198
Tao Baob656a152017-04-18 23:54:29 -0700199MemMapping::~MemMapping() {
200 for (const auto& range : ranges_) {
Tao Baoc3292f32016-11-04 10:52:13 -0700201 if (munmap(range.addr, range.length) == -1) {
Tao Baob656a152017-04-18 23:54:29 -0700202 PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 }
Tao Baob656a152017-04-18 23:54:29 -0700204 };
205 ranges_.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206}
Tao Bao2c526392018-05-03 23:01:13 -0700207
208bool 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 Bao1700cc42018-07-16 22:09:59 -0700215
216std::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}