blob: dfa2150733400a89ec9bfb7f1f3daf72ba814b84 [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
17#include "SysUtil.h"
18
Doug Zongker99916f02014-01-13 14:16:58 -080019#include <fcntl.h>
Tao Baob656a152017-04-18 23:54:29 -070020#include <stdint.h> // SIZE_MAX
Elliott Hughesf267dee2015-06-23 12:31:02 -070021#include <sys/mman.h>
22#include <sys/stat.h>
23#include <sys/types.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024
Tao Baoc3292f32016-11-04 10:52:13 -070025#include <string>
26#include <vector>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070027
Tao Baoc3292f32016-11-04 10:52:13 -070028#include <android-base/file.h>
29#include <android-base/logging.h>
30#include <android-base/strings.h>
31#include <android-base/unique_fd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Tao Baob656a152017-04-18 23:54:29 -070033bool MemMapping::MapFD(int fd) {
Tao Baoc3292f32016-11-04 10:52:13 -070034 struct stat sb;
35 if (fstat(fd, &sb) == -1) {
36 PLOG(ERROR) << "fstat(" << fd << ") failed";
37 return false;
38 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Tao Baoc3292f32016-11-04 10:52:13 -070040 void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
41 if (memPtr == MAP_FAILED) {
42 PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed";
43 return false;
44 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tao Baob656a152017-04-18 23:54:29 -070046 addr = static_cast<unsigned char*>(memPtr);
47 length = sb.st_size;
48 ranges_.clear();
49 ranges_.emplace_back(MappedRange{ memPtr, static_cast<size_t>(sb.st_size) });
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
Tao Baoc3292f32016-11-04 10:52:13 -070051 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052}
53
Tao Baoc3292f32016-11-04 10:52:13 -070054// A "block map" which looks like this (from uncrypt/uncrypt.cpp):
55//
Tao Baob656a152017-04-18 23:54:29 -070056// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device
57// 49652 4096 # file size in bytes, block size
58// 3 # count of block ranges
59// 1000 1008 # block range 0
60// 2100 2102 # ... block range 1
61// 30 33 # ... block range 2
Tao Baoc3292f32016-11-04 10:52:13 -070062//
Tao Baob656a152017-04-18 23:54:29 -070063// Each block range represents a half-open interval; the line "30 33" reprents the blocks
64// [30, 31, 32].
65bool MemMapping::MapBlockFile(const std::string& filename) {
Tao Baoc3292f32016-11-04 10:52:13 -070066 std::string content;
67 if (!android::base::ReadFileToString(filename, &content)) {
68 PLOG(ERROR) << "Failed to read " << filename;
Tao Baob656a152017-04-18 23:54:29 -070069 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070070 }
Doug Zongker99916f02014-01-13 14:16:58 -080071
Tao Baoc3292f32016-11-04 10:52:13 -070072 std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n");
73 if (lines.size() < 4) {
74 LOG(ERROR) << "Block map file is too short: " << lines.size();
Tao Baob656a152017-04-18 23:54:29 -070075 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070076 }
Doug Zongker99916f02014-01-13 14:16:58 -080077
Tao Baoc3292f32016-11-04 10:52:13 -070078 size_t size;
Tao Baob656a152017-04-18 23:54:29 -070079 size_t blksize;
80 if (sscanf(lines[1].c_str(), "%zu %zu", &size, &blksize) != 2) {
Tao Baoc3292f32016-11-04 10:52:13 -070081 LOG(ERROR) << "Failed to parse file size and block size: " << lines[1];
Tao Baob656a152017-04-18 23:54:29 -070082 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070083 }
Doug Zongker99916f02014-01-13 14:16:58 -080084
Tao Baoc3292f32016-11-04 10:52:13 -070085 size_t range_count;
86 if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) {
87 LOG(ERROR) << "Failed to parse block map header: " << lines[2];
Tao Baob656a152017-04-18 23:54:29 -070088 return false;
Tao Baoc3292f32016-11-04 10:52:13 -070089 }
Doug Zongker99916f02014-01-13 14:16:58 -080090
Tao Baoc3292f32016-11-04 10:52:13 -070091 size_t blocks;
92 if (blksize != 0) {
93 blocks = ((size - 1) / blksize) + 1;
94 }
95 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 ||
96 lines.size() != 3 + range_count) {
97 LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize
98 << ", range_count " << range_count << ", lines " << lines.size();
Tao Baob656a152017-04-18 23:54:29 -070099 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700100 }
Doug Zongker99916f02014-01-13 14:16:58 -0800101
Tao Baoc3292f32016-11-04 10:52:13 -0700102 // Reserve enough contiguous address space for the whole file.
103 void* reserve = mmap64(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
104 if (reserve == MAP_FAILED) {
105 PLOG(ERROR) << "failed to reserve address space";
Tao Baob656a152017-04-18 23:54:29 -0700106 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700107 }
Doug Zongker99916f02014-01-13 14:16:58 -0800108
Tao Baoc3292f32016-11-04 10:52:13 -0700109 const std::string& block_dev = lines[0];
110 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY)));
111 if (fd == -1) {
112 PLOG(ERROR) << "failed to open block device " << block_dev;
113 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700114 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700115 }
Doug Zongker99916f02014-01-13 14:16:58 -0800116
Tao Baob656a152017-04-18 23:54:29 -0700117 ranges_.clear();
Tao Baoc3292f32016-11-04 10:52:13 -0700118
119 unsigned char* next = static_cast<unsigned char*>(reserve);
120 size_t remaining_size = blocks * blksize;
121 bool success = true;
122 for (size_t i = 0; i < range_count; ++i) {
123 const std::string& line = lines[i + 3];
124
125 size_t start, end;
126 if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) {
Tao Baob656a152017-04-18 23:54:29 -0700127 LOG(ERROR) << "failed to parse range " << i << ": " << line;
Yabin Cui4f2df162016-02-18 11:32:10 -0800128 success = false;
Tao Baoc3292f32016-11-04 10:52:13 -0700129 break;
Yabin Cui4f2df162016-02-18 11:32:10 -0800130 }
Tao Baob656a152017-04-18 23:54:29 -0700131 size_t range_size = (end - start) * blksize;
132 if (end <= start || (end - start) > SIZE_MAX / blksize || range_size > remaining_size) {
133 LOG(ERROR) << "Invalid range: " << start << " " << end;
Tao Baoc3292f32016-11-04 10:52:13 -0700134 success = false;
135 break;
136 }
137
Tao Baob656a152017-04-18 23:54:29 -0700138 void* range_start = mmap64(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
139 static_cast<off64_t>(start) * blksize);
140 if (range_start == MAP_FAILED) {
141 PLOG(ERROR) << "failed to map range " << i << ": " << line;
Tao Baoc3292f32016-11-04 10:52:13 -0700142 success = false;
143 break;
144 }
Tao Baob656a152017-04-18 23:54:29 -0700145 ranges_.emplace_back(MappedRange{ range_start, range_size });
Tao Baoc3292f32016-11-04 10:52:13 -0700146
Tao Baob656a152017-04-18 23:54:29 -0700147 next += range_size;
148 remaining_size -= range_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700149 }
150 if (success && remaining_size != 0) {
Tao Baob656a152017-04-18 23:54:29 -0700151 LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size;
Tao Baoc3292f32016-11-04 10:52:13 -0700152 success = false;
153 }
154 if (!success) {
155 munmap(reserve, blocks * blksize);
Tao Baob656a152017-04-18 23:54:29 -0700156 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700157 }
158
Tao Baob656a152017-04-18 23:54:29 -0700159 addr = static_cast<unsigned char*>(reserve);
160 length = size;
Tao Baoc3292f32016-11-04 10:52:13 -0700161
162 LOG(INFO) << "mmapped " << range_count << " ranges";
163
Tao Baob656a152017-04-18 23:54:29 -0700164 return true;
Tao Baoc3292f32016-11-04 10:52:13 -0700165}
166
Tao Baob656a152017-04-18 23:54:29 -0700167bool MemMapping::MapFile(const std::string& fn) {
168 if (fn.empty()) {
169 LOG(ERROR) << "Empty filename";
170 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700171 }
172
Tao Baoc3292f32016-11-04 10:52:13 -0700173 if (fn[0] == '@') {
Tao Baob656a152017-04-18 23:54:29 -0700174 // Block map file "@/cache/recovery/block.map".
175 if (!MapBlockFile(fn.substr(1))) {
Tao Baoc3292f32016-11-04 10:52:13 -0700176 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700177 return false;
Tao Baoc3292f32016-11-04 10:52:13 -0700178 }
179 } else {
180 // This is a regular file.
Tao Baob656a152017-04-18 23:54:29 -0700181 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY)));
Tao Baoc3292f32016-11-04 10:52:13 -0700182 if (fd == -1) {
183 PLOG(ERROR) << "Unable to open '" << fn << "'";
Tao Baob656a152017-04-18 23:54:29 -0700184 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800185 }
186
Tao Baob656a152017-04-18 23:54:29 -0700187 if (!MapFD(fd)) {
Tao Baoc3292f32016-11-04 10:52:13 -0700188 LOG(ERROR) << "Map of '" << fn << "' failed";
Tao Baob656a152017-04-18 23:54:29 -0700189 return false;
Doug Zongker99916f02014-01-13 14:16:58 -0800190 }
Tao Baoc3292f32016-11-04 10:52:13 -0700191 }
Tao Baob656a152017-04-18 23:54:29 -0700192 return true;
Doug Zongker99916f02014-01-13 14:16:58 -0800193}
194
Tao Baob656a152017-04-18 23:54:29 -0700195MemMapping::~MemMapping() {
196 for (const auto& range : ranges_) {
Tao Baoc3292f32016-11-04 10:52:13 -0700197 if (munmap(range.addr, range.length) == -1) {
Tao Baob656a152017-04-18 23:54:29 -0700198 PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199 }
Tao Baob656a152017-04-18 23:54:29 -0700200 };
201 ranges_.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202}