1 /* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * 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. 15 */ 16 17 #include "otautil/SysUtil.h" 18 19 #include <errno.h> // TEMP_FAILURE_RETRY 20 #include <fcntl.h> 21 #include <stdint.h> // SIZE_MAX 22 #include <sys/mman.h> 23 #include <sys/stat.h> 24 #include <sys/types.h> 25 26 #include <string> 27 #include <vector> 28 29 #include <android-base/file.h> 30 #include <android-base/logging.h> 31 #include <android-base/strings.h> 32 #include <android-base/unique_fd.h> 33 34 bool MemMapping::MapFD(int fd) { 35 struct stat sb; 36 if (fstat(fd, &sb) == -1) { 37 PLOG(ERROR) << "fstat(" << fd << ") failed"; 38 return false; 39 } 40 41 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 } 46 47 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) }); 51 52 return true; 53 } 54 55 // A "block map" which looks like this (from uncrypt/uncrypt.cpp): 56 // 57 // /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 63 // 64 // Each block range represents a half-open interval; the line "30 33" reprents the blocks 65 // [30, 31, 32]. 66 bool MemMapping::MapBlockFile(const std::string& filename) { 67 std::string content; 68 if (!android::base::ReadFileToString(filename, &content)) { 69 PLOG(ERROR) << "Failed to read " << filename; 70 return false; 71 } 72 73 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(); 76 return false; 77 } 78 79 size_t size; 80 size_t blksize; 81 if (sscanf(lines[1].c_str(), "%zu %zu", &size, &blksize) != 2) { 82 LOG(ERROR) << "Failed to parse file size and block size: " << lines[1]; 83 return false; 84 } 85 86 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]; 89 return false; 90 } 91 92 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(); 100 return false; 101 } 102 103 // Reserve enough contiguous address space for the whole file. 104 void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0); 105 if (reserve == MAP_FAILED) { 106 PLOG(ERROR) << "failed to reserve address space"; 107 return false; 108 } 109 110 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); 115 return false; 116 } 117 118 ranges_.clear(); 119 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) { 128 LOG(ERROR) << "failed to parse range " << i << ": " << line; 129 success = false; 130 break; 131 } 132 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; 135 success = false; 136 break; 137 } 138 139 void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, 140 static_cast<off_t>(start) * blksize); 141 if (range_start == MAP_FAILED) { 142 PLOG(ERROR) << "failed to map range " << i << ": " << line; 143 success = false; 144 break; 145 } 146 ranges_.emplace_back(MappedRange{ range_start, range_size }); 147 148 next += range_size; 149 remaining_size -= range_size; 150 } 151 if (success && remaining_size != 0) { 152 LOG(ERROR) << "Invalid ranges: remaining_size " << remaining_size; 153 success = false; 154 } 155 if (!success) { 156 munmap(reserve, blocks * blksize); 157 return false; 158 } 159 160 addr = static_cast<unsigned char*>(reserve); 161 length = size; 162 163 LOG(INFO) << "mmapped " << range_count << " ranges"; 164 165 return true; 166 } 167 168 bool MemMapping::MapFile(const std::string& fn) { 169 if (fn.empty()) { 170 LOG(ERROR) << "Empty filename"; 171 return false; 172 } 173 174 if (fn[0] == '@') { 175 // Block map file "@/cache/recovery/block.map". 176 if (!MapBlockFile(fn.substr(1))) { 177 LOG(ERROR) << "Map of '" << fn << "' failed"; 178 return false; 179 } 180 } else { 181 // This is a regular file. 182 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY))); 183 if (fd == -1) { 184 PLOG(ERROR) << "Unable to open '" << fn << "'"; 185 return false; 186 } 187 188 if (!MapFD(fd)) { 189 LOG(ERROR) << "Map of '" << fn << "' failed"; 190 return false; 191 } 192 } 193 return true; 194 } 195 196 MemMapping::~MemMapping() { 197 for (const auto& range : ranges_) { 198 if (munmap(range.addr, range.length) == -1) { 199 PLOG(ERROR) << "Failed to munmap(" << range.addr << ", " << range.length << ")"; 200 } 201 }; 202 ranges_.clear(); 203 } 204