blob: 49369475b908069ec2609084eec0c26a497c65ea [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) {
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200105 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
106 if (result == -1 && fs_mgr_is_formattable(v)) {
107 LOGE("failed to mount %s (%s), formatting ...\n",
108 mount_point, strerror(errno));
109 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
110 if (fs_mgr_do_format(v, crypt_footer) == 0) {
111 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
112 } else {
113 LOGE("failed to format %s (%s)\n", mount_point, strerror(errno));
114 return -1;
115 }
116 }
117
118 if (result == -1) {
Elliott Hughes63a31922016-06-09 17:41:22 -0700119 LOGE("failed to mount %s (%s)\n", mount_point, strerror(errno));
120 return -1;
121 }
122 return 0;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700123 }
124
Tao Baoabb8f772015-07-30 14:43:27 -0700125 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126 return -1;
127}
128
Tao Baoabb8f772015-07-30 14:43:27 -0700129int ensure_path_mounted(const char* path) {
130 // Mount at the default mount point.
131 return ensure_path_mounted_at(path, nullptr);
132}
133
Doug Zongkerd4208f92010-09-20 12:16:13 -0700134int ensure_path_unmounted(const char* path) {
135 Volume* v = volume_for_path(path);
136 if (v == NULL) {
137 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138 return -1;
139 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700140 if (strcmp(v->fs_type, "ramdisk") == 0) {
141 // the ramdisk is always mounted; you can't unmount it.
142 return -1;
143 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144
Elliott Hughes63a31922016-06-09 17:41:22 -0700145 if (!scan_mounted_volumes()) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700146 LOGE("failed to scan mounted volumes\n");
147 return -1;
148 }
149
Elliott Hughes63a31922016-06-09 17:41:22 -0700150 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700151 if (mv == NULL) {
152 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153 return 0;
154 }
155
Doug Zongkerd4208f92010-09-20 12:16:13 -0700156 return unmount_mounted_volume(mv);
157}
158
JP Abgrall37aedb32014-06-16 19:07:39 -0700159static int exec_cmd(const char* path, char* const argv[]) {
160 int status;
161 pid_t child;
162 if ((child = vfork()) == 0) {
163 execv(path, argv);
164 _exit(-1);
165 }
166 waitpid(child, &status, 0);
167 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
168 LOGE("%s failed with status %d\n", path, WEXITSTATUS(status));
169 }
170 return WEXITSTATUS(status);
171}
172
Paul Lawrenced0db3372015-11-05 13:38:40 -0800173int format_volume(const char* volume, const char* directory) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700174 Volume* v = volume_for_path(volume);
175 if (v == NULL) {
176 LOGE("unknown volume \"%s\"\n", volume);
177 return -1;
178 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700179 if (strcmp(v->fs_type, "ramdisk") == 0) {
180 // you can't format the ramdisk.
181 LOGE("can't format_volume \"%s\"", volume);
182 return -1;
183 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700184 if (strcmp(v->mount_point, volume) != 0) {
185 LOGE("can't give path \"%s\" to format_volume\n", volume);
186 return -1;
187 }
188
189 if (ensure_path_unmounted(volume) != 0) {
190 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
191 return -1;
192 }
193
JP Abgrall37aedb32014-06-16 19:07:39 -0700194 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800195 // if there's a key_loc that looks like a path, it should be a
196 // block device for storing encryption metadata. wipe it too.
197 if (v->key_loc != NULL && v->key_loc[0] == '/') {
198 LOGI("wiping %s\n", v->key_loc);
199 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
200 if (fd < 0) {
201 LOGE("format_volume: failed to open %s\n", v->key_loc);
202 return -1;
203 }
204 wipe_block_device(fd, get_file_size(fd));
205 close(fd);
206 }
207
JP Abgrall37aedb32014-06-16 19:07:39 -0700208 ssize_t length = 0;
209 if (v->length != 0) {
210 length = v->length;
211 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
212 length = -CRYPT_FOOTER_OFFSET;
213 }
214 int result;
215 if (strcmp(v->fs_type, "ext4") == 0) {
Paul Lawrenced0db3372015-11-05 13:38:40 -0800216 result = make_ext4fs_directory(v->blk_device, length, volume, sehandle, directory);
JP Abgrall37aedb32014-06-16 19:07:39 -0700217 } else { /* Has to be f2fs because we checked earlier. */
218 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700219 LOGE("format_volume: crypt footer + negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700220 return -1;
221 }
222 if (length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700223 LOGE("format_volume: negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700224 return -1;
225 }
226 char *num_sectors;
JP Abgrall78d458c2014-08-04 16:44:33 -0700227 if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
JP Abgrall37aedb32014-06-16 19:07:39 -0700228 LOGE("format_volume: failed to create %s command for %s\n", v->fs_type, v->blk_device);
229 return -1;
230 }
231 const char *f2fs_path = "/sbin/mkfs.f2fs";
232 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
233
234 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
235 free(num_sectors);
236 }
237 if (result != 0) {
238 LOGE("format_volume: make %s failed on %s with %d(%s)\n", v->fs_type, v->blk_device, result, strerror(errno));
239 return -1;
240 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700241 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800242 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700243
244 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245 return -1;
246}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700247
Paul Lawrenced0db3372015-11-05 13:38:40 -0800248int format_volume(const char* volume) {
249 return format_volume(volume, NULL);
250}
251
Doug Zongker239ac6a2013-08-20 16:03:25 -0700252int setup_install_mounts() {
253 if (fstab == NULL) {
254 LOGE("can't set up install mounts: no fstab loaded\n");
255 return -1;
256 }
257 for (int i = 0; i < fstab->num_entries; ++i) {
258 Volume* v = fstab->recs + i;
259
260 if (strcmp(v->mount_point, "/tmp") == 0 ||
261 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800262 if (ensure_path_mounted(v->mount_point) != 0) {
263 LOGE("failed to mount %s\n", v->mount_point);
264 return -1;
265 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700266
267 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800268 if (ensure_path_unmounted(v->mount_point) != 0) {
269 LOGE("failed to unmount %s\n", v->mount_point);
270 return -1;
271 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700272 }
273 }
274 return 0;
275}