Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 <stdio.h> |
| 18 | #include <unistd.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/wait.h> |
| 23 | #include <errno.h> |
| 24 | #include <cutils/partition_utils.h> |
| 25 | #include <sys/mount.h> |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 26 | |
Tao Bao | 6d881d6 | 2016-10-05 17:53:30 -0700 | [diff] [blame] | 27 | #include <ext4_utils/ext4.h> |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 28 | #include <ext4_utils/ext4_utils.h> |
| 29 | #include <logwrap/logwrap.h> |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 30 | #include <selinux/android.h> |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 31 | #include <selinux/label.h> |
| 32 | #include <selinux/selinux.h> |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 33 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 34 | #include "fs_mgr_priv.h" |
Chuanxiao Dong | d78dff1 | 2016-03-08 15:54:55 +0100 | [diff] [blame] | 35 | #include "cryptfs.h" |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 36 | |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 37 | static int get_dev_sz(char *fs_blkdev, uint64_t *dev_sz) |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 38 | { |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 39 | int fd; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 40 | |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 41 | if ((fd = open(fs_blkdev, O_RDONLY)) < 0) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 42 | PERROR << "Cannot open block device"; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 43 | return -1; |
| 44 | } |
| 45 | |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 46 | if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 47 | PERROR << "Cannot get block device size"; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 48 | close(fd); |
| 49 | return -1; |
| 50 | } |
| 51 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 52 | close(fd); |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int format_ext4(char *fs_blkdev, char *fs_mnt_point, bool crypt_footer) |
| 57 | { |
| 58 | uint64_t dev_sz; |
| 59 | int rc = 0; |
| 60 | int status; |
| 61 | |
| 62 | rc = get_dev_sz(fs_blkdev, &dev_sz); |
| 63 | if (rc) { |
| 64 | return rc; |
| 65 | } |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 66 | |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 67 | /* Format the partition using the calculated length */ |
| 68 | if (crypt_footer) { |
| 69 | dev_sz -= CRYPT_FOOTER_OFFSET; |
| 70 | } |
| 71 | |
| 72 | std::string size_str = std::to_string(dev_sz / 4096); |
| 73 | const char* const mke2fs_args[] = { |
| 74 | "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", fs_blkdev, size_str.c_str(), nullptr}; |
| 75 | |
Ben Fennema | 6ac5322 | 2017-06-22 15:00:55 -0700 | [diff] [blame] | 76 | rc = android_fork_execvp_ext(arraysize(mke2fs_args), const_cast<char**>(mke2fs_args), NULL, |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 77 | true, LOG_KLOG, true, nullptr, nullptr, 0); |
| 78 | if (rc) { |
| 79 | LERROR << "mke2fs returned " << rc; |
| 80 | return rc; |
| 81 | } |
| 82 | |
| 83 | const char* const e2fsdroid_args[] = { |
| 84 | "/system/bin/e2fsdroid", |
| 85 | "-e", |
| 86 | "-a", |
| 87 | fs_mnt_point, |
| 88 | fs_blkdev, |
| 89 | nullptr}; |
| 90 | |
| 91 | rc = android_fork_execvp_ext(arraysize(e2fsdroid_args), const_cast<char**>(e2fsdroid_args), |
Ben Fennema | 6ac5322 | 2017-06-22 15:00:55 -0700 | [diff] [blame] | 92 | NULL, true, LOG_KLOG, true, nullptr, nullptr, 0); |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 93 | if (rc) { |
| 94 | LERROR << "e2fsdroid returned " << rc; |
William Roberts | 875476d | 2016-05-13 11:19:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 97 | return rc; |
| 98 | } |
| 99 | |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 100 | static int format_f2fs(char *fs_blkdev, uint64_t dev_sz, bool crypt_footer) |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 101 | { |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 102 | int status; |
| 103 | |
| 104 | if (!dev_sz) { |
| 105 | int rc = get_dev_sz(fs_blkdev, &dev_sz); |
| 106 | if (rc) { |
| 107 | return rc; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /* Format the partition using the calculated length */ |
| 112 | if (crypt_footer) { |
| 113 | dev_sz -= CRYPT_FOOTER_OFFSET; |
| 114 | } |
| 115 | |
| 116 | std::string size_str = std::to_string(dev_sz / 4096); |
Jaegeuk Kim | 9a67c69 | 2018-03-28 12:43:17 -0700 | [diff] [blame] | 117 | // clang-format off |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 118 | const char* const args[] = { |
Jaegeuk Kim | 9a67c69 | 2018-03-28 12:43:17 -0700 | [diff] [blame] | 119 | "/system/bin/make_f2fs", |
| 120 | "-d1", |
| 121 | "-f", |
| 122 | "-O", "encrypt", |
| 123 | "-O", "quota", |
Jaegeuk Kim | b68b02c | 2018-04-05 22:40:04 -0700 | [diff] [blame] | 124 | "-O", "verity", |
Jaegeuk Kim | 9a67c69 | 2018-03-28 12:43:17 -0700 | [diff] [blame] | 125 | "-w", "4096", |
| 126 | fs_blkdev, |
| 127 | size_str.c_str(), |
| 128 | nullptr |
| 129 | }; |
| 130 | // clang-format on |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 131 | |
Ben Fennema | 6ac5322 | 2017-06-22 15:00:55 -0700 | [diff] [blame] | 132 | return android_fork_execvp_ext(arraysize(args), const_cast<char**>(args), NULL, true, |
Jin Qian | f71bc0c | 2017-04-21 16:56:34 -0700 | [diff] [blame] | 133 | LOG_KLOG, true, nullptr, nullptr, 0); |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Chuanxiao Dong | d78dff1 | 2016-03-08 15:54:55 +0100 | [diff] [blame] | 136 | int fs_mgr_do_format(struct fstab_rec *fstab, bool crypt_footer) |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 137 | { |
| 138 | int rc = -EINVAL; |
| 139 | |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 140 | LERROR << __FUNCTION__ << ": Format " << fstab->blk_device |
| 141 | << " as '" << fstab->fs_type << "'"; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 142 | |
| 143 | if (!strncmp(fstab->fs_type, "f2fs", 4)) { |
Jaegeuk Kim | a367430 | 2017-06-12 16:57:56 -0700 | [diff] [blame] | 144 | rc = format_f2fs(fstab->blk_device, fstab->length, crypt_footer); |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 145 | } else if (!strncmp(fstab->fs_type, "ext4", 4)) { |
Chuanxiao Dong | d78dff1 | 2016-03-08 15:54:55 +0100 | [diff] [blame] | 146 | rc = format_ext4(fstab->blk_device, fstab->mount_point, crypt_footer); |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 147 | } else { |
bowgotsai | 47878de | 2017-01-23 14:04:34 +0800 | [diff] [blame] | 148 | LERROR << "File system type '" << fstab->fs_type << "' is not supported"; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return rc; |
| 152 | } |