blob: abfb4ce7781dae641aa548fd9fa47b74a9545753 [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
Koushik Dutta2f73e582010-04-18 16:00:21 -070030#include "extendedcommands.h"
31
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032typedef struct {
33 const char *name;
34 const char *device;
35 const char *device2; // If the first one doesn't work (may be NULL)
36 const char *partition_name;
37 const char *mount_point;
38 const char *filesystem;
39} RootInfo;
40
41/* Canonical pointers.
42xxx may just want to use enums
43 */
44static const char g_mtd_device[] = "@\0g_mtd_device";
45static const char g_raw[] = "@\0g_raw";
46static const char g_package_file[] = "@\0g_package_file";
47
48static RootInfo g_roots[] = {
49 { "BOOT:", g_mtd_device, NULL, "boot", NULL, g_raw },
50 { "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" },
51 { "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" },
52 { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
53 { "PACKAGE:", NULL, NULL, NULL, NULL, g_package_file },
54 { "RECOVERY:", g_mtd_device, NULL, "recovery", "/", g_raw },
55 { "SDCARD:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
Koushik K. Dutta3f995392010-03-30 23:29:43 -070056 { "SDEXT:", "/dev/block/mmcblk0p2", NULL, NULL, "/sd-ext", "ext4" },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057 { "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 },
60};
61#define NUM_ROOTS (sizeof(g_roots) / sizeof(g_roots[0]))
62
63// TODO: for SDCARD:, try /dev/block/mmcblk0 if mmcblk0p1 fails
64
65static const RootInfo *
66get_root_info_for_path(const char *root_path)
67{
68 const char *c;
69
70 /* Find the first colon.
71 */
72 c = root_path;
73 while (*c != '\0' && *c != ':') {
74 c++;
75 }
76 if (*c == '\0') {
77 return NULL;
78 }
79 size_t len = c - root_path + 1;
80 size_t i;
81 for (i = 0; i < NUM_ROOTS; i++) {
82 RootInfo *info = &g_roots[i];
83 if (strncmp(info->name, root_path, len) == 0) {
84 return info;
85 }
86 }
87 return NULL;
88}
89
90static const ZipArchive *g_package = NULL;
91static char *g_package_path = NULL;
92
93int
94register_package_root(const ZipArchive *package, const char *package_path)
95{
96 if (package != NULL) {
97 package_path = strdup(package_path);
98 if (package_path == NULL) {
99 return -1;
100 }
101 g_package_path = (char *)package_path;
102 } else {
103 free(g_package_path);
104 g_package_path = NULL;
105 }
106 g_package = package;
107 return 0;
108}
109
110int
111is_package_root_path(const char *root_path)
112{
113 const RootInfo *info = get_root_info_for_path(root_path);
114 return info != NULL && info->filesystem == g_package_file;
115}
116
117const char *
118translate_package_root_path(const char *root_path,
119 char *out_buf, size_t out_buf_len, const ZipArchive **out_package)
120{
121 const RootInfo *info = get_root_info_for_path(root_path);
122 if (info == NULL || info->filesystem != g_package_file) {
123 return NULL;
124 }
125
126 /* Strip the package root off of the path.
127 */
128 size_t root_len = strlen(info->name);
129 root_path += root_len;
130 size_t root_path_len = strlen(root_path);
131
132 if (out_buf_len < root_path_len + 1) {
133 return NULL;
134 }
135 strcpy(out_buf, root_path);
136 *out_package = g_package;
137 return out_buf;
138}
139
140/* Takes a string like "SYSTEM:lib" and turns it into a string
141 * like "/system/lib". The translated path is put in out_buf,
142 * and out_buf is returned if the translation succeeded.
143 */
144const char *
145translate_root_path(const char *root_path, char *out_buf, size_t out_buf_len)
146{
147 if (out_buf_len < 1) {
148 return NULL;
149 }
150
151 const RootInfo *info = get_root_info_for_path(root_path);
152 if (info == NULL || info->mount_point == NULL) {
153 return NULL;
154 }
155
156 /* Find the relative part of the non-root part of the path.
157 */
158 root_path += strlen(info->name); // strip off the "root:"
159 while (*root_path != '\0' && *root_path == '/') {
160 root_path++;
161 }
162
163 size_t mp_len = strlen(info->mount_point);
164 size_t rp_len = strlen(root_path);
165 if (mp_len + 1 + rp_len + 1 > out_buf_len) {
166 return NULL;
167 }
168
169 /* Glue the mount point to the relative part of the path.
170 */
171 memcpy(out_buf, info->mount_point, mp_len);
172 if (out_buf[mp_len - 1] != '/') out_buf[mp_len++] = '/';
173
174 memcpy(out_buf + mp_len, root_path, rp_len);
175 out_buf[mp_len + rp_len] = '\0';
176
177 return out_buf;
178}
179
180static int
181internal_root_mounted(const RootInfo *info)
182{
183 if (info->mount_point == NULL) {
184 return -1;
185 }
186//xxx if TMP: (or similar) just say "yes"
187
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 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700371
372 return format_non_mtd_device(root);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800373}