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>
26
27 #include <android-base/unique_fd.h>
28 #include <ext4_utils/ext4.h>
29 #include <ext4_utils/ext4_utils.h>
30 #include <logwrap/logwrap.h>
31 #include <selinux/android.h>
32 #include <selinux/label.h>
33 #include <selinux/selinux.h>
34
35 #include "fs_mgr_priv.h"
36 #include "cryptfs.h"
37
38 using android::base::unique_fd;
39
40 // Realistically, this file should be part of the android::fs_mgr namespace;
41 using namespace android::fs_mgr;
42
get_dev_sz(const std::string & fs_blkdev,uint64_t * dev_sz)43 static int get_dev_sz(const std::string& fs_blkdev, uint64_t* dev_sz) {
44 unique_fd fd(TEMP_FAILURE_RETRY(open(fs_blkdev.c_str(), O_RDONLY | O_CLOEXEC)));
45
46 if (fd < 0) {
47 PERROR << "Cannot open block device";
48 return -1;
49 }
50
51 if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) {
52 PERROR << "Cannot get block device size";
53 return -1;
54 }
55
56 return 0;
57 }
58
format_ext4(const std::string & fs_blkdev,const std::string & fs_mnt_point,bool crypt_footer)59 static int format_ext4(const std::string& fs_blkdev, const std::string& fs_mnt_point,
60 bool crypt_footer) {
61 uint64_t dev_sz;
62 int rc = 0;
63
64 rc = get_dev_sz(fs_blkdev, &dev_sz);
65 if (rc) {
66 return rc;
67 }
68
69 /* Format the partition using the calculated length */
70 if (crypt_footer) {
71 dev_sz -= CRYPT_FOOTER_OFFSET;
72 }
73
74 std::string size_str = std::to_string(dev_sz / 4096);
75 const char* const mke2fs_args[] = {
76 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", fs_blkdev.c_str(),
77 size_str.c_str(), nullptr};
78
79 rc = android_fork_execvp_ext(arraysize(mke2fs_args), const_cast<char**>(mke2fs_args), NULL,
80 true, LOG_KLOG, true, nullptr, nullptr, 0);
81 if (rc) {
82 LERROR << "mke2fs returned " << rc;
83 return rc;
84 }
85
86 const char* const e2fsdroid_args[] = {
87 "/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr};
88
89 rc = android_fork_execvp_ext(arraysize(e2fsdroid_args), const_cast<char**>(e2fsdroid_args),
90 NULL, true, LOG_KLOG, true, nullptr, nullptr, 0);
91 if (rc) {
92 LERROR << "e2fsdroid returned " << rc;
93 }
94
95 return rc;
96 }
97
format_f2fs(const std::string & fs_blkdev,uint64_t dev_sz,bool crypt_footer)98 static int format_f2fs(const std::string& fs_blkdev, uint64_t dev_sz, bool crypt_footer) {
99 if (!dev_sz) {
100 int rc = get_dev_sz(fs_blkdev, &dev_sz);
101 if (rc) {
102 return rc;
103 }
104 }
105
106 /* Format the partition using the calculated length */
107 if (crypt_footer) {
108 dev_sz -= CRYPT_FOOTER_OFFSET;
109 }
110
111 std::string size_str = std::to_string(dev_sz / 4096);
112 // clang-format off
113 const char* const args[] = {
114 "/system/bin/make_f2fs",
115 "-g", "android",
116 fs_blkdev.c_str(),
117 size_str.c_str(),
118 nullptr
119 };
120 // clang-format on
121
122 return android_fork_execvp_ext(arraysize(args), const_cast<char**>(args), NULL, true,
123 LOG_KLOG, true, nullptr, nullptr, 0);
124 }
125
fs_mgr_do_format(const FstabEntry & entry,bool crypt_footer)126 int fs_mgr_do_format(const FstabEntry& entry, bool crypt_footer) {
127 LERROR << __FUNCTION__ << ": Format " << entry.blk_device << " as '" << entry.fs_type << "'";
128
129 if (entry.fs_type == "f2fs") {
130 return format_f2fs(entry.blk_device, entry.length, crypt_footer);
131 } else if (entry.fs_type == "ext4") {
132 return format_ext4(entry.blk_device, entry.mount_point, crypt_footer);
133 } else {
134 LERROR << "File system type '" << entry.fs_type << "' is not supported";
135 return -EINVAL;
136 }
137 }
138