blob: d5754db3aa37c14e87eaeaf229fb40272f2101c2 [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";
Doug Zongker23ceeea2010-07-08 17:27:55 -070045static const char g_ramdisk[] = "@\0g_ramdisk";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046
47static RootInfo g_roots[] = {
48 { "BOOT:", g_mtd_device, NULL, "boot", NULL, g_raw },
49 { "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" },
50 { "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" },
51 { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
52 { "PACKAGE:", NULL, NULL, NULL, NULL, g_package_file },
53 { "RECOVERY:", g_mtd_device, NULL, "recovery", "/", g_raw },
54 { "SDCARD:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
55 { "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 },
Doug Zongker23ceeea2010-07-08 17:27:55 -070057 { "TMP:", NULL, NULL, NULL, "/tmp", g_ramdisk },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058};
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 }
Doug Zongker23ceeea2010-07-08 17:27:55 -0700184 if (info->filesystem == g_ramdisk) {
185 return 0;
186 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187
188 /* See if this root is already mounted.
189 */
190 int ret = scan_mounted_volumes();
191 if (ret < 0) {
192 return ret;
193 }
194 const MountedVolume *volume;
195 volume = find_mounted_volume_by_mount_point(info->mount_point);
196 if (volume != NULL) {
197 /* It's already mounted.
198 */
199 return 0;
200 }
201 return -1;
202}
203
204int
205is_root_path_mounted(const char *root_path)
206{
207 const RootInfo *info = get_root_info_for_path(root_path);
208 if (info == NULL) {
209 return -1;
210 }
211 return internal_root_mounted(info) >= 0;
212}
213
214int
215ensure_root_path_mounted(const char *root_path)
216{
217 const RootInfo *info = get_root_info_for_path(root_path);
218 if (info == NULL) {
219 return -1;
220 }
221
222 int ret = internal_root_mounted(info);
223 if (ret >= 0) {
224 /* It's already mounted.
225 */
226 return 0;
227 }
228
229 /* It's not mounted.
230 */
231 if (info->device == g_mtd_device) {
232 if (info->partition_name == NULL) {
233 return -1;
234 }
235//TODO: make the mtd stuff scan once when it needs to
236 mtd_scan_partitions();
237 const MtdPartition *partition;
238 partition = mtd_find_partition_by_name(info->partition_name);
239 if (partition == NULL) {
240 return -1;
241 }
242 return mtd_mount_partition(partition, info->mount_point,
243 info->filesystem, 0);
244 }
245
246 if (info->device == NULL || info->mount_point == NULL ||
247 info->filesystem == NULL ||
248 info->filesystem == g_raw ||
249 info->filesystem == g_package_file) {
250 return -1;
251 }
252
253 mkdir(info->mount_point, 0755); // in case it doesn't already exist
254 if (mount(info->device, info->mount_point, info->filesystem,
255 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
256 if (info->device2 == NULL) {
257 LOGE("Can't mount %s\n(%s)\n", info->device, strerror(errno));
258 return -1;
259 } else if (mount(info->device2, info->mount_point, info->filesystem,
260 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
261 LOGE("Can't mount %s (or %s)\n(%s)\n",
262 info->device, info->device2, strerror(errno));
263 return -1;
264 }
265 }
266 return 0;
267}
268
269int
270ensure_root_path_unmounted(const char *root_path)
271{
272 const RootInfo *info = get_root_info_for_path(root_path);
273 if (info == NULL) {
274 return -1;
275 }
276 if (info->mount_point == NULL) {
277 /* This root can't be mounted, so by definition it isn't.
278 */
279 return 0;
280 }
281//xxx if TMP: (or similar) just return error
282
283 /* See if this root is already mounted.
284 */
285 int ret = scan_mounted_volumes();
286 if (ret < 0) {
287 return ret;
288 }
289 const MountedVolume *volume;
290 volume = find_mounted_volume_by_mount_point(info->mount_point);
291 if (volume == NULL) {
292 /* It's not mounted.
293 */
294 return 0;
295 }
296
297 return unmount_mounted_volume(volume);
298}
299
300const MtdPartition *
301get_root_mtd_partition(const char *root_path)
302{
303 const RootInfo *info = get_root_info_for_path(root_path);
304 if (info == NULL || info->device != g_mtd_device ||
305 info->partition_name == NULL)
306 {
307 return NULL;
308 }
309 mtd_scan_partitions();
310 return mtd_find_partition_by_name(info->partition_name);
311}
312
313int
314format_root_device(const char *root)
315{
316 /* Be a little safer here; require that "root" is just
317 * a device with no relative path after it.
318 */
319 const char *c = root;
320 while (*c != '\0' && *c != ':') {
321 c++;
322 }
323 if (c[0] != ':' || c[1] != '\0') {
324 LOGW("format_root_device: bad root name \"%s\"\n", root);
325 return -1;
326 }
327
328 const RootInfo *info = get_root_info_for_path(root);
329 if (info == NULL || info->device == NULL) {
330 LOGW("format_root_device: can't resolve \"%s\"\n", root);
331 return -1;
332 }
333 if (info->mount_point != NULL) {
334 /* Don't try to format a mounted device.
335 */
336 int ret = ensure_root_path_unmounted(root);
337 if (ret < 0) {
338 LOGW("format_root_device: can't unmount \"%s\"\n", root);
339 return ret;
340 }
341 }
342
343 /* Format the device.
344 */
345 if (info->device == g_mtd_device) {
346 mtd_scan_partitions();
347 const MtdPartition *partition;
348 partition = mtd_find_partition_by_name(info->partition_name);
349 if (partition == NULL) {
350 LOGW("format_root_device: can't find mtd partition \"%s\"\n",
351 info->partition_name);
352 return -1;
353 }
354 if (info->filesystem == g_raw || !strcmp(info->filesystem, "yaffs2")) {
355 MtdWriteContext *write = mtd_write_partition(partition);
356 if (write == NULL) {
357 LOGW("format_root_device: can't open \"%s\"\n", root);
358 return -1;
359 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
360 LOGW("format_root_device: can't erase \"%s\"\n", root);
361 mtd_write_close(write);
362 return -1;
363 } else if (mtd_write_close(write)) {
364 LOGW("format_root_device: can't close \"%s\"\n", root);
365 return -1;
366 } else {
367 return 0;
368 }
369 }
370 }
371//TODO: handle other device types (sdcard, etc.)
372 LOGW("format_root_device: can't handle non-mtd device \"%s\"\n", root);
373 return -1;
374}