blob: 12c6b5ee26b4ec942d0d68101ceadb38b34b3642 [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>
JP Abgrall37aedb32014-06-16 19:07:39 -070022#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <unistd.h>
Doug Zongkerd4208f92010-09-20 12:16:13 -070024#include <ctype.h>
Doug Zongkerf39989a2013-12-11 15:40:28 -080025#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080027#include <fs_mgr.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include "mtdutils/mtdutils.h"
29#include "mtdutils/mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include "roots.h"
31#include "common.h"
Doug Zongkerd4208f92010-09-20 12:16:13 -070032#include "make_ext4fs.h"
Doug Zongkerf39989a2013-12-11 15:40:28 -080033#include "wipe.h"
34#include "cryptfs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080036static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Kenny Root41dda822012-03-30 20:48:34 -070038extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050039
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040void load_volume_table()
41{
42 int i;
43 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080044
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080045 fstab = fs_mgr_read_fstab("/etc/recovery.fstab");
46 if (!fstab) {
47 LOGE("failed to read /etc/recovery.fstab\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070048 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070050
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070051 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080052 if (ret < 0 ) {
53 LOGE("failed to add /tmp entry to fstab\n");
54 fs_mgr_free_fstab(fstab);
55 fstab = NULL;
56 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070058
Doug Zongkerd4208f92010-09-20 12:16:13 -070059 printf("recovery filesystem table\n");
60 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080061 for (i = 0; i < fstab->num_entries; ++i) {
62 Volume* v = &fstab->recs[i];
63 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
64 v->blk_device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -070065 }
66 printf("\n");
67}
68
69Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080070 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071}
72
Tao Baoabb8f772015-07-30 14:43:27 -070073// Mount the volume specified by path at the given mount_point.
74int ensure_path_mounted_at(const char* path, const char* mount_point) {
Doug Zongkerd4208f92010-09-20 12:16:13 -070075 Volume* v = volume_for_path(path);
76 if (v == NULL) {
77 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078 return -1;
79 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070080 if (strcmp(v->fs_type, "ramdisk") == 0) {
81 // the ramdisk is always mounted.
82 return 0;
83 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070084
85 int result;
86 result = scan_mounted_volumes();
87 if (result < 0) {
88 LOGE("failed to scan mounted volumes\n");
89 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070090 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091
Tao Baoabb8f772015-07-30 14:43:27 -070092 if (!mount_point) {
93 mount_point = v->mount_point;
94 }
95
Doug Zongkerd4208f92010-09-20 12:16:13 -070096 const MountedVolume* mv =
Tao Baoabb8f772015-07-30 14:43:27 -070097 find_mounted_volume_by_mount_point(mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -070098 if (mv) {
99 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 return 0;
101 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700102
Tao Baoabb8f772015-07-30 14:43:27 -0700103 mkdir(mount_point, 0755); // in case it doesn't already exist
Doug Zongkerd4208f92010-09-20 12:16:13 -0700104
105 if (strcmp(v->fs_type, "yaffs2") == 0) {
106 // mount an MTD partition as a YAFFS2 filesystem.
107 mtd_scan_partitions();
108 const MtdPartition* partition;
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800109 partition = mtd_find_partition_by_name(v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700110 if (partition == NULL) {
111 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Tao Baoabb8f772015-07-30 14:43:27 -0700112 v->blk_device, mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113 return -1;
114 }
Tao Baoabb8f772015-07-30 14:43:27 -0700115 return mtd_mount_partition(partition, mount_point, v->fs_type, 0);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700116 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash522ea722015-06-29 18:57:14 -0700117 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700118 strcmp(v->fs_type, "vfat") == 0) {
Tao Baoabb8f772015-07-30 14:43:27 -0700119 result = mount(v->blk_device, mount_point, v->fs_type,
Gaelle Nassiete853e962015-03-23 17:51:53 +0100120 v->flags, v->fs_options);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700121 if (result == 0) return 0;
122
Tao Baoabb8f772015-07-30 14:43:27 -0700123 LOGE("failed to mount %s (%s)\n", mount_point, strerror(errno));
Doug Zongkerd4208f92010-09-20 12:16:13 -0700124 return -1;
125 }
126
Tao Baoabb8f772015-07-30 14:43:27 -0700127 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800128 return -1;
129}
130
Tao Baoabb8f772015-07-30 14:43:27 -0700131int ensure_path_mounted(const char* path) {
132 // Mount at the default mount point.
133 return ensure_path_mounted_at(path, nullptr);
134}
135
Doug Zongkerd4208f92010-09-20 12:16:13 -0700136int ensure_path_unmounted(const char* path) {
137 Volume* v = volume_for_path(path);
138 if (v == NULL) {
139 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 return -1;
141 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700142 if (strcmp(v->fs_type, "ramdisk") == 0) {
143 // the ramdisk is always mounted; you can't unmount it.
144 return -1;
145 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146
Doug Zongkerd4208f92010-09-20 12:16:13 -0700147 int result;
148 result = scan_mounted_volumes();
149 if (result < 0) {
150 LOGE("failed to scan mounted volumes\n");
151 return -1;
152 }
153
154 const MountedVolume* mv =
155 find_mounted_volume_by_mount_point(v->mount_point);
156 if (mv == NULL) {
157 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 return 0;
159 }
160
Doug Zongkerd4208f92010-09-20 12:16:13 -0700161 return unmount_mounted_volume(mv);
162}
163
JP Abgrall37aedb32014-06-16 19:07:39 -0700164static int exec_cmd(const char* path, char* const argv[]) {
165 int status;
166 pid_t child;
167 if ((child = vfork()) == 0) {
168 execv(path, argv);
169 _exit(-1);
170 }
171 waitpid(child, &status, 0);
172 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
173 LOGE("%s failed with status %d\n", path, WEXITSTATUS(status));
174 }
175 return WEXITSTATUS(status);
176}
177
Doug Zongkerd4208f92010-09-20 12:16:13 -0700178int format_volume(const char* volume) {
179 Volume* v = volume_for_path(volume);
180 if (v == NULL) {
181 LOGE("unknown volume \"%s\"\n", volume);
182 return -1;
183 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700184 if (strcmp(v->fs_type, "ramdisk") == 0) {
185 // you can't format the ramdisk.
186 LOGE("can't format_volume \"%s\"", volume);
187 return -1;
188 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700189 if (strcmp(v->mount_point, volume) != 0) {
190 LOGE("can't give path \"%s\" to format_volume\n", volume);
191 return -1;
192 }
193
194 if (ensure_path_unmounted(volume) != 0) {
195 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
196 return -1;
197 }
198
199 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800201 const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202 if (partition == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800203 LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800204 return -1;
205 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206
Doug Zongkerd4208f92010-09-20 12:16:13 -0700207 MtdWriteContext *write = mtd_write_partition(partition);
208 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800209 LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700211 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800212 LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700213 mtd_write_close(write);
214 return -1;
215 } else if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800216 LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 return -1;
218 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219 return 0;
220 }
221
JP Abgrall37aedb32014-06-16 19:07:39 -0700222 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800223 // if there's a key_loc that looks like a path, it should be a
224 // block device for storing encryption metadata. wipe it too.
225 if (v->key_loc != NULL && v->key_loc[0] == '/') {
226 LOGI("wiping %s\n", v->key_loc);
227 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
228 if (fd < 0) {
229 LOGE("format_volume: failed to open %s\n", v->key_loc);
230 return -1;
231 }
232 wipe_block_device(fd, get_file_size(fd));
233 close(fd);
234 }
235
JP Abgrall37aedb32014-06-16 19:07:39 -0700236 ssize_t length = 0;
237 if (v->length != 0) {
238 length = v->length;
239 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
240 length = -CRYPT_FOOTER_OFFSET;
241 }
242 int result;
243 if (strcmp(v->fs_type, "ext4") == 0) {
244 result = make_ext4fs(v->blk_device, length, volume, sehandle);
245 } else { /* Has to be f2fs because we checked earlier. */
246 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700247 LOGE("format_volume: crypt footer + negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700248 return -1;
249 }
250 if (length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700251 LOGE("format_volume: negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700252 return -1;
253 }
254 char *num_sectors;
JP Abgrall78d458c2014-08-04 16:44:33 -0700255 if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
JP Abgrall37aedb32014-06-16 19:07:39 -0700256 LOGE("format_volume: failed to create %s command for %s\n", v->fs_type, v->blk_device);
257 return -1;
258 }
259 const char *f2fs_path = "/sbin/mkfs.f2fs";
260 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
261
262 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
263 free(num_sectors);
264 }
265 if (result != 0) {
266 LOGE("format_volume: make %s failed on %s with %d(%s)\n", v->fs_type, v->blk_device, result, strerror(errno));
267 return -1;
268 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700269 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700271
272 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800273 return -1;
274}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700275
276int setup_install_mounts() {
277 if (fstab == NULL) {
278 LOGE("can't set up install mounts: no fstab loaded\n");
279 return -1;
280 }
281 for (int i = 0; i < fstab->num_entries; ++i) {
282 Volume* v = fstab->recs + i;
283
284 if (strcmp(v->mount_point, "/tmp") == 0 ||
285 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800286 if (ensure_path_mounted(v->mount_point) != 0) {
287 LOGE("failed to mount %s\n", v->mount_point);
288 return -1;
289 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700290
291 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800292 if (ensure_path_unmounted(v->mount_point) != 0) {
293 LOGE("failed to unmount %s\n", v->mount_point);
294 return -1;
295 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700296 }
297 }
298 return 0;
299}