blob: 5369d2ff2e5dbb07779cfde4de700fc858e7c018 [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";
Doug Zongker23ceeea2010-07-08 17:27:55 -070050static const char g_ramdisk[] = "@\0g_ramdisk";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
52static RootInfo g_roots[] = {
53 { "BOOT:", g_mtd_device, NULL, "boot", NULL, g_raw },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054 { "PACKAGE:", NULL, NULL, NULL, NULL, g_package_file },
55 { "RECOVERY:", g_mtd_device, NULL, "recovery", "/", g_raw },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056 { "SYSTEM:", g_mtd_device, NULL, "system", "/system", "yaffs2" },
Doug Zongkerb128f542009-06-18 15:07:14 -070057 { "MBM:", g_mtd_device, NULL, "mbm", NULL, g_raw },
Doug Zongker23ceeea2010-07-08 17:27:55 -070058 { "TMP:", NULL, NULL, NULL, "/tmp", g_ramdisk },
Doug Zongker49c73a72010-06-29 17:36:28 -070059
60#ifdef USE_EXT4
61 { "CACHE:", "/dev/block/platform/sdhci-tegra.3/by-name/cache", NULL, NULL,
62 "/cache", "ext4" },
63 { "DATA:", "/dev/block/platform/sdhci-tegra.3/by-name/userdata", NULL, NULL,
64 "/data", "ext4" },
Doug Zongkerdc9e87c2010-07-29 17:08:50 -070065 { "EXT:", "/dev/block/sda1", NULL, NULL, "/sdcard", "vfat" },
Doug Zongker49c73a72010-06-29 17:36:28 -070066#else
67 { "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" },
68 { "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" },
Doug Zongkerdc9e87c2010-07-29 17:08:50 -070069 { "EXT:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
Doug Zongker04611da2010-08-12 15:35:29 -070070 { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
Doug Zongker49c73a72010-06-29 17:36:28 -070071#endif
72
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073};
74#define NUM_ROOTS (sizeof(g_roots) / sizeof(g_roots[0]))
75
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076static 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 }
Doug Zongker23ceeea2010-07-08 17:27:55 -0700197 if (info->filesystem == g_ramdisk) {
198 return 0;
199 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200
201 /* See if this root is already mounted.
202 */
203 int ret = scan_mounted_volumes();
204 if (ret < 0) {
205 return ret;
206 }
207 const MountedVolume *volume;
208 volume = find_mounted_volume_by_mount_point(info->mount_point);
209 if (volume != NULL) {
210 /* It's already mounted.
211 */
212 return 0;
213 }
214 return -1;
215}
216
217int
218is_root_path_mounted(const char *root_path)
219{
220 const RootInfo *info = get_root_info_for_path(root_path);
221 if (info == NULL) {
222 return -1;
223 }
224 return internal_root_mounted(info) >= 0;
225}
226
227int
228ensure_root_path_mounted(const char *root_path)
229{
230 const RootInfo *info = get_root_info_for_path(root_path);
231 if (info == NULL) {
232 return -1;
233 }
234
235 int ret = internal_root_mounted(info);
236 if (ret >= 0) {
237 /* It's already mounted.
238 */
239 return 0;
240 }
241
242 /* It's not mounted.
243 */
244 if (info->device == g_mtd_device) {
245 if (info->partition_name == NULL) {
246 return -1;
247 }
248//TODO: make the mtd stuff scan once when it needs to
249 mtd_scan_partitions();
250 const MtdPartition *partition;
251 partition = mtd_find_partition_by_name(info->partition_name);
252 if (partition == NULL) {
253 return -1;
254 }
255 return mtd_mount_partition(partition, info->mount_point,
256 info->filesystem, 0);
257 }
258
259 if (info->device == NULL || info->mount_point == NULL ||
260 info->filesystem == NULL ||
261 info->filesystem == g_raw ||
262 info->filesystem == g_package_file) {
263 return -1;
264 }
265
266 mkdir(info->mount_point, 0755); // in case it doesn't already exist
267 if (mount(info->device, info->mount_point, info->filesystem,
Doug Zongker49c73a72010-06-29 17:36:28 -0700268 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269 if (info->device2 == NULL) {
270 LOGE("Can't mount %s\n(%s)\n", info->device, strerror(errno));
271 return -1;
272 } else if (mount(info->device2, info->mount_point, info->filesystem,
273 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
274 LOGE("Can't mount %s (or %s)\n(%s)\n",
275 info->device, info->device2, strerror(errno));
276 return -1;
277 }
278 }
279 return 0;
280}
281
282int
283ensure_root_path_unmounted(const char *root_path)
284{
285 const RootInfo *info = get_root_info_for_path(root_path);
286 if (info == NULL) {
287 return -1;
288 }
289 if (info->mount_point == NULL) {
290 /* This root can't be mounted, so by definition it isn't.
291 */
292 return 0;
293 }
294//xxx if TMP: (or similar) just return error
295
296 /* See if this root is already mounted.
297 */
298 int ret = scan_mounted_volumes();
299 if (ret < 0) {
300 return ret;
301 }
302 const MountedVolume *volume;
303 volume = find_mounted_volume_by_mount_point(info->mount_point);
304 if (volume == NULL) {
305 /* It's not mounted.
306 */
307 return 0;
308 }
309
310 return unmount_mounted_volume(volume);
311}
312
313const MtdPartition *
314get_root_mtd_partition(const char *root_path)
315{
316 const RootInfo *info = get_root_info_for_path(root_path);
317 if (info == NULL || info->device != g_mtd_device ||
318 info->partition_name == NULL)
319 {
320 return NULL;
321 }
322 mtd_scan_partitions();
323 return mtd_find_partition_by_name(info->partition_name);
324}
325
326int
327format_root_device(const char *root)
328{
329 /* Be a little safer here; require that "root" is just
330 * a device with no relative path after it.
331 */
332 const char *c = root;
333 while (*c != '\0' && *c != ':') {
334 c++;
335 }
336 if (c[0] != ':' || c[1] != '\0') {
337 LOGW("format_root_device: bad root name \"%s\"\n", root);
338 return -1;
339 }
340
341 const RootInfo *info = get_root_info_for_path(root);
342 if (info == NULL || info->device == NULL) {
343 LOGW("format_root_device: can't resolve \"%s\"\n", root);
344 return -1;
345 }
346 if (info->mount_point != NULL) {
347 /* Don't try to format a mounted device.
348 */
349 int ret = ensure_root_path_unmounted(root);
350 if (ret < 0) {
351 LOGW("format_root_device: can't unmount \"%s\"\n", root);
352 return ret;
353 }
354 }
355
356 /* Format the device.
357 */
358 if (info->device == g_mtd_device) {
359 mtd_scan_partitions();
360 const MtdPartition *partition;
361 partition = mtd_find_partition_by_name(info->partition_name);
362 if (partition == NULL) {
363 LOGW("format_root_device: can't find mtd partition \"%s\"\n",
364 info->partition_name);
365 return -1;
366 }
367 if (info->filesystem == g_raw || !strcmp(info->filesystem, "yaffs2")) {
368 MtdWriteContext *write = mtd_write_partition(partition);
369 if (write == NULL) {
370 LOGW("format_root_device: can't open \"%s\"\n", root);
371 return -1;
372 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
373 LOGW("format_root_device: can't erase \"%s\"\n", root);
374 mtd_write_close(write);
375 return -1;
376 } else if (mtd_write_close(write)) {
377 LOGW("format_root_device: can't close \"%s\"\n", root);
378 return -1;
379 } else {
380 return 0;
381 }
382 }
383 }
Doug Zongker49c73a72010-06-29 17:36:28 -0700384
385#ifdef USE_EXT4
386 if (strcmp(info->filesystem, "ext4") == 0) {
Doug Zongker04611da2010-08-12 15:35:29 -0700387 LOGW("starting to reformat ext4\n");
Doug Zongker49c73a72010-06-29 17:36:28 -0700388 reset_ext4fs_info();
Ken Sumrall81011252010-08-13 16:08:03 -0700389 int result = make_ext4fs(info->device, NULL, NULL, 0, 0, 0);
Doug Zongker04611da2010-08-12 15:35:29 -0700390 LOGW("finished reformat ext4: result = %d\n", result);
Doug Zongker49c73a72010-06-29 17:36:28 -0700391 if (result != 0) {
392 LOGW("make_ext4fs failed: %d\n", result);
393 return -1;
394 }
395 return 0;
396 }
397#endif
398
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800399//TODO: handle other device types (sdcard, etc.)
Doug Zongker49c73a72010-06-29 17:36:28 -0700400
401 LOGW("format_root_device: unknown device \"%s\"\n", root);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800402 return -1;
403}