blob: 92962ba552eda7598e3c8ced8fbfa94196b7ad0a [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
Doug Zongkerd4208f92010-09-20 12:16:13 -070031static int num_volumes = 0;
32static Volume* device_volumes = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Doug Zongkerd4208f92010-09-20 12:16:13 -070034void load_volume_table() {
35 int alloc = 2;
36 device_volumes = malloc(alloc * sizeof(Volume));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Doug Zongkerc18eeb82010-09-21 16:49:26 -070038 // Insert an entry for /tmp, which is the ramdisk and is always mounted.
39 device_volumes[0].mount_point = "/tmp";
40 device_volumes[0].fs_type = "ramdisk";
41 device_volumes[0].device = NULL;
42 device_volumes[0].device2 = NULL;
Koushik Duttae2b929d2011-03-01 21:41:54 -080043 device_volumes[0].device = NULL;
44 device_volumes[0].fs_options = NULL;
45 device_volumes[0].fs_options2 = NULL;
Doug Zongkerc18eeb82010-09-21 16:49:26 -070046 num_volumes = 1;
47
Doug Zongkerd4208f92010-09-20 12:16:13 -070048 FILE* fstab = fopen("/etc/recovery.fstab", "r");
49 if (fstab == NULL) {
50 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
51 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070053
54 char buffer[1024];
55 int i;
56 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
57 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
58 if (buffer[i] == '\0' || buffer[i] == '#') continue;
59
60 char* original = strdup(buffer);
61
62 char* mount_point = strtok(buffer+i, " \t\n");
63 char* fs_type = strtok(NULL, " \t\n");
64 char* device = strtok(NULL, " \t\n");
65 // lines may optionally have a second device, to use if
66 // mounting the first one fails.
67 char* device2 = strtok(NULL, " \t\n");
Koushik Dutta64d79c62011-02-09 06:12:44 -080068 char* fs_type2 = strtok(NULL, " \t\n");
Koushik Duttae8bc2c82011-02-27 14:00:19 -080069 char* fs_options = strtok(NULL, " \t\n");
70 char* fs_options2 = strtok(NULL, " \t\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070071
72 if (mount_point && fs_type && device) {
73 while (num_volumes >= alloc) {
74 alloc *= 2;
75 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
76 }
77 device_volumes[num_volumes].mount_point = strdup(mount_point);
Koushik Duttac7ef9af2011-02-09 16:32:42 -080078 device_volumes[num_volumes].fs_type = fs_type2 != NULL ? strdup(fs_type2) : strdup(fs_type);
Doug Zongkerd4208f92010-09-20 12:16:13 -070079 device_volumes[num_volumes].device = strdup(device);
80 device_volumes[num_volumes].device2 =
Koushik Dutta64d79c62011-02-09 06:12:44 -080081 (device2 != NULL && strcmp(device2, "NULL") != 0) ? strdup(device2) : NULL;
Koushik Duttac7ef9af2011-02-09 16:32:42 -080082 device_volumes[num_volumes].fs_type2 = fs_type2 != NULL ? strdup(fs_type) : NULL;
Koushik Duttae8bc2c82011-02-27 14:00:19 -080083
84 if (fs_type2 != NULL) {
85 char *temp;
86 temp = fs_options2;
87 fs_options2 = fs_options;
88 fs_options = temp;
89 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070090 ++num_volumes;
91 } else {
92 LOGE("skipping malformed recovery.fstab line: %s\n", original);
93 }
94 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070096
97 fclose(fstab);
98
99 printf("recovery filesystem table\n");
100 printf("=========================\n");
101 for (i = 0; i < num_volumes; ++i) {
102 Volume* v = &device_volumes[i];
103 printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
104 v->device, v->device2);
105 }
106 printf("\n");
107}
108
109Volume* volume_for_path(const char* path) {
110 int i;
111 for (i = 0; i < num_volumes; ++i) {
112 Volume* v = device_volumes+i;
113 int len = strlen(v->mount_point);
114 if (strncmp(path, v->mount_point, len) == 0 &&
115 (path[len] == '\0' || path[len] == '/')) {
116 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 }
118 }
119 return NULL;
120}
121
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800122int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) {
Koushik Dutta64d79c62011-02-09 06:12:44 -0800123 if (device == NULL || mount_point == NULL || fs_type == NULL)
124 return -1;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800125 int ret = 0;
126 if (fs_options == NULL) {
127 ret = mount(device, mount_point, fs_type,
Koushik Dutta64d79c62011-02-09 06:12:44 -0800128 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800129 }
130 else {
131 char mount_cmd[PATH_MAX];
Koushik Dutta7161f352011-02-27 17:00:47 -0800132 sprintf(mount_cmd, "mount -t %s -o%s %s %s", fs_type, fs_options, device, mount_point);
Koushik Duttaa1f43bf2011-02-27 18:31:41 -0800133 LOGE("%s\n", mount_cmd);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800134 ret = __system(mount_cmd);
135 }
Koushik Dutta64d79c62011-02-09 06:12:44 -0800136 if (ret == 0)
137 return 0;
138 LOGW("failed to mount %s (%s)\n", device, strerror(errno));
139 return ret;
140}
141
Doug Zongkerd4208f92010-09-20 12:16:13 -0700142int ensure_path_mounted(const char* path) {
143 Volume* v = volume_for_path(path);
144 if (v == NULL) {
145 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146 return -1;
147 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700148 if (strcmp(v->fs_type, "ramdisk") == 0) {
149 // the ramdisk is always mounted.
150 return 0;
151 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700152
153 int result;
154 result = scan_mounted_volumes();
155 if (result < 0) {
156 LOGE("failed to scan mounted volumes\n");
157 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700158 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159
Doug Zongkerd4208f92010-09-20 12:16:13 -0700160 const MountedVolume* mv =
161 find_mounted_volume_by_mount_point(v->mount_point);
162 if (mv) {
163 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164 return 0;
165 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700166
167 mkdir(v->mount_point, 0755); // in case it doesn't already exist
168
169 if (strcmp(v->fs_type, "yaffs2") == 0) {
170 // mount an MTD partition as a YAFFS2 filesystem.
171 mtd_scan_partitions();
172 const MtdPartition* partition;
173 partition = mtd_find_partition_by_name(v->device);
174 if (partition == NULL) {
175 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
176 v->device, v->mount_point);
177 return -1;
178 }
179 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
180 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800181 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800182 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700183 strcmp(v->fs_type, "vfat") == 0) {
Koushik Duttae2b929d2011-03-01 21:41:54 -0800184 LOGE("Tracepoint 1\n");
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800185 if ((result = try_mount(v->device, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800186 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800187 if ((result = try_mount(v->device2, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800188 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800189 if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800190 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800191 if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800192 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800193 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800194 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800195 // let's try mounting with the mount binary and hope for the best.
196 char mount_cmd[PATH_MAX];
197 sprintf(mount_cmd, "mount %s", path);
198 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700199 }
200
201 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202 return -1;
203}
204
Doug Zongkerd4208f92010-09-20 12:16:13 -0700205int ensure_path_unmounted(const char* path) {
206 Volume* v = volume_for_path(path);
207 if (v == NULL) {
208 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 return -1;
210 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700211 if (strcmp(v->fs_type, "ramdisk") == 0) {
212 // the ramdisk is always mounted; you can't unmount it.
213 return -1;
214 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215
Doug Zongkerd4208f92010-09-20 12:16:13 -0700216 int result;
217 result = scan_mounted_volumes();
218 if (result < 0) {
219 LOGE("failed to scan mounted volumes\n");
220 return -1;
221 }
222
223 const MountedVolume* mv =
224 find_mounted_volume_by_mount_point(v->mount_point);
225 if (mv == NULL) {
226 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800227 return 0;
228 }
229
Doug Zongkerd4208f92010-09-20 12:16:13 -0700230 return unmount_mounted_volume(mv);
231}
232
233int format_volume(const char* volume) {
234 Volume* v = volume_for_path(volume);
235 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800236 // silent failure for sd-ext
237 if (strcmp(volume, "/sd-ext") == 0)
238 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700239 LOGE("unknown volume \"%s\"\n", volume);
240 return -1;
241 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700242 if (strcmp(v->fs_type, "ramdisk") == 0) {
243 // you can't format the ramdisk.
244 LOGE("can't format_volume \"%s\"", volume);
245 return -1;
246 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700247 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800248#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700249 LOGE("can't give path \"%s\" to format_volume\n", volume);
250 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800251#endif
252 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700253 }
254
255 if (ensure_path_unmounted(volume) != 0) {
256 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
257 return -1;
258 }
259
260 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700262 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700264 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265 return -1;
266 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800267
Doug Zongkerd4208f92010-09-20 12:16:13 -0700268 MtdWriteContext *write = mtd_write_partition(partition);
269 if (write == NULL) {
270 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700272 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
273 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
274 mtd_write_close(write);
275 return -1;
276 } else if (mtd_write_close(write)) {
277 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 return -1;
279 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800280 return 0;
281 }
282
Christopher Lais066a1022011-01-16 06:02:34 -0600283 if (strcmp(v->fs_type, "emmc") == 0) {
284 return erase_raw_partition(v->device);
285 }
286
Doug Zongkerd4208f92010-09-20 12:16:13 -0700287 if (strcmp(v->fs_type, "ext4") == 0) {
288 reset_ext4fs_info();
289 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
290 if (result != 0) {
291 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 return -1;
293 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700294 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700296
Koushik Dutta5d808172010-12-18 22:29:27 -0800297#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700298 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800300#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800301 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302}