blob: cb7e067a1711bc35bf8757d4424c03ed0f8f5173 [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,
Doug Zongker469243e2011-04-12 09:28:10 -0700180 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
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,
Doug Zongker469243e2011-04-12 09:28:10 -0700187 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700188 if (result == 0) return 0;
189 }
190
191 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
192 return -1;
193 }
194
195 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800196 return -1;
197}
198
Doug Zongkerd4208f92010-09-20 12:16:13 -0700199int ensure_path_unmounted(const char* path) {
200 Volume* v = volume_for_path(path);
201 if (v == NULL) {
202 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 return -1;
204 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700205 if (strcmp(v->fs_type, "ramdisk") == 0) {
206 // the ramdisk is always mounted; you can't unmount it.
207 return -1;
208 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209
Doug Zongkerd4208f92010-09-20 12:16:13 -0700210 int result;
211 result = scan_mounted_volumes();
212 if (result < 0) {
213 LOGE("failed to scan mounted volumes\n");
214 return -1;
215 }
216
217 const MountedVolume* mv =
218 find_mounted_volume_by_mount_point(v->mount_point);
219 if (mv == NULL) {
220 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221 return 0;
222 }
223
Doug Zongkerd4208f92010-09-20 12:16:13 -0700224 return unmount_mounted_volume(mv);
225}
226
227int format_volume(const char* volume) {
228 Volume* v = volume_for_path(volume);
229 if (v == NULL) {
230 LOGE("unknown volume \"%s\"\n", volume);
231 return -1;
232 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700233 if (strcmp(v->fs_type, "ramdisk") == 0) {
234 // you can't format the ramdisk.
235 LOGE("can't format_volume \"%s\"", volume);
236 return -1;
237 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700238 if (strcmp(v->mount_point, volume) != 0) {
239 LOGE("can't give path \"%s\" to format_volume\n", volume);
240 return -1;
241 }
242
243 if (ensure_path_unmounted(volume) != 0) {
244 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
245 return -1;
246 }
247
248 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800249 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700250 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700252 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253 return -1;
254 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255
Doug Zongkerd4208f92010-09-20 12:16:13 -0700256 MtdWriteContext *write = mtd_write_partition(partition);
257 if (write == NULL) {
258 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800259 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700260 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
261 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
262 mtd_write_close(write);
263 return -1;
264 } else if (mtd_write_close(write)) {
265 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800266 return -1;
267 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800268 return 0;
269 }
270
Doug Zongkerd4208f92010-09-20 12:16:13 -0700271 if (strcmp(v->fs_type, "ext4") == 0) {
Doug Zongker2810ced2011-02-17 15:55:21 -0800272 int result = make_ext4fs(v->device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700273 if (result != 0) {
274 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275 return -1;
276 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700277 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700279
280 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281 return -1;
282}