blob: 1143b76bc4b51b8a02af20034b2273fce623e194 [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"
26#include "mtdutils/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 Zongker2810ced2011-02-17 15:55:21 -080034static int parse_options(char* options, Volume* volume) {
35 char* option;
36 while (option = strtok(options, ",")) {
37 options = NULL;
38
39 if (strncmp(option, "length=", 7) == 0) {
40 volume->length = strtoll(option+7, NULL, 10);
41 } else {
42 LOGE("bad option \"%s\"\n", option);
43 return -1;
44 }
45 }
46 return 0;
47}
48
Doug Zongkerd4208f92010-09-20 12:16:13 -070049void load_volume_table() {
50 int alloc = 2;
51 device_volumes = malloc(alloc * sizeof(Volume));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052
Doug Zongkerc18eeb82010-09-21 16:49:26 -070053 // Insert an entry for /tmp, which is the ramdisk and is always mounted.
54 device_volumes[0].mount_point = "/tmp";
55 device_volumes[0].fs_type = "ramdisk";
56 device_volumes[0].device = NULL;
57 device_volumes[0].device2 = NULL;
Doug Zongker2810ced2011-02-17 15:55:21 -080058 device_volumes[0].length = 0;
Doug Zongkerc18eeb82010-09-21 16:49:26 -070059 num_volumes = 1;
60
Doug Zongkerd4208f92010-09-20 12:16:13 -070061 FILE* fstab = fopen("/etc/recovery.fstab", "r");
62 if (fstab == NULL) {
63 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
64 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070066
67 char buffer[1024];
68 int i;
69 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
70 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
71 if (buffer[i] == '\0' || buffer[i] == '#') continue;
72
73 char* original = strdup(buffer);
74
75 char* mount_point = strtok(buffer+i, " \t\n");
76 char* fs_type = strtok(NULL, " \t\n");
77 char* device = strtok(NULL, " \t\n");
78 // lines may optionally have a second device, to use if
79 // mounting the first one fails.
Doug Zongker2810ced2011-02-17 15:55:21 -080080 char* options = NULL;
Doug Zongkerd4208f92010-09-20 12:16:13 -070081 char* device2 = strtok(NULL, " \t\n");
Doug Zongker2810ced2011-02-17 15:55:21 -080082 if (device2) {
83 if (device2[0] == '/') {
84 options = strtok(NULL, " \t\n");
85 } else {
86 options = device2;
87 device2 = NULL;
88 }
89 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070090
91 if (mount_point && fs_type && device) {
92 while (num_volumes >= alloc) {
93 alloc *= 2;
94 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
95 }
96 device_volumes[num_volumes].mount_point = strdup(mount_point);
97 device_volumes[num_volumes].fs_type = strdup(fs_type);
98 device_volumes[num_volumes].device = strdup(device);
99 device_volumes[num_volumes].device2 =
100 device2 ? strdup(device2) : NULL;
Doug Zongker2810ced2011-02-17 15:55:21 -0800101
102 device_volumes[num_volumes].length = 0;
103 if (parse_options(options, device_volumes + num_volumes) != 0) {
104 LOGE("skipping malformed recovery.fstab line: %s\n", original);
105 } else {
106 ++num_volumes;
107 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700108 } else {
109 LOGE("skipping malformed recovery.fstab line: %s\n", original);
110 }
111 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113
114 fclose(fstab);
115
116 printf("recovery filesystem table\n");
117 printf("=========================\n");
118 for (i = 0; i < num_volumes; ++i) {
119 Volume* v = &device_volumes[i];
Doug Zongker2810ced2011-02-17 15:55:21 -0800120 printf(" %d %s %s %s %s %lld\n", i, v->mount_point, v->fs_type,
121 v->device, v->device2, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700122 }
123 printf("\n");
124}
125
126Volume* volume_for_path(const char* path) {
127 int i;
128 for (i = 0; i < num_volumes; ++i) {
129 Volume* v = device_volumes+i;
130 int len = strlen(v->mount_point);
131 if (strncmp(path, v->mount_point, len) == 0 &&
132 (path[len] == '\0' || path[len] == '/')) {
133 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134 }
135 }
136 return NULL;
137}
138
Doug Zongkerd4208f92010-09-20 12:16:13 -0700139int ensure_path_mounted(const char* path) {
140 Volume* v = volume_for_path(path);
141 if (v == NULL) {
142 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143 return -1;
144 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700145 if (strcmp(v->fs_type, "ramdisk") == 0) {
146 // the ramdisk is always mounted.
147 return 0;
148 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700149
150 int result;
151 result = scan_mounted_volumes();
152 if (result < 0) {
153 LOGE("failed to scan mounted volumes\n");
154 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700155 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156
Doug Zongkerd4208f92010-09-20 12:16:13 -0700157 const MountedVolume* mv =
158 find_mounted_volume_by_mount_point(v->mount_point);
159 if (mv) {
160 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 return 0;
162 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700163
164 mkdir(v->mount_point, 0755); // in case it doesn't already exist
165
166 if (strcmp(v->fs_type, "yaffs2") == 0) {
167 // mount an MTD partition as a YAFFS2 filesystem.
168 mtd_scan_partitions();
169 const MtdPartition* partition;
170 partition = mtd_find_partition_by_name(v->device);
171 if (partition == NULL) {
172 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
173 v->device, v->mount_point);
174 return -1;
175 }
176 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
177 } else if (strcmp(v->fs_type, "ext4") == 0 ||
178 strcmp(v->fs_type, "vfat") == 0) {
179 result = mount(v->device, v->mount_point, v->fs_type,
Iliyan Malchev201aa882011-03-11 16:27:56 -0800180 MS_NOATIME | MS_NODEV | MS_NODIRATIME | MS_RDONLY, "");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181 if (result == 0) return 0;
182
183 if (v->device2) {
184 LOGW("failed to mount %s (%s); trying %s\n",
185 v->device, strerror(errno), v->device2);
186 result = mount(v->device2, v->mount_point, v->fs_type,
Iliyan Malchev201aa882011-03-11 16:27:56 -0800187 MS_NOATIME | MS_NODEV |
188 MS_NODIRATIME | MS_RDONLY, "");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700189 if (result == 0) return 0;
190 }
191
192 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
193 return -1;
194 }
195
196 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197 return -1;
198}
199
Doug Zongkerd4208f92010-09-20 12:16:13 -0700200int ensure_path_unmounted(const char* path) {
201 Volume* v = volume_for_path(path);
202 if (v == NULL) {
203 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800204 return -1;
205 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700206 if (strcmp(v->fs_type, "ramdisk") == 0) {
207 // the ramdisk is always mounted; you can't unmount it.
208 return -1;
209 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210
Doug Zongkerd4208f92010-09-20 12:16:13 -0700211 int result;
212 result = scan_mounted_volumes();
213 if (result < 0) {
214 LOGE("failed to scan mounted volumes\n");
215 return -1;
216 }
217
218 const MountedVolume* mv =
219 find_mounted_volume_by_mount_point(v->mount_point);
220 if (mv == NULL) {
221 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222 return 0;
223 }
224
Doug Zongkerd4208f92010-09-20 12:16:13 -0700225 return unmount_mounted_volume(mv);
226}
227
228int format_volume(const char* volume) {
229 Volume* v = volume_for_path(volume);
230 if (v == NULL) {
231 LOGE("unknown volume \"%s\"\n", volume);
232 return -1;
233 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700234 if (strcmp(v->fs_type, "ramdisk") == 0) {
235 // you can't format the ramdisk.
236 LOGE("can't format_volume \"%s\"", volume);
237 return -1;
238 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700239 if (strcmp(v->mount_point, volume) != 0) {
240 LOGE("can't give path \"%s\" to format_volume\n", volume);
241 return -1;
242 }
243
244 if (ensure_path_unmounted(volume) != 0) {
245 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
246 return -1;
247 }
248
249 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700251 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700253 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800254 return -1;
255 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256
Doug Zongkerd4208f92010-09-20 12:16:13 -0700257 MtdWriteContext *write = mtd_write_partition(partition);
258 if (write == NULL) {
259 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800260 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700261 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
262 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
263 mtd_write_close(write);
264 return -1;
265 } else if (mtd_write_close(write)) {
266 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800267 return -1;
268 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269 return 0;
270 }
271
Doug Zongkerd4208f92010-09-20 12:16:13 -0700272 if (strcmp(v->fs_type, "ext4") == 0) {
Doug Zongker2810ced2011-02-17 15:55:21 -0800273 int result = make_ext4fs(v->device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700274 if (result != 0) {
275 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800276 return -1;
277 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700278 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700280
281 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 return -1;
283}