blob: 6e5ef98104bcb860a31c24027f80fcb3ecc04b25 [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
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070029#include <android-base/logging.h>
Tao Baode40ba52016-10-05 23:17:01 -070030#include <ext4_utils/make_ext4fs.h>
31#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080032#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070033
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "common.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070035#include "mounts.h"
Doug Zongkerf39989a2013-12-11 15:40:28 -080036#include "cryptfs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080038static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Kenny Root41dda822012-03-30 20:48:34 -070040extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050041
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080042void load_volume_table()
43{
44 int i;
45 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080046
Bowgo Tsai93024722017-03-29 15:13:58 +080047 fstab = fs_mgr_read_fstab_default();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080048 if (!fstab) {
Bowgo Tsai93024722017-03-29 15:13:58 +080049 LOG(ERROR) << "failed to read default fstab";
Doug Zongkerd4208f92010-09-20 12:16:13 -070050 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070052
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070053 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080054 if (ret < 0 ) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070055 LOG(ERROR) << "failed to add /tmp entry to fstab";
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080056 fs_mgr_free_fstab(fstab);
57 fstab = NULL;
58 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070060
Doug Zongkerd4208f92010-09-20 12:16:13 -070061 printf("recovery filesystem table\n");
62 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080063 for (i = 0; i < fstab->num_entries; ++i) {
64 Volume* v = &fstab->recs[i];
65 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
66 v->blk_device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -070067 }
68 printf("\n");
69}
70
71Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080072 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073}
74
Tao Baoabb8f772015-07-30 14:43:27 -070075// Mount the volume specified by path at the given mount_point.
76int ensure_path_mounted_at(const char* path, const char* mount_point) {
Doug Zongkerd4208f92010-09-20 12:16:13 -070077 Volume* v = volume_for_path(path);
78 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070079 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080 return -1;
81 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070082 if (strcmp(v->fs_type, "ramdisk") == 0) {
83 // the ramdisk is always mounted.
84 return 0;
85 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070086
Elliott Hughes63a31922016-06-09 17:41:22 -070087 if (!scan_mounted_volumes()) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070088 LOG(ERROR) << "failed to scan mounted volumes";
Doug Zongkerd4208f92010-09-20 12:16:13 -070089 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
Elliott Hughes63a31922016-06-09 17:41:22 -070096 MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -070097 if (mv) {
98 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 return 0;
100 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101
Tao Baoabb8f772015-07-30 14:43:27 -0700102 mkdir(mount_point, 0755); // in case it doesn't already exist
Doug Zongkerd4208f92010-09-20 12:16:13 -0700103
Elliott Hughes63a31922016-06-09 17:41:22 -0700104 if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash522ea722015-06-29 18:57:14 -0700105 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700106 strcmp(v->fs_type, "vfat") == 0) {
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200107 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
108 if (result == -1 && fs_mgr_is_formattable(v)) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700109 LOG(ERROR) << "failed to mount " << mount_point << " (" << strerror(errno)
110 << ") , formatting.....";
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200111 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
112 if (fs_mgr_do_format(v, crypt_footer) == 0) {
113 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
114 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700115 PLOG(ERROR) << "failed to format " << mount_point;
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200116 return -1;
117 }
118 }
119
120 if (result == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700121 PLOG(ERROR) << "failed to mount " << mount_point;
Elliott Hughes63a31922016-06-09 17:41:22 -0700122 return -1;
123 }
124 return 0;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700125 }
126
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700127 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << 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) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700139 LOG(ERROR) << "unknown volume for path [" << 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
Elliott Hughes63a31922016-06-09 17:41:22 -0700147 if (!scan_mounted_volumes()) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700148 LOG(ERROR) << "failed to scan mounted volumes";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700149 return -1;
150 }
151
Elliott Hughes63a31922016-06-09 17:41:22 -0700152 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700153 if (mv == NULL) {
154 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 return 0;
156 }
157
Doug Zongkerd4208f92010-09-20 12:16:13 -0700158 return unmount_mounted_volume(mv);
159}
160
JP Abgrall37aedb32014-06-16 19:07:39 -0700161static int exec_cmd(const char* path, char* const argv[]) {
162 int status;
163 pid_t child;
164 if ((child = vfork()) == 0) {
165 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800166 _exit(EXIT_FAILURE);
JP Abgrall37aedb32014-06-16 19:07:39 -0700167 }
168 waitpid(child, &status, 0);
169 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700170 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700171 }
172 return WEXITSTATUS(status);
173}
174
Paul Lawrenced0db3372015-11-05 13:38:40 -0800175int format_volume(const char* volume, const char* directory) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700176 Volume* v = volume_for_path(volume);
177 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700178 LOG(ERROR) << "unknown volume \"" << volume << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700179 return -1;
180 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700181 if (strcmp(v->fs_type, "ramdisk") == 0) {
182 // you can't format the ramdisk.
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700183 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700184 return -1;
185 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700186 if (strcmp(v->mount_point, volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700187 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700188 return -1;
189 }
190
191 if (ensure_path_unmounted(volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700192 LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700193 return -1;
194 }
195
JP Abgrall37aedb32014-06-16 19:07:39 -0700196 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800197 // if there's a key_loc that looks like a path, it should be a
198 // block device for storing encryption metadata. wipe it too.
199 if (v->key_loc != NULL && v->key_loc[0] == '/') {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700200 LOG(INFO) << "wiping " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800201 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
202 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700203 LOG(ERROR) << "format_volume: failed to open " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800204 return -1;
205 }
206 wipe_block_device(fd, get_file_size(fd));
207 close(fd);
208 }
209
JP Abgrall37aedb32014-06-16 19:07:39 -0700210 ssize_t length = 0;
211 if (v->length != 0) {
212 length = v->length;
213 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
214 length = -CRYPT_FOOTER_OFFSET;
215 }
216 int result;
217 if (strcmp(v->fs_type, "ext4") == 0) {
Connor O'Brien98a658b2017-01-24 17:31:14 -0800218 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
219 result = make_ext4fs_directory_align(v->blk_device, length, volume, sehandle,
220 directory, v->erase_blk_size, v->logical_blk_size);
221 } else {
222 result = make_ext4fs_directory(v->blk_device, length, volume, sehandle, directory);
223 }
JP Abgrall37aedb32014-06-16 19:07:39 -0700224 } else { /* Has to be f2fs because we checked earlier. */
225 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700226 LOG(ERROR) << "format_volume: crypt footer + negative length (" << length
227 << ") not supported on " << v->fs_type;
JP Abgrall37aedb32014-06-16 19:07:39 -0700228 return -1;
229 }
230 if (length < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700231 LOG(ERROR) << "format_volume: negative length (" << length
232 << ") not supported on " << v->fs_type;
JP Abgrall37aedb32014-06-16 19:07:39 -0700233 return -1;
234 }
235 char *num_sectors;
JP Abgrall78d458c2014-08-04 16:44:33 -0700236 if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700237 LOG(ERROR) << "format_volume: failed to create " << v->fs_type
238 << " command for " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700239 return -1;
240 }
241 const char *f2fs_path = "/sbin/mkfs.f2fs";
242 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
243
244 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
245 free(num_sectors);
246 }
247 if (result != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700248 PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700249 return -1;
250 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700251 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700253
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700254 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 return -1;
256}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700257
Paul Lawrenced0db3372015-11-05 13:38:40 -0800258int format_volume(const char* volume) {
259 return format_volume(volume, NULL);
260}
261
Doug Zongker239ac6a2013-08-20 16:03:25 -0700262int setup_install_mounts() {
263 if (fstab == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700264 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700265 return -1;
266 }
267 for (int i = 0; i < fstab->num_entries; ++i) {
268 Volume* v = fstab->recs + i;
269
270 if (strcmp(v->mount_point, "/tmp") == 0 ||
271 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800272 if (ensure_path_mounted(v->mount_point) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700273 LOG(ERROR) << "failed to mount " << v->mount_point;
Doug Zongker99916f02014-01-13 14:16:58 -0800274 return -1;
275 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700276
277 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800278 if (ensure_path_unmounted(v->mount_point) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700279 LOG(ERROR) << "failed to unmount " << v->mount_point;
Doug Zongker99916f02014-01-13 14:16:58 -0800280 return -1;
281 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700282 }
283 }
284 return 0;
285}