blob: a510145e50c1d80aed4ba8ab5891b35c9d8dd71a [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[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048 { "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" },
49 { "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" },
50 { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051 { "SDCARD:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
Doug Zongker23ceeea2010-07-08 17:27:55 -070052 { "TMP:", NULL, NULL, NULL, "/tmp", g_ramdisk },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053};
54#define NUM_ROOTS (sizeof(g_roots) / sizeof(g_roots[0]))
55
56// TODO: for SDCARD:, try /dev/block/mmcblk0 if mmcblk0p1 fails
57
58static const RootInfo *
59get_root_info_for_path(const char *root_path)
60{
61 const char *c;
62
63 /* Find the first colon.
64 */
65 c = root_path;
66 while (*c != '\0' && *c != ':') {
67 c++;
68 }
69 if (*c == '\0') {
70 return NULL;
71 }
72 size_t len = c - root_path + 1;
73 size_t i;
74 for (i = 0; i < NUM_ROOTS; i++) {
75 RootInfo *info = &g_roots[i];
76 if (strncmp(info->name, root_path, len) == 0) {
77 return info;
78 }
79 }
80 return NULL;
81}
82
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083/* Takes a string like "SYSTEM:lib" and turns it into a string
84 * like "/system/lib". The translated path is put in out_buf,
85 * and out_buf is returned if the translation succeeded.
86 */
87const char *
88translate_root_path(const char *root_path, char *out_buf, size_t out_buf_len)
89{
90 if (out_buf_len < 1) {
91 return NULL;
92 }
93
94 const RootInfo *info = get_root_info_for_path(root_path);
95 if (info == NULL || info->mount_point == NULL) {
96 return NULL;
97 }
98
99 /* Find the relative part of the non-root part of the path.
100 */
101 root_path += strlen(info->name); // strip off the "root:"
102 while (*root_path != '\0' && *root_path == '/') {
103 root_path++;
104 }
105
106 size_t mp_len = strlen(info->mount_point);
107 size_t rp_len = strlen(root_path);
108 if (mp_len + 1 + rp_len + 1 > out_buf_len) {
109 return NULL;
110 }
111
112 /* Glue the mount point to the relative part of the path.
113 */
114 memcpy(out_buf, info->mount_point, mp_len);
115 if (out_buf[mp_len - 1] != '/') out_buf[mp_len++] = '/';
116
117 memcpy(out_buf + mp_len, root_path, rp_len);
118 out_buf[mp_len + rp_len] = '\0';
119
120 return out_buf;
121}
122
123static int
124internal_root_mounted(const RootInfo *info)
125{
126 if (info->mount_point == NULL) {
127 return -1;
128 }
Doug Zongker23ceeea2010-07-08 17:27:55 -0700129 if (info->filesystem == g_ramdisk) {
130 return 0;
131 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800132
133 /* See if this root is already mounted.
134 */
135 int ret = scan_mounted_volumes();
136 if (ret < 0) {
137 return ret;
138 }
139 const MountedVolume *volume;
140 volume = find_mounted_volume_by_mount_point(info->mount_point);
141 if (volume != NULL) {
142 /* It's already mounted.
143 */
144 return 0;
145 }
146 return -1;
147}
148
149int
150is_root_path_mounted(const char *root_path)
151{
152 const RootInfo *info = get_root_info_for_path(root_path);
153 if (info == NULL) {
154 return -1;
155 }
156 return internal_root_mounted(info) >= 0;
157}
158
159int
160ensure_root_path_mounted(const char *root_path)
161{
162 const RootInfo *info = get_root_info_for_path(root_path);
163 if (info == NULL) {
164 return -1;
165 }
166
167 int ret = internal_root_mounted(info);
168 if (ret >= 0) {
169 /* It's already mounted.
170 */
171 return 0;
172 }
173
174 /* It's not mounted.
175 */
176 if (info->device == g_mtd_device) {
177 if (info->partition_name == NULL) {
178 return -1;
179 }
180//TODO: make the mtd stuff scan once when it needs to
181 mtd_scan_partitions();
182 const MtdPartition *partition;
183 partition = mtd_find_partition_by_name(info->partition_name);
184 if (partition == NULL) {
185 return -1;
186 }
187 return mtd_mount_partition(partition, info->mount_point,
188 info->filesystem, 0);
189 }
190
191 if (info->device == NULL || info->mount_point == NULL ||
192 info->filesystem == NULL ||
193 info->filesystem == g_raw ||
194 info->filesystem == g_package_file) {
195 return -1;
196 }
197
198 mkdir(info->mount_point, 0755); // in case it doesn't already exist
199 if (mount(info->device, info->mount_point, info->filesystem,
200 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
201 if (info->device2 == NULL) {
202 LOGE("Can't mount %s\n(%s)\n", info->device, strerror(errno));
203 return -1;
204 } else if (mount(info->device2, info->mount_point, info->filesystem,
205 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) {
206 LOGE("Can't mount %s (or %s)\n(%s)\n",
207 info->device, info->device2, strerror(errno));
208 return -1;
209 }
210 }
211 return 0;
212}
213
214int
215ensure_root_path_unmounted(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 if (info->mount_point == NULL) {
222 /* This root can't be mounted, so by definition it isn't.
223 */
224 return 0;
225 }
226//xxx if TMP: (or similar) just return error
227
228 /* See if this root is already mounted.
229 */
230 int ret = scan_mounted_volumes();
231 if (ret < 0) {
232 return ret;
233 }
234 const MountedVolume *volume;
235 volume = find_mounted_volume_by_mount_point(info->mount_point);
236 if (volume == NULL) {
237 /* It's not mounted.
238 */
239 return 0;
240 }
241
242 return unmount_mounted_volume(volume);
243}
244
245const MtdPartition *
246get_root_mtd_partition(const char *root_path)
247{
248 const RootInfo *info = get_root_info_for_path(root_path);
249 if (info == NULL || info->device != g_mtd_device ||
250 info->partition_name == NULL)
251 {
252 return NULL;
253 }
254 mtd_scan_partitions();
255 return mtd_find_partition_by_name(info->partition_name);
256}
257
258int
259format_root_device(const char *root)
260{
261 /* Be a little safer here; require that "root" is just
262 * a device with no relative path after it.
263 */
264 const char *c = root;
265 while (*c != '\0' && *c != ':') {
266 c++;
267 }
268 if (c[0] != ':' || c[1] != '\0') {
269 LOGW("format_root_device: bad root name \"%s\"\n", root);
270 return -1;
271 }
272
273 const RootInfo *info = get_root_info_for_path(root);
274 if (info == NULL || info->device == NULL) {
275 LOGW("format_root_device: can't resolve \"%s\"\n", root);
276 return -1;
277 }
278 if (info->mount_point != NULL) {
279 /* Don't try to format a mounted device.
280 */
281 int ret = ensure_root_path_unmounted(root);
282 if (ret < 0) {
283 LOGW("format_root_device: can't unmount \"%s\"\n", root);
284 return ret;
285 }
286 }
287
288 /* Format the device.
289 */
290 if (info->device == g_mtd_device) {
291 mtd_scan_partitions();
292 const MtdPartition *partition;
293 partition = mtd_find_partition_by_name(info->partition_name);
294 if (partition == NULL) {
295 LOGW("format_root_device: can't find mtd partition \"%s\"\n",
296 info->partition_name);
297 return -1;
298 }
299 if (info->filesystem == g_raw || !strcmp(info->filesystem, "yaffs2")) {
300 MtdWriteContext *write = mtd_write_partition(partition);
301 if (write == NULL) {
302 LOGW("format_root_device: can't open \"%s\"\n", root);
303 return -1;
304 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
305 LOGW("format_root_device: can't erase \"%s\"\n", root);
306 mtd_write_close(write);
307 return -1;
308 } else if (mtd_write_close(write)) {
309 LOGW("format_root_device: can't close \"%s\"\n", root);
310 return -1;
311 } else {
312 return 0;
313 }
314 }
315 }
316//TODO: handle other device types (sdcard, etc.)
317 LOGW("format_root_device: can't handle non-mtd device \"%s\"\n", root);
318 return -1;
319}