blob: a8d5f4ad373aea85a42d43be1131b743ca6b6f42 [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
Elliott Hughesf267dee2015-06-23 12:31:02 -070019#include <assert.h>
20#include <errno.h>
Doug Zongker99916f02014-01-13 14:16:58 -080021#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include <limits.h>
Elliott Hughesf267dee2015-06-23 12:31:02 -070023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030
Tao Baoc3292f32016-11-04 10:52:13 -070031#include <algorithm>
32#include <string>
33#include <vector>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070034
Tao Baoc3292f32016-11-04 10:52:13 -070035#include <android-base/file.h>
36#include <android-base/logging.h>
37#include <android-base/strings.h>
38#include <android-base/unique_fd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Elliott Hughesf267dee2015-06-23 12:31:02 -070040static bool sysMapFD(int fd, MemMapping* pMap) {
Tao Baoc3292f32016-11-04 10:52:13 -070041 CHECK(pMap != nullptr);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Tao Baoc3292f32016-11-04 10:52:13 -070043 struct stat sb;
44 if (fstat(fd, &sb) == -1) {
45 PLOG(ERROR) << "fstat(" << fd << ") failed";
46 return false;
47 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Tao Baoc3292f32016-11-04 10:52:13 -070049 void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
50 if (memPtr == MAP_FAILED) {
51 PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed";
52 return false;
53 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054
Tao Baoc3292f32016-11-04 10:52:13 -070055 pMap->addr = static_cast<unsigned char*>(memPtr);
56 pMap->length = sb.st_size;
57 pMap->ranges.push_back({ memPtr, static_cast<size_t>(sb.st_size) });
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058
Tao Baoc3292f32016-11-04 10:52:13 -070059 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060}
61
Tao Baoc3292f32016-11-04 10:52:13 -070062// A "block map" which looks like this (from uncrypt/uncrypt.cpp):
63//
64// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device
65// 49652 4096 # file size in bytes, block size
66// 3 # count of block ranges
67// 1000 1008 # block range 0
68// 2100 2102 # ... block range 1
69// 30 33 # ... block range 2
70//
71// Each block range represents a half-open interval; the line "30 33"
72// reprents the blocks [30, 31, 32].
73static int sysMapBlockFile(const char* filename, MemMapping* pMap) {
74 CHECK(pMap != nullptr);
Doug Zongker99916f02014-01-13 14:16:58 -080075
Tao Baoc3292f32016-11-04 10:52:13 -070076 std::string content;
77 if (!android::base::ReadFileToString(filename, &content)) {
78 PLOG(ERROR) << "Failed to read " << filename;
79 return -1;
80 }
Doug Zongker99916f02014-01-13 14:16:58 -080081
Tao Baoc3292f32016-11-04 10:52:13 -070082 std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n");
83 if (lines.size() < 4) {
84 LOG(ERROR) << "Block map file is too short: " << lines.size();
85 return -1;
86 }
Doug Zongker99916f02014-01-13 14:16:58 -080087
Tao Baoc3292f32016-11-04 10:52:13 -070088 size_t size;
89 unsigned int blksize;
90 if (sscanf(lines[1].c_str(), "%zu %u", &size, &blksize) != 2) {
91 LOG(ERROR) << "Failed to parse file size and block size: " << lines[1];
92 return -1;
93 }
Doug Zongker99916f02014-01-13 14:16:58 -080094
Tao Baoc3292f32016-11-04 10:52:13 -070095 size_t range_count;
96 if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) {
97 LOG(ERROR) << "Failed to parse block map header: " << lines[2];
98 return -1;
99 }
Doug Zongker99916f02014-01-13 14:16:58 -0800100
Tao Baoc3292f32016-11-04 10:52:13 -0700101 size_t blocks;
102 if (blksize != 0) {
103 blocks = ((size - 1) / blksize) + 1;
104 }
105 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 ||
106 lines.size() != 3 + range_count) {
107 LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize
108 << ", range_count " << range_count << ", lines " << lines.size();
109 return -1;
110 }
Doug Zongker99916f02014-01-13 14:16:58 -0800111
Tao Baoc3292f32016-11-04 10:52:13 -0700112 // Reserve enough contiguous address space for the whole file.
113 void* reserve = mmap64(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
114 if (reserve == MAP_FAILED) {
115 PLOG(ERROR) << "failed to reserve address space";
116 return -1;
117 }
Doug Zongker99916f02014-01-13 14:16:58 -0800118
Tao Baoc3292f32016-11-04 10:52:13 -0700119 const std::string& block_dev = lines[0];
120 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY)));
121 if (fd == -1) {
122 PLOG(ERROR) << "failed to open block device " << block_dev;
123 munmap(reserve, blocks * blksize);
124 return -1;
125 }
Doug Zongker99916f02014-01-13 14:16:58 -0800126
Tao Baoc3292f32016-11-04 10:52:13 -0700127 pMap->ranges.resize(range_count);
128
129 unsigned char* next = static_cast<unsigned char*>(reserve);
130 size_t remaining_size = blocks * blksize;
131 bool success = true;
132 for (size_t i = 0; i < range_count; ++i) {
133 const std::string& line = lines[i + 3];
134
135 size_t start, end;
136 if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) {
137 LOG(ERROR) << "failed to parse range " << i << " in block map: " << line;
Yabin Cui4f2df162016-02-18 11:32:10 -0800138 success = false;
Tao Baoc3292f32016-11-04 10:52:13 -0700139 break;
Yabin Cui4f2df162016-02-18 11:32:10 -0800140 }
Tao Baoc3292f32016-11-04 10:52:13 -0700141 size_t length = (end - start) * blksize;
142 if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) {
143 LOG(ERROR) << "unexpected range in block map: " << start << " " << end;
144 success = false;
145 break;
146 }
147
148 void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
149 static_cast<off64_t>(start) * blksize);
150 if (addr == MAP_FAILED) {
151 PLOG(ERROR) << "failed to map block " << i;
152 success = false;
153 break;
154 }
155 pMap->ranges[i].addr = addr;
156 pMap->ranges[i].length = length;
157
158 next += length;
159 remaining_size -= length;
160 }
161 if (success && remaining_size != 0) {
162 LOG(ERROR) << "ranges in block map are invalid: remaining_size = " << remaining_size;
163 success = false;
164 }
165 if (!success) {
166 munmap(reserve, blocks * blksize);
167 return -1;
168 }
169
170 pMap->addr = static_cast<unsigned char*>(reserve);
171 pMap->length = size;
172
173 LOG(INFO) << "mmapped " << range_count << " ranges";
174
175 return 0;
176}
177
178int sysMapFile(const char* fn, MemMapping* pMap) {
179 if (fn == nullptr || pMap == nullptr) {
180 LOG(ERROR) << "Invalid argument(s)";
181 return -1;
182 }
183
184 *pMap = {};
185
186 if (fn[0] == '@') {
187 if (sysMapBlockFile(fn + 1, pMap) != 0) {
188 LOG(ERROR) << "Map of '" << fn << "' failed";
189 return -1;
190 }
191 } else {
192 // This is a regular file.
193 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn, O_RDONLY)));
194 if (fd == -1) {
195 PLOG(ERROR) << "Unable to open '" << fn << "'";
Yabin Cui4f2df162016-02-18 11:32:10 -0800196 return -1;
Doug Zongker99916f02014-01-13 14:16:58 -0800197 }
198
Tao Baoc3292f32016-11-04 10:52:13 -0700199 if (!sysMapFD(fd, pMap)) {
200 LOG(ERROR) << "Map of '" << fn << "' failed";
201 return -1;
Doug Zongker99916f02014-01-13 14:16:58 -0800202 }
Tao Baoc3292f32016-11-04 10:52:13 -0700203 }
204 return 0;
Doug Zongker99916f02014-01-13 14:16:58 -0800205}
206
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 * Release a memory mapping.
209 */
Tao Baoc3292f32016-11-04 10:52:13 -0700210void sysReleaseMap(MemMapping* pMap) {
211 std::for_each(pMap->ranges.cbegin(), pMap->ranges.cend(), [](const MappedRange& range) {
212 if (munmap(range.addr, range.length) == -1) {
213 PLOG(ERROR) << "munmap(" << range.addr << ", " << range.length << ") failed";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214 }
Tao Baoc3292f32016-11-04 10:52:13 -0700215 });
216 pMap->ranges.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217}