blob: e68cb20bcabd2ed7d0529eb3ae4b6bd3b7da0229 [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>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053021#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070023#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024#include <sys/mount.h>
25#include <sys/stat.h>
26#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070027#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include <unistd.h>
29
Tao Bao3c00fac2017-07-22 16:46:54 -070030#include <algorithm>
31#include <string>
32#include <vector>
33
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070034#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070035#include <android-base/properties.h>
36#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070037#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070038#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070039#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040#include <fs_mgr.h>
David Anderson2b2f4232018-10-29 18:48:56 -070041#include <fs_mgr_dm_linear.h>
Tao Baode40ba52016-10-05 23:17:01 -070042
Tao Bao9a319f02018-01-04 13:19:11 -080043#include "otautil/mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Tao Baobb10e582017-07-22 16:30:34 -070045static struct fstab* fstab = nullptr;
David Anderson2b2f4232018-10-29 18:48:56 -070046static bool did_map_logical_partitions = false;
Yifan Hong49327802018-11-26 14:59:09 -080047static constexpr const char* SYSTEM_ROOT = "/system";
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];
70 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
71 }
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 Bao2dfc1a32017-09-27 13:12:11 -070079// Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match
80// only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log",
81// "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the
82// first match or nullptr.
Tao Bao3e18f2b2017-09-29 17:11:13 -070083static Volume* volume_for_path(const char* path) {
Tao Bao2dfc1a32017-09-27 13:12:11 -070084 if (path == nullptr || path[0] == '\0') return nullptr;
85 std::string str(path);
86 while (true) {
Tao Baoad774b22017-09-29 10:39:08 -070087 Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str);
Tao Bao2dfc1a32017-09-27 13:12:11 -070088 if (result != nullptr || str == "/") {
89 return result;
90 }
91 size_t slash = str.find_last_of('/');
92 if (slash == std::string::npos) return nullptr;
93 if (slash == 0) {
94 str = "/";
95 } else {
96 str = str.substr(0, slash);
97 }
98 }
99 return nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100}
101
Tao Baoabb8f772015-07-30 14:43:27 -0700102// Mount the volume specified by path at the given mount_point.
103int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -0700104 Volume* v = volume_for_path(path);
105 if (v == nullptr) {
106 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800107 return -1;
Tao Baobb10e582017-07-22 16:30:34 -0700108 }
109 if (strcmp(v->fs_type, "ramdisk") == 0) {
110 // The ramdisk is always mounted.
111 return 0;
112 }
113
114 if (!scan_mounted_volumes()) {
115 LOG(ERROR) << "Failed to scan mounted volumes";
116 return -1;
117 }
118
119 if (!mount_point) {
120 mount_point = v->mount_point;
121 }
122
David Anderson2b2f4232018-10-29 18:48:56 -0700123 // If we can't acquire the block device for a logical partition, it likely
124 // was never created. In that case we try to create it.
125 if (fs_mgr_is_logical(v) && !fs_mgr_update_logical_partition(v)) {
126 if (did_map_logical_partitions) {
127 LOG(ERROR) << "Failed to find block device for partition";
128 return -1;
129 }
130 std::string super_name = fs_mgr_get_super_partition_name();
131 if (!android::fs_mgr::CreateLogicalPartitions(super_name)) {
132 LOG(ERROR) << "Failed to create logical partitions";
133 return -1;
134 }
135 did_map_logical_partitions = true;
136 if (!fs_mgr_update_logical_partition(v)) {
137 LOG(ERROR) << "Failed to find block device for partition";
138 return -1;
139 }
140 }
141
Tao Baobb10e582017-07-22 16:30:34 -0700142 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
143 if (mv != nullptr) {
144 // Volume is already mounted.
145 return 0;
146 }
147
148 mkdir(mount_point, 0755); // in case it doesn't already exist
149
150 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
Lianjun Huang5d7be6b2018-09-03 18:07:35 +0800151 strcmp(v->fs_type, "vfat") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700152 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
153 if (result == -1 && fs_mgr_is_formattable(v)) {
154 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
155 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
156 if (fs_mgr_do_format(v, crypt_footer) == 0) {
157 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
158 } else {
159 PLOG(ERROR) << "Failed to format " << mount_point;
160 return -1;
161 }
162 }
163
164 if (result == -1) {
165 PLOG(ERROR) << "Failed to mount " << mount_point;
166 return -1;
167 }
168 return 0;
169 }
170
171 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
172 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800173}
174
Tao Baoabb8f772015-07-30 14:43:27 -0700175int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700176 // Mount at the default mount point.
177 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700178}
179
Doug Zongkerd4208f92010-09-20 12:16:13 -0700180int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700181 const Volume* v = volume_for_path(path);
182 if (v == nullptr) {
183 LOG(ERROR) << "unknown volume for path [" << path << "]";
184 return -1;
185 }
186 if (strcmp(v->fs_type, "ramdisk") == 0) {
187 // The ramdisk is always mounted; you can't unmount it.
188 return -1;
189 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190
Tao Baobb10e582017-07-22 16:30:34 -0700191 if (!scan_mounted_volumes()) {
192 LOG(ERROR) << "Failed to scan mounted volumes";
193 return -1;
194 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700195
Tao Baobb10e582017-07-22 16:30:34 -0700196 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
197 if (mv == nullptr) {
198 // Volume is already unmounted.
199 return 0;
200 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800201
Tao Baobb10e582017-07-22 16:30:34 -0700202 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700203}
204
Tao Bao3c00fac2017-07-22 16:46:54 -0700205static int exec_cmd(const std::vector<std::string>& args) {
206 CHECK_NE(static_cast<size_t>(0), args.size());
207
208 std::vector<char*> argv(args.size());
209 std::transform(args.cbegin(), args.cend(), argv.begin(),
210 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
211 argv.push_back(nullptr);
212
213 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800214 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700215 execv(argv[0], argv.data());
216 _exit(EXIT_FAILURE);
217 }
218
219 int status;
220 waitpid(child, &status, 0);
221 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
222 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
223 }
224 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700225}
226
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530227static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700228 struct stat buf;
229 int ret = fstat(fd, &buf);
230 if (ret) return 0;
231
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530232 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700233 if (S_ISREG(buf.st_mode)) {
234 computed_size = buf.st_size - reserve_len;
235 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530236 uint64_t block_device_size = get_block_device_size(fd);
237 if (block_device_size < reserve_len ||
238 block_device_size > std::numeric_limits<int64_t>::max()) {
239 computed_size = 0;
240 } else {
241 computed_size = block_device_size - reserve_len;
242 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700243 } else {
244 computed_size = 0;
245 }
246
247 return computed_size;
248}
249
Paul Lawrenced0db3372015-11-05 13:38:40 -0800250int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700251 const Volume* v = volume_for_path(volume);
252 if (v == nullptr) {
253 LOG(ERROR) << "unknown volume \"" << volume << "\"";
254 return -1;
255 }
256 if (strcmp(v->fs_type, "ramdisk") == 0) {
257 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
258 return -1;
259 }
260 if (strcmp(v->mount_point, volume) != 0) {
261 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
262 return -1;
263 }
264 if (ensure_path_unmounted(volume) != 0) {
265 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
266 return -1;
267 }
268 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700269 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700271 }
272
273 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
274 // metadata. Wipe it too.
275 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
276 LOG(INFO) << "Wiping " << v->key_loc;
277 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
278 if (fd == -1) {
279 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
280 return -1;
281 }
282 wipe_block_device(fd, get_file_size(fd));
283 close(fd);
284 }
285
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530286 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800287 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700288 length = v->length;
Jin Qiancc100082017-11-17 23:53:22 -0800289 } else if (v->length < 0 ||
290 (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700291 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
292 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530293 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700294 return -1;
295 }
Jin Qiancc100082017-11-17 23:53:22 -0800296 length =
297 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700298 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800299 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
300 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700301 return -1;
302 }
303 }
304
305 if (strcmp(v->fs_type, "ext4") == 0) {
306 static constexpr int kBlockSize = 4096;
307 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900308 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700309 };
310
311 int raid_stride = v->logical_blk_size / kBlockSize;
312 int raid_stripe_width = v->erase_blk_size / kBlockSize;
313 // stride should be the max of 8KB and logical block size
314 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
315 raid_stride = 8192 / kBlockSize;
316 }
317 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
318 mke2fs_args.push_back("-E");
319 mke2fs_args.push_back(
320 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
321 }
322 mke2fs_args.push_back(v->blk_device);
323 if (length != 0) {
324 mke2fs_args.push_back(std::to_string(length / kBlockSize));
325 }
326
327 int result = exec_cmd(mke2fs_args);
328 if (result == 0 && directory != nullptr) {
329 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900330 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700331 };
332 result = exec_cmd(e2fsdroid_args);
333 }
334
335 if (result != 0) {
336 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
337 return -1;
338 }
339 return 0;
340 }
341
342 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700343 static constexpr int kSectorSize = 4096;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800344 std::string cmd("/sbin/mkfs.f2fs");
Jaegeuk Kim43582622018-04-02 13:37:35 -0700345 // clang-format off
346 std::vector<std::string> make_f2fs_cmd = {
347 cmd,
348 "-d1",
349 "-f",
350 "-O", "encrypt",
351 "-O", "quota",
Jaegeuk Kim2e5dc842018-04-05 22:42:13 -0700352 "-O", "verity",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700353 "-w", std::to_string(kSectorSize),
354 v->blk_device,
355 };
356 // clang-format on
357 if (length >= kSectorSize) {
358 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700359 }
360
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800361 int result = exec_cmd(make_f2fs_cmd);
362 if (result == 0 && directory != nullptr) {
363 cmd = "/sbin/sload.f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700364 // clang-format off
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800365 std::vector<std::string> sload_f2fs_cmd = {
Jaegeuk Kim43582622018-04-02 13:37:35 -0700366 cmd,
367 "-f", directory,
368 "-t", volume,
369 v->blk_device,
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800370 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700371 // clang-format on
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800372 result = exec_cmd(sload_f2fs_cmd);
373 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700374 if (result != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800375 PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700376 return -1;
377 }
378 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800379}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700380
Paul Lawrenced0db3372015-11-05 13:38:40 -0800381int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700382 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800383}
384
Doug Zongker239ac6a2013-08-20 16:03:25 -0700385int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700386 if (fstab == nullptr) {
387 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
388 return -1;
389 }
390 for (int i = 0; i < fstab->num_entries; ++i) {
391 const Volume* v = fstab->recs + i;
392
393 // We don't want to do anything with "/".
394 if (strcmp(v->mount_point, "/") == 0) {
395 continue;
396 }
397
398 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
399 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700400 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700401 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700402 }
403 } else {
404 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700405 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700406 return -1;
407 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700408 }
Tao Bao57130c42017-05-10 12:11:21 -0700409 }
410 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700411}
David Anderson2b2f4232018-10-29 18:48:56 -0700412
413bool logical_partitions_mapped() {
414 return did_map_logical_partitions;
415}
Yifan Hong49327802018-11-26 14:59:09 -0800416
417std::string get_system_root() {
418 if (volume_for_mount_point(SYSTEM_ROOT) == nullptr) {
419 return "/";
420 } else {
421 return SYSTEM_ROOT;
422 }
423}