blob: c1c37a377ac7bb491ded2b5ab23f766f6cf9e216 [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 Duttae8bc2c82011-02-27 14:00:19 -0800156 ret = __system(mount_cmd);
157 }
Koushik Dutta64d79c62011-02-09 06:12:44 -0800158 if (ret == 0)
159 return 0;
160 LOGW("failed to mount %s (%s)\n", device, strerror(errno));
161 return ret;
162}
163
Doug Zongkerd4208f92010-09-20 12:16:13 -0700164int ensure_path_mounted(const char* path) {
165 Volume* v = volume_for_path(path);
166 if (v == NULL) {
167 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168 return -1;
169 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700170 if (strcmp(v->fs_type, "ramdisk") == 0) {
171 // the ramdisk is always mounted.
172 return 0;
173 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700174
175 int result;
176 result = scan_mounted_volumes();
177 if (result < 0) {
178 LOGE("failed to scan mounted volumes\n");
179 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700180 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181
Doug Zongkerd4208f92010-09-20 12:16:13 -0700182 const MountedVolume* mv =
183 find_mounted_volume_by_mount_point(v->mount_point);
184 if (mv) {
185 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186 return 0;
187 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700188
189 mkdir(v->mount_point, 0755); // in case it doesn't already exist
190
191 if (strcmp(v->fs_type, "yaffs2") == 0) {
192 // mount an MTD partition as a YAFFS2 filesystem.
193 mtd_scan_partitions();
194 const MtdPartition* partition;
195 partition = mtd_find_partition_by_name(v->device);
196 if (partition == NULL) {
197 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
198 v->device, v->mount_point);
199 return -1;
200 }
201 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
202 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800203 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800204 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700205 strcmp(v->fs_type, "vfat") == 0) {
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800206 if ((result = try_mount(v->device, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800207 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800208 if ((result = try_mount(v->device2, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800209 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800210 if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800211 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800212 if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800213 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800214 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800215 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800216 // let's try mounting with the mount binary and hope for the best.
217 char mount_cmd[PATH_MAX];
218 sprintf(mount_cmd, "mount %s", path);
219 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700220 }
221
222 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223 return -1;
224}
225
Doug Zongkerd4208f92010-09-20 12:16:13 -0700226int ensure_path_unmounted(const char* path) {
227 Volume* v = volume_for_path(path);
228 if (v == NULL) {
229 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800230 return -1;
231 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700232 if (strcmp(v->fs_type, "ramdisk") == 0) {
233 // the ramdisk is always mounted; you can't unmount it.
234 return -1;
235 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236
Doug Zongkerd4208f92010-09-20 12:16:13 -0700237 int result;
238 result = scan_mounted_volumes();
239 if (result < 0) {
240 LOGE("failed to scan mounted volumes\n");
241 return -1;
242 }
243
244 const MountedVolume* mv =
245 find_mounted_volume_by_mount_point(v->mount_point);
246 if (mv == NULL) {
247 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 return 0;
249 }
250
Doug Zongkerd4208f92010-09-20 12:16:13 -0700251 return unmount_mounted_volume(mv);
252}
253
254int format_volume(const char* volume) {
255 Volume* v = volume_for_path(volume);
256 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800257 // silent failure for sd-ext
258 if (strcmp(volume, "/sd-ext") == 0)
259 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700260 LOGE("unknown volume \"%s\"\n", volume);
261 return -1;
262 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700263 if (strcmp(v->fs_type, "ramdisk") == 0) {
264 // you can't format the ramdisk.
265 LOGE("can't format_volume \"%s\"", volume);
266 return -1;
267 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700268 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800269#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700270 LOGE("can't give path \"%s\" to format_volume\n", volume);
271 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800272#endif
273 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700274 }
275
276 if (ensure_path_unmounted(volume) != 0) {
277 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
278 return -1;
279 }
280
281 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700283 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800284 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700285 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800286 return -1;
287 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288
Doug Zongkerd4208f92010-09-20 12:16:13 -0700289 MtdWriteContext *write = mtd_write_partition(partition);
290 if (write == NULL) {
291 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700293 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
294 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
295 mtd_write_close(write);
296 return -1;
297 } else if (mtd_write_close(write)) {
298 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299 return -1;
300 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301 return 0;
302 }
303
Christopher Lais066a1022011-01-16 06:02:34 -0600304 if (strcmp(v->fs_type, "emmc") == 0) {
Koushik Duttae62132b2011-05-26 11:29:29 -0700305 return erase_raw_partition("emmc", v->device);
Christopher Lais066a1022011-01-16 06:02:34 -0600306 }
307
Doug Zongkerd4208f92010-09-20 12:16:13 -0700308 if (strcmp(v->fs_type, "ext4") == 0) {
309 reset_ext4fs_info();
310 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
311 if (result != 0) {
312 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313 return -1;
314 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700315 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800316 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700317
Koushik Dutta5d808172010-12-18 22:29:27 -0800318#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700319 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800320 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800321#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800322 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800323}