blob: 90b82d7ed38f9f1fc6eb2836105010cedafeb603 [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 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;
162 }
163
164 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800165 return -1;
166}
167
Doug Zongkerd4208f92010-09-20 12:16:13 -0700168int ensure_path_unmounted(const char* path) {
169 Volume* v = volume_for_path(path);
170 if (v == NULL) {
171 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172 return -1;
173 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700174 if (strcmp(v->fs_type, "ramdisk") == 0) {
175 // the ramdisk is always mounted; you can't unmount it.
176 return -1;
177 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800178
Doug Zongkerd4208f92010-09-20 12:16:13 -0700179 int result;
180 result = scan_mounted_volumes();
181 if (result < 0) {
182 LOGE("failed to scan mounted volumes\n");
183 return -1;
184 }
185
186 const MountedVolume* mv =
187 find_mounted_volume_by_mount_point(v->mount_point);
188 if (mv == NULL) {
189 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190 return 0;
191 }
192
Doug Zongkerd4208f92010-09-20 12:16:13 -0700193 return unmount_mounted_volume(mv);
194}
195
196int format_volume(const char* volume) {
197 Volume* v = volume_for_path(volume);
198 if (v == NULL) {
199 LOGE("unknown volume \"%s\"\n", volume);
200 return -1;
201 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700202 if (strcmp(v->fs_type, "ramdisk") == 0) {
203 // you can't format the ramdisk.
204 LOGE("can't format_volume \"%s\"", volume);
205 return -1;
206 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700207 if (strcmp(v->mount_point, volume) != 0) {
208 LOGE("can't give path \"%s\" to format_volume\n", volume);
209 return -1;
210 }
211
212 if (ensure_path_unmounted(volume) != 0) {
213 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
214 return -1;
215 }
216
217 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700219 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800220 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700221 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222 return -1;
223 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224
Doug Zongkerd4208f92010-09-20 12:16:13 -0700225 MtdWriteContext *write = mtd_write_partition(partition);
226 if (write == NULL) {
227 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800228 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700229 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
230 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
231 mtd_write_close(write);
232 return -1;
233 } else if (mtd_write_close(write)) {
234 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800235 return -1;
236 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237 return 0;
238 }
239
Doug Zongkerd4208f92010-09-20 12:16:13 -0700240 if (strcmp(v->fs_type, "ext4") == 0) {
241 reset_ext4fs_info();
242 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
243 if (result != 0) {
244 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245 return -1;
246 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700247 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700249
250 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251 return -1;
252}