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