blob: 4ecc4ddf5c2e82a95d016ad68bde1ea327fd1e27 [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) {
170 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171 return -1;
172 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700173 if (strcmp(v->fs_type, "ramdisk") == 0) {
174 // the ramdisk is always mounted.
175 return 0;
176 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700177
178 int result;
179 result = scan_mounted_volumes();
180 if (result < 0) {
181 LOGE("failed to scan mounted volumes\n");
182 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700183 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800184
Doug Zongkerd4208f92010-09-20 12:16:13 -0700185 const MountedVolume* mv =
186 find_mounted_volume_by_mount_point(v->mount_point);
187 if (mv) {
188 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800189 return 0;
190 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700191
192 mkdir(v->mount_point, 0755); // in case it doesn't already exist
193
194 if (strcmp(v->fs_type, "yaffs2") == 0) {
195 // mount an MTD partition as a YAFFS2 filesystem.
196 mtd_scan_partitions();
197 const MtdPartition* partition;
198 partition = mtd_find_partition_by_name(v->device);
199 if (partition == NULL) {
200 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
201 v->device, v->mount_point);
202 return -1;
203 }
204 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
205 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800206 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800207 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700208 strcmp(v->fs_type, "vfat") == 0) {
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800209 if ((result = try_mount(v->device, 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->device2, v->mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800212 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800213 if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800214 return 0;
Koushik Duttae8bc2c82011-02-27 14:00:19 -0800215 if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800216 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800217 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800218 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800219 // let's try mounting with the mount binary and hope for the best.
220 char mount_cmd[PATH_MAX];
221 sprintf(mount_cmd, "mount %s", path);
222 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700223 }
224
225 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800226 return -1;
227}
228
Doug Zongkerd4208f92010-09-20 12:16:13 -0700229int ensure_path_unmounted(const char* path) {
230 Volume* v = volume_for_path(path);
231 if (v == NULL) {
232 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800233 return -1;
234 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700235 if (strcmp(v->fs_type, "ramdisk") == 0) {
236 // the ramdisk is always mounted; you can't unmount it.
237 return -1;
238 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239
Doug Zongkerd4208f92010-09-20 12:16:13 -0700240 int result;
241 result = scan_mounted_volumes();
242 if (result < 0) {
243 LOGE("failed to scan mounted volumes\n");
244 return -1;
245 }
246
247 const MountedVolume* mv =
248 find_mounted_volume_by_mount_point(v->mount_point);
249 if (mv == NULL) {
250 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251 return 0;
252 }
253
Doug Zongkerd4208f92010-09-20 12:16:13 -0700254 return unmount_mounted_volume(mv);
255}
256
257int format_volume(const char* volume) {
258 Volume* v = volume_for_path(volume);
259 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800260 // silent failure for sd-ext
261 if (strcmp(volume, "/sd-ext") == 0)
262 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700263 LOGE("unknown volume \"%s\"\n", volume);
264 return -1;
265 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700266 if (strcmp(v->fs_type, "ramdisk") == 0) {
267 // you can't format the ramdisk.
268 LOGE("can't format_volume \"%s\"", volume);
269 return -1;
270 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700271 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800272#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700273 LOGE("can't give path \"%s\" to format_volume\n", volume);
274 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800275#endif
276 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700277 }
278
279 if (ensure_path_unmounted(volume) != 0) {
280 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
281 return -1;
282 }
283
284 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700286 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700288 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289 return -1;
290 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800291
Doug Zongkerd4208f92010-09-20 12:16:13 -0700292 MtdWriteContext *write = mtd_write_partition(partition);
293 if (write == NULL) {
294 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700296 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
297 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
298 mtd_write_close(write);
299 return -1;
300 } else if (mtd_write_close(write)) {
301 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302 return -1;
303 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800304 return 0;
305 }
306
Doug Zongkerd4208f92010-09-20 12:16:13 -0700307 if (strcmp(v->fs_type, "ext4") == 0) {
308 reset_ext4fs_info();
309 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
310 if (result != 0) {
311 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312 return -1;
313 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700314 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800315 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700316
Koushik Dutta5d808172010-12-18 22:29:27 -0800317#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700318 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800319 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800320#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800321 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322}