blob: bfb828fa02d8af2c3cb1052bd485d23116e14621 [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 {
163 // let's just give it a shot and see what happens...
164 result = mount(v->device, v->mount_point, v->fs_type,
165 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
166 if (result == 0) return 0;
167
168 if (v->device2) {
169 LOGW("failed to mount %s (%s); trying %s\n",
170 v->device, strerror(errno), v->device2);
171 result = mount(v->device2, v->mount_point, v->fs_type,
172 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
173 if (result == 0) return 0;
174 }
175
176 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
177 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700178 }
179
180 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181 return -1;
182}
183
Doug Zongkerd4208f92010-09-20 12:16:13 -0700184int ensure_path_unmounted(const char* path) {
185 Volume* v = volume_for_path(path);
186 if (v == NULL) {
187 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188 return -1;
189 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700190 if (strcmp(v->fs_type, "ramdisk") == 0) {
191 // the ramdisk is always mounted; you can't unmount it.
192 return -1;
193 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194
Doug Zongkerd4208f92010-09-20 12:16:13 -0700195 int result;
196 result = scan_mounted_volumes();
197 if (result < 0) {
198 LOGE("failed to scan mounted volumes\n");
199 return -1;
200 }
201
202 const MountedVolume* mv =
203 find_mounted_volume_by_mount_point(v->mount_point);
204 if (mv == NULL) {
205 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206 return 0;
207 }
208
Doug Zongkerd4208f92010-09-20 12:16:13 -0700209 return unmount_mounted_volume(mv);
210}
211
212int format_volume(const char* volume) {
213 Volume* v = volume_for_path(volume);
214 if (v == NULL) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800215 // silent failure for sd-ext
216 if (strcmp(volume, "/sd-ext") == 0)
217 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700218 LOGE("unknown volume \"%s\"\n", volume);
219 return -1;
220 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700221 if (strcmp(v->fs_type, "ramdisk") == 0) {
222 // you can't format the ramdisk.
223 LOGE("can't format_volume \"%s\"", volume);
224 return -1;
225 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700226 if (strcmp(v->mount_point, volume) != 0) {
Koushik Duttabf4444f2010-12-18 18:57:47 -0800227#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700228 LOGE("can't give path \"%s\" to format_volume\n", volume);
229 return -1;
Koushik Duttabf4444f2010-12-18 18:57:47 -0800230#endif
231 return format_unknown_device(volume);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700232 }
233
234 if (ensure_path_unmounted(volume) != 0) {
235 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
236 return -1;
237 }
238
239 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800240 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700241 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800242 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700243 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244 return -1;
245 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800246
Doug Zongkerd4208f92010-09-20 12:16:13 -0700247 MtdWriteContext *write = mtd_write_partition(partition);
248 if (write == NULL) {
249 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700251 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
252 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
253 mtd_write_close(write);
254 return -1;
255 } else if (mtd_write_close(write)) {
256 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800257 return -1;
258 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800259 return 0;
260 }
261
Doug Zongkerd4208f92010-09-20 12:16:13 -0700262 if (strcmp(v->fs_type, "ext4") == 0) {
263 reset_ext4fs_info();
264 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
265 if (result != 0) {
266 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800267 return -1;
268 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700269 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700271
Koushik Dutta5d808172010-12-18 22:29:27 -0800272#if 0
Doug Zongkerd4208f92010-09-20 12:16:13 -0700273 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800274 return -1;
Koushik Dutta5d808172010-12-18 22:29:27 -0800275#endif
276 return format_unknown_device(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277}