blob: ba1786b11b6975274a5e7f1ca1bd80ddcbfbd518 [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
Koushik Dutta38a92142011-06-10 09:45:52 -0700167int is_data_media() {
168 Volume *data = volume_for_path("/data");
169 return data != NULL && strcmp(data->fs_type, "auto") == 0 && volume_for_path("/sdcard") == NULL;
170}
171
172void setup_data_media() {
173 rmdir("/sdcard");
174 mkdir("/data/media", 0755);
175 symlink("/data/media", "/sdcard");
176}
177
Doug Zongkerd4208f92010-09-20 12:16:13 -0700178int ensure_path_mounted(const char* path) {
Koushik Dutta52681312011-08-31 23:26:45 -0700179 return ensure_path_mounted_at_mount_point(path, NULL);
180}
181
182int ensure_path_mounted_at_mount_point(const char* path, const char* mount_point) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700183 Volume* v = volume_for_path(path);
184 if (v == NULL) {
Koushik Dutta50822992011-06-08 19:03:27 -0700185 // no /sdcard? let's assume /data/media
Koushik Dutta38a92142011-06-10 09:45:52 -0700186 if (strstr(path, "/sdcard") == path && is_data_media()) {
Koushik Dutta50822992011-06-08 19:03:27 -0700187 LOGW("using /data/media, no /sdcard found.\n");
188 int ret;
189 if (0 != (ret = ensure_path_mounted("/data")))
190 return ret;
Koushik Dutta38a92142011-06-10 09:45:52 -0700191 setup_data_media();
Koushik Dutta50822992011-06-08 19:03:27 -0700192 return 0;
193 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700194 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 return -1;
196 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700197 if (strcmp(v->fs_type, "ramdisk") == 0) {
198 // the ramdisk is always mounted.
199 return 0;
200 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700201
202 int result;
203 result = scan_mounted_volumes();
204 if (result < 0) {
205 LOGE("failed to scan mounted volumes\n");
206 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700207 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208
Koushik Dutta52681312011-08-31 23:26:45 -0700209 if (NULL == mount_point)
210 mount_point = v->mount_point;
211
Doug Zongkerd4208f92010-09-20 12:16:13 -0700212 const MountedVolume* mv =
Koushik Dutta52681312011-08-31 23:26:45 -0700213 find_mounted_volume_by_mount_point(mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700214 if (mv) {
215 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 return 0;
217 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700218
Koushik Dutta52681312011-08-31 23:26:45 -0700219 mkdir(mount_point, 0755); // in case it doesn't already exist
Doug Zongkerd4208f92010-09-20 12:16:13 -0700220
221 if (strcmp(v->fs_type, "yaffs2") == 0) {
222 // mount an MTD partition as a YAFFS2 filesystem.
223 mtd_scan_partitions();
224 const MtdPartition* partition;
225 partition = mtd_find_partition_by_name(v->device);
226 if (partition == NULL) {
227 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Koushik Dutta52681312011-08-31 23:26:45 -0700228 v->device, mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700229 return -1;
230 }
Koushik Dutta52681312011-08-31 23:26:45 -0700231 return mtd_mount_partition(partition, mount_point, v->fs_type, 0);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700232 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Koushik Dutta64d79c62011-02-09 06:12:44 -0800233 strcmp(v->fs_type, "ext3") == 0 ||
Koushik Duttaab1f8b82011-02-27 17:28:30 -0800234 strcmp(v->fs_type, "rfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700235 strcmp(v->fs_type, "vfat") == 0) {
Koushik Dutta52681312011-08-31 23:26:45 -0700236 if ((result = try_mount(v->device, mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800237 return 0;
Koushik Dutta52681312011-08-31 23:26:45 -0700238 if ((result = try_mount(v->device2, mount_point, v->fs_type, v->fs_options)) == 0)
Koushik Dutta49ee9a82011-02-09 16:15:54 -0800239 return 0;
Koushik Dutta52681312011-08-31 23:26:45 -0700240 if ((result = try_mount(v->device, mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800241 return 0;
Koushik Dutta52681312011-08-31 23:26:45 -0700242 if ((result = try_mount(v->device2, mount_point, v->fs_type2, v->fs_options2)) == 0)
Koushik Duttac7ef9af2011-02-09 16:32:42 -0800243 return 0;
Koushik Dutta64d79c62011-02-09 06:12:44 -0800244 return result;
Koushik Dutta49951142010-12-18 21:58:43 -0800245 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800246 // let's try mounting with the mount binary and hope for the best.
247 char mount_cmd[PATH_MAX];
248 sprintf(mount_cmd, "mount %s", path);
249 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700250 }
251
Koushik Dutta52681312011-08-31 23:26:45 -0700252 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253 return -1;
254}
255
Doug Zongkerd4208f92010-09-20 12:16:13 -0700256int ensure_path_unmounted(const char* path) {
Koushik Dutta50822992011-06-08 19:03:27 -0700257 // if we are using /data/media, do not ever unmount volumes /data or /sdcard
258 if (volume_for_path("/sdcard") == NULL && (strstr(path, "/sdcard") == path || strstr(path, "/data") == path)) {
259 return 0;
260 }
261
Doug Zongkerd4208f92010-09-20 12:16:13 -0700262 Volume* v = volume_for_path(path);
263 if (v == NULL) {
Koushik Dutta50822992011-06-08 19:03:27 -0700264 // no /sdcard? let's assume /data/media
Koushik Dutta38a92142011-06-10 09:45:52 -0700265 if (strstr(path, "/sdcard") == path && is_data_media()) {
Koushik Dutta50822992011-06-08 19:03:27 -0700266 return ensure_path_unmounted("/data");
267 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700268 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269 return -1;
270 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700271 if (strcmp(v->fs_type, "ramdisk") == 0) {
272 // the ramdisk is always mounted; you can't unmount it.
273 return -1;
274 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275
Doug Zongkerd4208f92010-09-20 12:16:13 -0700276 int result;
277 result = scan_mounted_volumes();
278 if (result < 0) {
279 LOGE("failed to scan mounted volumes\n");
280 return -1;
281 }
282
283 const MountedVolume* mv =
284 find_mounted_volume_by_mount_point(v->mount_point);
285 if (mv == NULL) {
286 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287 return 0;
288 }
289
Doug Zongkerd4208f92010-09-20 12:16:13 -0700290 return unmount_mounted_volume(mv);
291}
292
293int format_volume(const char* volume) {
294 Volume* v = volume_for_path(volume);
295 if (v == NULL) {
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700296 // no /sdcard? let's assume /data/media
Koushik Dutta7905c802011-06-15 00:00:55 -0700297 if (strstr(volume, "/sdcard") == volume && is_data_media()) {
298 return format_unknown_device(NULL, volume, NULL);
Koushik Dutta55e5e7b2011-06-14 23:39:59 -0700299 }
Koushik Duttabf4444f2010-12-18 18:57:47 -0800300 // silent failure for sd-ext
301 if (strcmp(volume, "/sd-ext") == 0)
302 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700303 LOGE("unknown volume \"%s\"\n", volume);
304 return -1;
305 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700306 if (strcmp(v->fs_type, "ramdisk") == 0) {
307 // you can't format the ramdisk.
308 LOGE("can't format_volume \"%s\"", volume);
309 return -1;
310 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700311 if (strcmp(v->mount_point, volume) != 0) {
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800312#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700313 LOGE("can't give path \"%s\" to format_volume\n", volume);
314 return -1;
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800315#endif
316 return format_unknown_device(v->device, volume, NULL);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700317 }
318
319 if (ensure_path_unmounted(volume) != 0) {
320 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
321 return -1;
322 }
323
324 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800325 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700326 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800327 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700328 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800329 return -1;
330 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331
Doug Zongkerd4208f92010-09-20 12:16:13 -0700332 MtdWriteContext *write = mtd_write_partition(partition);
333 if (write == NULL) {
334 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700336 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
337 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
338 mtd_write_close(write);
339 return -1;
340 } else if (mtd_write_close(write)) {
341 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800342 return -1;
343 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344 return 0;
345 }
346
Doug Zongkerd4208f92010-09-20 12:16:13 -0700347 if (strcmp(v->fs_type, "ext4") == 0) {
348 reset_ext4fs_info();
349 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
350 if (result != 0) {
351 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352 return -1;
353 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700354 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800355 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700356
Koushik Dutta5d808172010-12-18 22:29:27 -0800357#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700358 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800359 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800360#endif
Koushik Duttaa8708c62011-01-01 18:00:27 -0800361 return format_unknown_device(v->device, volume, v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800362}