blob: dbb04bb9cadfc21c2d166eea96537fca0bf4aa72 [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>
Doug Zongkerd4208f92010-09-20 12:16:13 -070023#include <ctype.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024
25#include "mtdutils/mtdutils.h"
Koushik Duttadf1e4062010-12-18 17:42:31 -080026#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include "roots.h"
28#include "common.h"
Doug Zongkerd4208f92010-09-20 12:16:13 -070029#include "make_ext4fs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030
Koushik Dutta7adeadc2011-05-26 11:47:56 -070031#include "flashutils/flashutils.h"
32#include "extendedcommands.h"
33
Kolja Dummann92796ec2011-02-13 20:59:30 +010034int num_volumes;
35Volume* device_volumes;
36
37int get_num_volumes() {
38 return num_volumes;
39}
40
41Volume* get_device_volumes() {
42 return device_volumes;
43}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Koushik Duttae734dad2011-03-02 12:32:13 -080045static int is_null(const char* sz) {
46 if (sz == NULL)
47 return 1;
48 if (strcmp("NULL", sz) == 0)
49 return 1;
50 return 0;
51}
52
53static char* dupe_string(const char* sz) {
54 if (is_null(sz))
55 return NULL;
56 return strdup(sz);
57}
58
Doug Zongker2810ced2011-02-17 15:55:21 -080059static int parse_options(char* options, Volume* volume) {
60 char* option;
61 while (option = strtok(options, ",")) {
62 options = NULL;
63
64 if (strncmp(option, "length=", 7) == 0) {
65 volume->length = strtoll(option+7, NULL, 10);
Koushik Dutta0df56f42011-11-16 16:00:35 -080066 } else if (strncmp(option, "fstype2=", 8) == 0) {
67 volume->fs_type2 = volume->fs_type;
68 volume->fs_type = strdup(option);
69 } else if (strncmp(option, "fs_options=", 11) == 0) {
70 volume->fs_options = strdup(option);
71 } else if (strncmp(option, "fs_options2=", 12) == 0) {
72 volume->fs_options2 = strdup(option);
Doug Zongker2810ced2011-02-17 15:55:21 -080073 } else {
74 LOGE("bad option \"%s\"\n", option);
75 return -1;
76 }
77 }
78 return 0;
79}
80
Doug Zongkerd4208f92010-09-20 12:16:13 -070081void load_volume_table() {
82 int alloc = 2;
83 device_volumes = malloc(alloc * sizeof(Volume));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084
Doug Zongkerc18eeb82010-09-21 16:49:26 -070085 // Insert an entry for /tmp, which is the ramdisk and is always mounted.
86 device_volumes[0].mount_point = "/tmp";
87 device_volumes[0].fs_type = "ramdisk";
88 device_volumes[0].device = NULL;
89 device_volumes[0].device2 = NULL;
Koushik Dutta0df56f42011-11-16 16:00:35 -080090 device_volumes[0].fs_type2 = NULL;
Koushik Duttae2b929d2011-03-01 21:41:54 -080091 device_volumes[0].fs_options = NULL;
92 device_volumes[0].fs_options2 = NULL;
Doug Zongker2810ced2011-02-17 15:55:21 -080093 device_volumes[0].length = 0;
Doug Zongkerc18eeb82010-09-21 16:49:26 -070094 num_volumes = 1;
95
Doug Zongkerd4208f92010-09-20 12:16:13 -070096 FILE* fstab = fopen("/etc/recovery.fstab", "r");
97 if (fstab == NULL) {
98 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
99 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101
102 char buffer[1024];
103 int i;
104 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
105 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
106 if (buffer[i] == '\0' || buffer[i] == '#') continue;
107
108 char* original = strdup(buffer);
109
110 char* mount_point = strtok(buffer+i, " \t\n");
111 char* fs_type = strtok(NULL, " \t\n");
112 char* device = strtok(NULL, " \t\n");
113 // lines may optionally have a second device, to use if
114 // mounting the first one fails.
Doug Zongker2810ced2011-02-17 15:55:21 -0800115 char* options = NULL;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700116 char* device2 = strtok(NULL, " \t\n");
Doug Zongker2810ced2011-02-17 15:55:21 -0800117 if (device2) {
118 if (device2[0] == '/') {
119 options = strtok(NULL, " \t\n");
120 } else {
121 options = device2;
122 device2 = NULL;
123 }
124 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700125
126 if (mount_point && fs_type && device) {
127 while (num_volumes >= alloc) {
128 alloc *= 2;
129 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
130 }
131 device_volumes[num_volumes].mount_point = strdup(mount_point);
132 device_volumes[num_volumes].fs_type = strdup(fs_type);
133 device_volumes[num_volumes].device = strdup(device);
134 device_volumes[num_volumes].device2 =
135 device2 ? strdup(device2) : NULL;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800136
Pawit Pornkitprasanff316eb2011-11-19 13:31:21 +0700137 device_volumes[num_volumes].fs_type2 = NULL;
138 device_volumes[num_volumes].fs_options = NULL;
139 device_volumes[num_volumes].fs_options2 = NULL;
Doug Zongker2810ced2011-02-17 15:55:21 -0800140 device_volumes[num_volumes].length = 0;
Koushik Duttaddc12412011-11-23 14:06:12 -0800141
142 device_volumes[num_volumes].fs_type2 = NULL;
143 device_volumes[num_volumes].fs_options = NULL;
144 device_volumes[num_volumes].fs_options2 = NULL;
145
Doug Zongker2810ced2011-02-17 15:55:21 -0800146 if (parse_options(options, device_volumes + num_volumes) != 0) {
147 LOGE("skipping malformed recovery.fstab line: %s\n", original);
148 } else {
149 ++num_volumes;
Koushik Duttae734dad2011-03-02 12:32:13 -0800150 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700151 } else {
152 LOGE("skipping malformed recovery.fstab line: %s\n", original);
153 }
154 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700156
157 fclose(fstab);
158
159 printf("recovery filesystem table\n");
160 printf("=========================\n");
161 for (i = 0; i < num_volumes; ++i) {
162 Volume* v = &device_volumes[i];
Doug Zongker2810ced2011-02-17 15:55:21 -0800163 printf(" %d %s %s %s %s %lld\n", i, v->mount_point, v->fs_type,
164 v->device, v->device2, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700165 }
166 printf("\n");
167}
168
169Volume* volume_for_path(const char* path) {
170 int i;
171 for (i = 0; i < num_volumes; ++i) {
172 Volume* v = device_volumes+i;
173 int len = strlen(v->mount_point);
174 if (strncmp(path, v->mount_point, len) == 0 &&
175 (path[len] == '\0' || path[len] == '/')) {
176 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177 }
178 }
179 return NULL;
180}
181
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800182int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) {
Koushik Dutta64d79c62011-02-09 06:12:44 -0800183 if (device == NULL || mount_point == NULL || fs_type == NULL)
184 return -1;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800185 int ret = 0;
186 if (fs_options == NULL) {
187 ret = mount(device, mount_point, fs_type,
Koushik Dutta64d79c62011-02-09 06:12:44 -0800188 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800189 }
190 else {
191 char mount_cmd[PATH_MAX];
Koushik Dutta7161f352011-02-27 17:00:47 -0800192 sprintf(mount_cmd, "mount -t %s -o%s %s %s", fs_type, fs_options, device, mount_point);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800193 ret = __system(mount_cmd);
194 }
Koushik Dutta64d79c62011-02-09 06:12:44 -0800195 if (ret == 0)
196 return 0;
197 LOGW("failed to mount %s (%s)\n", device, strerror(errno));
198 return ret;
199}
200
Koushik Dutta38a92142011-06-10 09:45:52 -0700201int is_data_media() {
202 Volume *data = volume_for_path("/data");
203 return data != NULL && strcmp(data->fs_type, "auto") == 0 && volume_for_path("/sdcard") == NULL;
204}
205
206void setup_data_media() {
207 rmdir("/sdcard");
208 mkdir("/data/media", 0755);
209 symlink("/data/media", "/sdcard");
210}
211
Doug Zongkerd4208f92010-09-20 12:16:13 -0700212int ensure_path_mounted(const char* path) {
Koushik Dutta52681312011-08-31 23:26:45 -0700213 return ensure_path_mounted_at_mount_point(path, NULL);
214}
215
216int ensure_path_mounted_at_mount_point(const char* path, const char* mount_point) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700217 Volume* v = volume_for_path(path);
218 if (v == NULL) {
Koushik Dutta50822992011-06-08 19:03:27 -0700219 // no /sdcard? let's assume /data/media
Koushik Dutta38a92142011-06-10 09:45:52 -0700220 if (strstr(path, "/sdcard") == path && is_data_media()) {
Koushik Duttaddc12412011-11-23 14:06:12 -0800221 LOGI("using /data/media, no /sdcard found.\n");
Koushik Dutta50822992011-06-08 19:03:27 -0700222 int ret;
223 if (0 != (ret = ensure_path_mounted("/data")))
224 return ret;
Koushik Dutta38a92142011-06-10 09:45:52 -0700225 setup_data_media();
Koushik Dutta50822992011-06-08 19:03:27 -0700226 return 0;
227 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700228 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229 return -1;
230 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700231 if (strcmp(v->fs_type, "ramdisk") == 0) {
232 // the ramdisk is always mounted.
233 return 0;
234 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700235
236 int result;
237 result = scan_mounted_volumes();
238 if (result < 0) {
239 LOGE("failed to scan mounted volumes\n");
240 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700241 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800242
Koushik Dutta52681312011-08-31 23:26:45 -0700243 if (NULL == mount_point)
244 mount_point = v->mount_point;
245
Doug Zongkerd4208f92010-09-20 12:16:13 -0700246 const MountedVolume* mv =
Koushik Dutta52681312011-08-31 23:26:45 -0700247 find_mounted_volume_by_mount_point(mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700248 if (mv) {
249 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250 return 0;
251 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700252
Koushik Dutta52681312011-08-31 23:26:45 -0700253 mkdir(mount_point, 0755); // in case it doesn't already exist
Doug Zongkerd4208f92010-09-20 12:16:13 -0700254
255 if (strcmp(v->fs_type, "yaffs2") == 0) {
256 // mount an MTD partition as a YAFFS2 filesystem.
257 mtd_scan_partitions();
258 const MtdPartition* partition;
259 partition = mtd_find_partition_by_name(v->device);
260 if (partition == NULL) {
261 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Koushik Dutta52681312011-08-31 23:26:45 -0700262 v->device, mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700263 return -1;
264 }
Koushik Dutta52681312011-08-31 23:26:45 -0700265 return mtd_mount_partition(partition, mount_point, v->fs_type, 0);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700266 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800267 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800268 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700269 strcmp(v->fs_type, "vfat") == 0) {
Koushik Dutta52681312011-08-31 23:26:45 -0700270 if ((result = try_mount(v->device, mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800271 return 0;
Koushik Dutta52681312011-08-31 23:26:45 -0700272 if ((result = try_mount(v->device2, mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800273 return 0;
Koushik Dutta52681312011-08-31 23:26:45 -0700274 if ((result = try_mount(v->device, mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800275 return 0;
Koushik Dutta52681312011-08-31 23:26:45 -0700276 if ((result = try_mount(v->device2, mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800277 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800278 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800279 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800280 // let's try mounting with the mount binary and hope for the best.
281 char mount_cmd[PATH_MAX];
282 sprintf(mount_cmd, "mount %s", path);
283 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700284 }
285
Koushik Dutta52681312011-08-31 23:26:45 -0700286 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287 return -1;
288}
289
Doug Zongkerd4208f92010-09-20 12:16:13 -0700290int ensure_path_unmounted(const char* path) {
Koushik Dutta50822992011-06-08 19:03:27 -0700291 // if we are using /data/media, do not ever unmount volumes /data or /sdcard
292 if (volume_for_path("/sdcard") == NULL && (strstr(path, "/sdcard") == path || strstr(path, "/data") == path)) {
293 return 0;
294 }
295
Doug Zongkerd4208f92010-09-20 12:16:13 -0700296 Volume* v = volume_for_path(path);
297 if (v == NULL) {
Koushik Dutta50822992011-06-08 19:03:27 -0700298 // no /sdcard? let's assume /data/media
Koushik Dutta38a92142011-06-10 09:45:52 -0700299 if (strstr(path, "/sdcard") == path && is_data_media()) {
Koushik Dutta50822992011-06-08 19:03:27 -0700300 return ensure_path_unmounted("/data");
301 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700302 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303 return -1;
304 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700305 if (strcmp(v->fs_type, "ramdisk") == 0) {
306 // the ramdisk is always mounted; you can't unmount it.
307 return -1;
308 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800309
Doug Zongkerd4208f92010-09-20 12:16:13 -0700310 int result;
311 result = scan_mounted_volumes();
312 if (result < 0) {
313 LOGE("failed to scan mounted volumes\n");
314 return -1;
315 }
316
317 const MountedVolume* mv =
318 find_mounted_volume_by_mount_point(v->mount_point);
319 if (mv == NULL) {
320 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321 return 0;
322 }
323
Doug Zongkerd4208f92010-09-20 12:16:13 -0700324 return unmount_mounted_volume(mv);
325}
326
327int format_volume(const char* volume) {
328 Volume* v = volume_for_path(volume);
329 if (v == NULL) {
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700330 // no /sdcard? let's assume /data/media
Koushik Dutta7905c802011-06-15 00:00:55 -0700331 if (strstr(volume, "/sdcard") == volume && is_data_media()) {
332 return format_unknown_device(NULL, volume, NULL);
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700333 }
Koushik Duttabf4444f2010-12-18 18:57:47 -0800334 // silent failure for sd-ext
335 if (strcmp(volume, "/sd-ext") == 0)
336 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 LOGE("unknown volume \"%s\"\n", volume);
338 return -1;
339 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700340 if (strcmp(v->fs_type, "ramdisk") == 0) {
341 // you can't format the ramdisk.
342 LOGE("can't format_volume \"%s\"", volume);
343 return -1;
344 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700345 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800346#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700347 LOGE("can't give path \"%s\" to format_volume\n", volume);
348 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800349#endif
350 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700351 }
352
353 if (ensure_path_unmounted(volume) != 0) {
354 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
355 return -1;
356 }
357
358 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800359 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700360 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800361 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700362 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800363 return -1;
364 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800365
Doug Zongkerd4208f92010-09-20 12:16:13 -0700366 MtdWriteContext *write = mtd_write_partition(partition);
367 if (write == NULL) {
368 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800369 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700370 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
371 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
372 mtd_write_close(write);
373 return -1;
374 } else if (mtd_write_close(write)) {
375 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800376 return -1;
377 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800378 return 0;
379 }
380
Doug Zongkerd4208f92010-09-20 12:16:13 -0700381 if (strcmp(v->fs_type, "ext4") == 0) {
Doug Zongker2810ced2011-02-17 15:55:21 -0800382 int result = make_ext4fs(v->device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700383 if (result != 0) {
384 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800385 return -1;
386 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700387 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800388 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700389
Koushik Dutta5d808172010-12-18 22:29:27 -0800390#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700391 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800392 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800393#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800394 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800395}