blob: ab1513088982e066f50068ed581733a66ab0b45f [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 Baoc3292f32016-11-04 10:52:13 -070026#include <string>
27#include <vector>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070028
Tao Baoc3292f32016-11-04 10:52:13 -070029#include <android-base/file.h>
30#include <android-base/logging.h>
Tao Bao2c526392018-05-03 23:01:13 -070031#include <android-base/properties.h>
Tao Baoc3292f32016-11-04 10:52:13 -070032#include <android-base/strings.h>
33#include <android-base/unique_fd.h>
Tao Bao2c526392018-05-03 23:01:13 -070034#include <cutils/android_reboot.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035
Tao Baob656a152017-04-18 23:54:29 -070036bool MemMapping::MapFD(int fd) {
Tao Baoc3292f32016-11-04 10:52:13 -070037 struct stat sb;
38 if (fstat(fd, &sb) == -1) {
39 PLOG(ERROR) << "fstat(" << fd << ") failed";
40 return false;
41 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Tao Baoc3292f32016-11-04 10:52:13 -070043 void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
44 if (memPtr == MAP_FAILED) {
45 PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed";
46 return false;
47 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Tao Baob656a152017-04-18 23:54:29 -070049 addr = static_cast<unsigned char*>(memPtr);
50 length = sb.st_size;
51 ranges_.clear();
52 ranges_.emplace_back(MappedRange{ memPtr, static_cast<size_t>(sb.st_size) });
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053
Tao Baoc3292f32016-11-04 10:52:13 -070054 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055}
56
Tao Baoc3292f32016-11-04 10:52:13 -070057// A "block map" which looks like this (from uncrypt/uncrypt.cpp):
58//
Tao Baob656a152017-04-18 23:54:29 -070059// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device
60// 49652 4096 # file size in bytes, block size
61// 3 # count of block ranges
62// 1000 1008 # block range 0
63// 2100 2102 # ... block range 1
64// 30 33 # ... block range 2
Tao Baoc3292f32016-11-04 10:52:13 -070065//
Tao Baob656a152017-04-18 23:54:29 -070066// Each block range represents a half-open interval; the line "30 33" reprents the blocks
67// [30, 31, 32].
68bool MemMapping::MapBlockFile(const std::string& filename) {
Tao Baoc3292f32016-11-04 10:52:13 -070069 std::string content;
70 if (!android::base::ReadFileToString(filename, &content)) {
71 PLOG(ERROR) << "Failed to read " << filename;
Tao Baob656a152017-04-18 23:54:29 -070072 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070073 }
Doug Zongker99916f02014-01-13 14:16:58 -080074
Tao Baoc3292f32016-11-04 10:52:13 -070075 std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n");
76 if (lines.size() < 4) {
77 LOG(ERROR) << "Block map file is too short: " << lines.size();
Tao Baob656a152017-04-18 23:54:29 -070078 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070079 }
Doug Zongker99916f02014-01-13 14:16:58 -080080
Tao Baoc3292f32016-11-04 10:52:13 -070081 size_t size;
Tao Baob656a152017-04-18 23:54:29 -070082 size_t blksize;
83 if (sscanf(lines[1].c_str(), "%zu %zu", &size, &blksize) != 2) {
Tao Baoc3292f32016-11-04 10:52:13 -070084 LOG(ERROR) << "Failed to parse file size and block size: " << lines[1];
Tao Baob656a152017-04-18 23:54:29 -070085 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070086 }
Doug Zongker99916f02014-01-13 14:16:58 -080087
Tao Baoc3292f32016-11-04 10:52:13 -070088 size_t range_count;
89 if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) {
90 LOG(ERROR) << "Failed to parse block map header: " << lines[2];
Tao Baob656a152017-04-18 23:54:29 -070091 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070092 }
Doug Zongker99916f02014-01-13 14:16:58 -080093
Tao Baoc3292f32016-11-04 10:52:13 -070094 size_t blocks;
95 if (blksize != 0) {
96 blocks = ((size - 1) / blksize) + 1;
97 }
98 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 ||
99 lines.size() != 3 + range_count) {
100 LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize
101 << ", range_count " << range_count << ", lines " << lines.size();
Tao Baob656a152017-04-18 23:54:29 -0700102 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700103 }
Doug Zongker99916f02014-01-13 14:16:58 -0800104
Tao Baoc3292f32016-11-04 10:52:13 -0700105 // Reserve enough contiguous address space for the whole file.
Tao Baoc13d2ec2017-10-10 10:56:09 -0700106 void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
Tao Baoc3292f32016-11-04 10:52:13 -0700107 if (reserve == MAP_FAILED) {
108 PLOG(ERROR) << "failed to reserve address space";
Tao Baob656a152017-04-18 23:54:29 -0700109 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700110 }
Doug Zongker99916f02014-01-13 14:16:58 -0800111
Tao Baoc3292f32016-11-04 10:52:13 -0700112 const std::string& block_dev = lines[0];
113 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY)));
114 if (fd == -1) {
115 PLOG(ERROR) << "failed to open block device " << block_dev;
116 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700117 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700118 }
Doug Zongker99916f02014-01-13 14:16:58 -0800119
Tao Baob656a152017-04-18 23:54:29 -0700120 ranges_.clear();
Tao Baoc3292f32016-11-04 10:52:13 -0700121
122 unsigned char* next = static_cast<unsigned char*>(reserve);
123 size_t remaining_size = blocks * blksize;
124 bool success = true;
125 for (size_t i = 0; i < range_count; ++i) {
126 const std::string& line = lines[i + 3];
127
128 size_t start, end;
129 if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) {
Tao Baob656a152017-04-18 23:54:29 -0700130 LOG(ERROR) << "failed to parse range " << i << ": " << line;
Yabin Cui4f2df162016-02-18 11:32:10 -0800131 success = false;
Tao Baoc3292f32016-11-04 10:52:13 -0700132 break;
Yabin Cui4f2df162016-02-18 11:32:10 -0800133 }
Tao Baob656a152017-04-18 23:54:29 -0700134 size_t range_size = (end - start) * blksize;
135 if (end <= start || (end - start) > SIZE_MAX / blksize || range_size > remaining_size) {
136 LOG(ERROR) << "Invalid range: " << start << " " << end;
Tao Baoc3292f32016-11-04 10:52:13 -0700137 success = false;
138 break;
139 }
140
Tao Baoc13d2ec2017-10-10 10:56:09 -0700141 void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
142 static_cast<off_t>(start) * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700143 if (range_start == MAP_FAILED) {
144 PLOG(ERROR) << "failed to map range " << i << ": " << line;
Tao Baoc3292f32016-11-04 10:52:13 -0700145 success = false;
146 break;
147 }
Tao Baob656a152017-04-18 23:54:29 -0700148 ranges_.emplace_back(MappedRange{ range_start, range_size });
Tao Baoc3292f32016-11-04 10:52:13 -0700149
Tao Baob656a152017-04-18 23:54:29 -0700150 next += range_size;
151 remaining_size -= range_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700152 }
153 if (success && remaining_size != 0) {
Tao Baob656a152017-04-18 23:54:29 -0700154 LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700155 success = false;
156 }
157 if (!success) {
158 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700159 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700160 }
161
Tao Baob656a152017-04-18 23:54:29 -0700162 addr = static_cast<unsigned char*>(reserve);
163 length = size;
Tao Baoc3292f32016-11-04 10:52:13 -0700164
165 LOG(INFO) << "mmapped " << range_count << " ranges";
166
Tao Baob656a152017-04-18 23:54:29 -0700167 return true;
Tao Baoc3292f32016-11-04 10:52:13 -0700168}
169
Tao Baob656a152017-04-18 23:54:29 -0700170bool MemMapping::MapFile(const std::string& fn) {
171 if (fn.empty()) {
172 LOG(ERROR) << "Empty filename";
173 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700174 }
175
Tao Baoc3292f32016-11-04 10:52:13 -0700176 if (fn[0] == '@') {
Tao Baob656a152017-04-18 23:54:29 -0700177 // Block map file "@/cache/recovery/block.map".
178 if (!MapBlockFile(fn.substr(1))) {
Tao Baoc3292f32016-11-04 10:52:13 -0700179 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700180 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700181 }
182 } else {
183 // This is a regular file.
Tao Baob656a152017-04-18 23:54:29 -0700184 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700185 if (fd == -1) {
186 PLOG(ERROR) << "Unable to open '" << fn << "'";
Tao Baob656a152017-04-18 23:54:29 -0700187 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800188 }
189
Tao Baob656a152017-04-18 23:54:29 -0700190 if (!MapFD(fd)) {
Tao Baoc3292f32016-11-04 10:52:13 -0700191 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700192 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800193 }
Tao Baoc3292f32016-11-04 10:52:13 -0700194 }
Tao Baob656a152017-04-18 23:54:29 -0700195 return true;
Doug Zongker99916f02014-01-13 14:16:58 -0800196}
197
Tao Baob656a152017-04-18 23:54:29 -0700198MemMapping::~MemMapping() {
199 for (const auto& range : ranges_) {
Tao Baoc3292f32016-11-04 10:52:13 -0700200 if (munmap(range.addr, range.length) == -1) {
Tao Baob656a152017-04-18 23:54:29 -0700201 PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202 }
Tao Baob656a152017-04-18 23:54:29 -0700203 };
204 ranges_.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800205}
Tao Bao2c526392018-05-03 23:01:13 -0700206
207bool reboot(const std::string& command) {
208 std::string cmd = command;
209 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
210 cmd += ",quiescent";
211 }
212 return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd);
213}