blob: 31c7bdd7a42118a1fbaada4af14e65044c3badfc [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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 <errno.h>
18#include <stdlib.h>
19#include <sys/mount.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#include "mtdutils/mtdutils.h"
25#include "mtdutils/mounts.h"
26#include "minzip/Zip.h"
27#include "roots.h"
28#include "common.h"
29
30typedef struct {
31 const char *name;
32 const char *device;
33 const char *device2; // If the first one doesn't work (may be NULL)
34 const char *partition_name;
35 const char *mount_point;
36 const char *filesystem;
37} RootInfo;
38
39/* Canonical pointers.
40xxx may just want to use enums
41 */
42static const char g_mtd_device[] = "@\0g_mtd_device";
43static const char g_raw[] = "@\0g_raw";
44static const char g_package_file[] = "@\0g_package_file";
45
46static RootInfo g_roots[] = {
47 { "BOOT:", g_mtd_device, NULL, "boot", NULL, g_raw },
48 { "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" },
49 { "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" },
50 { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
51 { "PACKAGE:", NULL, NULL, NULL, NULL, g_package_file },
52 { "RECOVERY:", g_mtd_device, NULL, "recovery", "/", g_raw },
53 { "SDCARD:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
Koushik K. Dutta3f995392010-03-30 23:29:43 -070054 { "SDEXT:", "/dev/block/mmcblk0p2", NULL, NULL, "/sd-ext", "ext4" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055 { "SYSTEM:", g_mtd_device, NULL, "system", "/system", "yaffs2" },
Doug Zongkerb128f542009-06-18 15:07:14 -070056 { "MBM:", g_mtd_device, NULL, "mbm", NULL, g_raw },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057 { "TMP:", NULL, NULL, NULL, "/tmp", NULL },
58};
59#define NUM_ROOTS (sizeof(g_roots) / sizeof(g_roots[0]))
60
61// TODO: for SDCARD:, try /dev/block/mmcblk0 if mmcblk0p1 fails
62
63static const RootInfo *
64get_root_info_for_path(const char *root_path)
65{
66 const char *c;
67
68 /* Find the first colon.
69 */
70 c = root_path;
71 while (*c != '\0' && *c != ':') {
72 c++;
73 }
74 if (*c == '\0') {
75 return NULL;
76 }
77 size_t len = c - root_path + 1;
78 size_t i;
79 for (i = 0; i < NUM_ROOTS; i++) {
80 RootInfo *info = &g_roots[i];
81 if (strncmp(info->name, root_path, len) == 0) {
82 return info;
83 }
84 }
85 return NULL;
86}
87
88static const ZipArchive *g_package = NULL;
89static char *g_package_path = NULL;
90
91int
92register_package_root(const ZipArchive *package, const char *package_path)
93{
94 if (package != NULL) {
95 package_path = strdup(package_path);
96 if (package_path == NULL) {
97 return -1;
98 }
99 g_package_path = (char *)package_path;
100 } else {
101 free(g_package_path);
102 g_package_path = NULL;
103 }
104 g_package = package;
105 return 0;
106}
107
108int
109is_package_root_path(const char *root_path)
110{
111 const RootInfo *info = get_root_info_for_path(root_path);
112 return info != NULL && info->filesystem == g_package_file;
113}
114
115const char *
116translate_package_root_path(const char *root_path,
117 char *out_buf, size_t out_buf_len, const ZipArchive **out_package)
118{
119 const RootInfo *info = get_root_info_for_path(root_path);
120 if (info == NULL || info->filesystem != g_package_file) {
121 return NULL;
122 }
123
124 /* Strip the package root off of the path.
125 */
126 size_t root_len = strlen(info->name);
127 root_path += root_len;
128 size_t root_path_len = strlen(root_path);
129
130 if (out_buf_len < root_path_len + 1) {
131 return NULL;
132 }
133 strcpy(out_buf, root_path);
134 *out_package = g_package;
135 return out_buf;
136}
137
138/* Takes a string like "SYSTEM:lib" and turns it into a string
139 * like "/system/lib". The translated path is put in out_buf,
140 * and out_buf is returned if the translation succeeded.
141 */
142const char *
143translate_root_path(const char *root_path, char *out_buf, size_t out_buf_len)
144{
145 if (out_buf_len < 1) {
146 return NULL;
147 }
148
149 const RootInfo *info = get_root_info_for_path(root_path);
150 if (info == NULL || info->mount_point == NULL) {
151 return NULL;
152 }
153
154 /* Find the relative part of the non-root part of the path.
155 */
156 root_path += strlen(info->name); // strip off the "root:"
157 while (*root_path != '\0' && *root_path == '/') {
158 root_path++;
159 }
160
161 size_t mp_len = strlen(info->mount_point);
162 size_t rp_len = strlen(root_path);
163 if (mp_len + 1 + rp_len + 1 > out_buf_len) {
164 return NULL;
165 }
166
167 /* Glue the mount point to the relative part of the path.
168 */
169 memcpy(out_buf, info->mount_point, mp_len);
170 if (out_buf[mp_len - 1] != '/') out_buf[mp_len++] = '/';
171
172 memcpy(out_buf + mp_len, root_path, rp_len);
173 out_buf[mp_len + rp_len] = '\0';
174
175 return out_buf;
176}
177
178static int
179internal_root_mounted(const RootInfo *info)
180{
181 if (info->mount_point == NULL) {
182 return -1;
183 }
184//xxx if TMP: (or similar) just say "yes"
185
186 /* See if this root is already mounted.
187 */
188 int ret = scan_mounted_volumes();
189 if (ret < 0) {
190 return ret;
191 }
192 const MountedVolume *volume;
193 volume = find_mounted_volume_by_mount_point(info->mount_point);
194 if (volume != NULL) {
195 /* It's already mounted.
196 */
197 return 0;
198 }
199 return -1;
200}
201
202int
203is_root_path_mounted(const char *root_path)
204{
205 const RootInfo *info = get_root_info_for_path(root_path);
206 if (info == NULL) {
207 return -1;
208 }
209 return internal_root_mounted(info) >= 0;
210}
211
212int
213ensure_root_path_mounted(const char *root_path)
214{
215 const RootInfo *info = get_root_info_for_path(root_path);
216 if (info == NULL) {
217 return -1;
218 }
219
220 int ret = internal_root_mounted(info);
221 if (ret >= 0) {
222 /* It's already mounted.
223 */
224 return 0;
225 }
226
227 /* It's not mounted.
228 */
229 if (info->device == g_mtd_device) {
230 if (info->partition_name == NULL) {
231 return -1;
232 }
233//TODO: make the mtd stuff scan once when it needs to
234 mtd_scan_partitions();
235 const MtdPartition *partition;
236 partition = mtd_find_partition_by_name(info->partition_name);
237 if (partition == NULL) {
238 return -1;
239 }
240 return mtd_mount_partition(partition, info->mount_point,
241 info->filesystem, 0);
242 }
243
244 if (info->device == NULL || info->mount_point == NULL ||
245 info->filesystem == NULL ||
246 info->filesystem == g_raw ||
247 info->filesystem == g_package_file) {
248 return -1;
249 }
250
251 mkdir(info->mount_point, 0755); // in case it doesn't already exist
252 if (mount(info->device, info->mount_point, info->filesystem,
253 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
254 if (info->device2 == NULL) {
255 LOGE("Can't mount %s\n(%s)\n", info->device, strerror(errno));
256 return -1;
257 } else if (mount(info->device2, info->mount_point, info->filesystem,
258 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
259 LOGE("Can't mount %s (or %s)\n(%s)\n",
260 info->device, info->device2, strerror(errno));
261 return -1;
262 }
263 }
264 return 0;
265}
266
267int
268ensure_root_path_unmounted(const char *root_path)
269{
270 const RootInfo *info = get_root_info_for_path(root_path);
271 if (info == NULL) {
272 return -1;
273 }
274 if (info->mount_point == NULL) {
275 /* This root can't be mounted, so by definition it isn't.
276 */
277 return 0;
278 }
279//xxx if TMP: (or similar) just return error
280
281 /* See if this root is already mounted.
282 */
283 int ret = scan_mounted_volumes();
284 if (ret < 0) {
285 return ret;
286 }
287 const MountedVolume *volume;
288 volume = find_mounted_volume_by_mount_point(info->mount_point);
289 if (volume == NULL) {
290 /* It's not mounted.
291 */
292 return 0;
293 }
294
295 return unmount_mounted_volume(volume);
296}
297
298const MtdPartition *
299get_root_mtd_partition(const char *root_path)
300{
301 const RootInfo *info = get_root_info_for_path(root_path);
302 if (info == NULL || info->device != g_mtd_device ||
303 info->partition_name == NULL)
304 {
305 return NULL;
306 }
307 mtd_scan_partitions();
308 return mtd_find_partition_by_name(info->partition_name);
309}
310
311int
312format_root_device(const char *root)
313{
314 /* Be a little safer here; require that "root" is just
315 * a device with no relative path after it.
316 */
317 const char *c = root;
318 while (*c != '\0' && *c != ':') {
319 c++;
320 }
321 if (c[0] != ':' || c[1] != '\0') {
322 LOGW("format_root_device: bad root name \"%s\"\n", root);
323 return -1;
324 }
325
326 const RootInfo *info = get_root_info_for_path(root);
327 if (info == NULL || info->device == NULL) {
328 LOGW("format_root_device: can't resolve \"%s\"\n", root);
329 return -1;
330 }
331 if (info->mount_point != NULL) {
332 /* Don't try to format a mounted device.
333 */
334 int ret = ensure_root_path_unmounted(root);
335 if (ret < 0) {
336 LOGW("format_root_device: can't unmount \"%s\"\n", root);
337 return ret;
338 }
339 }
340
341 /* Format the device.
342 */
343 if (info->device == g_mtd_device) {
344 mtd_scan_partitions();
345 const MtdPartition *partition;
346 partition = mtd_find_partition_by_name(info->partition_name);
347 if (partition == NULL) {
348 LOGW("format_root_device: can't find mtd partition \"%s\"\n",
349 info->partition_name);
350 return -1;
351 }
352 if (info->filesystem == g_raw || !strcmp(info->filesystem, "yaffs2")) {
353 MtdWriteContext *write = mtd_write_partition(partition);
354 if (write == NULL) {
355 LOGW("format_root_device: can't open \"%s\"\n", root);
356 return -1;
357 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
358 LOGW("format_root_device: can't erase \"%s\"\n", root);
359 mtd_write_close(write);
360 return -1;
361 } else if (mtd_write_close(write)) {
362 LOGW("format_root_device: can't close \"%s\"\n", root);
363 return -1;
364 } else {
365 return 0;
366 }
367 }
368 }
369//TODO: handle other device types (sdcard, etc.)
370 LOGW("format_root_device: can't handle non-mtd device \"%s\"\n", root);
371 return -1;
372}