blob: a3c46775f5c0e4370b035c9c4c6d9e415d340d9c [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
Kolja Dummann92796ec2011-02-13 20:59:30 +010031int num_volumes;
32Volume* device_volumes;
33
34int get_num_volumes() {
35 return num_volumes;
36}
37
38Volume* get_device_volumes() {
39 return device_volumes;
40}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
Koushik Duttae734dad2011-03-02 12:32:13 -080042static int is_null(const char* sz) {
43 if (sz == NULL)
44 return 1;
45 if (strcmp("NULL", sz) == 0)
46 return 1;
47 return 0;
48}
49
50static char* dupe_string(const char* sz) {
51 if (is_null(sz))
52 return NULL;
53 return strdup(sz);
54}
55
Doug Zongkerd4208f92010-09-20 12:16:13 -070056void load_volume_table() {
57 int alloc = 2;
58 device_volumes = malloc(alloc * sizeof(Volume));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059
Doug Zongkerc18eeb82010-09-21 16:49:26 -070060 // Insert an entry for /tmp, which is the ramdisk and is always mounted.
61 device_volumes[0].mount_point = "/tmp";
62 device_volumes[0].fs_type = "ramdisk";
63 device_volumes[0].device = NULL;
64 device_volumes[0].device2 = NULL;
Koushik Duttae2b929d2011-03-01 21:41:54 -080065 device_volumes[0].fs_options = NULL;
66 device_volumes[0].fs_options2 = NULL;
Doug Zongkerc18eeb82010-09-21 16:49:26 -070067 num_volumes = 1;
68
Doug Zongkerd4208f92010-09-20 12:16:13 -070069 FILE* fstab = fopen("/etc/recovery.fstab", "r");
70 if (fstab == NULL) {
71 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
72 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070074
75 char buffer[1024];
76 int i;
77 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
78 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
79 if (buffer[i] == '\0' || buffer[i] == '#') continue;
80
81 char* original = strdup(buffer);
82
83 char* mount_point = strtok(buffer+i, " \t\n");
84 char* fs_type = strtok(NULL, " \t\n");
85 char* device = strtok(NULL, " \t\n");
86 // lines may optionally have a second device, to use if
87 // mounting the first one fails.
88 char* device2 = strtok(NULL, " \t\n");
Koushik Dutta64d79c62011-02-09 06:12:44 -080089 char* fs_type2 = strtok(NULL, " \t\n");
Koushik Duttae8bc2c82011-02-27 14:00:19 -080090 char* fs_options = strtok(NULL, " \t\n");
91 char* fs_options2 = strtok(NULL, " \t\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070092
93 if (mount_point && fs_type && device) {
94 while (num_volumes >= alloc) {
95 alloc *= 2;
96 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
97 }
98 device_volumes[num_volumes].mount_point = strdup(mount_point);
Koushik Duttae734dad2011-03-02 12:32:13 -080099 device_volumes[num_volumes].fs_type = !is_null(fs_type2) ? strdup(fs_type2) : strdup(fs_type);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700100 device_volumes[num_volumes].device = strdup(device);
101 device_volumes[num_volumes].device2 =
Koushik Dutta13ac7a42011-03-02 12:36:10 -0800102 !is_null(device2) ? strdup(device2) : NULL;
Koushik Duttae734dad2011-03-02 12:32:13 -0800103 device_volumes[num_volumes].fs_type2 = !is_null(fs_type2) ? strdup(fs_type) : NULL;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800104
Koushik Duttae734dad2011-03-02 12:32:13 -0800105 if (!is_null(fs_type2)) {
Koushik Dutta13ac7a42011-03-02 12:36:10 -0800106 device_volumes[num_volumes].fs_options2 = dupe_string(fs_options);
107 device_volumes[num_volumes].fs_options = dupe_string(fs_options2);
Koushik Duttae734dad2011-03-02 12:32:13 -0800108 }
109 else {
Koushik Dutta13ac7a42011-03-02 12:36:10 -0800110 device_volumes[num_volumes].fs_options2 = NULL;
111 device_volumes[num_volumes].fs_options = dupe_string(fs_options);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800112 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113 ++num_volumes;
114 } else {
115 LOGE("skipping malformed recovery.fstab line: %s\n", original);
116 }
117 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700119
120 fclose(fstab);
121
122 printf("recovery filesystem table\n");
123 printf("=========================\n");
124 for (i = 0; i < num_volumes; ++i) {
125 Volume* v = &device_volumes[i];
126 printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
127 v->device, v->device2);
128 }
129 printf("\n");
130}
131
132Volume* volume_for_path(const char* path) {
133 int i;
134 for (i = 0; i < num_volumes; ++i) {
135 Volume* v = device_volumes+i;
136 int len = strlen(v->mount_point);
137 if (strncmp(path, v->mount_point, len) == 0 &&
138 (path[len] == '\0' || path[len] == '/')) {
139 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 }
141 }
142 return NULL;
143}
144
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800145int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) {
Koushik Dutta64d79c62011-02-09 06:12:44 -0800146 if (device == NULL || mount_point == NULL || fs_type == NULL)
147 return -1;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800148 int ret = 0;
149 if (fs_options == NULL) {
150 ret = mount(device, mount_point, fs_type,
Koushik Dutta64d79c62011-02-09 06:12:44 -0800151 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800152 }
153 else {
154 char mount_cmd[PATH_MAX];
Koushik Dutta7161f352011-02-27 17:00:47 -0800155 sprintf(mount_cmd, "mount -t %s -o%s %s %s", fs_type, fs_options, device, mount_point);
Koushik Duttaa1f43bf2011-02-27 18:31:41 -0800156 LOGE("%s\n", mount_cmd);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800157 ret = __system(mount_cmd);
158 }
Koushik Dutta64d79c62011-02-09 06:12:44 -0800159 if (ret == 0)
160 return 0;
161 LOGW("failed to mount %s (%s)\n", device, strerror(errno));
162 return ret;
163}
164
Doug Zongkerd4208f92010-09-20 12:16:13 -0700165int ensure_path_mounted(const char* path) {
166 Volume* v = volume_for_path(path);
167 if (v == NULL) {
168 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800169 return -1;
170 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700171 if (strcmp(v->fs_type, "ramdisk") == 0) {
172 // the ramdisk is always mounted.
173 return 0;
174 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700175
176 int result;
177 result = scan_mounted_volumes();
178 if (result < 0) {
179 LOGE("failed to scan mounted volumes\n");
180 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700181 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182
Doug Zongkerd4208f92010-09-20 12:16:13 -0700183 const MountedVolume* mv =
184 find_mounted_volume_by_mount_point(v->mount_point);
185 if (mv) {
186 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187 return 0;
188 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700189
190 mkdir(v->mount_point, 0755); // in case it doesn't already exist
191
192 if (strcmp(v->fs_type, "yaffs2") == 0) {
193 // mount an MTD partition as a YAFFS2 filesystem.
194 mtd_scan_partitions();
195 const MtdPartition* partition;
196 partition = mtd_find_partition_by_name(v->device);
197 if (partition == NULL) {
198 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
199 v->device, v->mount_point);
200 return -1;
201 }
202 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
203 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800204 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800205 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700206 strcmp(v->fs_type, "vfat") == 0) {
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800207 if ((result = try_mount(v->device, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800208 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800209 if ((result = try_mount(v->device2, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800210 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800211 if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800212 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800213 if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800214 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800215 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800216 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800217 // let's try mounting with the mount binary and hope for the best.
218 char mount_cmd[PATH_MAX];
219 sprintf(mount_cmd, "mount %s", path);
220 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700221 }
222
223 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224 return -1;
225}
226
Doug Zongkerd4208f92010-09-20 12:16:13 -0700227int ensure_path_unmounted(const char* path) {
228 Volume* v = volume_for_path(path);
229 if (v == NULL) {
230 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800231 return -1;
232 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700233 if (strcmp(v->fs_type, "ramdisk") == 0) {
234 // the ramdisk is always mounted; you can't unmount it.
235 return -1;
236 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237
Doug Zongkerd4208f92010-09-20 12:16:13 -0700238 int result;
239 result = scan_mounted_volumes();
240 if (result < 0) {
241 LOGE("failed to scan mounted volumes\n");
242 return -1;
243 }
244
245 const MountedVolume* mv =
246 find_mounted_volume_by_mount_point(v->mount_point);
247 if (mv == NULL) {
248 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800249 return 0;
250 }
251
Doug Zongkerd4208f92010-09-20 12:16:13 -0700252 return unmount_mounted_volume(mv);
253}
254
255int format_volume(const char* volume) {
256 Volume* v = volume_for_path(volume);
257 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800258 // silent failure for sd-ext
259 if (strcmp(volume, "/sd-ext") == 0)
260 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700261 LOGE("unknown volume \"%s\"\n", volume);
262 return -1;
263 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700264 if (strcmp(v->fs_type, "ramdisk") == 0) {
265 // you can't format the ramdisk.
266 LOGE("can't format_volume \"%s\"", volume);
267 return -1;
268 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700269 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800270#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700271 LOGE("can't give path \"%s\" to format_volume\n", volume);
272 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800273#endif
274 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700275 }
276
277 if (ensure_path_unmounted(volume) != 0) {
278 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
279 return -1;
280 }
281
282 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800283 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700284 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700286 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287 return -1;
288 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289
Doug Zongkerd4208f92010-09-20 12:16:13 -0700290 MtdWriteContext *write = mtd_write_partition(partition);
291 if (write == NULL) {
292 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700294 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
295 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
296 mtd_write_close(write);
297 return -1;
298 } else if (mtd_write_close(write)) {
299 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300 return -1;
301 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302 return 0;
303 }
304
Christopher Lais066a1022011-01-16 06:02:34 -0600305 if (strcmp(v->fs_type, "emmc") == 0) {
306 return erase_raw_partition(v->device);
307 }
308
Doug Zongkerd4208f92010-09-20 12:16:13 -0700309 if (strcmp(v->fs_type, "ext4") == 0) {
310 reset_ext4fs_info();
311 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
312 if (result != 0) {
313 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800314 return -1;
315 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700316 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700318
Koushik Dutta5d808172010-12-18 22:29:27 -0800319#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700320 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800322#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800323 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800324}