blob: 7a922b8eeb4736d6d006792f727e90248201edad [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
Tao Baobb10e582017-07-22 16:30:34 -070019#include <ctype.h>
20#include <fcntl.h>
Tom Cherry45e505a2018-11-29 13:33:07 -080021#include <inttypes.h>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053022#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070024#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070028#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <unistd.h>
30
Yifan Hongd81b8e32018-12-17 14:29:06 -080031#include <iostream>
Tao Bao3c00fac2017-07-22 16:46:54 -070032#include <string>
33#include <vector>
34
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070035#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070036#include <android-base/properties.h>
37#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070038#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070039#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070040#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080041#include <fs_mgr.h>
Yifan Hong0f339e22018-12-03 13:44:01 -080042#include <fs_mgr/roots.h>
David Anderson2b2f4232018-10-29 18:48:56 -070043#include <fs_mgr_dm_linear.h>
Tao Baode40ba52016-10-05 23:17:01 -070044
Tao Bao9a319f02018-01-04 13:19:11 -080045#include "otautil/mounts.h"
Tao Bao3d69f0d2018-12-20 09:44:06 -080046#include "otautil/sysutil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Tom Cherry72a114a2019-01-30 15:59:53 -080048using android::fs_mgr::Fstab;
49using android::fs_mgr::FstabEntry;
50using android::fs_mgr::ReadDefaultFstab;
51
Yifan Hongd81b8e32018-12-17 14:29:06 -080052static Fstab fstab;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053
Tao Baobb10e582017-07-22 16:30:34 -070054extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050055
Tao Baobb10e582017-07-22 16:30:34 -070056void load_volume_table() {
Yifan Hongd81b8e32018-12-17 14:29:06 -080057 if (!ReadDefaultFstab(&fstab)) {
Tao Baobb10e582017-07-22 16:30:34 -070058 LOG(ERROR) << "Failed to read default fstab";
59 return;
60 }
Doug Zongker2810ced2011-02-17 15:55:21 -080061
Yifan Hongd81b8e32018-12-17 14:29:06 -080062 fstab.emplace_back(FstabEntry{
63 .mount_point = "/tmp", .fs_type = "ramdisk", .blk_device = "ramdisk", .length = 0 });
Doug Zongkerd4208f92010-09-20 12:16:13 -070064
Yifan Hongd81b8e32018-12-17 14:29:06 -080065 std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
66 for (size_t i = 0; i < fstab.size(); ++i) {
67 const auto& entry = fstab[i];
68 std::cout << " " << i << " " << entry.mount_point << " "
69 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
70 << std::endl;
Tao Baobb10e582017-07-22 16:30:34 -070071 }
Yifan Hongd81b8e32018-12-17 14:29:06 -080072 std::cout << std::endl;
Doug Zongkerd4208f92010-09-20 12:16:13 -070073}
74
Tao Bao3e18f2b2017-09-29 17:11:13 -070075Volume* volume_for_mount_point(const std::string& mount_point) {
Tom Cherry72a114a2019-01-30 15:59:53 -080076 return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
Tao Bao3e18f2b2017-09-29 17:11:13 -070077}
78
Tao Baoabb8f772015-07-30 14:43:27 -070079// Mount the volume specified by path at the given mount_point.
Yifan Hongd81b8e32018-12-17 14:29:06 -080080int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
81 return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082}
83
Yifan Hongd81b8e32018-12-17 14:29:06 -080084int ensure_path_mounted(const std::string& path) {
Tao Baobb10e582017-07-22 16:30:34 -070085 // Mount at the default mount point.
Yifan Hongd81b8e32018-12-17 14:29:06 -080086 return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
Tao Baoabb8f772015-07-30 14:43:27 -070087}
88
Yifan Hongd81b8e32018-12-17 14:29:06 -080089int ensure_path_unmounted(const std::string& path) {
90 return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -070091}
92
Tao Bao3c00fac2017-07-22 16:46:54 -070093static int exec_cmd(const std::vector<std::string>& args) {
Tao Bao3d69f0d2018-12-20 09:44:06 -080094 CHECK(!args.empty());
95 auto argv = StringVectorToNullTerminatedArray(args);
Tao Bao3c00fac2017-07-22 16:46:54 -070096
97 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -080098 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -070099 execv(argv[0], argv.data());
100 _exit(EXIT_FAILURE);
101 }
102
103 int status;
104 waitpid(child, &status, 0);
105 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
106 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
107 }
108 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700109}
110
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530111static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700112 struct stat buf;
113 int ret = fstat(fd, &buf);
114 if (ret) return 0;
115
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530116 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700117 if (S_ISREG(buf.st_mode)) {
118 computed_size = buf.st_size - reserve_len;
119 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530120 uint64_t block_device_size = get_block_device_size(fd);
121 if (block_device_size < reserve_len ||
122 block_device_size > std::numeric_limits<int64_t>::max()) {
123 computed_size = 0;
124 } else {
125 computed_size = block_device_size - reserve_len;
126 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700127 } else {
128 computed_size = 0;
129 }
130
131 return computed_size;
132}
133
Yifan Hongd81b8e32018-12-17 14:29:06 -0800134int format_volume(const std::string& volume, const std::string& directory) {
135 const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume);
Tao Bao3c00fac2017-07-22 16:46:54 -0700136 if (v == nullptr) {
137 LOG(ERROR) << "unknown volume \"" << volume << "\"";
138 return -1;
139 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800140 if (v->fs_type == "ramdisk") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700141 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
142 return -1;
143 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800144 if (v->mount_point != volume) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700145 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
146 return -1;
147 }
148 if (ensure_path_unmounted(volume) != 0) {
149 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
150 return -1;
151 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800152 if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700153 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700155 }
156
157 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
158 // metadata. Wipe it too.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800159 if (!v->key_loc.empty() && v->key_loc[0] == '/') {
Tao Bao3c00fac2017-07-22 16:46:54 -0700160 LOG(INFO) << "Wiping " << v->key_loc;
Yifan Hongd81b8e32018-12-17 14:29:06 -0800161 int fd = open(v->key_loc.c_str(), O_WRONLY | O_CREAT, 0644);
Tao Bao3c00fac2017-07-22 16:46:54 -0700162 if (fd == -1) {
163 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
164 return -1;
165 }
166 wipe_block_device(fd, get_file_size(fd));
167 close(fd);
168 }
169
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530170 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800171 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700172 length = v->length;
Yifan Hongd81b8e32018-12-17 14:29:06 -0800173 } else if (v->length < 0 || v->key_loc == "footer") {
174 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
Tao Bao3c00fac2017-07-22 16:46:54 -0700175 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530176 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700177 return -1;
178 }
Jin Qiancc100082017-11-17 23:53:22 -0800179 length =
180 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700181 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800182 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
183 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700184 return -1;
185 }
186 }
187
Yifan Hongd81b8e32018-12-17 14:29:06 -0800188 if (v->fs_type == "ext4") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700189 static constexpr int kBlockSize = 4096;
190 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900191 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700192 };
193
194 int raid_stride = v->logical_blk_size / kBlockSize;
195 int raid_stripe_width = v->erase_blk_size / kBlockSize;
196 // stride should be the max of 8KB and logical block size
197 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
198 raid_stride = 8192 / kBlockSize;
199 }
200 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
201 mke2fs_args.push_back("-E");
202 mke2fs_args.push_back(
203 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
204 }
205 mke2fs_args.push_back(v->blk_device);
206 if (length != 0) {
207 mke2fs_args.push_back(std::to_string(length / kBlockSize));
208 }
209
210 int result = exec_cmd(mke2fs_args);
Yifan Hongd81b8e32018-12-17 14:29:06 -0800211 if (result == 0 && !directory.empty()) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700212 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900213 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700214 };
215 result = exec_cmd(e2fsdroid_args);
216 }
217
218 if (result != 0) {
219 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
220 return -1;
221 }
222 return 0;
223 }
224
225 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700226 static constexpr int kSectorSize = 4096;
Jaegeuk Kim43582622018-04-02 13:37:35 -0700227 std::vector<std::string> make_f2fs_cmd = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800228 "/system/bin/make_f2fs",
229 "-g",
230 "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700231 v->blk_device,
232 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700233 if (length >= kSectorSize) {
234 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700235 }
236
Tao Baoc674dfb2018-12-20 14:25:15 -0800237 if (exec_cmd(make_f2fs_cmd) != 0) {
238 PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700239 return -1;
240 }
Tao Baoc674dfb2018-12-20 14:25:15 -0800241 if (!directory.empty()) {
242 std::vector<std::string> sload_f2fs_cmd = {
243 "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
244 };
245 if (exec_cmd(sload_f2fs_cmd) != 0) {
246 PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
247 return -1;
248 }
249 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700250 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700252
Yifan Hongd81b8e32018-12-17 14:29:06 -0800253int format_volume(const std::string& volume) {
254 return format_volume(volume, "");
Paul Lawrenced0db3372015-11-05 13:38:40 -0800255}
256
Doug Zongker239ac6a2013-08-20 16:03:25 -0700257int setup_install_mounts() {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800258 if (fstab.empty()) {
Tao Bao57130c42017-05-10 12:11:21 -0700259 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
260 return -1;
261 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800262 for (const FstabEntry& entry : fstab) {
Tao Bao57130c42017-05-10 12:11:21 -0700263 // We don't want to do anything with "/".
Yifan Hongd81b8e32018-12-17 14:29:06 -0800264 if (entry.mount_point == "/") {
Tao Bao57130c42017-05-10 12:11:21 -0700265 continue;
266 }
267
Yifan Hongd81b8e32018-12-17 14:29:06 -0800268 if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
269 if (ensure_path_mounted(entry.mount_point) != 0) {
270 LOG(ERROR) << "Failed to mount " << entry.mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700271 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700272 }
273 } else {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800274 if (ensure_path_unmounted(entry.mount_point) != 0) {
275 LOG(ERROR) << "Failed to unmount " << entry.mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700276 return -1;
277 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700278 }
Tao Bao57130c42017-05-10 12:11:21 -0700279 }
280 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700281}
David Anderson2b2f4232018-10-29 18:48:56 -0700282
283bool logical_partitions_mapped() {
Yifan Hong0f339e22018-12-03 13:44:01 -0800284 return android::fs_mgr::LogicalPartitionsMapped();
David Anderson2b2f4232018-10-29 18:48:56 -0700285}
Yifan Hong49327802018-11-26 14:59:09 -0800286
287std::string get_system_root() {
Yifan Hong0f339e22018-12-03 13:44:01 -0800288 return android::fs_mgr::GetSystemRoot();
Yifan Hong49327802018-11-26 14:59:09 -0800289}