blob: fb495fede80f6ce363f1ea9e172ab5f4664b8017 [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 Zongkerd4208f92010-09-20 12:16:13 -070038 FILE* fstab = fopen("/etc/recovery.fstab", "r");
39 if (fstab == NULL) {
40 LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno));
41 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070043
44 char buffer[1024];
45 int i;
46 while (fgets(buffer, sizeof(buffer)-1, fstab)) {
47 for (i = 0; buffer[i] && isspace(buffer[i]); ++i);
48 if (buffer[i] == '\0' || buffer[i] == '#') continue;
49
50 char* original = strdup(buffer);
51
52 char* mount_point = strtok(buffer+i, " \t\n");
53 char* fs_type = strtok(NULL, " \t\n");
54 char* device = strtok(NULL, " \t\n");
55 // lines may optionally have a second device, to use if
56 // mounting the first one fails.
57 char* device2 = strtok(NULL, " \t\n");
58
59 if (mount_point && fs_type && device) {
60 while (num_volumes >= alloc) {
61 alloc *= 2;
62 device_volumes = realloc(device_volumes, alloc*sizeof(Volume));
63 }
64 device_volumes[num_volumes].mount_point = strdup(mount_point);
65 device_volumes[num_volumes].fs_type = strdup(fs_type);
66 device_volumes[num_volumes].device = strdup(device);
67 device_volumes[num_volumes].device2 =
68 device2 ? strdup(device2) : NULL;
69 ++num_volumes;
70 } else {
71 LOGE("skipping malformed recovery.fstab line: %s\n", original);
72 }
73 free(original);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070075
76 fclose(fstab);
77
78 printf("recovery filesystem table\n");
79 printf("=========================\n");
80 for (i = 0; i < num_volumes; ++i) {
81 Volume* v = &device_volumes[i];
82 printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type,
83 v->device, v->device2);
84 }
85 printf("\n");
86}
87
88Volume* volume_for_path(const char* path) {
89 int i;
90 for (i = 0; i < num_volumes; ++i) {
91 Volume* v = device_volumes+i;
92 int len = strlen(v->mount_point);
93 if (strncmp(path, v->mount_point, len) == 0 &&
94 (path[len] == '\0' || path[len] == '/')) {
95 return v;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096 }
97 }
98 return NULL;
99}
100
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101int ensure_path_mounted(const char* path) {
102 Volume* v = volume_for_path(path);
103 if (v == NULL) {
104 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800105 return -1;
106 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700107
108 int result;
109 result = scan_mounted_volumes();
110 if (result < 0) {
111 LOGE("failed to scan mounted volumes\n");
112 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -0700113 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114
Doug Zongkerd4208f92010-09-20 12:16:13 -0700115 const MountedVolume* mv =
116 find_mounted_volume_by_mount_point(v->mount_point);
117 if (mv) {
118 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800119 return 0;
120 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700121
122 mkdir(v->mount_point, 0755); // in case it doesn't already exist
123
124 if (strcmp(v->fs_type, "yaffs2") == 0) {
125 // mount an MTD partition as a YAFFS2 filesystem.
126 mtd_scan_partitions();
127 const MtdPartition* partition;
128 partition = mtd_find_partition_by_name(v->device);
129 if (partition == NULL) {
130 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
131 v->device, v->mount_point);
132 return -1;
133 }
134 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
135 } else if (strcmp(v->fs_type, "ext4") == 0 ||
136 strcmp(v->fs_type, "vfat") == 0) {
137 result = mount(v->device, v->mount_point, v->fs_type,
138 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
139 if (result == 0) return 0;
140
141 if (v->device2) {
142 LOGW("failed to mount %s (%s); trying %s\n",
143 v->device, strerror(errno), v->device2);
144 result = mount(v->device2, v->mount_point, v->fs_type,
145 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
146 if (result == 0) return 0;
147 }
148
149 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
150 return -1;
151 }
152
153 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 return -1;
155}
156
Doug Zongkerd4208f92010-09-20 12:16:13 -0700157int ensure_path_unmounted(const char* path) {
158 Volume* v = volume_for_path(path);
159 if (v == NULL) {
160 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 return -1;
162 }
163
Doug Zongkerd4208f92010-09-20 12:16:13 -0700164 int result;
165 result = scan_mounted_volumes();
166 if (result < 0) {
167 LOGE("failed to scan mounted volumes\n");
168 return -1;
169 }
170
171 const MountedVolume* mv =
172 find_mounted_volume_by_mount_point(v->mount_point);
173 if (mv == NULL) {
174 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175 return 0;
176 }
177
Doug Zongkerd4208f92010-09-20 12:16:13 -0700178 return unmount_mounted_volume(mv);
179}
180
181int format_volume(const char* volume) {
182 Volume* v = volume_for_path(volume);
183 if (v == NULL) {
184 LOGE("unknown volume \"%s\"\n", volume);
185 return -1;
186 }
187 if (strcmp(v->mount_point, volume) != 0) {
188 LOGE("can't give path \"%s\" to format_volume\n", volume);
189 return -1;
190 }
191
192 if (ensure_path_unmounted(volume) != 0) {
193 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
194 return -1;
195 }
196
197 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198 mtd_scan_partitions();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700199 const MtdPartition* partition = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 if (partition == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700201 LOGE("format_volume: no MTD partition \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202 return -1;
203 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800204
Doug Zongkerd4208f92010-09-20 12:16:13 -0700205 MtdWriteContext *write = mtd_write_partition(partition);
206 if (write == NULL) {
207 LOGW("format_volume: can't open MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700209 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
210 LOGW("format_volume: can't erase MTD \"%s\"\n", v->device);
211 mtd_write_close(write);
212 return -1;
213 } else if (mtd_write_close(write)) {
214 LOGW("format_volume: can't close MTD \"%s\"\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 return -1;
216 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 return 0;
218 }
219
Doug Zongkerd4208f92010-09-20 12:16:13 -0700220 if (strcmp(v->fs_type, "ext4") == 0) {
221 reset_ext4fs_info();
222 int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0);
223 if (result != 0) {
224 LOGE("format_volume: make_extf4fs failed on %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 return -1;
226 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700227 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800228 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700229
230 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800231 return -1;
232}