2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-10-01 20:55:36 +02:00
|
|
|
#include "recovery_utils/roots.h"
|
2016-06-10 02:41:22 +02:00
|
|
|
|
2017-07-23 01:30:34 +02:00
|
|
|
#include <fcntl.h>
|
2017-08-24 11:57:16 +02:00
|
|
|
#include <stdint.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <stdlib.h>
|
2017-09-29 19:39:08 +02:00
|
|
|
#include <string.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2014-06-17 04:07:39 +02:00
|
|
|
#include <sys/wait.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
#include <iostream>
|
2017-07-23 01:46:54 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-08-06 03:00:04 +02:00
|
|
|
#include <android-base/logging.h>
|
2019-12-17 03:32:00 +01:00
|
|
|
#include <android-base/properties.h>
|
2017-04-21 23:36:12 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-07-24 19:34:35 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2021-11-09 01:45:49 +01:00
|
|
|
#include <ext4_utils/ext4_utils.h>
|
2021-12-01 03:39:31 +01:00
|
|
|
#include <ext4_utils/wipe.h>
|
2013-02-13 21:59:35 +01:00
|
|
|
#include <fs_mgr.h>
|
2018-12-03 22:44:01 +01:00
|
|
|
#include <fs_mgr/roots.h>
|
2016-10-06 08:17:01 +02:00
|
|
|
|
2018-12-20 18:44:06 +01:00
|
|
|
#include "otautil/sysutil.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2019-01-31 00:59:53 +01:00
|
|
|
using android::fs_mgr::Fstab;
|
|
|
|
using android::fs_mgr::FstabEntry;
|
|
|
|
using android::fs_mgr::ReadDefaultFstab;
|
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
static Fstab fstab;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
constexpr const char* CACHE_ROOT = "/cache";
|
|
|
|
|
2017-07-23 01:30:34 +02:00
|
|
|
void load_volume_table() {
|
2018-12-17 23:29:06 +01:00
|
|
|
if (!ReadDefaultFstab(&fstab)) {
|
2017-07-23 01:30:34 +02:00
|
|
|
LOG(ERROR) << "Failed to read default fstab";
|
|
|
|
return;
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
fstab.emplace_back(FstabEntry{
|
2019-10-11 01:33:58 +02:00
|
|
|
.blk_device = "ramdisk",
|
|
|
|
.mount_point = "/tmp",
|
|
|
|
.fs_type = "ramdisk",
|
|
|
|
.length = 0,
|
|
|
|
});
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
|
|
|
|
for (size_t i = 0; i < fstab.size(); ++i) {
|
|
|
|
const auto& entry = fstab[i];
|
|
|
|
std::cout << " " << i << " " << entry.mount_point << " "
|
|
|
|
<< " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
|
|
|
|
<< std::endl;
|
2017-07-23 01:30:34 +02:00
|
|
|
}
|
2018-12-17 23:29:06 +01:00
|
|
|
std::cout << std::endl;
|
2010-09-20 21:16:13 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-09-30 02:11:13 +02:00
|
|
|
Volume* volume_for_mount_point(const std::string& mount_point) {
|
2019-01-31 00:59:53 +01:00
|
|
|
return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
|
2017-09-30 02:11:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 23:43:27 +02:00
|
|
|
// Mount the volume specified by path at the given mount_point.
|
2018-12-17 23:29:06 +01:00
|
|
|
int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
|
|
|
|
return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
int ensure_path_mounted(const std::string& path) {
|
2017-07-23 01:30:34 +02:00
|
|
|
// Mount at the default mount point.
|
2018-12-17 23:29:06 +01:00
|
|
|
return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
|
2015-07-30 23:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
int ensure_path_unmounted(const std::string& path) {
|
|
|
|
return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-23 01:46:54 +02:00
|
|
|
static int exec_cmd(const std::vector<std::string>& args) {
|
2018-12-20 18:44:06 +01:00
|
|
|
CHECK(!args.empty());
|
|
|
|
auto argv = StringVectorToNullTerminatedArray(args);
|
2017-07-23 01:46:54 +02:00
|
|
|
|
|
|
|
pid_t child;
|
2018-02-18 02:48:45 +01:00
|
|
|
if ((child = fork()) == 0) {
|
2017-07-23 01:46:54 +02:00
|
|
|
execv(argv[0], argv.data());
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int status;
|
|
|
|
waitpid(child, &status, 0);
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
|
|
|
LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
|
|
|
|
}
|
|
|
|
return WEXITSTATUS(status);
|
2014-06-17 04:07:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 11:57:16 +02:00
|
|
|
static int64_t get_file_size(int fd, uint64_t reserve_len) {
|
2017-07-24 19:34:35 +02:00
|
|
|
struct stat buf;
|
|
|
|
int ret = fstat(fd, &buf);
|
|
|
|
if (ret) return 0;
|
|
|
|
|
2017-08-24 11:57:16 +02:00
|
|
|
int64_t computed_size;
|
2017-07-24 19:34:35 +02:00
|
|
|
if (S_ISREG(buf.st_mode)) {
|
|
|
|
computed_size = buf.st_size - reserve_len;
|
|
|
|
} else if (S_ISBLK(buf.st_mode)) {
|
2017-08-24 11:57:16 +02:00
|
|
|
uint64_t block_device_size = get_block_device_size(fd);
|
|
|
|
if (block_device_size < reserve_len ||
|
|
|
|
block_device_size > std::numeric_limits<int64_t>::max()) {
|
|
|
|
computed_size = 0;
|
|
|
|
} else {
|
|
|
|
computed_size = block_device_size - reserve_len;
|
|
|
|
}
|
2017-07-24 19:34:35 +02:00
|
|
|
} else {
|
|
|
|
computed_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return computed_size;
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
int format_volume(const std::string& volume, const std::string& directory) {
|
|
|
|
const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume);
|
2017-07-23 01:46:54 +02:00
|
|
|
if (v == nullptr) {
|
|
|
|
LOG(ERROR) << "unknown volume \"" << volume << "\"";
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-17 23:29:06 +01:00
|
|
|
if (v->fs_type == "ramdisk") {
|
2017-07-23 01:46:54 +02:00
|
|
|
LOG(ERROR) << "can't format_volume \"" << volume << "\"";
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-17 23:29:06 +01:00
|
|
|
if (v->mount_point != volume) {
|
2017-07-23 01:46:54 +02:00
|
|
|
LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ensure_path_unmounted(volume) != 0) {
|
|
|
|
LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-17 23:29:06 +01:00
|
|
|
if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
|
2017-07-23 01:46:54 +02:00
|
|
|
LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-12-17 03:32:00 +01:00
|
|
|
bool needs_casefold = false;
|
|
|
|
|
|
|
|
if (volume == "/data") {
|
2020-04-15 11:52:21 +02:00
|
|
|
needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
|
2019-12-17 03:32:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-24 11:57:16 +02:00
|
|
|
int64_t length = 0;
|
2017-11-18 08:53:22 +01:00
|
|
|
if (v->length > 0) {
|
2017-07-23 01:46:54 +02:00
|
|
|
length = v->length;
|
2021-11-09 01:45:49 +01:00
|
|
|
} else if (v->length < 0) {
|
2018-12-17 23:29:06 +01:00
|
|
|
android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
|
2017-07-23 01:46:54 +02:00
|
|
|
if (fd == -1) {
|
2017-08-24 11:57:16 +02:00
|
|
|
PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
|
2017-07-23 01:46:54 +02:00
|
|
|
return -1;
|
2010-09-22 01:49:26 +02:00
|
|
|
}
|
2021-11-09 01:45:49 +01:00
|
|
|
length = get_file_size(fd.get(), -v->length);
|
2017-07-23 01:46:54 +02:00
|
|
|
if (length <= 0) {
|
2019-03-23 00:08:52 +01:00
|
|
|
LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
|
2017-07-23 01:46:54 +02:00
|
|
|
return -1;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-07-23 01:46:54 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2021-12-01 03:39:31 +01:00
|
|
|
// If the raw disk will be used as a metadata encrypted device mapper target,
|
|
|
|
// next boot will do encrypt_in_place the raw disk which gives a subtle duration
|
|
|
|
// to get any failure in the process. In order to avoid it, let's simply wipe
|
|
|
|
// the raw disk if we don't reserve any space, which behaves exactly same as booting
|
|
|
|
// after "fastboot -w".
|
2021-12-02 21:51:20 +01:00
|
|
|
if (!v->metadata_key_dir.empty() && length == 0) {
|
2021-12-01 03:39:31 +01:00
|
|
|
android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDWR));
|
|
|
|
if (fd == -1) {
|
|
|
|
PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int64_t device_size = get_file_size(fd.get(), 0);
|
|
|
|
if (device_size > 0 && !wipe_block_device(fd.get(), device_size)) {
|
|
|
|
LOG(INFO) << "format_volume: wipe metadata encrypted " << v->blk_device << " with size "
|
|
|
|
<< device_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
if (v->fs_type == "ext4") {
|
2017-07-23 01:46:54 +02:00
|
|
|
static constexpr int kBlockSize = 4096;
|
|
|
|
std::vector<std::string> mke2fs_args = {
|
2018-06-20 07:18:18 +02:00
|
|
|
"/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
|
2017-07-23 01:46:54 +02:00
|
|
|
};
|
|
|
|
|
2022-02-02 14:43:39 +01:00
|
|
|
// Following is added for Project ID's quota as they require wider inodes.
|
|
|
|
// The Quotas themselves are enabled by tune2fs on boot.
|
|
|
|
mke2fs_args.push_back("-I");
|
|
|
|
mke2fs_args.push_back("512");
|
2019-12-17 03:32:00 +01:00
|
|
|
|
2020-02-12 00:24:54 +01:00
|
|
|
if (v->fs_mgr_flags.ext_meta_csum) {
|
|
|
|
mke2fs_args.push_back("-O");
|
|
|
|
mke2fs_args.push_back("metadata_csum");
|
|
|
|
mke2fs_args.push_back("-O");
|
|
|
|
mke2fs_args.push_back("64bit");
|
|
|
|
mke2fs_args.push_back("-O");
|
|
|
|
mke2fs_args.push_back("extent");
|
|
|
|
}
|
|
|
|
|
2017-07-23 01:46:54 +02:00
|
|
|
int raid_stride = v->logical_blk_size / kBlockSize;
|
|
|
|
int raid_stripe_width = v->erase_blk_size / kBlockSize;
|
|
|
|
// stride should be the max of 8KB and logical block size
|
|
|
|
if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
|
|
|
|
raid_stride = 8192 / kBlockSize;
|
|
|
|
}
|
|
|
|
if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
|
|
|
|
mke2fs_args.push_back("-E");
|
|
|
|
mke2fs_args.push_back(
|
|
|
|
android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
|
|
|
|
}
|
|
|
|
mke2fs_args.push_back(v->blk_device);
|
|
|
|
if (length != 0) {
|
|
|
|
mke2fs_args.push_back(std::to_string(length / kBlockSize));
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2017-07-23 01:46:54 +02:00
|
|
|
int result = exec_cmd(mke2fs_args);
|
2018-12-17 23:29:06 +01:00
|
|
|
if (result == 0 && !directory.empty()) {
|
2017-07-23 01:46:54 +02:00
|
|
|
std::vector<std::string> e2fsdroid_args = {
|
2018-06-20 07:18:18 +02:00
|
|
|
"/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
|
2017-07-23 01:46:54 +02:00
|
|
|
};
|
|
|
|
result = exec_cmd(e2fsdroid_args);
|
2010-09-20 21:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-07-23 01:46:54 +02:00
|
|
|
if (result != 0) {
|
|
|
|
PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has to be f2fs because we checked earlier.
|
2018-04-02 22:37:35 +02:00
|
|
|
static constexpr int kSectorSize = 4096;
|
|
|
|
std::vector<std::string> make_f2fs_cmd = {
|
2018-12-20 23:25:15 +01:00
|
|
|
"/system/bin/make_f2fs",
|
|
|
|
"-g",
|
|
|
|
"android",
|
2018-04-02 22:37:35 +02:00
|
|
|
};
|
2022-02-02 14:43:39 +01:00
|
|
|
|
|
|
|
make_f2fs_cmd.push_back("-O");
|
|
|
|
make_f2fs_cmd.push_back("project_quota,extra_attr");
|
|
|
|
|
2019-12-17 03:32:00 +01:00
|
|
|
if (needs_casefold) {
|
|
|
|
make_f2fs_cmd.push_back("-O");
|
|
|
|
make_f2fs_cmd.push_back("casefold");
|
|
|
|
make_f2fs_cmd.push_back("-C");
|
|
|
|
make_f2fs_cmd.push_back("utf8");
|
|
|
|
}
|
2020-01-14 20:00:37 +01:00
|
|
|
if (v->fs_mgr_flags.fs_compress) {
|
|
|
|
make_f2fs_cmd.push_back("-O");
|
|
|
|
make_f2fs_cmd.push_back("compression");
|
|
|
|
make_f2fs_cmd.push_back("-O");
|
|
|
|
make_f2fs_cmd.push_back("extra_attr");
|
|
|
|
}
|
2019-12-17 03:32:00 +01:00
|
|
|
make_f2fs_cmd.push_back(v->blk_device);
|
2018-04-02 22:37:35 +02:00
|
|
|
if (length >= kSectorSize) {
|
|
|
|
make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
|
2017-07-23 01:46:54 +02:00
|
|
|
}
|
|
|
|
|
2018-12-20 23:25:15 +01:00
|
|
|
if (exec_cmd(make_f2fs_cmd) != 0) {
|
|
|
|
PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!directory.empty()) {
|
2017-11-29 04:48:05 +01:00
|
|
|
std::vector<std::string> sload_f2fs_cmd = {
|
2018-12-20 23:25:15 +01:00
|
|
|
"/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
|
2017-11-29 04:48:05 +01:00
|
|
|
};
|
2018-12-20 23:25:15 +01:00
|
|
|
if (exec_cmd(sload_f2fs_cmd) != 0) {
|
|
|
|
PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-07-23 01:46:54 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2013-08-21 01:03:25 +02:00
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
int format_volume(const std::string& volume) {
|
|
|
|
return format_volume(volume, "");
|
2015-11-05 22:38:40 +01:00
|
|
|
}
|
|
|
|
|
2013-08-21 01:03:25 +02:00
|
|
|
int setup_install_mounts() {
|
2018-12-17 23:29:06 +01:00
|
|
|
if (fstab.empty()) {
|
2017-05-10 21:11:21 +02:00
|
|
|
LOG(ERROR) << "can't set up install mounts: no fstab loaded";
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-17 23:29:06 +01:00
|
|
|
for (const FstabEntry& entry : fstab) {
|
2017-05-10 21:11:21 +02:00
|
|
|
// We don't want to do anything with "/".
|
2018-12-17 23:29:06 +01:00
|
|
|
if (entry.mount_point == "/") {
|
2017-05-10 21:11:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-21 01:03:25 +02:00
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
|
|
|
|
if (ensure_path_mounted(entry.mount_point) != 0) {
|
|
|
|
LOG(ERROR) << "Failed to mount " << entry.mount_point;
|
2017-05-10 21:11:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-17 23:29:06 +01:00
|
|
|
if (ensure_path_unmounted(entry.mount_point) != 0) {
|
|
|
|
LOG(ERROR) << "Failed to unmount " << entry.mount_point;
|
2017-05-10 21:11:21 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-08-21 01:03:25 +02:00
|
|
|
}
|
2017-05-10 21:11:21 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2013-08-21 01:03:25 +02:00
|
|
|
}
|
2019-05-15 22:59:39 +02:00
|
|
|
|
|
|
|
bool HasCache() {
|
|
|
|
CHECK(!fstab.empty());
|
|
|
|
static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
|
|
|
|
return has_cache;
|
|
|
|
}
|