blob: 49b772919ec1e885d1f0a53b9fbddadd4d7453c1 [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 Zongkerd4208f92010-09-20 12:16:13 -070059void load_volume_table() {
60 int alloc = 2;
61 device_volumes = malloc(alloc * sizeof(Volume));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062
Doug Zongkerc18eeb82010-09-21 16:49:26 -070063 // Insert an entry for /tmp, which is the ramdisk and is always mounted.
64 device_volumes[0].mount_point = "/tmp";
65 device_volumes[0].fs_type = "ramdisk";
66 device_volumes[0].device = NULL;
67 device_volumes[0].device2 = NULL;
Koushik Duttae2b929d2011-03-01 21:41:54 -080068 device_volumes[0].fs_options = NULL;
69 device_volumes[0].fs_options2 = NULL;
Doug Zongkerc18eeb82010-09-21 16:49:26 -070070 num_volumes = 1;
71
Doug Zongkerd4208f92010-09-20 12:16:13 -070072 FILE* fstab = fopen("/etc/recovery.fstab", "r");
73 if (fstab == NULL) {
74 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
75 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070077
78 char buffer[1024];
79 int i;
80 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
81 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
82 if (buffer[i] == '\0' || buffer[i] == '#') continue;
83
84 char* original = strdup(buffer);
85
86 char* mount_point = strtok(buffer+i, " \t\n");
87 char* fs_type = strtok(NULL, " \t\n");
88 char* device = strtok(NULL, " \t\n");
89 // lines may optionally have a second device, to use if
90 // mounting the first one fails.
91 char* device2 = strtok(NULL, " \t\n");
Koushik Dutta64d79c62011-02-09 06:12:44 -080092 char* fs_type2 = strtok(NULL, " \t\n");
Koushik Duttae8bc2c82011-02-27 14:00:19 -080093 char* fs_options = strtok(NULL, " \t\n");
94 char* fs_options2 = strtok(NULL, " \t\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070095
96 if (mount_point && fs_type && device) {
97 while (num_volumes >= alloc) {
98 alloc *= 2;
99 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
100 }
101 device_volumes[num_volumes].mount_point = strdup(mount_point);
Koushik Duttae734dad2011-03-02 12:32:13 -0800102 device_volumes[num_volumes].fs_type = !is_null(fs_type2) ? strdup(fs_type2) : strdup(fs_type);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700103 device_volumes[num_volumes].device = strdup(device);
104 device_volumes[num_volumes].device2 =
Koushik Dutta13ac7a42011-03-02 12:36:10 -0800105 !is_null(device2) ? strdup(device2) : NULL;
Koushik Duttae734dad2011-03-02 12:32:13 -0800106 device_volumes[num_volumes].fs_type2 = !is_null(fs_type2) ? strdup(fs_type) : NULL;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800107
Koushik Duttae734dad2011-03-02 12:32:13 -0800108 if (!is_null(fs_type2)) {
Koushik Dutta13ac7a42011-03-02 12:36:10 -0800109 device_volumes[num_volumes].fs_options2 = dupe_string(fs_options);
110 device_volumes[num_volumes].fs_options = dupe_string(fs_options2);
Koushik Duttae734dad2011-03-02 12:32:13 -0800111 }
112 else {
Koushik Dutta13ac7a42011-03-02 12:36:10 -0800113 device_volumes[num_volumes].fs_options2 = NULL;
114 device_volumes[num_volumes].fs_options = dupe_string(fs_options);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800115 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700116 ++num_volumes;
117 } else {
118 LOGE("skipping malformed recovery.fstab line: %s\n", original);
119 }
120 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700122
123 fclose(fstab);
124
125 printf("recovery filesystem table\n");
126 printf("=========================\n");
127 for (i = 0; i < num_volumes; ++i) {
128 Volume* v = &device_volumes[i];
129 printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
130 v->device, v->device2);
131 }
132 printf("\n");
133}
134
135Volume* volume_for_path(const char* path) {
136 int i;
137 for (i = 0; i < num_volumes; ++i) {
138 Volume* v = device_volumes+i;
139 int len = strlen(v->mount_point);
140 if (strncmp(path, v->mount_point, len) == 0 &&
141 (path[len] == '\0' || path[len] == '/')) {
142 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143 }
144 }
145 return NULL;
146}
147
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800148int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) {
Koushik Dutta64d79c62011-02-09 06:12:44 -0800149 if (device == NULL || mount_point == NULL || fs_type == NULL)
150 return -1;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800151 int ret = 0;
152 if (fs_options == NULL) {
153 ret = mount(device, mount_point, fs_type,
Koushik Dutta64d79c62011-02-09 06:12:44 -0800154 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800155 }
156 else {
157 char mount_cmd[PATH_MAX];
Koushik Dutta7161f352011-02-27 17:00:47 -0800158 sprintf(mount_cmd, "mount -t %s -o%s %s %s", fs_type, fs_options, device, mount_point);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800159 ret = __system(mount_cmd);
160 }
Koushik Dutta64d79c62011-02-09 06:12:44 -0800161 if (ret == 0)
162 return 0;
163 LOGW("failed to mount %s (%s)\n", device, strerror(errno));
164 return ret;
165}
166
Doug Zongkerd4208f92010-09-20 12:16:13 -0700167int ensure_path_mounted(const char* path) {
168 Volume* v = volume_for_path(path);
169 if (v == NULL) {
Koushik Dutta50822992011-06-08 19:03:27 -0700170 // no /sdcard? let's assume /data/media
171 if (strstr(path, "/sdcard") == path) {
172 LOGW("using /data/media, no /sdcard found.\n");
173 int ret;
174 if (0 != (ret = ensure_path_mounted("/data")))
175 return ret;
176 rmdir("/sdcard");
177 symlink("/data/media", "/sdcard");
178 return 0;
179 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700180 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181 return -1;
182 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700183 if (strcmp(v->fs_type, "ramdisk") == 0) {
184 // the ramdisk is always mounted.
185 return 0;
186 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700187
188 int result;
189 result = scan_mounted_volumes();
190 if (result < 0) {
191 LOGE("failed to scan mounted volumes\n");
192 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700193 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194
Doug Zongkerd4208f92010-09-20 12:16:13 -0700195 const MountedVolume* mv =
196 find_mounted_volume_by_mount_point(v->mount_point);
197 if (mv) {
198 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199 return 0;
200 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700201
202 mkdir(v->mount_point, 0755); // in case it doesn't already exist
203
204 if (strcmp(v->fs_type, "yaffs2") == 0) {
205 // mount an MTD partition as a YAFFS2 filesystem.
206 mtd_scan_partitions();
207 const MtdPartition* partition;
208 partition = mtd_find_partition_by_name(v->device);
209 if (partition == NULL) {
210 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
211 v->device, v->mount_point);
212 return -1;
213 }
214 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
215 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800216 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800217 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700218 strcmp(v->fs_type, "vfat") == 0) {
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800219 if ((result = try_mount(v->device, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800220 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800221 if ((result = try_mount(v->device2, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800222 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800223 if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800224 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800225 if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800226 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800227 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800228 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800229 // let's try mounting with the mount binary and hope for the best.
230 char mount_cmd[PATH_MAX];
231 sprintf(mount_cmd, "mount %s", path);
232 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700233 }
234
235 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236 return -1;
237}
238
Doug Zongkerd4208f92010-09-20 12:16:13 -0700239int ensure_path_unmounted(const char* path) {
Koushik Dutta50822992011-06-08 19:03:27 -0700240 // if we are using /data/media, do not ever unmount volumes /data or /sdcard
241 if (volume_for_path("/sdcard") == NULL && (strstr(path, "/sdcard") == path || strstr(path, "/data") == path)) {
242 return 0;
243 }
244
Doug Zongkerd4208f92010-09-20 12:16:13 -0700245 Volume* v = volume_for_path(path);
246 if (v == NULL) {
Koushik Dutta50822992011-06-08 19:03:27 -0700247 // no /sdcard? let's assume /data/media
248 if (strstr(path, "/sdcard") == path) {
249 return ensure_path_unmounted("/data");
250 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700251 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252 return -1;
253 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700254 if (strcmp(v->fs_type, "ramdisk") == 0) {
255 // the ramdisk is always mounted; you can't unmount it.
256 return -1;
257 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800258
Doug Zongkerd4208f92010-09-20 12:16:13 -0700259 int result;
260 result = scan_mounted_volumes();
261 if (result < 0) {
262 LOGE("failed to scan mounted volumes\n");
263 return -1;
264 }
265
266 const MountedVolume* mv =
267 find_mounted_volume_by_mount_point(v->mount_point);
268 if (mv == NULL) {
269 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 return 0;
271 }
272
Doug Zongkerd4208f92010-09-20 12:16:13 -0700273 return unmount_mounted_volume(mv);
274}
275
276int format_volume(const char* volume) {
277 Volume* v = volume_for_path(volume);
278 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800279 // silent failure for sd-ext
280 if (strcmp(volume, "/sd-ext") == 0)
281 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700282 LOGE("unknown volume \"%s\"\n", volume);
283 return -1;
284 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700285 if (strcmp(v->fs_type, "ramdisk") == 0) {
286 // you can't format the ramdisk.
287 LOGE("can't format_volume \"%s\"", volume);
288 return -1;
289 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700290 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800291#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700292 LOGE("can't give path \"%s\" to format_volume\n", volume);
293 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800294#endif
295 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700296 }
297
298 if (ensure_path_unmounted(volume) != 0) {
299 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
300 return -1;
301 }
302
303 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800304 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700305 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700307 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 return -1;
309 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310
Doug Zongkerd4208f92010-09-20 12:16:13 -0700311 MtdWriteContext *write = mtd_write_partition(partition);
312 if (write == NULL) {
313 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800314 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700315 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
316 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
317 mtd_write_close(write);
318 return -1;
319 } else if (mtd_write_close(write)) {
320 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321 return -1;
322 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800323 return 0;
324 }
325
Doug Zongkerd4208f92010-09-20 12:16:13 -0700326 if (strcmp(v->fs_type, "ext4") == 0) {
327 reset_ext4fs_info();
328 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
329 if (result != 0) {
330 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331 return -1;
332 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700333 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800334 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700335
Koushik Dutta5d808172010-12-18 22:29:27 -0800336#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800339#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800340 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800341}