blob: b06b9c616698b7bb2826539ccc976e6f94e77bcf [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
Elliott Hughes63a31922016-06-09 17:41:22 -070017#include "roots.h"
18
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <errno.h>
20#include <stdlib.h>
21#include <sys/mount.h>
22#include <sys/stat.h>
23#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070024#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <unistd.h>
Doug Zongkerd4208f92010-09-20 12:16:13 -070026#include <ctype.h>
Doug Zongkerf39989a2013-12-11 15:40:28 -080027#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080029#include <fs_mgr.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include "common.h"
Doug Zongkerd4208f92010-09-20 12:16:13 -070031#include "make_ext4fs.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070032#include "mounts.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
Elliott Hughes63a31922016-06-09 17:41:22 -070085 if (!scan_mounted_volumes()) {
Doug Zongkerd4208f92010-09-20 12:16:13 -070086 LOGE("failed to scan mounted volumes\n");
87 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070088 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089
Tao Baoabb8f772015-07-30 14:43:27 -070090 if (!mount_point) {
91 mount_point = v->mount_point;
92 }
93
Elliott Hughes63a31922016-06-09 17:41:22 -070094 MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -070095 if (mv) {
96 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 return 0;
98 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070099
Tao Baoabb8f772015-07-30 14:43:27 -0700100 mkdir(mount_point, 0755); // in case it doesn't already exist
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101
Elliott Hughes63a31922016-06-09 17:41:22 -0700102 if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash522ea722015-06-29 18:57:14 -0700103 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700104 strcmp(v->fs_type, "vfat") == 0) {
Elliott Hughes63a31922016-06-09 17:41:22 -0700105 if (mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options) == -1) {
106 LOGE("failed to mount %s (%s)\n", mount_point, strerror(errno));
107 return -1;
108 }
109 return 0;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700110 }
111
Tao Baoabb8f772015-07-30 14:43:27 -0700112 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113 return -1;
114}
115
Tao Baoabb8f772015-07-30 14:43:27 -0700116int ensure_path_mounted(const char* path) {
117 // Mount at the default mount point.
118 return ensure_path_mounted_at(path, nullptr);
119}
120
Doug Zongkerd4208f92010-09-20 12:16:13 -0700121int ensure_path_unmounted(const char* path) {
122 Volume* v = volume_for_path(path);
123 if (v == NULL) {
124 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 return -1;
126 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700127 if (strcmp(v->fs_type, "ramdisk") == 0) {
128 // the ramdisk is always mounted; you can't unmount it.
129 return -1;
130 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800131
Elliott Hughes63a31922016-06-09 17:41:22 -0700132 if (!scan_mounted_volumes()) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700133 LOGE("failed to scan mounted volumes\n");
134 return -1;
135 }
136
Elliott Hughes63a31922016-06-09 17:41:22 -0700137 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700138 if (mv == NULL) {
139 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 return 0;
141 }
142
Doug Zongkerd4208f92010-09-20 12:16:13 -0700143 return unmount_mounted_volume(mv);
144}
145
JP Abgrall37aedb32014-06-16 19:07:39 -0700146static int exec_cmd(const char* path, char* const argv[]) {
147 int status;
148 pid_t child;
149 if ((child = vfork()) == 0) {
150 execv(path, argv);
151 _exit(-1);
152 }
153 waitpid(child, &status, 0);
154 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
155 LOGE("%s failed with status %d\n", path, WEXITSTATUS(status));
156 }
157 return WEXITSTATUS(status);
158}
159
Doug Zongkerd4208f92010-09-20 12:16:13 -0700160int format_volume(const char* volume) {
161 Volume* v = volume_for_path(volume);
162 if (v == NULL) {
163 LOGE("unknown volume \"%s\"\n", volume);
164 return -1;
165 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700166 if (strcmp(v->fs_type, "ramdisk") == 0) {
167 // you can't format the ramdisk.
168 LOGE("can't format_volume \"%s\"", volume);
169 return -1;
170 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700171 if (strcmp(v->mount_point, volume) != 0) {
172 LOGE("can't give path \"%s\" to format_volume\n", volume);
173 return -1;
174 }
175
176 if (ensure_path_unmounted(volume) != 0) {
177 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
178 return -1;
179 }
180
JP Abgrall37aedb32014-06-16 19:07:39 -0700181 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800182 // if there's a key_loc that looks like a path, it should be a
183 // block device for storing encryption metadata. wipe it too.
184 if (v->key_loc != NULL && v->key_loc[0] == '/') {
185 LOGI("wiping %s\n", v->key_loc);
186 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
187 if (fd < 0) {
188 LOGE("format_volume: failed to open %s\n", v->key_loc);
189 return -1;
190 }
191 wipe_block_device(fd, get_file_size(fd));
192 close(fd);
193 }
194
JP Abgrall37aedb32014-06-16 19:07:39 -0700195 ssize_t length = 0;
196 if (v->length != 0) {
197 length = v->length;
198 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
199 length = -CRYPT_FOOTER_OFFSET;
200 }
201 int result;
202 if (strcmp(v->fs_type, "ext4") == 0) {
203 result = make_ext4fs(v->blk_device, length, volume, sehandle);
204 } else { /* Has to be f2fs because we checked earlier. */
205 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700206 LOGE("format_volume: crypt footer + negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700207 return -1;
208 }
209 if (length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700210 LOGE("format_volume: negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700211 return -1;
212 }
213 char *num_sectors;
JP Abgrall78d458c2014-08-04 16:44:33 -0700214 if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
JP Abgrall37aedb32014-06-16 19:07:39 -0700215 LOGE("format_volume: failed to create %s command for %s\n", v->fs_type, v->blk_device);
216 return -1;
217 }
218 const char *f2fs_path = "/sbin/mkfs.f2fs";
219 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
220
221 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
222 free(num_sectors);
223 }
224 if (result != 0) {
225 LOGE("format_volume: make %s failed on %s with %d(%s)\n", v->fs_type, v->blk_device, result, strerror(errno));
226 return -1;
227 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700228 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700230
231 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800232 return -1;
233}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700234
235int setup_install_mounts() {
236 if (fstab == NULL) {
237 LOGE("can't set up install mounts: no fstab loaded\n");
238 return -1;
239 }
240 for (int i = 0; i < fstab->num_entries; ++i) {
241 Volume* v = fstab->recs + i;
242
243 if (strcmp(v->mount_point, "/tmp") == 0 ||
244 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800245 if (ensure_path_mounted(v->mount_point) != 0) {
246 LOGE("failed to mount %s\n", v->mount_point);
247 return -1;
248 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700249
250 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800251 if (ensure_path_unmounted(v->mount_point) != 0) {
252 LOGE("failed to unmount %s\n", v->mount_point);
253 return -1;
254 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700255 }
256 }
257 return 0;
258}