blob: 09ec8768f03ae1134760cdbae2e04aecd6c94954 [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>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/mman.h>
15#include <sys/stat.h>
16#include <sys/types.h>
17#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018
Doug Zongker99916f02014-01-13 14:16:58 -080019#define LOG_TAG "sysutil"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080020#include "Log.h"
21#include "SysUtil.h"
22
Elliott Hughesf267dee2015-06-23 12:31:02 -070023static bool sysMapFD(int fd, MemMapping* pMap) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024 assert(pMap != NULL);
25
Elliott Hughesf267dee2015-06-23 12:31:02 -070026 struct stat sb;
27 if (fstat(fd, &sb) == -1) {
Tao Baod7d0f752015-07-15 14:13:06 -070028 LOGE("fstat(%d) failed: %s\n", fd, strerror(errno));
Elliott Hughesf267dee2015-06-23 12:31:02 -070029 return false;
30 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Elliott Hughesf267dee2015-06-23 12:31:02 -070032 void* memPtr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033 if (memPtr == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -070034 LOGE("mmap(%d, R, PRIVATE, %d, 0) failed: %s\n", (int) sb.st_size, fd, strerror(errno));
Elliott Hughesf267dee2015-06-23 12:31:02 -070035 return false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036 }
37
Doug Zongker99916f02014-01-13 14:16:58 -080038 pMap->addr = memPtr;
Elliott Hughesf267dee2015-06-23 12:31:02 -070039 pMap->length = sb.st_size;
Doug Zongker99916f02014-01-13 14:16:58 -080040 pMap->range_count = 1;
41 pMap->ranges = malloc(sizeof(MappedRange));
42 pMap->ranges[0].addr = memPtr;
Elliott Hughesf267dee2015-06-23 12:31:02 -070043 pMap->ranges[0].length = sb.st_size;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Elliott Hughesf267dee2015-06-23 12:31:02 -070045 return true;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046}
47
Doug Zongker99916f02014-01-13 14:16:58 -080048static int sysMapBlockFile(FILE* mapf, MemMapping* pMap)
49{
50 char block_dev[PATH_MAX+1];
51 size_t size;
52 unsigned int blksize;
53 unsigned int blocks;
54 unsigned int range_count;
55 unsigned int i;
56
57 if (fgets(block_dev, sizeof(block_dev), mapf) == NULL) {
Tao Baod7d0f752015-07-15 14:13:06 -070058 LOGE("failed to read block device from header\n");
Doug Zongker99916f02014-01-13 14:16:58 -080059 return -1;
60 }
61 for (i = 0; i < sizeof(block_dev); ++i) {
62 if (block_dev[i] == '\n') {
63 block_dev[i] = 0;
64 break;
65 }
66 }
67
Mark Salyzyn76b245c2014-03-17 15:35:52 -070068 if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) {
Tao Baod7d0f752015-07-15 14:13:06 -070069 LOGE("failed to parse block map header\n");
Doug Zongker99916f02014-01-13 14:16:58 -080070 return -1;
71 }
72
73 blocks = ((size-1) / blksize) + 1;
74
75 pMap->range_count = range_count;
76 pMap->ranges = malloc(range_count * sizeof(MappedRange));
77 memset(pMap->ranges, 0, range_count * sizeof(MappedRange));
78
79 // Reserve enough contiguous address space for the whole file.
80 unsigned char* reserve;
81 reserve = mmap64(NULL, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
82 if (reserve == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -070083 LOGE("failed to reserve address space: %s\n", strerror(errno));
Doug Zongker99916f02014-01-13 14:16:58 -080084 return -1;
85 }
86
87 pMap->ranges[range_count-1].addr = reserve;
88 pMap->ranges[range_count-1].length = blocks * blksize;
89
90 int fd = open(block_dev, O_RDONLY);
91 if (fd < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -070092 LOGE("failed to open block device %s: %s\n", block_dev, strerror(errno));
Doug Zongker99916f02014-01-13 14:16:58 -080093 return -1;
94 }
95
96 unsigned char* next = reserve;
97 for (i = 0; i < range_count; ++i) {
98 int start, end;
99 if (fscanf(mapf, "%d %d\n", &start, &end) != 2) {
Tao Baod7d0f752015-07-15 14:13:06 -0700100 LOGE("failed to parse range %d in block map\n", i);
Doug Zongker99916f02014-01-13 14:16:58 -0800101 return -1;
102 }
103
104 void* addr = mmap64(next, (end-start)*blksize, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ((off64_t)start)*blksize);
105 if (addr == MAP_FAILED) {
Tao Baod7d0f752015-07-15 14:13:06 -0700106 LOGE("failed to map block %d: %s\n", i, strerror(errno));
Doug Zongker99916f02014-01-13 14:16:58 -0800107 return -1;
108 }
109 pMap->ranges[i].addr = addr;
110 pMap->ranges[i].length = (end-start)*blksize;
111
112 next += pMap->ranges[i].length;
113 }
114
115 pMap->addr = reserve;
116 pMap->length = size;
117
Doug Zongker19a8e242014-01-21 09:25:41 -0800118 LOGI("mmapped %d ranges\n", range_count);
119
Doug Zongker99916f02014-01-13 14:16:58 -0800120 return 0;
121}
122
123int sysMapFile(const char* fn, MemMapping* pMap)
124{
125 memset(pMap, 0, sizeof(*pMap));
126
127 if (fn && fn[0] == '@') {
128 // A map of blocks
129 FILE* mapf = fopen(fn+1, "r");
130 if (mapf == NULL) {
Tao Baod7d0f752015-07-15 14:13:06 -0700131 LOGE("Unable to open '%s': %s\n", fn+1, strerror(errno));
Doug Zongker99916f02014-01-13 14:16:58 -0800132 return -1;
133 }
134
135 if (sysMapBlockFile(mapf, pMap) != 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700136 LOGE("Map of '%s' failed\n", fn);
Doug Zongker99916f02014-01-13 14:16:58 -0800137 return -1;
138 }
139
140 fclose(mapf);
141 } else {
142 // This is a regular file.
Elliott Hughesf267dee2015-06-23 12:31:02 -0700143 int fd = open(fn, O_RDONLY);
144 if (fd == -1) {
Doug Zongker99916f02014-01-13 14:16:58 -0800145 LOGE("Unable to open '%s': %s\n", fn, strerror(errno));
146 return -1;
147 }
148
Elliott Hughesf267dee2015-06-23 12:31:02 -0700149 if (!sysMapFD(fd, pMap)) {
Doug Zongker99916f02014-01-13 14:16:58 -0800150 LOGE("Map of '%s' failed\n", fn);
151 close(fd);
152 return -1;
153 }
154
155 close(fd);
156 }
157 return 0;
158}
159
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 * Release a memory mapping.
162 */
Doug Zongker99916f02014-01-13 14:16:58 -0800163void sysReleaseMap(MemMapping* pMap)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164{
Doug Zongker99916f02014-01-13 14:16:58 -0800165 int i;
166 for (i = 0; i < pMap->range_count; ++i) {
167 if (munmap(pMap->ranges[i].addr, pMap->ranges[i].length) < 0) {
Tao Baod7d0f752015-07-15 14:13:06 -0700168 LOGE("munmap(%p, %d) failed: %s\n",
Doug Zongker99916f02014-01-13 14:16:58 -0800169 pMap->ranges[i].addr, (int)pMap->ranges[i].length, strerror(errno));
170 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171 }
Doug Zongker99916f02014-01-13 14:16:58 -0800172 free(pMap->ranges);
173 pMap->ranges = NULL;
174 pMap->range_count = 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175}