blob: 6cce36c62b8ab6e848342a4f56b1a76333f0904b [file] [log] [blame]
xunchang316e9712019-04-12 16:22:15 -07001/*
2 * Copyright (C) 2019 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
17#include "install/wipe_data.h"
18
Tim Zimmermannbc664412021-06-08 16:14:55 +020019#include <fcntl.h>
20#include <linux/fs.h>
xunchang316e9712019-04-12 16:22:15 -070021#include <string.h>
Tim Zimmermannbc664412021-06-08 16:14:55 +020022#include <sys/ioctl.h>
xunchang316e9712019-04-12 16:22:15 -070023
24#include <functional>
xunchang316e9712019-04-12 16:22:15 -070025#include <vector>
26
27#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/stringprintf.h>
Michael Bestasa9454ef2019-09-27 19:52:13 +030030#include <fs_mgr/roots.h>
Tim Zimmermannbc664412021-06-08 16:14:55 +020031#include <libdm/dm.h>
xunchang316e9712019-04-12 16:22:15 -070032
Florian Mayer21d50b22022-06-23 13:36:21 -070033#include "bootloader_message/bootloader_message.h"
David Anderson89d2d052019-10-15 13:22:20 -070034#include "install/snapshot_utils.h"
xunchang316e9712019-04-12 16:22:15 -070035#include "recovery_ui/ui.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070036#include "recovery_utils/logging.h"
37#include "recovery_utils/roots.h"
xunchang316e9712019-04-12 16:22:15 -070038
39constexpr const char* CACHE_ROOT = "/cache";
40constexpr const char* DATA_ROOT = "/data";
41constexpr const char* METADATA_ROOT = "/metadata";
42
Kelvin Zhang73549942024-01-04 16:43:53 -080043static bool EraseVolume(const char* volume, RecoveryUI* ui, std::string_view new_fstype) {
44 LOG(INFO) << "Erasing volume " << volume << " with new filesystem type " << new_fstype;
xunchang316e9712019-04-12 16:22:15 -070045 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
xunchang316e9712019-04-12 16:22:15 -070046
xunchang316e9712019-04-12 16:22:15 -070047 std::vector<saved_log_file> log_files;
xunchang316e9712019-04-12 16:22:15 -070048 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070049 // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
50 // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
51 log_files = ReadLogFilesToMemory();
xunchang316e9712019-04-12 16:22:15 -070052 }
53
54 ui->Print("Formatting %s...\n", volume);
55
Simon Shieldseb476a02019-10-02 00:21:45 +100056 Volume* vol = volume_for_mount_point(volume);
Tim Zimmermannbc664412021-06-08 16:14:55 +020057 if (vol->fs_mgr_flags.logical) {
58 android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
59
60 map_logical_partitions();
61 // map_logical_partitions is non-blocking, so check for some limited time
62 // if it succeeded
63 for (int i = 0; i < 500; i++) {
64 if (vol->blk_device[0] == '/' ||
65 dm.GetState(vol->blk_device) == android::dm::DmDeviceState::ACTIVE)
66 break;
67 std::this_thread::sleep_for(std::chrono::milliseconds(1));
68 }
69
70 if (vol->blk_device[0] != '/' && !dm.GetDmDevicePathByName(vol->blk_device, &vol->blk_device)) {
71 PLOG(ERROR) << "Failed to find dm device path for " << vol->blk_device;
72 return false;
73 }
74
75 int fd = open(vol->blk_device.c_str(), O_RDWR);
76 if (fd < 0) {
77 PLOG(ERROR) << "Failed to open " << vol->blk_device;
78 return false;
79 }
80
81 int val = 0;
82 if (ioctl(fd, BLKROSET, &val) != 0) {
83 PLOG(ERROR) << "Failed to set " << vol->blk_device << " rw";
84 close(fd);
85 return false;
86 }
87
88 close(fd);
89 }
Simon Shieldseb476a02019-10-02 00:21:45 +100090
91 std::string blk_device;
92
93 if (!android::base::Realpath(vol->blk_device, &blk_device)) {
94 PLOG(ERROR) << "Failed to convert \"" << vol->blk_device << "\" to absolute path";
95 return false;
96 }
97
98 if (ensure_volume_unmounted(blk_device) == -1) {
99 PLOG(ERROR) << "Failed to unmount volume!";
100 return false;
101 }
xunchang316e9712019-04-12 16:22:15 -0700102
Kelvin Zhang73549942024-01-04 16:43:53 -0800103 int result = format_volume(volume, "", new_fstype);
xunchang316e9712019-04-12 16:22:15 -0700104
105 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -0700106 RestoreLogFilesAfterFormat(log_files);
xunchang316e9712019-04-12 16:22:15 -0700107 }
108
109 return (result == 0);
110}
111
Kelvin Zhang73549942024-01-04 16:43:53 -0800112bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func,
113 std::string_view new_fstype) {
xunchang316e9712019-04-12 16:22:15 -0700114 bool has_cache = volume_for_mount_point("/cache") != nullptr;
115 if (!has_cache) {
116 ui->Print("No /cache partition found.\n");
117 return false;
118 }
119
120 if (confirm_func && !confirm_func()) {
121 return false;
122 }
123
124 ui->Print("\n-- Wiping cache...\n");
Tianjie32b4e722021-03-02 16:42:07 -0800125 ui->SetBackground(RecoveryUI::ERASING);
126 ui->SetProgressType(RecoveryUI::INDETERMINATE);
127
Kelvin Zhang73549942024-01-04 16:43:53 -0800128 bool success = EraseVolume("/cache", ui, new_fstype);
xunchang316e9712019-04-12 16:22:15 -0700129 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
130 return success;
131}
132
Kelvin Zhang73549942024-01-04 16:43:53 -0800133bool WipeData(Device* device, bool keep_memtag_mode, std::string_view data_fstype) {
xunchang316e9712019-04-12 16:22:15 -0700134 RecoveryUI* ui = device->GetUI();
Kelvin Zhang73549942024-01-04 16:43:53 -0800135 ui->Print("\n-- Wiping data %.*s...\n", static_cast<int>(data_fstype.size()), data_fstype.data());
Tianjie32b4e722021-03-02 16:42:07 -0800136 ui->SetBackground(RecoveryUI::ERASING);
137 ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -0700138
139 if (!FinishPendingSnapshotMerges(device)) {
140 ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
141 return false;
142 }
143
xunchang316e9712019-04-12 16:22:15 -0700144 bool success = device->PreWipeData();
145 if (success) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800146 success &= EraseVolume(DATA_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700147 bool has_cache = volume_for_mount_point("/cache") != nullptr;
148 if (has_cache) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800149 success &= EraseVolume(CACHE_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700150 }
151 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800152 success &= EraseVolume(METADATA_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700153 }
154 }
Florian Mayer0feef552023-10-30 17:40:56 +0000155 if (keep_memtag_mode) {
156 ui->Print("NOT resetting memtag message as per request...\n");
157 } else {
158 ui->Print("Resetting memtag message...\n");
159 std::string err;
160 if (!WriteMiscMemtagMessage({}, &err)) {
161 ui->Print("Failed to reset memtag message: %s\n", err.c_str());
162 success = false;
163 }
Florian Mayer21d50b22022-06-23 13:36:21 -0700164 }
xunchang316e9712019-04-12 16:22:15 -0700165 if (success) {
166 success &= device->PostWipeData();
167 }
168 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
169 return success;
Tao Baoe3f09a72019-10-01 11:55:36 -0700170}
Michael Bestasa9454ef2019-09-27 19:52:13 +0300171
172bool WipeSystem(RecoveryUI* ui, const std::function<bool()>& confirm_func,
173 std::string_view new_fstype) {
174 if (confirm_func && !confirm_func()) {
175 return false;
176 }
177
178 ui->Print("\n-- Wiping system...\n");
179 bool success = EraseVolume(android::fs_mgr::GetSystemRoot().c_str(), ui, new_fstype);
180 ui->Print("System wipe %s.\n", success ? "complete" : "failed");
181 return success;
182}