blob: 5c95cba07e37d73ad77967cab1766209d0415e88 [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
Tao Baoe3f09a72019-10-01 11:55:36 -070017#include "recovery_utils/roots.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070018
Tao Baobb10e582017-07-22 16:30:34 -070019#include <fcntl.h>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053020#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <sys/stat.h>
24#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070025#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include <unistd.h>
27
Yifan Hongd81b8e32018-12-17 14:29:06 -080028#include <iostream>
Tao Bao3c00fac2017-07-22 16:46:54 -070029#include <string>
30#include <vector>
31
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070032#include <android-base/logging.h>
Daniel Rosenbergf25b9772019-12-16 18:32:00 -080033#include <android-base/properties.h>
Jin Qianded2dac2017-04-21 14:36:12 -070034#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070035#include <android-base/unique_fd.h>
Eric Biggersa762e142021-11-08 16:45:49 -080036#include <ext4_utils/ext4_utils.h>
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -080037#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080038#include <fs_mgr.h>
Yifan Hong0f339e22018-12-03 13:44:01 -080039#include <fs_mgr/roots.h>
Tao Baode40ba52016-10-05 23:17:01 -070040
Tao Bao3d69f0d2018-12-20 09:44:06 -080041#include "otautil/sysutil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Tom Cherry72a114a2019-01-30 15:59:53 -080043using android::fs_mgr::Fstab;
44using android::fs_mgr::FstabEntry;
45using android::fs_mgr::ReadDefaultFstab;
46
Yifan Hongd81b8e32018-12-17 14:29:06 -080047static Fstab fstab;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Tianjie Xu164c60a2019-05-15 13:59:39 -070049constexpr const char* CACHE_ROOT = "/cache";
50
Tao Baobb10e582017-07-22 16:30:34 -070051void load_volume_table() {
Yifan Hongd81b8e32018-12-17 14:29:06 -080052 if (!ReadDefaultFstab(&fstab)) {
Tao Baobb10e582017-07-22 16:30:34 -070053 LOG(ERROR) << "Failed to read default fstab";
54 return;
55 }
Doug Zongker2810ced2011-02-17 15:55:21 -080056
Yifan Hongd81b8e32018-12-17 14:29:06 -080057 fstab.emplace_back(FstabEntry{
Nick Desaulniers1cb510d2019-10-10 16:33:58 -070058 .blk_device = "ramdisk",
59 .mount_point = "/tmp",
60 .fs_type = "ramdisk",
61 .length = 0,
62 });
Doug Zongkerd4208f92010-09-20 12:16:13 -070063
Yifan Hongd81b8e32018-12-17 14:29:06 -080064 std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
65 for (size_t i = 0; i < fstab.size(); ++i) {
66 const auto& entry = fstab[i];
67 std::cout << " " << i << " " << entry.mount_point << " "
68 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
69 << std::endl;
Tao Baobb10e582017-07-22 16:30:34 -070070 }
Yifan Hongd81b8e32018-12-17 14:29:06 -080071 std::cout << std::endl;
Doug Zongkerd4208f92010-09-20 12:16:13 -070072}
73
Tao Bao3e18f2b2017-09-29 17:11:13 -070074Volume* volume_for_mount_point(const std::string& mount_point) {
Tom Cherry72a114a2019-01-30 15:59:53 -080075 return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
Tao Bao3e18f2b2017-09-29 17:11:13 -070076}
77
Tao Baoabb8f772015-07-30 14:43:27 -070078// Mount the volume specified by path at the given mount_point.
Yifan Hongd81b8e32018-12-17 14:29:06 -080079int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
80 return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081}
82
Yifan Hongd81b8e32018-12-17 14:29:06 -080083int ensure_path_mounted(const std::string& path) {
Tao Baobb10e582017-07-22 16:30:34 -070084 // Mount at the default mount point.
Yifan Hongd81b8e32018-12-17 14:29:06 -080085 return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
Tao Baoabb8f772015-07-30 14:43:27 -070086}
87
Yifan Hongd81b8e32018-12-17 14:29:06 -080088int ensure_path_unmounted(const std::string& path) {
89 return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -070090}
91
Tao Bao3c00fac2017-07-22 16:46:54 -070092static int exec_cmd(const std::vector<std::string>& args) {
Tao Bao3d69f0d2018-12-20 09:44:06 -080093 CHECK(!args.empty());
94 auto argv = StringVectorToNullTerminatedArray(args);
Tao Bao3c00fac2017-07-22 16:46:54 -070095
96 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -080097 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -070098 execv(argv[0], argv.data());
99 _exit(EXIT_FAILURE);
100 }
101
102 int status;
103 waitpid(child, &status, 0);
104 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
105 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
106 }
107 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700108}
109
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530110static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700111 struct stat buf;
112 int ret = fstat(fd, &buf);
113 if (ret) return 0;
114
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530115 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700116 if (S_ISREG(buf.st_mode)) {
117 computed_size = buf.st_size - reserve_len;
118 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530119 uint64_t block_device_size = get_block_device_size(fd);
120 if (block_device_size < reserve_len ||
121 block_device_size > std::numeric_limits<int64_t>::max()) {
122 computed_size = 0;
123 } else {
124 computed_size = block_device_size - reserve_len;
125 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700126 } else {
127 computed_size = 0;
128 }
129
130 return computed_size;
131}
132
Yifan Hongd81b8e32018-12-17 14:29:06 -0800133int format_volume(const std::string& volume, const std::string& directory) {
134 const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume);
Tao Bao3c00fac2017-07-22 16:46:54 -0700135 if (v == nullptr) {
136 LOG(ERROR) << "unknown volume \"" << volume << "\"";
137 return -1;
138 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800139 if (v->fs_type == "ramdisk") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700140 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
141 return -1;
142 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800143 if (v->mount_point != volume) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700144 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
145 return -1;
146 }
147 if (ensure_path_unmounted(volume) != 0) {
148 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
149 return -1;
150 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800151 if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700152 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700154 }
155
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800156 bool needs_casefold = false;
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800157
158 if (volume == "/data") {
Martijn Coenen5a4a7ff2020-04-15 11:52:21 +0200159 needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800160 }
161
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530162 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800163 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700164 length = v->length;
Eric Biggersa762e142021-11-08 16:45:49 -0800165 } else if (v->length < 0) {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800166 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
Tao Bao3c00fac2017-07-22 16:46:54 -0700167 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530168 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700169 return -1;
170 }
Eric Biggersa762e142021-11-08 16:45:49 -0800171 length = get_file_size(fd.get(), -v->length);
Tao Bao3c00fac2017-07-22 16:46:54 -0700172 if (length <= 0) {
xunchang24788852019-03-22 16:08:52 -0700173 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700174 return -1;
175 }
176 }
177
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -0800178 // If the raw disk will be used as a metadata encrypted device mapper target,
179 // next boot will do encrypt_in_place the raw disk which gives a subtle duration
180 // to get any failure in the process. In order to avoid it, let's simply wipe
181 // the raw disk if we don't reserve any space, which behaves exactly same as booting
182 // after "fastboot -w".
Jaegeuk Kimfa951652021-12-02 12:51:20 -0800183 if (!v->metadata_key_dir.empty() && length == 0) {
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -0800184 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDWR));
185 if (fd == -1) {
186 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
187 return -1;
188 }
189 int64_t device_size = get_file_size(fd.get(), 0);
190 if (device_size > 0 && !wipe_block_device(fd.get(), device_size)) {
191 LOG(INFO) << "format_volume: wipe metadata encrypted " << v->blk_device << " with size "
192 << device_size;
193 return 0;
194 }
195 }
196
Yifan Hongd81b8e32018-12-17 14:29:06 -0800197 if (v->fs_type == "ext4") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700198 static constexpr int kBlockSize = 4096;
199 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900200 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700201 };
202
Shikha Malhotrad21e5162022-02-02 13:43:39 +0000203 // Following is added for Project ID's quota as they require wider inodes.
204 // The Quotas themselves are enabled by tune2fs on boot.
205 mke2fs_args.push_back("-I");
206 mke2fs_args.push_back("512");
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800207
Jaegeuk Kima8d36e12020-02-11 15:24:54 -0800208 if (v->fs_mgr_flags.ext_meta_csum) {
209 mke2fs_args.push_back("-O");
210 mke2fs_args.push_back("metadata_csum");
211 mke2fs_args.push_back("-O");
212 mke2fs_args.push_back("64bit");
213 mke2fs_args.push_back("-O");
214 mke2fs_args.push_back("extent");
215 }
216
Tao Bao3c00fac2017-07-22 16:46:54 -0700217 int raid_stride = v->logical_blk_size / kBlockSize;
218 int raid_stripe_width = v->erase_blk_size / kBlockSize;
219 // stride should be the max of 8KB and logical block size
220 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
221 raid_stride = 8192 / kBlockSize;
222 }
223 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
224 mke2fs_args.push_back("-E");
225 mke2fs_args.push_back(
226 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
227 }
228 mke2fs_args.push_back(v->blk_device);
229 if (length != 0) {
230 mke2fs_args.push_back(std::to_string(length / kBlockSize));
231 }
232
233 int result = exec_cmd(mke2fs_args);
Yifan Hongd81b8e32018-12-17 14:29:06 -0800234 if (result == 0 && !directory.empty()) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700235 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900236 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700237 };
238 result = exec_cmd(e2fsdroid_args);
239 }
240
241 if (result != 0) {
242 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
243 return -1;
244 }
245 return 0;
246 }
247
248 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700249 static constexpr int kSectorSize = 4096;
Jaegeuk Kim43582622018-04-02 13:37:35 -0700250 std::vector<std::string> make_f2fs_cmd = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800251 "/system/bin/make_f2fs",
252 "-g",
253 "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700254 };
Shikha Malhotrad21e5162022-02-02 13:43:39 +0000255
256 make_f2fs_cmd.push_back("-O");
257 make_f2fs_cmd.push_back("project_quota,extra_attr");
258
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800259 if (needs_casefold) {
260 make_f2fs_cmd.push_back("-O");
261 make_f2fs_cmd.push_back("casefold");
262 make_f2fs_cmd.push_back("-C");
263 make_f2fs_cmd.push_back("utf8");
264 }
Jaegeuk Kim32106002020-01-14 11:00:37 -0800265 if (v->fs_mgr_flags.fs_compress) {
266 make_f2fs_cmd.push_back("-O");
267 make_f2fs_cmd.push_back("compression");
268 make_f2fs_cmd.push_back("-O");
269 make_f2fs_cmd.push_back("extra_attr");
270 }
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800271 make_f2fs_cmd.push_back(v->blk_device);
Jaegeuk Kim43582622018-04-02 13:37:35 -0700272 if (length >= kSectorSize) {
273 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700274 }
275
Tao Baoc674dfb2018-12-20 14:25:15 -0800276 if (exec_cmd(make_f2fs_cmd) != 0) {
277 PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700278 return -1;
279 }
Tao Baoc674dfb2018-12-20 14:25:15 -0800280 if (!directory.empty()) {
281 std::vector<std::string> sload_f2fs_cmd = {
282 "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
283 };
284 if (exec_cmd(sload_f2fs_cmd) != 0) {
285 PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
286 return -1;
287 }
288 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700289 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700291
Yifan Hongd81b8e32018-12-17 14:29:06 -0800292int format_volume(const std::string& volume) {
293 return format_volume(volume, "");
Paul Lawrenced0db3372015-11-05 13:38:40 -0800294}
295
Doug Zongker239ac6a2013-08-20 16:03:25 -0700296int setup_install_mounts() {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800297 if (fstab.empty()) {
Tao Bao57130c42017-05-10 12:11:21 -0700298 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
299 return -1;
300 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800301 for (const FstabEntry& entry : fstab) {
Tao Bao57130c42017-05-10 12:11:21 -0700302 // We don't want to do anything with "/".
Yifan Hongd81b8e32018-12-17 14:29:06 -0800303 if (entry.mount_point == "/") {
Tao Bao57130c42017-05-10 12:11:21 -0700304 continue;
305 }
306
Yifan Hongd81b8e32018-12-17 14:29:06 -0800307 if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
308 if (ensure_path_mounted(entry.mount_point) != 0) {
309 LOG(ERROR) << "Failed to mount " << entry.mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700310 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700311 }
312 } else {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800313 if (ensure_path_unmounted(entry.mount_point) != 0) {
314 LOG(ERROR) << "Failed to unmount " << entry.mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700315 return -1;
316 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700317 }
Tao Bao57130c42017-05-10 12:11:21 -0700318 }
319 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700320}
Tianjie Xu164c60a2019-05-15 13:59:39 -0700321
322bool HasCache() {
323 CHECK(!fstab.empty());
324 static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
325 return has_cache;
326}