blob: 7520e1e1bed81facec60a9840ce9c82809289f91 [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
Koushik Duttae734dad2011-03-02 12:32:13 -080034static int is_null(const char* sz) {
35 if (sz == NULL)
36 return 1;
37 if (strcmp("NULL", sz) == 0)
38 return 1;
39 return 0;
40}
41
42static char* dupe_string(const char* sz) {
43 if (is_null(sz))
44 return NULL;
45 return strdup(sz);
46}
47
Doug Zongkerd4208f92010-09-20 12:16:13 -070048void load_volume_table() {
49 int alloc = 2;
50 device_volumes = malloc(alloc * sizeof(Volume));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Doug Zongkerc18eeb82010-09-21 16:49:26 -070052 // Insert an entry for /tmp, which is the ramdisk and is always mounted.
53 device_volumes[0].mount_point = "/tmp";
54 device_volumes[0].fs_type = "ramdisk";
55 device_volumes[0].device = NULL;
56 device_volumes[0].device2 = NULL;
Koushik Duttae2b929d2011-03-01 21:41:54 -080057 device_volumes[0].fs_options = NULL;
58 device_volumes[0].fs_options2 = NULL;
Doug Zongkerc18eeb82010-09-21 16:49:26 -070059 num_volumes = 1;
60
Doug Zongkerd4208f92010-09-20 12:16:13 -070061 FILE* fstab = fopen("/etc/recovery.fstab", "r");
62 if (fstab == NULL) {
63 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
64 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070066
67 char buffer[1024];
68 int i;
69 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
70 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
71 if (buffer[i] == '\0' || buffer[i] == '#') continue;
72
73 char* original = strdup(buffer);
74
75 char* mount_point = strtok(buffer+i, " \t\n");
76 char* fs_type = strtok(NULL, " \t\n");
77 char* device = strtok(NULL, " \t\n");
78 // lines may optionally have a second device, to use if
79 // mounting the first one fails.
80 char* device2 = strtok(NULL, " \t\n");
Koushik Dutta64d79c62011-02-09 06:12:44 -080081 char* fs_type2 = strtok(NULL, " \t\n");
Koushik Duttae8bc2c82011-02-27 14:00:19 -080082 char* fs_options = strtok(NULL, " \t\n");
83 char* fs_options2 = strtok(NULL, " \t\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070084
85 if (mount_point && fs_type && device) {
86 while (num_volumes >= alloc) {
87 alloc *= 2;
88 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
89 }
90 device_volumes[num_volumes].mount_point = strdup(mount_point);
Koushik Duttae734dad2011-03-02 12:32:13 -080091 device_volumes[num_volumes].fs_type = !is_null(fs_type2) ? strdup(fs_type2) : strdup(fs_type);
Doug Zongkerd4208f92010-09-20 12:16:13 -070092 device_volumes[num_volumes].device = strdup(device);
93 device_volumes[num_volumes].device2 =
Koushik Duttae734dad2011-03-02 12:32:13 -080094 (!is_null(device2)) != 0) ? strdup(device2) : NULL;
95 device_volumes[num_volumes].fs_type2 = !is_null(fs_type2) ? strdup(fs_type) : NULL;
Koushik Duttae8bc2c82011-02-27 14:00:19 -080096
Koushik Duttae734dad2011-03-02 12:32:13 -080097 if (!is_null(fs_type2)) {
98 device_volumes[num_volumes]fs_options2 = dupe_string(fs_options);
99 device_volumes[num_volumes]fs_options = dupe_string(fs_options2);
100 }
101 else {
102 device_volumes[num_volumes]fs_options2 = NULL;
103 device_volumes[num_volumes]fs_options = dupe_string(fs_options);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800104 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700105 ++num_volumes;
106 } else {
107 LOGE("skipping malformed recovery.fstab line: %s\n", original);
108 }
109 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800110 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700111
112 fclose(fstab);
113
114 printf("recovery filesystem table\n");
115 printf("=========================\n");
116 for (i = 0; i < num_volumes; ++i) {
117 Volume* v = &device_volumes[i];
118 printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
119 v->device, v->device2);
120 }
121 printf("\n");
122}
123
124Volume* volume_for_path(const char* path) {
125 int i;
126 for (i = 0; i < num_volumes; ++i) {
127 Volume* v = device_volumes+i;
128 int len = strlen(v->mount_point);
129 if (strncmp(path, v->mount_point, len) == 0 &&
130 (path[len] == '\0' || path[len] == '/')) {
131 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800132 }
133 }
134 return NULL;
135}
136
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800137int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) {
Koushik Dutta64d79c62011-02-09 06:12:44 -0800138 if (device == NULL || mount_point == NULL || fs_type == NULL)
139 return -1;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800140 int ret = 0;
141 if (fs_options == NULL) {
142 ret = mount(device, mount_point, fs_type,
Koushik Dutta64d79c62011-02-09 06:12:44 -0800143 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800144 }
145 else {
146 char mount_cmd[PATH_MAX];
Koushik Dutta7161f352011-02-27 17:00:47 -0800147 sprintf(mount_cmd, "mount -t %s -o%s %s %s", fs_type, fs_options, device, mount_point);
Koushik Duttaa1f43bf2011-02-27 18:31:41 -0800148 LOGE("%s\n", mount_cmd);
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800149 ret = __system(mount_cmd);
150 }
Koushik Dutta64d79c62011-02-09 06:12:44 -0800151 if (ret == 0)
152 return 0;
153 LOGW("failed to mount %s (%s)\n", device, strerror(errno));
154 return ret;
155}
156
Doug Zongkerd4208f92010-09-20 12:16:13 -0700157int ensure_path_mounted(const char* path) {
158 Volume* v = volume_for_path(path);
159 if (v == NULL) {
160 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 return -1;
162 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700163 if (strcmp(v->fs_type, "ramdisk") == 0) {
164 // the ramdisk is always mounted.
165 return 0;
166 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700167
168 int result;
169 result = scan_mounted_volumes();
170 if (result < 0) {
171 LOGE("failed to scan mounted volumes\n");
172 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700173 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174
Doug Zongkerd4208f92010-09-20 12:16:13 -0700175 const MountedVolume* mv =
176 find_mounted_volume_by_mount_point(v->mount_point);
177 if (mv) {
178 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179 return 0;
180 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181
182 mkdir(v->mount_point, 0755); // in case it doesn't already exist
183
184 if (strcmp(v->fs_type, "yaffs2") == 0) {
185 // mount an MTD partition as a YAFFS2 filesystem.
186 mtd_scan_partitions();
187 const MtdPartition* partition;
188 partition = mtd_find_partition_by_name(v->device);
189 if (partition == NULL) {
190 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
191 v->device, v->mount_point);
192 return -1;
193 }
194 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
195 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800196 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800197 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700198 strcmp(v->fs_type, "vfat") == 0) {
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800199 if ((result = try_mount(v->device, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800200 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800201 if ((result = try_mount(v->device2, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800202 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800203 if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800204 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800205 if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800206 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800207 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800208 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800209 // let's try mounting with the mount binary and hope for the best.
210 char mount_cmd[PATH_MAX];
211 sprintf(mount_cmd, "mount %s", path);
212 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700213 }
214
215 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 return -1;
217}
218
Doug Zongkerd4208f92010-09-20 12:16:13 -0700219int ensure_path_unmounted(const char* path) {
220 Volume* v = volume_for_path(path);
221 if (v == NULL) {
222 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223 return -1;
224 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700225 if (strcmp(v->fs_type, "ramdisk") == 0) {
226 // the ramdisk is always mounted; you can't unmount it.
227 return -1;
228 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229
Doug Zongkerd4208f92010-09-20 12:16:13 -0700230 int result;
231 result = scan_mounted_volumes();
232 if (result < 0) {
233 LOGE("failed to scan mounted volumes\n");
234 return -1;
235 }
236
237 const MountedVolume* mv =
238 find_mounted_volume_by_mount_point(v->mount_point);
239 if (mv == NULL) {
240 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241 return 0;
242 }
243
Doug Zongkerd4208f92010-09-20 12:16:13 -0700244 return unmount_mounted_volume(mv);
245}
246
247int format_volume(const char* volume) {
248 Volume* v = volume_for_path(volume);
249 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800250 // silent failure for sd-ext
251 if (strcmp(volume, "/sd-ext") == 0)
252 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700253 LOGE("unknown volume \"%s\"\n", volume);
254 return -1;
255 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700256 if (strcmp(v->fs_type, "ramdisk") == 0) {
257 // you can't format the ramdisk.
258 LOGE("can't format_volume \"%s\"", volume);
259 return -1;
260 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700261 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800262#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700263 LOGE("can't give path \"%s\" to format_volume\n", volume);
264 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800265#endif
266 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700267 }
268
269 if (ensure_path_unmounted(volume) != 0) {
270 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
271 return -1;
272 }
273
274 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700276 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700278 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279 return -1;
280 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281
Doug Zongkerd4208f92010-09-20 12:16:13 -0700282 MtdWriteContext *write = mtd_write_partition(partition);
283 if (write == NULL) {
284 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700286 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
287 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
288 mtd_write_close(write);
289 return -1;
290 } else if (mtd_write_close(write)) {
291 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 return -1;
293 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 return 0;
295 }
296
Christopher Lais066a1022011-01-16 06:02:34 -0600297 if (strcmp(v->fs_type, "emmc") == 0) {
298 return erase_raw_partition(v->device);
299 }
300
Doug Zongkerd4208f92010-09-20 12:16:13 -0700301 if (strcmp(v->fs_type, "ext4") == 0) {
302 reset_ext4fs_info();
303 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
304 if (result != 0) {
305 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 return -1;
307 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700308 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800309 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700310
Koushik Dutta5d808172010-12-18 22:29:27 -0800311#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700312 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800314#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800315 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800316}