blob: fe3a07aa20999bb9c2cdf18ca63706fb609dde05 [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>
Jin Qianded2dac2017-04-21 14:36:12 -070033#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070034#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070035#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070036#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080037#include <fs_mgr.h>
Yifan Hong0f339e22018-12-03 13:44:01 -080038#include <fs_mgr/roots.h>
Tao Baode40ba52016-10-05 23:17:01 -070039
Tao Bao3d69f0d2018-12-20 09:44:06 -080040#include "otautil/sysutil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
Tom Cherry72a114a2019-01-30 15:59:53 -080042using android::fs_mgr::Fstab;
43using android::fs_mgr::FstabEntry;
44using android::fs_mgr::ReadDefaultFstab;
45
Yifan Hongd81b8e32018-12-17 14:29:06 -080046static Fstab fstab;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Tianjie Xu164c60a2019-05-15 13:59:39 -070048constexpr const char* CACHE_ROOT = "/cache";
49
Tao Baobb10e582017-07-22 16:30:34 -070050void load_volume_table() {
Yifan Hongd81b8e32018-12-17 14:29:06 -080051 if (!ReadDefaultFstab(&fstab)) {
Tao Baobb10e582017-07-22 16:30:34 -070052 LOG(ERROR) << "Failed to read default fstab";
53 return;
54 }
Doug Zongker2810ced2011-02-17 15:55:21 -080055
Yifan Hongd81b8e32018-12-17 14:29:06 -080056 fstab.emplace_back(FstabEntry{
Nick Desaulniers1cb510d2019-10-10 16:33:58 -070057 .blk_device = "ramdisk",
58 .mount_point = "/tmp",
59 .fs_type = "ramdisk",
60 .length = 0,
61 });
Doug Zongkerd4208f92010-09-20 12:16:13 -070062
Yifan Hongd81b8e32018-12-17 14:29:06 -080063 std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
64 for (size_t i = 0; i < fstab.size(); ++i) {
65 const auto& entry = fstab[i];
66 std::cout << " " << i << " " << entry.mount_point << " "
67 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
68 << std::endl;
Tao Baobb10e582017-07-22 16:30:34 -070069 }
Yifan Hongd81b8e32018-12-17 14:29:06 -080070 std::cout << std::endl;
Doug Zongkerd4208f92010-09-20 12:16:13 -070071}
72
Tao Bao3e18f2b2017-09-29 17:11:13 -070073Volume* volume_for_mount_point(const std::string& mount_point) {
Tom Cherry72a114a2019-01-30 15:59:53 -080074 return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
Tao Bao3e18f2b2017-09-29 17:11:13 -070075}
76
Tao Baoabb8f772015-07-30 14:43:27 -070077// Mount the volume specified by path at the given mount_point.
Yifan Hongd81b8e32018-12-17 14:29:06 -080078int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
79 return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080}
81
Yifan Hongd81b8e32018-12-17 14:29:06 -080082int ensure_path_mounted(const std::string& path) {
Tao Baobb10e582017-07-22 16:30:34 -070083 // Mount at the default mount point.
Yifan Hongd81b8e32018-12-17 14:29:06 -080084 return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
Tao Baoabb8f772015-07-30 14:43:27 -070085}
86
Yifan Hongd81b8e32018-12-17 14:29:06 -080087int ensure_path_unmounted(const std::string& path) {
88 return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -070089}
90
Tao Bao3c00fac2017-07-22 16:46:54 -070091static int exec_cmd(const std::vector<std::string>& args) {
Tao Bao3d69f0d2018-12-20 09:44:06 -080092 CHECK(!args.empty());
93 auto argv = StringVectorToNullTerminatedArray(args);
Tao Bao3c00fac2017-07-22 16:46:54 -070094
95 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -080096 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -070097 execv(argv[0], argv.data());
98 _exit(EXIT_FAILURE);
99 }
100
101 int status;
102 waitpid(child, &status, 0);
103 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
104 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
105 }
106 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700107}
108
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530109static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700110 struct stat buf;
111 int ret = fstat(fd, &buf);
112 if (ret) return 0;
113
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530114 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700115 if (S_ISREG(buf.st_mode)) {
116 computed_size = buf.st_size - reserve_len;
117 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530118 uint64_t block_device_size = get_block_device_size(fd);
119 if (block_device_size < reserve_len ||
120 block_device_size > std::numeric_limits<int64_t>::max()) {
121 computed_size = 0;
122 } else {
123 computed_size = block_device_size - reserve_len;
124 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700125 } else {
126 computed_size = 0;
127 }
128
129 return computed_size;
130}
131
Yifan Hongd81b8e32018-12-17 14:29:06 -0800132int format_volume(const std::string& volume, const std::string& directory) {
133 const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume);
Tao Bao3c00fac2017-07-22 16:46:54 -0700134 if (v == nullptr) {
135 LOG(ERROR) << "unknown volume \"" << volume << "\"";
136 return -1;
137 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800138 if (v->fs_type == "ramdisk") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700139 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
140 return -1;
141 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800142 if (v->mount_point != volume) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700143 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
144 return -1;
145 }
146 if (ensure_path_unmounted(volume) != 0) {
147 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
148 return -1;
149 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800150 if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700151 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700153 }
154
155 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
156 // metadata. Wipe it too.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800157 if (!v->key_loc.empty() && v->key_loc[0] == '/') {
Tao Bao3c00fac2017-07-22 16:46:54 -0700158 LOG(INFO) << "Wiping " << v->key_loc;
Yifan Hongd81b8e32018-12-17 14:29:06 -0800159 int fd = open(v->key_loc.c_str(), O_WRONLY | O_CREAT, 0644);
Tao Bao3c00fac2017-07-22 16:46:54 -0700160 if (fd == -1) {
161 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
162 return -1;
163 }
164 wipe_block_device(fd, get_file_size(fd));
165 close(fd);
166 }
167
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530168 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800169 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700170 length = v->length;
Yifan Hongd81b8e32018-12-17 14:29:06 -0800171 } else if (v->length < 0 || v->key_loc == "footer") {
172 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
Tao Bao3c00fac2017-07-22 16:46:54 -0700173 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530174 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700175 return -1;
176 }
xunchang24788852019-03-22 16:08:52 -0700177 length = get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700178 if (length <= 0) {
xunchang24788852019-03-22 16:08:52 -0700179 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700180 return -1;
181 }
182 }
183
Yifan Hongd81b8e32018-12-17 14:29:06 -0800184 if (v->fs_type == "ext4") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700185 static constexpr int kBlockSize = 4096;
186 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900187 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700188 };
189
190 int raid_stride = v->logical_blk_size / kBlockSize;
191 int raid_stripe_width = v->erase_blk_size / kBlockSize;
192 // stride should be the max of 8KB and logical block size
193 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
194 raid_stride = 8192 / kBlockSize;
195 }
196 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
197 mke2fs_args.push_back("-E");
198 mke2fs_args.push_back(
199 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
200 }
201 mke2fs_args.push_back(v->blk_device);
202 if (length != 0) {
203 mke2fs_args.push_back(std::to_string(length / kBlockSize));
204 }
205
206 int result = exec_cmd(mke2fs_args);
Yifan Hongd81b8e32018-12-17 14:29:06 -0800207 if (result == 0 && !directory.empty()) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700208 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900209 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700210 };
211 result = exec_cmd(e2fsdroid_args);
212 }
213
214 if (result != 0) {
215 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
216 return -1;
217 }
218 return 0;
219 }
220
221 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700222 static constexpr int kSectorSize = 4096;
Jaegeuk Kim43582622018-04-02 13:37:35 -0700223 std::vector<std::string> make_f2fs_cmd = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800224 "/system/bin/make_f2fs",
225 "-g",
226 "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700227 v->blk_device,
228 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700229 if (length >= kSectorSize) {
230 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700231 }
232
Tao Baoc674dfb2018-12-20 14:25:15 -0800233 if (exec_cmd(make_f2fs_cmd) != 0) {
234 PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700235 return -1;
236 }
Tao Baoc674dfb2018-12-20 14:25:15 -0800237 if (!directory.empty()) {
238 std::vector<std::string> sload_f2fs_cmd = {
239 "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
240 };
241 if (exec_cmd(sload_f2fs_cmd) != 0) {
242 PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
243 return -1;
244 }
245 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700246 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800247}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700248
Yifan Hongd81b8e32018-12-17 14:29:06 -0800249int format_volume(const std::string& volume) {
250 return format_volume(volume, "");
Paul Lawrenced0db3372015-11-05 13:38:40 -0800251}
252
Doug Zongker239ac6a2013-08-20 16:03:25 -0700253int setup_install_mounts() {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800254 if (fstab.empty()) {
Tao Bao57130c42017-05-10 12:11:21 -0700255 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
256 return -1;
257 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800258 for (const FstabEntry& entry : fstab) {
Tao Bao57130c42017-05-10 12:11:21 -0700259 // We don't want to do anything with "/".
Yifan Hongd81b8e32018-12-17 14:29:06 -0800260 if (entry.mount_point == "/") {
Tao Bao57130c42017-05-10 12:11:21 -0700261 continue;
262 }
263
Yifan Hongd81b8e32018-12-17 14:29:06 -0800264 if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
265 if (ensure_path_mounted(entry.mount_point) != 0) {
266 LOG(ERROR) << "Failed to mount " << entry.mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700267 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700268 }
269 } else {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800270 if (ensure_path_unmounted(entry.mount_point) != 0) {
271 LOG(ERROR) << "Failed to unmount " << entry.mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700272 return -1;
273 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700274 }
Tao Bao57130c42017-05-10 12:11:21 -0700275 }
276 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700277}
Tianjie Xu164c60a2019-05-15 13:59:39 -0700278
279bool HasCache() {
280 CHECK(!fstab.empty());
281 static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
282 return has_cache;
283}