blob: 5156514f012b7338d9c8ffd15a3aa2157e2f32ed [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 *
Tao Bao9c05a822016-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 Bao9c05a822016-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>
Yabin Cuia029c9a2016-02-18 11:32:10 -080023#include <stdint.h>
Elliott Hughesf267dee2015-06-23 12:31:02 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/mman.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Tao Bao9c05a822016-11-04 10:52:13 -070032#include <algorithm>
33#include <string>
34#include <vector>
Tianjie Xu74778142016-08-05 18:00:04 -070035
Tao Bao9c05a822016-11-04 10:52:13 -070036#include <android-base/file.h>
37#include <android-base/logging.h>
38#include <android-base/strings.h>
39#include <android-base/unique_fd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040
Elliott Hughesf267dee2015-06-23 12:31:02 -070041static bool sysMapFD(int fd, MemMapping* pMap) {
Tao Bao9c05a822016-11-04 10:52:13 -070042 CHECK(pMap != nullptr);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Tao Bao9c05a822016-11-04 10:52:13 -070044 struct stat sb;
45 if (fstat(fd, &sb) == -1) {
46 PLOG(ERROR) << "fstat(" << fd << ") failed";
47 return false;
48 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
Tao Bao9c05a822016-11-04 10:52:13 -070050 void* memPtr = mmap(nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
51 if (memPtr == MAP_FAILED) {
52 PLOG(ERROR) << "mmap(" << sb.st_size << ", R, PRIVATE, " << fd << ", 0) failed";
53 return false;
54 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055
Tao Bao9c05a822016-11-04 10:52:13 -070056 pMap->addr = static_cast<unsigned char*>(memPtr);
57 pMap->length = sb.st_size;
58 pMap->ranges.push_back({ memPtr, static_cast<size_t>(sb.st_size) });
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059
Tao Bao9c05a822016-11-04 10:52:13 -070060 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061}
62
Tao Bao9c05a822016-11-04 10:52:13 -070063// A "block map" which looks like this (from uncrypt/uncrypt.cpp):
64//
65// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device
66// 49652 4096 # file size in bytes, block size
67// 3 # count of block ranges
68// 1000 1008 # block range 0
69// 2100 2102 # ... block range 1
70// 30 33 # ... block range 2
71//
72// Each block range represents a half-open interval; the line "30 33"
73// reprents the blocks [30, 31, 32].
74static int sysMapBlockFile(const char* filename, MemMapping* pMap) {
75 CHECK(pMap != nullptr);
Doug Zongker99916f02014-01-13 14:16:58 -080076
Tao Bao9c05a822016-11-04 10:52:13 -070077 std::string content;
78 if (!android::base::ReadFileToString(filename, &content)) {
79 PLOG(ERROR) << "Failed to read " << filename;
80 return -1;
81 }
82
83 std::vector<std::string> lines = android::base::Split(android::base::Trim(content), "\n");
84 if (lines.size() < 4) {
85 LOG(ERROR) << "Block map file is too short: " << lines.size();
86 return -1;
87 }
88
89 size_t size;
90 unsigned int blksize;
91 if (sscanf(lines[1].c_str(), "%zu %u", &size, &blksize) != 2) {
92 LOG(ERROR) << "Failed to parse file size and block size: " << lines[1];
93 return -1;
94 }
95
96 size_t range_count;
97 if (sscanf(lines[2].c_str(), "%zu", &range_count) != 1) {
98 LOG(ERROR) << "Failed to parse block map header: " << lines[2];
99 return -1;
100 }
101
102 size_t blocks;
103 if (blksize != 0) {
104 blocks = ((size - 1) / blksize) + 1;
105 }
106 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0 ||
107 lines.size() != 3 + range_count) {
108 LOG(ERROR) << "Invalid data in block map file: size " << size << ", blksize " << blksize
109 << ", range_count " << range_count << ", lines " << lines.size();
110 return -1;
111 }
112
113 // Reserve enough contiguous address space for the whole file.
114 void* reserve = mmap64(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
115 if (reserve == MAP_FAILED) {
116 PLOG(ERROR) << "failed to reserve address space";
117 return -1;
118 }
119
120 const std::string& block_dev = lines[0];
121 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(block_dev.c_str(), O_RDONLY)));
122 if (fd == -1) {
123 PLOG(ERROR) << "failed to open block device " << block_dev;
124 munmap(reserve, blocks * blksize);
125 return -1;
126 }
127
128 pMap->ranges.resize(range_count);
129
130 unsigned char* next = static_cast<unsigned char*>(reserve);
131 size_t remaining_size = blocks * blksize;
132 bool success = true;
133 for (size_t i = 0; i < range_count; ++i) {
134 const std::string& line = lines[i + 3];
135
136 size_t start, end;
137 if (sscanf(line.c_str(), "%zu %zu\n", &start, &end) != 2) {
138 LOG(ERROR) << "failed to parse range " << i << " in block map: " << line;
139 success = false;
140 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800141 }
Tao Bao9c05a822016-11-04 10:52:13 -0700142 size_t length = (end - start) * blksize;
143 if (end <= start || (end - start) > SIZE_MAX / blksize || length > remaining_size) {
144 LOG(ERROR) << "unexpected range in block map: " << start << " " << end;
145 success = false;
146 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800147 }
148
Tao Bao9c05a822016-11-04 10:52:13 -0700149 void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
150 static_cast<off64_t>(start) * blksize);
151 if (addr == MAP_FAILED) {
152 PLOG(ERROR) << "failed to map block " << i;
153 success = false;
154 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800155 }
Tao Bao9c05a822016-11-04 10:52:13 -0700156 pMap->ranges[i].addr = addr;
157 pMap->ranges[i].length = length;
Doug Zongker99916f02014-01-13 14:16:58 -0800158
Tao Bao9c05a822016-11-04 10:52:13 -0700159 next += length;
160 remaining_size -= length;
161 }
162 if (success && remaining_size != 0) {
163 LOG(ERROR) << "ranges in block map are invalid: remaining_size = " << remaining_size;
164 success = false;
165 }
166 if (!success) {
167 munmap(reserve, blocks * blksize);
168 return -1;
169 }
Doug Zongker99916f02014-01-13 14:16:58 -0800170
Tao Bao9c05a822016-11-04 10:52:13 -0700171 pMap->addr = static_cast<unsigned char*>(reserve);
172 pMap->length = size;
Doug Zongker99916f02014-01-13 14:16:58 -0800173
Tao Bao9c05a822016-11-04 10:52:13 -0700174 LOG(INFO) << "mmapped " << range_count << " ranges";
Doug Zongker99916f02014-01-13 14:16:58 -0800175
Tao Bao9c05a822016-11-04 10:52:13 -0700176 return 0;
Doug Zongker99916f02014-01-13 14:16:58 -0800177}
178
Tao Bao9c05a822016-11-04 10:52:13 -0700179int sysMapFile(const char* fn, MemMapping* pMap) {
180 if (fn == nullptr || pMap == nullptr) {
181 LOG(ERROR) << "Invalid argument(s)";
182 return -1;
183 }
Doug Zongker99916f02014-01-13 14:16:58 -0800184
Tao Bao9c05a822016-11-04 10:52:13 -0700185 *pMap = {};
Doug Zongker99916f02014-01-13 14:16:58 -0800186
Tao Bao9c05a822016-11-04 10:52:13 -0700187 if (fn[0] == '@') {
188 if (sysMapBlockFile(fn + 1, pMap) != 0) {
189 LOG(ERROR) << "Map of '" << fn << "' failed";
190 return -1;
Doug Zongker99916f02014-01-13 14:16:58 -0800191 }
Tao Bao9c05a822016-11-04 10:52:13 -0700192 } else {
193 // This is a regular file.
194 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn, O_RDONLY)));
195 if (fd == -1) {
196 PLOG(ERROR) << "Unable to open '" << fn << "'";
197 return -1;
198 }
199
200 if (!sysMapFD(fd, pMap)) {
201 LOG(ERROR) << "Map of '" << fn << "' failed";
202 return -1;
203 }
204 }
205 return 0;
Doug Zongker99916f02014-01-13 14:16:58 -0800206}
207
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 * Release a memory mapping.
210 */
Tao Bao9c05a822016-11-04 10:52:13 -0700211void sysReleaseMap(MemMapping* pMap) {
212 std::for_each(pMap->ranges.cbegin(), pMap->ranges.cend(), [](const MappedRange& range) {
213 if (munmap(range.addr, range.length) == -1) {
214 PLOG(ERROR) << "munmap(" << range.addr << ", " << range.length << ") failed";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 }
Tao Bao9c05a822016-11-04 10:52:13 -0700216 });
217 pMap->ranges.clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218}