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