blob: c4afd5de3a87e0e5540201bb7c1693bf6d510d76 [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>
Jin Qianded2dac2017-04-21 14:36:12 -070030#include <android-base/properties.h>
31#include <android-base/stringprintf.h>
Tao Baode40ba52016-10-05 23:17:01 -070032#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080033#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070034
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "common.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070036#include "mounts.h"
Doug Zongkerf39989a2013-12-11 15:40:28 -080037#include "cryptfs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080039static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040
Kenny Root41dda822012-03-30 20:48:34 -070041extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050042
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080043void load_volume_table()
44{
45 int i;
46 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080047
Bowgo Tsai84a06482017-03-29 15:13:58 +080048 fstab = fs_mgr_read_fstab_default();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080049 if (!fstab) {
Bowgo Tsai84a06482017-03-29 15:13:58 +080050 LOG(ERROR) << "failed to read default fstab";
Doug Zongkerd4208f92010-09-20 12:16:13 -070051 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070053
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070054 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080055 if (ret < 0 ) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070056 LOG(ERROR) << "failed to add /tmp entry to fstab";
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080057 fs_mgr_free_fstab(fstab);
58 fstab = NULL;
59 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070061
Doug Zongkerd4208f92010-09-20 12:16:13 -070062 printf("recovery filesystem table\n");
63 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080064 for (i = 0; i < fstab->num_entries; ++i) {
65 Volume* v = &fstab->recs[i];
66 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
67 v->blk_device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -070068 }
69 printf("\n");
70}
71
72Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080073 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074}
75
Tao Baoabb8f772015-07-30 14:43:27 -070076// Mount the volume specified by path at the given mount_point.
77int ensure_path_mounted_at(const char* path, const char* mount_point) {
Doug Zongkerd4208f92010-09-20 12:16:13 -070078 Volume* v = volume_for_path(path);
79 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070080 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 return -1;
82 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070083 if (strcmp(v->fs_type, "ramdisk") == 0) {
84 // the ramdisk is always mounted.
85 return 0;
86 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070087
Elliott Hughes63a31922016-06-09 17:41:22 -070088 if (!scan_mounted_volumes()) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070089 LOG(ERROR) << "failed to scan mounted volumes";
Doug Zongkerd4208f92010-09-20 12:16:13 -070090 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070091 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092
Tao Baoabb8f772015-07-30 14:43:27 -070093 if (!mount_point) {
94 mount_point = v->mount_point;
95 }
96
Elliott Hughes63a31922016-06-09 17:41:22 -070097 MountedVolume* mv = 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
Elliott Hughes63a31922016-06-09 17:41:22 -0700105 if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash522ea722015-06-29 18:57:14 -0700106 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700107 strcmp(v->fs_type, "vfat") == 0) {
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200108 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
109 if (result == -1 && fs_mgr_is_formattable(v)) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700110 LOG(ERROR) << "failed to mount " << mount_point << " (" << strerror(errno)
111 << ") , formatting.....";
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200112 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
113 if (fs_mgr_do_format(v, crypt_footer) == 0) {
114 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
115 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700116 PLOG(ERROR) << "failed to format " << mount_point;
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200117 return -1;
118 }
119 }
120
121 if (result == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700122 PLOG(ERROR) << "failed to mount " << mount_point;
Elliott Hughes63a31922016-06-09 17:41:22 -0700123 return -1;
124 }
125 return 0;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700126 }
127
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700128 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800129 return -1;
130}
131
Tao Baoabb8f772015-07-30 14:43:27 -0700132int ensure_path_mounted(const char* path) {
133 // Mount at the default mount point.
134 return ensure_path_mounted_at(path, nullptr);
135}
136
Doug Zongkerd4208f92010-09-20 12:16:13 -0700137int ensure_path_unmounted(const char* path) {
138 Volume* v = volume_for_path(path);
139 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700140 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141 return -1;
142 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700143 if (strcmp(v->fs_type, "ramdisk") == 0) {
144 // the ramdisk is always mounted; you can't unmount it.
145 return -1;
146 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147
Elliott Hughes63a31922016-06-09 17:41:22 -0700148 if (!scan_mounted_volumes()) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700149 LOG(ERROR) << "failed to scan mounted volumes";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700150 return -1;
151 }
152
Elliott Hughes63a31922016-06-09 17:41:22 -0700153 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700154 if (mv == NULL) {
155 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156 return 0;
157 }
158
Doug Zongkerd4208f92010-09-20 12:16:13 -0700159 return unmount_mounted_volume(mv);
160}
161
JP Abgrall37aedb32014-06-16 19:07:39 -0700162static int exec_cmd(const char* path, char* const argv[]) {
163 int status;
164 pid_t child;
165 if ((child = vfork()) == 0) {
166 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800167 _exit(EXIT_FAILURE);
JP Abgrall37aedb32014-06-16 19:07:39 -0700168 }
169 waitpid(child, &status, 0);
170 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700171 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700172 }
173 return WEXITSTATUS(status);
174}
175
Paul Lawrenced0db3372015-11-05 13:38:40 -0800176int format_volume(const char* volume, const char* directory) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700177 Volume* v = volume_for_path(volume);
178 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700179 LOG(ERROR) << "unknown volume \"" << volume << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700180 return -1;
181 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700182 if (strcmp(v->fs_type, "ramdisk") == 0) {
183 // you can't format the ramdisk.
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700184 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700185 return -1;
186 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700187 if (strcmp(v->mount_point, volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700188 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700189 return -1;
190 }
191
192 if (ensure_path_unmounted(volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700193 LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700194 return -1;
195 }
196
JP Abgrall37aedb32014-06-16 19:07:39 -0700197 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800198 // if there's a key_loc that looks like a path, it should be a
199 // block device for storing encryption metadata. wipe it too.
200 if (v->key_loc != NULL && v->key_loc[0] == '/') {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700201 LOG(INFO) << "wiping " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800202 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
203 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700204 LOG(ERROR) << "format_volume: failed to open " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800205 return -1;
206 }
207 wipe_block_device(fd, get_file_size(fd));
208 close(fd);
209 }
210
JP Abgrall37aedb32014-06-16 19:07:39 -0700211 ssize_t length = 0;
212 if (v->length != 0) {
213 length = v->length;
214 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
215 length = -CRYPT_FOOTER_OFFSET;
216 }
217 int result;
218 if (strcmp(v->fs_type, "ext4") == 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700219 static constexpr int block_size = 4096;
220 int raid_stride = v->logical_blk_size / block_size;
221 int raid_stripe_width = v->erase_blk_size / block_size;
222
223 // stride should be the max of 8kb and logical block size
224 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
225 raid_stride = 8192 / block_size;
226 }
227
228 const char* mke2fs_argv[] = { "/sbin/mke2fs_static",
229 "-F",
230 "-t",
231 "ext4",
232 "-b",
233 nullptr,
234 nullptr,
235 nullptr,
236 nullptr,
237 nullptr,
238 nullptr };
239
240 int i = 5;
241 std::string block_size_str = std::to_string(block_size);
242 mke2fs_argv[i++] = block_size_str.c_str();
243
244 std::string ext_args;
245 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
246 ext_args = android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride,
247 raid_stripe_width);
248 mke2fs_argv[i++] = "-E";
249 mke2fs_argv[i++] = ext_args.c_str();
250 }
251
252 mke2fs_argv[i++] = v->blk_device;
253
254 std::string size_str = std::to_string(length / block_size);
255 if (length != 0) {
256 mke2fs_argv[i++] = size_str.c_str();
257 }
258
259 result = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
260 if (result == 0 && directory != nullptr) {
261 const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static",
262 "-e",
Jin Qianded2dac2017-04-21 14:36:12 -0700263 "-f",
264 directory,
265 "-a",
266 volume,
267 v->blk_device,
268 nullptr };
269
270 result = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
Tao Bao338be532017-07-11 16:45:04 -0700271 }
JP Abgrall37aedb32014-06-16 19:07:39 -0700272 } else { /* Has to be f2fs because we checked earlier. */
273 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700274 LOG(ERROR) << "format_volume: crypt footer + negative length (" << length
275 << ") not supported on " << v->fs_type;
JP Abgrall37aedb32014-06-16 19:07:39 -0700276 return -1;
277 }
278 if (length < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700279 LOG(ERROR) << "format_volume: negative length (" << length
280 << ") not supported on " << v->fs_type;
JP Abgrall37aedb32014-06-16 19:07:39 -0700281 return -1;
282 }
Jin Qianadeb41a2017-04-28 16:15:13 -0700283 char *num_sectors = nullptr;
284 if (length >= 512 && asprintf(&num_sectors, "%zd", length / 512) <= 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700285 LOG(ERROR) << "format_volume: failed to create " << v->fs_type
286 << " command for " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700287 return -1;
288 }
289 const char *f2fs_path = "/sbin/mkfs.f2fs";
Jin Qianadeb41a2017-04-28 16:15:13 -0700290 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700291
292 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
293 free(num_sectors);
294 }
295 if (result != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700296 PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700297 return -1;
298 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700299 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700301
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700302 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303 return -1;
304}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700305
Paul Lawrenced0db3372015-11-05 13:38:40 -0800306int format_volume(const char* volume) {
307 return format_volume(volume, NULL);
308}
309
Doug Zongker239ac6a2013-08-20 16:03:25 -0700310int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700311 if (fstab == nullptr) {
312 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
313 return -1;
314 }
315 for (int i = 0; i < fstab->num_entries; ++i) {
316 const Volume* v = fstab->recs + i;
317
318 // We don't want to do anything with "/".
319 if (strcmp(v->mount_point, "/") == 0) {
320 continue;
321 }
322
323 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
324 if (ensure_path_mounted(v->mount_point) != 0) {
325 LOG(ERROR) << "failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700326 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700327 }
328 } else {
329 if (ensure_path_unmounted(v->mount_point) != 0) {
330 LOG(ERROR) << "failed to unmount " << v->mount_point;
331 return -1;
332 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700333 }
Tao Bao57130c42017-05-10 12:11:21 -0700334 }
335 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700336}