blob: de47edfd9bc3e7d4d43faba77e1bef7e194f1caf [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * System utilities.
5 */
Elliott Hughesf267dee2015-06-23 12:31:02 -07006#include <assert.h>
7#include <errno.h>
Doug Zongker99916f02014-01-13 14:16:58 -08008#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08009#include <limits.h>
Elliott Hughesf267dee2015-06-23 12:31:02 -070010#include <stdbool.h>
Yabin Cuia029c9a2016-02-18 11:32:10 -080011#include <stdint.h>
Elliott Hughesf267dee2015-06-23 12:31:02 -070012#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
17#include <sys/types.h>
18#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019
Doug Zongker99916f02014-01-13 14:16:58 -080020#define LOG_TAG "sysutil"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include "Log.h"
22#include "SysUtil.h"
23
Elliott Hughesf267dee2015-06-23 12:31:02 -070024static bool sysMapFD(int fd, MemMapping* pMap) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025 assert(pMap != NULL);
26
Elliott Hughesf267dee2015-06-23 12:31:02 -070027 struct stat sb;
28 if (fstat(fd, &sb) == -1) {
Tao Baod7d0f752015-07-15 14:13:06 -070029 LOGE("fstat(%d) failed: %s\n", fd, strerror(errno));
Elliott Hughesf267dee2015-06-23 12:31:02 -070030 return false;
31 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Elliott Hughesf267dee2015-06-23 12:31:02 -070033 void* memPtr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034 if (memPtr == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -070035 LOGE("mmap(%d, R, PRIVATE, %d, 0) failed: %s\n", (int) sb.st_size, fd, strerror(errno));
Elliott Hughesf267dee2015-06-23 12:31:02 -070036 return false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037 }
38
Doug Zongker99916f02014-01-13 14:16:58 -080039 pMap->addr = memPtr;
Elliott Hughesf267dee2015-06-23 12:31:02 -070040 pMap->length = sb.st_size;
Doug Zongker99916f02014-01-13 14:16:58 -080041 pMap->range_count = 1;
42 pMap->ranges = malloc(sizeof(MappedRange));
Yabin Cuia029c9a2016-02-18 11:32:10 -080043 if (pMap->ranges == NULL) {
44 LOGE("malloc failed: %s\n", strerror(errno));
45 munmap(memPtr, sb.st_size);
46 return false;
47 }
Doug Zongker99916f02014-01-13 14:16:58 -080048 pMap->ranges[0].addr = memPtr;
Elliott Hughesf267dee2015-06-23 12:31:02 -070049 pMap->ranges[0].length = sb.st_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
Elliott Hughesf267dee2015-06-23 12:31:02 -070051 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052}
53
Doug Zongker99916f02014-01-13 14:16:58 -080054static int sysMapBlockFile(FILE* mapf, MemMapping* pMap)
55{
56 char block_dev[PATH_MAX+1];
57 size_t size;
58 unsigned int blksize;
Yabin Cuia029c9a2016-02-18 11:32:10 -080059 size_t blocks;
Doug Zongker99916f02014-01-13 14:16:58 -080060 unsigned int range_count;
61 unsigned int i;
62
63 if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) {
Tao Baod7d0f752015-07-15 14:13:06 -070064 LOGE("failed to read block device from header\n");
Doug Zongker99916f02014-01-13 14:16:58 -080065 return -1;
66 }
67 for (i = 0; i < sizeof(block_dev); ++i) {
68 if (block_dev[i] == '\n') {
69 block_dev[i] = 0;
70 break;
71 }
72 }
73
Mark Salyzyn76b245c2014-03-17 15:35:52 -070074 if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) {
Tao Baod7d0f752015-07-15 14:13:06 -070075 LOGE("failed to parse block map header\n");
Doug Zongker99916f02014-01-13 14:16:58 -080076 return -1;
77 }
Yabin Cuia029c9a2016-02-18 11:32:10 -080078 if (blksize != 0) {
79 blocks = ((size-1) / blksize) + 1;
80 }
Yabin Cui2e947ea2016-02-19 14:44:37 -080081 if (size == 0 || blksize == 0 || blocks > SIZE_MAX / blksize || range_count == 0) {
Yabin Cuia029c9a2016-02-18 11:32:10 -080082 LOGE("invalid data in block map file: size %zu, blksize %u, range_count %u\n",
83 size, blksize, range_count);
84 return -1;
85 }
Doug Zongker99916f02014-01-13 14:16:58 -080086
87 pMap->range_count = range_count;
Yabin Cuia029c9a2016-02-18 11:32:10 -080088 pMap->ranges = calloc(range_count, sizeof(MappedRange));
89 if (pMap->ranges == NULL) {
90 LOGE("calloc(%u, %zu) failed: %s\n", range_count, sizeof(MappedRange), strerror(errno));
91 return -1;
92 }
Doug Zongker99916f02014-01-13 14:16:58 -080093
94 // Reserve enough contiguous address space for the whole file.
95 unsigned char* reserve;
96 reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
97 if (reserve == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -070098 LOGE("failed to reserve address space: %s\n", strerror(errno));
Yabin Cuia029c9a2016-02-18 11:32:10 -080099 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800100 return -1;
101 }
102
Doug Zongker99916f02014-01-13 14:16:58 -0800103 int fd = open(block_dev, O_RDONLY);
104 if (fd < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700105 LOGE("failed to open block device %s: %s\n", block_dev, strerror(errno));
Yabin Cuia029c9a2016-02-18 11:32:10 -0800106 munmap(reserve, blocks * blksize);
107 free(pMap->ranges);
Doug Zongker99916f02014-01-13 14:16:58 -0800108 return -1;
109 }
110
111 unsigned char* next = reserve;
Yabin Cui2e947ea2016-02-19 14:44:37 -0800112 size_t remaining_size = blocks * blksize;
113 bool success = true;
Doug Zongker99916f02014-01-13 14:16:58 -0800114 for (i = 0; i < range_count; ++i) {
Yabin Cui2e947ea2016-02-19 14:44:37 -0800115 size_t start, end;
116 if (fscanf(mapf, "%zu %zu\n", &start, &end) != 2) {
Tao Baod7d0f752015-07-15 14:13:06 -0700117 LOGE("failed to parse range %d in block map\n", i);
Yabin Cui2e947ea2016-02-19 14:44:37 -0800118 success = false;
119 break;
120 }
121 size_t length = (end - start) * blksize;
122 if (end <= start || ((end - start) > SIZE_MAX / blksize) || length > remaining_size) {
123 LOGE("unexpected range in block map: %zu %zu\n", start, end);
124 success = false;
125 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800126 }
127
Yabin Cui2e947ea2016-02-19 14:44:37 -0800128 void* addr = mmap64(next, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)(start*blksize)));
Doug Zongker99916f02014-01-13 14:16:58 -0800129 if (addr == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -0700130 LOGE("failed to map block %d: %s\n", i, strerror(errno));
Yabin Cui2e947ea2016-02-19 14:44:37 -0800131 success = false;
132 break;
Doug Zongker99916f02014-01-13 14:16:58 -0800133 }
134 pMap->ranges[i].addr = addr;
Yabin Cui2e947ea2016-02-19 14:44:37 -0800135 pMap->ranges[i].length = length;
Doug Zongker99916f02014-01-13 14:16:58 -0800136
Yabin Cui2e947ea2016-02-19 14:44:37 -0800137 next += length;
138 remaining_size -= length;
139 }
140 if (success && remaining_size != 0) {
141 LOGE("ranges in block map are invalid: remaining_size = %zu\n", remaining_size);
142 success = false;
143 }
144 if (!success) {
145 close(fd);
146 munmap(reserve, blocks * blksize);
147 free(pMap->ranges);
148 return -1;
Doug Zongker99916f02014-01-13 14:16:58 -0800149 }
150
Yabin Cui2e947ea2016-02-19 14:44:37 -0800151 close(fd);
Doug Zongker99916f02014-01-13 14:16:58 -0800152 pMap->addr = reserve;
153 pMap->length = size;
154
Doug Zongker19a8e242014-01-21 09:25:41 -0800155 LOGI("mmapped %d ranges\n", range_count);
156
Doug Zongker99916f02014-01-13 14:16:58 -0800157 return 0;
158}
159
160int sysMapFile(const char* fn, MemMapping* pMap)
161{
162 memset(pMap, 0, sizeof(*pMap));
163
164 if (fn && fn[0] == '@') {
165 // A map of blocks
166 FILE* mapf = fopen(fn+1, "r");
167 if (mapf == NULL) {
Tao Baod7d0f752015-07-15 14:13:06 -0700168 LOGE("Unable to open '%s': %s\n", fn+1, strerror(errno));
Doug Zongker99916f02014-01-13 14:16:58 -0800169 return -1;
170 }
171
172 if (sysMapBlockFile(mapf, pMap) != 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700173 LOGE("Map of '%s' failed\n", fn);
Yabin Cui2e947ea2016-02-19 14:44:37 -0800174 fclose(mapf);
Doug Zongker99916f02014-01-13 14:16:58 -0800175 return -1;
176 }
177
178 fclose(mapf);
179 } else {
180 // This is a regular file.
Elliott Hughesf267dee2015-06-23 12:31:02 -0700181 int fd = open(fn, O_RDONLY);
182 if (fd == -1) {
Doug Zongker99916f02014-01-13 14:16:58 -0800183 LOGE("Unable to open '%s': %s\n", fn, strerror(errno));
184 return -1;
185 }
186
Elliott Hughesf267dee2015-06-23 12:31:02 -0700187 if (!sysMapFD(fd, pMap)) {
Doug Zongker99916f02014-01-13 14:16:58 -0800188 LOGE("Map of '%s' failed\n", fn);
189 close(fd);
190 return -1;
191 }
192
193 close(fd);
194 }
195 return 0;
196}
197
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199 * Release a memory mapping.
200 */
Doug Zongker99916f02014-01-13 14:16:58 -0800201void sysReleaseMap(MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202{
Doug Zongker99916f02014-01-13 14:16:58 -0800203 int i;
204 for (i = 0; i < pMap->range_count; ++i) {
205 if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700206 LOGE("munmap(%p, %d) failed: %s\n",
Doug Zongker99916f02014-01-13 14:16:58 -0800207 pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno));
208 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 }
Doug Zongker99916f02014-01-13 14:16:58 -0800210 free(pMap->ranges);
211 pMap->ranges = NULL;
212 pMap->range_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213}