blob: d4c003327cb123ef1732b9506cf14f606b2c5800 [file] [log] [blame]
preludedrew38058dc2011-01-29 23:30:44 -07001/*
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>
23#include <ctype.h>
24
25#include "mtdutils/mtdutils.h"
26#include "mounts.h"
27#include "roots.h"
28#include "common.h"
29#include "make_ext4fs.h"
30
31static int num_volumes = 0;
32static Volume* device_volumes = NULL;
33
34void load_volume_table() {
35 int alloc = 2;
36 device_volumes = malloc(alloc * sizeof(Volume));
37
38 // 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
45 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;
49 }
50
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);
81 }
82
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;
103 }
104 }
105 return NULL;
106}
107
108int 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);
112 return -1;
113 }
114 if (strcmp(v->fs_type, "ramdisk") == 0) {
115 // the ramdisk is always mounted.
116 return 0;
117 }
118
119 int result;
120 result = scan_mounted_volumes();
121 if (result < 0) {
122 LOGE("failed to scan mounted volumes\n");
123 return -1;
124 }
125
126 const MountedVolume* mv =
127 find_mounted_volume_by_mount_point(v->mount_point);
128 if (mv) {
129 // volume is already mounted
130 return 0;
131 }
132
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;
162 } else {
163 // 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);
167 }
168
169 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
170 return -1;
171}
172
173int 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);
177 return -1;
178 }
179 if (strcmp(v->fs_type, "ramdisk") == 0) {
180 // the ramdisk is always mounted; you can't unmount it.
181 return -1;
182 }
183
184 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
195 return 0;
196 }
197
198 return unmount_mounted_volume(mv);
199}
200
201int format_volume(const char* volume) {
202 Volume* v = volume_for_path(volume);
203 if (v == NULL) {
204 // silent failure for sd-ext
205 if (strcmp(volume, "/sd-ext") == 0)
206 return -1;
207 LOGE("unknown volume \"%s\"\n", volume);
208 return -1;
209 }
210 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 }
215 if (strcmp(v->mount_point, volume) != 0) {
216#if 0
217 LOGE("can't give path \"%s\" to format_volume\n", volume);
218 return -1;
219#endif
220 return format_unknown_device(v->device, volume, NULL);
221 }
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) {
229 mtd_scan_partitions();
230 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
231 if (partition == NULL) {
232 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
233 return -1;
234 }
235
236 MtdWriteContext *write = mtd_write_partition(partition);
237 if (write == NULL) {
238 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
239 return -1;
240 } 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);
246 return -1;
247 }
248 return 0;
249 }
250
251 if (strcmp(v->fs_type, "emmc") == 0) {
252 return erase_raw_partition(v->device);
253 }
254
255 if (strcmp(v->fs_type, "ext4") == 0) {
256 reset_ext4fs_info();
257 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
258 if (result != 0) {
259 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
260 return -1;
261 }
262 return 0;
263 }
264
265#if 0
266 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
267 return -1;
268#endif
269 return format_unknown_device(v->device, volume, v->fs_type);
270}