blob: 845cca9dc2f636639175d3204a9075e02bf2f24b [file] [log] [blame]
Chris Fries79f33842013-09-05 13:19:21 -05001/*
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 Roberts875476d2016-05-13 11:19:42 -070026
Tao Bao6d881d62016-10-05 17:53:30 -070027#include <ext4_utils/ext4.h>
Jin Qianf71bc0c2017-04-21 16:56:34 -070028#include <ext4_utils/ext4_utils.h>
29#include <logwrap/logwrap.h>
William Roberts875476d2016-05-13 11:19:42 -070030#include <selinux/android.h>
Jin Qianf71bc0c2017-04-21 16:56:34 -070031#include <selinux/label.h>
32#include <selinux/selinux.h>
William Roberts875476d2016-05-13 11:19:42 -070033
Chris Fries79f33842013-09-05 13:19:21 -050034#include "fs_mgr_priv.h"
Chuanxiao Dongd78dff12016-03-08 15:54:55 +010035#include "cryptfs.h"
Chris Fries79f33842013-09-05 13:19:21 -050036
Jaegeuk Kima3674302017-06-12 16:57:56 -070037static int get_dev_sz(char *fs_blkdev, uint64_t *dev_sz)
Chris Fries79f33842013-09-05 13:19:21 -050038{
Jaegeuk Kima3674302017-06-12 16:57:56 -070039 int fd;
Chris Fries79f33842013-09-05 13:19:21 -050040
Jaegeuk Kima3674302017-06-12 16:57:56 -070041 if ((fd = open(fs_blkdev, O_RDONLY)) < 0) {
bowgotsai47878de2017-01-23 14:04:34 +080042 PERROR << "Cannot open block device";
Chris Fries79f33842013-09-05 13:19:21 -050043 return -1;
44 }
45
Jaegeuk Kima3674302017-06-12 16:57:56 -070046 if ((ioctl(fd, BLKGETSIZE64, dev_sz)) == -1) {
bowgotsai47878de2017-01-23 14:04:34 +080047 PERROR << "Cannot get block device size";
Chris Fries79f33842013-09-05 13:19:21 -050048 close(fd);
49 return -1;
50 }
51
Chris Fries79f33842013-09-05 13:19:21 -050052 close(fd);
Jaegeuk Kima3674302017-06-12 16:57:56 -070053 return 0;
54}
55
56static 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 Fries79f33842013-09-05 13:19:21 -050066
Jin Qianf71bc0c2017-04-21 16:56:34 -070067 /* 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 Fennema6ac53222017-06-22 15:00:55 -070076 rc = android_fork_execvp_ext(arraysize(mke2fs_args), const_cast<char**>(mke2fs_args), NULL,
Jin Qianf71bc0c2017-04-21 16:56:34 -070077 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 Fennema6ac53222017-06-22 15:00:55 -070092 NULL, true, LOG_KLOG, true, nullptr, nullptr, 0);
Jin Qianf71bc0c2017-04-21 16:56:34 -070093 if (rc) {
94 LERROR << "e2fsdroid returned " << rc;
William Roberts875476d2016-05-13 11:19:42 -070095 }
96
Chris Fries79f33842013-09-05 13:19:21 -050097 return rc;
98}
99
Jaegeuk Kima3674302017-06-12 16:57:56 -0700100static int format_f2fs(char *fs_blkdev, uint64_t dev_sz, bool crypt_footer)
Chris Fries79f33842013-09-05 13:19:21 -0500101{
Jaegeuk Kima3674302017-06-12 16:57:56 -0700102 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 Kim9a67c692018-03-28 12:43:17 -0700117 // clang-format off
Jaegeuk Kima3674302017-06-12 16:57:56 -0700118 const char* const args[] = {
Jaegeuk Kim9a67c692018-03-28 12:43:17 -0700119 "/system/bin/make_f2fs",
120 "-d1",
121 "-f",
122 "-O", "encrypt",
123 "-O", "quota",
Jaegeuk Kimb68b02c2018-04-05 22:40:04 -0700124 "-O", "verity",
Jaegeuk Kim9a67c692018-03-28 12:43:17 -0700125 "-w", "4096",
126 fs_blkdev,
127 size_str.c_str(),
128 nullptr
129 };
130 // clang-format on
Chris Fries79f33842013-09-05 13:19:21 -0500131
Ben Fennema6ac53222017-06-22 15:00:55 -0700132 return android_fork_execvp_ext(arraysize(args), const_cast<char**>(args), NULL, true,
Jin Qianf71bc0c2017-04-21 16:56:34 -0700133 LOG_KLOG, true, nullptr, nullptr, 0);
Chris Fries79f33842013-09-05 13:19:21 -0500134}
135
Chuanxiao Dongd78dff12016-03-08 15:54:55 +0100136int fs_mgr_do_format(struct fstab_rec *fstab, bool crypt_footer)
Chris Fries79f33842013-09-05 13:19:21 -0500137{
138 int rc = -EINVAL;
139
bowgotsai47878de2017-01-23 14:04:34 +0800140 LERROR << __FUNCTION__ << ": Format " << fstab->blk_device
141 << " as '" << fstab->fs_type << "'";
Chris Fries79f33842013-09-05 13:19:21 -0500142
143 if (!strncmp(fstab->fs_type, "f2fs", 4)) {
Jaegeuk Kima3674302017-06-12 16:57:56 -0700144 rc = format_f2fs(fstab->blk_device, fstab->length, crypt_footer);
Chris Fries79f33842013-09-05 13:19:21 -0500145 } else if (!strncmp(fstab->fs_type, "ext4", 4)) {
Chuanxiao Dongd78dff12016-03-08 15:54:55 +0100146 rc = format_ext4(fstab->blk_device, fstab->mount_point, crypt_footer);
Chris Fries79f33842013-09-05 13:19:21 -0500147 } else {
bowgotsai47878de2017-01-23 14:04:34 +0800148 LERROR << "File system type '" << fstab->fs_type << "' is not supported";
Chris Fries79f33842013-09-05 13:19:21 -0500149 }
150
151 return rc;
152}