blob: 45e07273593748ba1b9a8d40106d8a837e3afd64 [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;
43 num_volumes = 1;
44
Doug Zongkerd4208f92010-09-20 12:16:13 -070045 FILE* fstab = fopen("/etc/recovery.fstab", "r");
46 if (fstab == NULL) {
47 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
48 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070050
51 char buffer[1024];
52 int i;
53 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
54 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
55 if (buffer[i] == '\0' || buffer[i] == '#') continue;
56
57 char* original = strdup(buffer);
58
59 char* mount_point = strtok(buffer+i, " \t\n");
60 char* fs_type = strtok(NULL, " \t\n");
61 char* device = strtok(NULL, " \t\n");
62 // lines may optionally have a second device, to use if
63 // mounting the first one fails.
64 char* device2 = strtok(NULL, " \t\n");
65
66 if (mount_point && fs_type && device) {
67 while (num_volumes >= alloc) {
68 alloc *= 2;
69 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
70 }
71 device_volumes[num_volumes].mount_point = strdup(mount_point);
72 device_volumes[num_volumes].fs_type = strdup(fs_type);
73 device_volumes[num_volumes].device = strdup(device);
74 device_volumes[num_volumes].device2 =
75 device2 ? strdup(device2) : NULL;
76 ++num_volumes;
77 } else {
78 LOGE("skipping malformed recovery.fstab line: %s\n", original);
79 }
80 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070082
83 fclose(fstab);
84
85 printf("recovery filesystem table\n");
86 printf("=========================\n");
87 for (i = 0; i < num_volumes; ++i) {
88 Volume* v = &device_volumes[i];
89 printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
90 v->device, v->device2);
91 }
92 printf("\n");
93}
94
95Volume* volume_for_path(const char* path) {
96 int i;
97 for (i = 0; i < num_volumes; ++i) {
98 Volume* v = device_volumes+i;
99 int len = strlen(v->mount_point);
100 if (strncmp(path, v->mount_point, len) == 0 &&
101 (path[len] == '\0' || path[len] == '/')) {
102 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 }
104 }
105 return NULL;
106}
107
Doug Zongkerd4208f92010-09-20 12:16:13 -0700108int ensure_path_mounted(const char* path) {
109 Volume* v = volume_for_path(path);
110 if (v == NULL) {
111 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 return -1;
113 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700114 if (strcmp(v->fs_type, "ramdisk") == 0) {
115 // the ramdisk is always mounted.
116 return 0;
117 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700118
119 int result;
120 result = scan_mounted_volumes();
121 if (result < 0) {
122 LOGE("failed to scan mounted volumes\n");
123 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700124 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125
Doug Zongkerd4208f92010-09-20 12:16:13 -0700126 const MountedVolume* mv =
127 find_mounted_volume_by_mount_point(v->mount_point);
128 if (mv) {
129 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 return 0;
131 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700132
133 mkdir(v->mount_point, 0755); // in case it doesn't already exist
134
135 if (strcmp(v->fs_type, "yaffs2") == 0) {
136 // mount an MTD partition as a YAFFS2 filesystem.
137 mtd_scan_partitions();
138 const MtdPartition* partition;
139 partition = mtd_find_partition_by_name(v->device);
140 if (partition == NULL) {
141 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
142 v->device, v->mount_point);
143 return -1;
144 }
145 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
146 } else if (strcmp(v->fs_type, "ext4") == 0 ||
147 strcmp(v->fs_type, "vfat") == 0) {
148 result = mount(v->device, v->mount_point, v->fs_type,
149 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
150 if (result == 0) return 0;
151
152 if (v->device2) {
153 LOGW("failed to mount %s (%s); trying %s\n",
154 v->device, strerror(errno), v->device2);
155 result = mount(v->device2, v->mount_point, v->fs_type,
156 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
157 if (result == 0) return 0;
158 }
159
160 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
161 return -1;
Koushik Dutta49951142010-12-18 21:58:43 -0800162 } else {
Koushik Dutta4196e0f2010-12-19 03:38:29 -0800163 // let's try mounting with the mount binary and hope for the best.
164 char mount_cmd[PATH_MAX];
165 sprintf(mount_cmd, "mount %s", path);
166 return __system(mount_cmd);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700167 }
168
169 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800170 return -1;
171}
172
Doug Zongkerd4208f92010-09-20 12:16:13 -0700173int ensure_path_unmounted(const char* path) {
174 Volume* v = volume_for_path(path);
175 if (v == NULL) {
176 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177 return -1;
178 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700179 if (strcmp(v->fs_type, "ramdisk") == 0) {
180 // the ramdisk is always mounted; you can't unmount it.
181 return -1;
182 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800183
Doug Zongkerd4208f92010-09-20 12:16:13 -0700184 int result;
185 result = scan_mounted_volumes();
186 if (result < 0) {
187 LOGE("failed to scan mounted volumes\n");
188 return -1;
189 }
190
191 const MountedVolume* mv =
192 find_mounted_volume_by_mount_point(v->mount_point);
193 if (mv == NULL) {
194 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 return 0;
196 }
197
Doug Zongkerd4208f92010-09-20 12:16:13 -0700198 return unmount_mounted_volume(mv);
199}
200
201int format_volume(const char* volume) {
202 Volume* v = volume_for_path(volume);
203 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800204 // silent failure for sd-ext
205 if (strcmp(volume, "/sd-ext") == 0)
206 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700207 LOGE("unknown volume \"%s\"\n", volume);
208 return -1;
209 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700210 if (strcmp(v->fs_type, "ramdisk") == 0) {
211 // you can't format the ramdisk.
212 LOGE("can't format_volume \"%s\"", volume);
213 return -1;
214 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700215 if (strcmp(v->mount_point, volume) != 0) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800216#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700217 LOGE("can't give path \"%s\" to format_volume\n", volume);
218 return -1;
Koushik Duttabf4444f2010-12-18 18:57:47 -0800219#endif
220 return format_unknown_device(volume);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700221 }
222
223 if (ensure_path_unmounted(volume) != 0) {
224 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
225 return -1;
226 }
227
228 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700230 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800231 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700232 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800233 return -1;
234 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800235
Doug Zongkerd4208f92010-09-20 12:16:13 -0700236 MtdWriteContext *write = mtd_write_partition(partition);
237 if (write == NULL) {
238 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700240 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
241 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
242 mtd_write_close(write);
243 return -1;
244 } else if (mtd_write_close(write)) {
245 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800246 return -1;
247 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 return 0;
249 }
250
Doug Zongkerd4208f92010-09-20 12:16:13 -0700251 if (strcmp(v->fs_type, "ext4") == 0) {
252 reset_ext4fs_info();
253 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
254 if (result != 0) {
255 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256 return -1;
257 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700258 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800259 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700260
Koushik Dutta5d808172010-12-18 22:29:27 -0800261#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700262 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800264#endif
265 return format_unknown_device(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800266}