blob: 48336ad072ccfd14673ccaad7321864e5e2b9cae [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 Baocfe53c22017-10-03 14:37:21 -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>
31#include <android-base/strings.h>
32#include <android-base/unique_fd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Tao Baob656a152017-04-18 23:54:29 -070034bool MemMapping::MapFD(int fd) {
Tao Baoc3292f32016-11-04 10:52:13 -070035 struct stat sb;
36 if (fstat(fd, &sb) == -1) {
37 PLOG(ERROR) << "fstat(" << fd << ") failed";
38 return false;
39 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040
Tao Baoc3292f32016-11-04 10:52:13 -070041 void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
42 if (memPtr == MAP_FAILED) {
43 PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed";
44 return false;
45 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046
Tao Baob656a152017-04-18 23:54:29 -070047 addr = static_cast<unsigned char*>(memPtr);
48 length = sb.st_size;
49 ranges_.clear();
50 ranges_.emplace_back(MappedRange{ memPtr, static_cast<size_t>(sb.st_size) });
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Tao Baoc3292f32016-11-04 10:52:13 -070052 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053}
54
Tao Baoc3292f32016-11-04 10:52:13 -070055// A "block map" which looks like this (from uncrypt/uncrypt.cpp):
56//
Tao Baob656a152017-04-18 23:54:29 -070057// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device
58// 49652 4096 # file size in bytes, block size
59// 3 # count of block ranges
60// 1000 1008 # block range 0
61// 2100 2102 # ... block range 1
62// 30 33 # ... block range 2
Tao Baoc3292f32016-11-04 10:52:13 -070063//
Tao Baob656a152017-04-18 23:54:29 -070064// Each block range represents a half-open interval; the line "30 33" reprents the blocks
65// [30, 31, 32].
66bool MemMapping::MapBlockFile(const std::string& filename) {
Tao Baoc3292f32016-11-04 10:52:13 -070067 std::string content;
68 if (!android::base::ReadFileToString(filename, &content)) {
69 PLOG(ERROR) << "Failed to read " << filename;
Tao Baob656a152017-04-18 23:54:29 -070070 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070071 }
Doug Zongker99916f02014-01-13 14:16:58 -080072
Tao Baoc3292f32016-11-04 10:52:13 -070073 std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n");
74 if (lines.size() < 4) {
75 LOG(ERROR) << "Block map file is too short: " << lines.size();
Tao Baob656a152017-04-18 23:54:29 -070076 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070077 }
Doug Zongker99916f02014-01-13 14:16:58 -080078
Tao Baoc3292f32016-11-04 10:52:13 -070079 size_t size;
Tao Baob656a152017-04-18 23:54:29 -070080 size_t blksize;
81 if (sscanf(lines[1].c_str(), "%zu %zu", &size, &blksize) != 2) {
Tao Baoc3292f32016-11-04 10:52:13 -070082 LOG(ERROR) << "Failed to parse file size and block size: " << lines[1];
Tao Baob656a152017-04-18 23:54:29 -070083 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070084 }
Doug Zongker99916f02014-01-13 14:16:58 -080085
Tao Baoc3292f32016-11-04 10:52:13 -070086 size_t range_count;
87 if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) {
88 LOG(ERROR) << "Failed to parse block map header: " << lines[2];
Tao Baob656a152017-04-18 23:54:29 -070089 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070090 }
Doug Zongker99916f02014-01-13 14:16:58 -080091
Tao Baoc3292f32016-11-04 10:52:13 -070092 size_t blocks;
93 if (blksize != 0) {
94 blocks = ((size - 1) / blksize) + 1;
95 }
96 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 ||
97 lines.size() != 3 + range_count) {
98 LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize
99 << ", range_count " << range_count << ", lines " << lines.size();
Tao Baob656a152017-04-18 23:54:29 -0700100 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700101 }
Doug Zongker99916f02014-01-13 14:16:58 -0800102
Tao Baoc3292f32016-11-04 10:52:13 -0700103 // Reserve enough contiguous address space for the whole file.
Tao Baoc13d2ec2017-10-10 10:56:09 -0700104 void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
Tao Baoc3292f32016-11-04 10:52:13 -0700105 if (reserve == MAP_FAILED) {
106 PLOG(ERROR) << "failed to reserve address space";
Tao Baob656a152017-04-18 23:54:29 -0700107 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700108 }
Doug Zongker99916f02014-01-13 14:16:58 -0800109
Tao Baoc3292f32016-11-04 10:52:13 -0700110 const std::string& block_dev = lines[0];
111 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY)));
112 if (fd == -1) {
113 PLOG(ERROR) << "failed to open block device " << block_dev;
114 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700115 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700116 }
Doug Zongker99916f02014-01-13 14:16:58 -0800117
Tao Baob656a152017-04-18 23:54:29 -0700118 ranges_.clear();
Tao Baoc3292f32016-11-04 10:52:13 -0700119
120 unsigned char* next = static_cast<unsigned char*>(reserve);
121 size_t remaining_size = blocks * blksize;
122 bool success = true;
123 for (size_t i = 0; i < range_count; ++i) {
124 const std::string& line = lines[i + 3];
125
126 size_t start, end;
127 if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) {
Tao Baob656a152017-04-18 23:54:29 -0700128 LOG(ERROR) << "failed to parse range " << i << ": " << line;
Yabin Cui4f2df162016-02-18 11:32:10 -0800129 success = false;
Tao Baoc3292f32016-11-04 10:52:13 -0700130 break;
Yabin Cui4f2df162016-02-18 11:32:10 -0800131 }
Tao Baob656a152017-04-18 23:54:29 -0700132 size_t range_size = (end - start) * blksize;
133 if (end <= start || (end - start) > SIZE_MAX / blksize || range_size > remaining_size) {
134 LOG(ERROR) << "Invalid range: " << start << " " << end;
Tao Baoc3292f32016-11-04 10:52:13 -0700135 success = false;
136 break;
137 }
138
Tao Baoc13d2ec2017-10-10 10:56:09 -0700139 void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
140 static_cast<off_t>(start) * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700141 if (range_start == MAP_FAILED) {
142 PLOG(ERROR) << "failed to map range " << i << ": " << line;
Tao Baoc3292f32016-11-04 10:52:13 -0700143 success = false;
144 break;
145 }
Tao Baob656a152017-04-18 23:54:29 -0700146 ranges_.emplace_back(MappedRange{ range_start, range_size });
Tao Baoc3292f32016-11-04 10:52:13 -0700147
Tao Baob656a152017-04-18 23:54:29 -0700148 next += range_size;
149 remaining_size -= range_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700150 }
151 if (success && remaining_size != 0) {
Tao Baob656a152017-04-18 23:54:29 -0700152 LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700153 success = false;
154 }
155 if (!success) {
156 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700157 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700158 }
159
Tao Baob656a152017-04-18 23:54:29 -0700160 addr = static_cast<unsigned char*>(reserve);
161 length = size;
Tao Baoc3292f32016-11-04 10:52:13 -0700162
163 LOG(INFO) << "mmapped " << range_count << " ranges";
164
Tao Baob656a152017-04-18 23:54:29 -0700165 return true;
Tao Baoc3292f32016-11-04 10:52:13 -0700166}
167
Tao Baob656a152017-04-18 23:54:29 -0700168bool MemMapping::MapFile(const std::string& fn) {
169 if (fn.empty()) {
170 LOG(ERROR) << "Empty filename";
171 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700172 }
173
Tao Baoc3292f32016-11-04 10:52:13 -0700174 if (fn[0] == '@') {
Tao Baob656a152017-04-18 23:54:29 -0700175 // Block map file "@/cache/recovery/block.map".
176 if (!MapBlockFile(fn.substr(1))) {
Tao Baoc3292f32016-11-04 10:52:13 -0700177 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700178 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700179 }
180 } else {
181 // This is a regular file.
Tao Baob656a152017-04-18 23:54:29 -0700182 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700183 if (fd == -1) {
184 PLOG(ERROR) << "Unable to open '" << fn << "'";
Tao Baob656a152017-04-18 23:54:29 -0700185 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800186 }
187
Tao Baob656a152017-04-18 23:54:29 -0700188 if (!MapFD(fd)) {
Tao Baoc3292f32016-11-04 10:52:13 -0700189 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700190 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800191 }
Tao Baoc3292f32016-11-04 10:52:13 -0700192 }
Tao Baob656a152017-04-18 23:54:29 -0700193 return true;
Doug Zongker99916f02014-01-13 14:16:58 -0800194}
195
Tao Baob656a152017-04-18 23:54:29 -0700196MemMapping::~MemMapping() {
197 for (const auto& range : ranges_) {
Tao Baoc3292f32016-11-04 10:52:13 -0700198 if (munmap(range.addr, range.length) == -1) {
Tao Baob656a152017-04-18 23:54:29 -0700199 PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 }
Tao Baob656a152017-04-18 23:54:29 -0700201 };
202 ranges_.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203}