blob: 3bd680157c8ab4ec3ba86f21aa77d908bb528c97 [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
Dees_Troy51a0e822012-09-05 15:24:24 -040025extern "C" {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include "mtdutils/mtdutils.h"
27#include "mtdutils/mounts.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040028}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include "roots.h"
30#include "common.h"
Doug Zongkerd4208f92010-09-20 12:16:13 -070031#include "make_ext4fs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Doug Zongkerd4208f92010-09-20 12:16:13 -070033static int num_volumes = 0;
34static Volume* device_volumes = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035
Kenny Root41dda822012-03-30 20:48:34 -070036extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050037
Doug Zongker2810ced2011-02-17 15:55:21 -080038static int parse_options(char* options, Volume* volume) {
39 char* option;
Doug Zongker28ce47c2011-10-28 10:33:05 -070040 while ((option = strtok(options, ","))) {
Doug Zongker2810ced2011-02-17 15:55:21 -080041 options = NULL;
42
43 if (strncmp(option, "length=", 7) == 0) {
44 volume->length = strtoll(option+7, NULL, 10);
45 } else {
46 LOGE("bad option \"%s\"\n", option);
47 return -1;
48 }
49 }
50 return 0;
51}
52
Doug Zongkerd4208f92010-09-20 12:16:13 -070053void load_volume_table() {
54 int alloc = 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -070055 device_volumes = (Volume*)malloc(alloc * sizeof(Volume));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056
Doug Zongkerc18eeb82010-09-21 16:49:26 -070057 // Insert an entry for /tmp, which is the ramdisk and is always mounted.
58 device_volumes[0].mount_point = "/tmp";
59 device_volumes[0].fs_type = "ramdisk";
60 device_volumes[0].device = NULL;
61 device_volumes[0].device2 = NULL;
Doug Zongker2810ced2011-02-17 15:55:21 -080062 device_volumes[0].length = 0;
Doug Zongkerc18eeb82010-09-21 16:49:26 -070063 num_volumes = 1;
64
Doug Zongkerd4208f92010-09-20 12:16:13 -070065 FILE* fstab = fopen("/etc/recovery.fstab", "r");
66 if (fstab == NULL) {
67 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
68 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070070
71 char buffer[1024];
72 int i;
73 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
74 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
75 if (buffer[i] == '\0' || buffer[i] == '#') continue;
76
77 char* original = strdup(buffer);
78
79 char* mount_point = strtok(buffer+i, " \t\n");
80 char* fs_type = strtok(NULL, " \t\n");
81 char* device = strtok(NULL, " \t\n");
82 // lines may optionally have a second device, to use if
83 // mounting the first one fails.
Doug Zongker2810ced2011-02-17 15:55:21 -080084 char* options = NULL;
Doug Zongkerd4208f92010-09-20 12:16:13 -070085 char* device2 = strtok(NULL, " \t\n");
Doug Zongker2810ced2011-02-17 15:55:21 -080086 if (device2) {
87 if (device2[0] == '/') {
88 options = strtok(NULL, " \t\n");
89 } else {
90 options = device2;
91 device2 = NULL;
92 }
93 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070094
95 if (mount_point && fs_type && device) {
96 while (num_volumes >= alloc) {
97 alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -070098 device_volumes = (Volume*)realloc(device_volumes, alloc*sizeof(Volume));
Doug Zongkerd4208f92010-09-20 12:16:13 -070099 }
100 device_volumes[num_volumes].mount_point = strdup(mount_point);
101 device_volumes[num_volumes].fs_type = strdup(fs_type);
102 device_volumes[num_volumes].device = strdup(device);
103 device_volumes[num_volumes].device2 =
104 device2 ? strdup(device2) : NULL;
Doug Zongker2810ced2011-02-17 15:55:21 -0800105
106 device_volumes[num_volumes].length = 0;
107 if (parse_options(options, device_volumes + num_volumes) != 0) {
108 LOGE("skipping malformed recovery.fstab line: %s\n", original);
109 } else {
110 ++num_volumes;
111 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700112 } else {
113 LOGE("skipping malformed recovery.fstab line: %s\n", original);
114 }
115 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700117
118 fclose(fstab);
119
120 printf("recovery filesystem table\n");
121 printf("=========================\n");
122 for (i = 0; i < num_volumes; ++i) {
123 Volume* v = &device_volumes[i];
Doug Zongker2810ced2011-02-17 15:55:21 -0800124 printf(" %d %s %s %s %s %lld\n", i, v->mount_point, v->fs_type,
125 v->device, v->device2, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700126 }
127 printf("\n");
128}
129
130Volume* volume_for_path(const char* path) {
131 int i;
132 for (i = 0; i < num_volumes; ++i) {
133 Volume* v = device_volumes+i;
134 int len = strlen(v->mount_point);
135 if (strncmp(path, v->mount_point, len) == 0 &&
136 (path[len] == '\0' || path[len] == '/')) {
137 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138 }
139 }
140 return NULL;
141}
142
Doug Zongkerd4208f92010-09-20 12:16:13 -0700143int ensure_path_mounted(const char* path) {
144 Volume* v = volume_for_path(path);
145 if (v == NULL) {
146 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147 return -1;
148 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700149 if (strcmp(v->fs_type, "ramdisk") == 0) {
150 // the ramdisk is always mounted.
151 return 0;
152 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700153
154 int result;
155 result = scan_mounted_volumes();
156 if (result < 0) {
157 LOGE("failed to scan mounted volumes\n");
158 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700159 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160
Doug Zongkerd4208f92010-09-20 12:16:13 -0700161 const MountedVolume* mv =
162 find_mounted_volume_by_mount_point(v->mount_point);
163 if (mv) {
164 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165 return 0;
166 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700167
168 mkdir(v->mount_point, 0755); // in case it doesn't already exist
169
170 if (strcmp(v->fs_type, "yaffs2") == 0) {
171 // mount an MTD partition as a YAFFS2 filesystem.
172 mtd_scan_partitions();
173 const MtdPartition* partition;
174 partition = mtd_find_partition_by_name(v->device);
175 if (partition == NULL) {
176 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
177 v->device, v->mount_point);
178 return -1;
179 }
180 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
181 } else if (strcmp(v->fs_type, "ext4") == 0 ||
182 strcmp(v->fs_type, "vfat") == 0) {
183 result = mount(v->device, v->mount_point, v->fs_type,
Doug Zongker469243e2011-04-12 09:28:10 -0700184 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700185 if (result == 0) return 0;
186
187 if (v->device2) {
188 LOGW("failed to mount %s (%s); trying %s\n",
189 v->device, strerror(errno), v->device2);
190 result = mount(v->device2, v->mount_point, v->fs_type,
Doug Zongker469243e2011-04-12 09:28:10 -0700191 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700192 if (result == 0) return 0;
193 }
194
195 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
196 return -1;
197 }
198
199 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 return -1;
201}
202
Doug Zongkerd4208f92010-09-20 12:16:13 -0700203int ensure_path_unmounted(const char* path) {
204 Volume* v = volume_for_path(path);
205 if (v == NULL) {
206 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207 return -1;
208 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700209 if (strcmp(v->fs_type, "ramdisk") == 0) {
210 // the ramdisk is always mounted; you can't unmount it.
211 return -1;
212 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213
Doug Zongkerd4208f92010-09-20 12:16:13 -0700214 int result;
215 result = scan_mounted_volumes();
216 if (result < 0) {
217 LOGE("failed to scan mounted volumes\n");
218 return -1;
219 }
220
221 const MountedVolume* mv =
222 find_mounted_volume_by_mount_point(v->mount_point);
223 if (mv == NULL) {
224 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 return 0;
226 }
227
Doug Zongkerd4208f92010-09-20 12:16:13 -0700228 return unmount_mounted_volume(mv);
229}
230
231int format_volume(const char* volume) {
232 Volume* v = volume_for_path(volume);
233 if (v == NULL) {
234 LOGE("unknown volume \"%s\"\n", volume);
235 return -1;
236 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700237 if (strcmp(v->fs_type, "ramdisk") == 0) {
238 // you can't format the ramdisk.
239 LOGE("can't format_volume \"%s\"", volume);
240 return -1;
241 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700242 if (strcmp(v->mount_point, volume) != 0) {
243 LOGE("can't give path \"%s\" to format_volume\n", volume);
244 return -1;
245 }
246
247 if (ensure_path_unmounted(volume) != 0) {
248 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
249 return -1;
250 }
251
252 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700254 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700256 LOGE("format_volume: no MTD partition \"%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
Doug Zongkerd4208f92010-09-20 12:16:13 -0700260 MtdWriteContext *write = mtd_write_partition(partition);
261 if (write == NULL) {
262 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700264 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
265 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
266 mtd_write_close(write);
267 return -1;
268 } else if (mtd_write_close(write)) {
269 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 return -1;
271 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800272 return 0;
273 }
274
Doug Zongkerd4208f92010-09-20 12:16:13 -0700275 if (strcmp(v->fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500276 int result = make_ext4fs(v->device, v->length, volume, sehandle);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700277 if (result != 0) {
278 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279 return -1;
280 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700281 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700283
284 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800285 return -1;
286}