blob: d448e6e89941af528e4ccbb9f2b8f38a590523f6 [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
Tao Bao3c00fac2017-07-22 16:46:54 -070031#include <algorithm>
32#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"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046
Tao Baobb10e582017-07-22 16:30:34 -070047static struct fstab* fstab = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Tao Baobb10e582017-07-22 16:30:34 -070049extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050050
Tao Baobb10e582017-07-22 16:30:34 -070051void load_volume_table() {
52 fstab = fs_mgr_read_fstab_default();
53 if (!fstab) {
54 LOG(ERROR) << "Failed to read default fstab";
55 return;
56 }
Doug Zongker2810ced2011-02-17 15:55:21 -080057
Tao Baobb10e582017-07-22 16:30:34 -070058 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
59 if (ret == -1) {
60 LOG(ERROR) << "Failed to add /tmp entry to fstab";
61 fs_mgr_free_fstab(fstab);
62 fstab = nullptr;
63 return;
64 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070065
Tao Baobb10e582017-07-22 16:30:34 -070066 printf("recovery filesystem table\n");
67 printf("=========================\n");
68 for (int i = 0; i < fstab->num_entries; ++i) {
69 const Volume* v = &fstab->recs[i];
Tom Cherry45e505a2018-11-29 13:33:07 -080070 printf(" %d %s %s %s %" PRId64 "\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
Tao Baobb10e582017-07-22 16:30:34 -070071 }
72 printf("\n");
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) {
76 return fs_mgr_get_entry_for_mount_point(fstab, mount_point);
77}
78
Tao Baoabb8f772015-07-30 14:43:27 -070079// Mount the volume specified by path at the given mount_point.
80int ensure_path_mounted_at(const char* path, const char* mount_point) {
Yifan Hong0f339e22018-12-03 13:44:01 -080081 return android::fs_mgr::EnsurePathMounted(fstab, path, mount_point) ? 0 : -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082}
83
Tao Baoabb8f772015-07-30 14:43:27 -070084int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -070085 // Mount at the default mount point.
Yifan Hong0f339e22018-12-03 13:44:01 -080086 return android::fs_mgr::EnsurePathMounted(fstab, path) ? 0 : -1;
Tao Baoabb8f772015-07-30 14:43:27 -070087}
88
Doug Zongkerd4208f92010-09-20 12:16:13 -070089int ensure_path_unmounted(const char* path) {
Yifan Hong0f339e22018-12-03 13:44:01 -080090 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) {
94 CHECK_NE(static_cast<size_t>(0), args.size());
95
96 std::vector<char*> argv(args.size());
97 std::transform(args.cbegin(), args.cend(), argv.begin(),
98 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
99 argv.push_back(nullptr);
100
101 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800102 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700103 execv(argv[0], argv.data());
104 _exit(EXIT_FAILURE);
105 }
106
107 int status;
108 waitpid(child, &status, 0);
109 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
110 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
111 }
112 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700113}
114
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530115static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700116 struct stat buf;
117 int ret = fstat(fd, &buf);
118 if (ret) return 0;
119
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530120 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700121 if (S_ISREG(buf.st_mode)) {
122 computed_size = buf.st_size - reserve_len;
123 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530124 uint64_t block_device_size = get_block_device_size(fd);
125 if (block_device_size < reserve_len ||
126 block_device_size > std::numeric_limits<int64_t>::max()) {
127 computed_size = 0;
128 } else {
129 computed_size = block_device_size - reserve_len;
130 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700131 } else {
132 computed_size = 0;
133 }
134
135 return computed_size;
136}
137
Paul Lawrenced0db3372015-11-05 13:38:40 -0800138int format_volume(const char* volume, const char* directory) {
Yifan Hong0f339e22018-12-03 13:44:01 -0800139 const Volume* v = android::fs_mgr::GetEntryForPath(fstab, volume);
Tao Bao3c00fac2017-07-22 16:46:54 -0700140 if (v == nullptr) {
141 LOG(ERROR) << "unknown volume \"" << volume << "\"";
142 return -1;
143 }
144 if (strcmp(v->fs_type, "ramdisk") == 0) {
145 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
146 return -1;
147 }
148 if (strcmp(v->mount_point, volume) != 0) {
149 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
150 return -1;
151 }
152 if (ensure_path_unmounted(volume) != 0) {
153 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
154 return -1;
155 }
156 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700157 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700159 }
160
161 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
162 // metadata. Wipe it too.
163 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
164 LOG(INFO) << "Wiping " << v->key_loc;
165 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
166 if (fd == -1) {
167 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
168 return -1;
169 }
170 wipe_block_device(fd, get_file_size(fd));
171 close(fd);
172 }
173
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530174 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800175 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700176 length = v->length;
Jin Qiancc100082017-11-17 23:53:22 -0800177 } else if (v->length < 0 ||
178 (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700179 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
180 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530181 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700182 return -1;
183 }
Jin Qiancc100082017-11-17 23:53:22 -0800184 length =
185 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700186 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800187 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
188 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700189 return -1;
190 }
191 }
192
193 if (strcmp(v->fs_type, "ext4") == 0) {
194 static constexpr int kBlockSize = 4096;
195 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900196 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700197 };
198
199 int raid_stride = v->logical_blk_size / kBlockSize;
200 int raid_stripe_width = v->erase_blk_size / kBlockSize;
201 // stride should be the max of 8KB and logical block size
202 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
203 raid_stride = 8192 / kBlockSize;
204 }
205 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
206 mke2fs_args.push_back("-E");
207 mke2fs_args.push_back(
208 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
209 }
210 mke2fs_args.push_back(v->blk_device);
211 if (length != 0) {
212 mke2fs_args.push_back(std::to_string(length / kBlockSize));
213 }
214
215 int result = exec_cmd(mke2fs_args);
216 if (result == 0 && directory != nullptr) {
217 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900218 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700219 };
220 result = exec_cmd(e2fsdroid_args);
221 }
222
223 if (result != 0) {
224 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
225 return -1;
226 }
227 return 0;
228 }
229
230 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700231 static constexpr int kSectorSize = 4096;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800232 std::string cmd("/sbin/mkfs.f2fs");
Jaegeuk Kim43582622018-04-02 13:37:35 -0700233 // clang-format off
234 std::vector<std::string> make_f2fs_cmd = {
235 cmd,
Jaegeuk Kim91e631d2018-11-21 11:12:54 -0800236 "-g", "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700237 v->blk_device,
238 };
239 // clang-format on
240 if (length >= kSectorSize) {
241 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700242 }
243
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800244 int result = exec_cmd(make_f2fs_cmd);
245 if (result == 0 && directory != nullptr) {
246 cmd = "/sbin/sload.f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700247 // clang-format off
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800248 std::vector<std::string> sload_f2fs_cmd = {
Jaegeuk Kim43582622018-04-02 13:37:35 -0700249 cmd,
250 "-f", directory,
251 "-t", volume,
252 v->blk_device,
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800253 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700254 // clang-format on
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800255 result = exec_cmd(sload_f2fs_cmd);
256 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700257 if (result != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800258 PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700259 return -1;
260 }
261 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800262}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700263
Paul Lawrenced0db3372015-11-05 13:38:40 -0800264int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700265 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800266}
267
Doug Zongker239ac6a2013-08-20 16:03:25 -0700268int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700269 if (fstab == nullptr) {
270 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
271 return -1;
272 }
273 for (int i = 0; i < fstab->num_entries; ++i) {
274 const Volume* v = fstab->recs + i;
275
276 // We don't want to do anything with "/".
277 if (strcmp(v->mount_point, "/") == 0) {
278 continue;
279 }
280
281 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
282 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700283 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700284 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700285 }
286 } else {
287 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700288 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700289 return -1;
290 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700291 }
Tao Bao57130c42017-05-10 12:11:21 -0700292 }
293 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700294}
David Anderson2b2f4232018-10-29 18:48:56 -0700295
296bool logical_partitions_mapped() {
Yifan Hong0f339e22018-12-03 13:44:01 -0800297 return android::fs_mgr::LogicalPartitionsMapped();
David Anderson2b2f4232018-10-29 18:48:56 -0700298}
Yifan Hong49327802018-11-26 14:59:09 -0800299
300std::string get_system_root() {
Yifan Hong0f339e22018-12-03 13:44:01 -0800301 return android::fs_mgr::GetSystemRoot();
Yifan Hong49327802018-11-26 14:59:09 -0800302}