blob: 3352654a34c6fe8b9d94d6f61bcf07d0643aeac3 [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
xunchang316e9712019-04-12 16:22:15 -070019#include <string.h>
xunchang316e9712019-04-12 16:22:15 -070020
21#include <functional>
xunchang316e9712019-04-12 16:22:15 -070022#include <vector>
23
24#include <android-base/file.h>
25#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
Michael Bestas87091fe2019-09-27 19:52:13 +030027#include <fs_mgr/roots.h>
xunchang316e9712019-04-12 16:22:15 -070028
Florian Mayer21d50b22022-06-23 13:36:21 -070029#include "bootloader_message/bootloader_message.h"
David Anderson89d2d052019-10-15 13:22:20 -070030#include "install/snapshot_utils.h"
xunchang316e9712019-04-12 16:22:15 -070031#include "recovery_ui/ui.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070032#include "recovery_utils/logging.h"
33#include "recovery_utils/roots.h"
xunchang316e9712019-04-12 16:22:15 -070034
35constexpr const char* CACHE_ROOT = "/cache";
36constexpr const char* DATA_ROOT = "/data";
37constexpr const char* METADATA_ROOT = "/metadata";
38
Kelvin Zhang73549942024-01-04 16:43:53 -080039static bool EraseVolume(const char* volume, RecoveryUI* ui, std::string_view new_fstype) {
40 LOG(INFO) << "Erasing volume " << volume << " with new filesystem type " << new_fstype;
xunchang316e9712019-04-12 16:22:15 -070041 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
xunchang316e9712019-04-12 16:22:15 -070042
xunchang316e9712019-04-12 16:22:15 -070043 std::vector<saved_log_file> log_files;
xunchang316e9712019-04-12 16:22:15 -070044 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070045 // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
46 // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
47 log_files = ReadLogFilesToMemory();
xunchang316e9712019-04-12 16:22:15 -070048 }
49
50 ui->Print("Formatting %s...\n", volume);
51
Simon Shields93c1e1e2019-10-02 00:21:45 +100052 Volume* vol = volume_for_mount_point(volume);
53 std::string blk_device;
54
55 if (!android::base::Realpath(vol->blk_device, &blk_device)) {
56 PLOG(ERROR) << "Failed to convert \"" << vol->blk_device << "\" to absolute path";
57 return false;
58 }
59
60 if (ensure_volume_unmounted(blk_device) == -1) {
61 PLOG(ERROR) << "Failed to unmount volume!";
62 return false;
63 }
xunchang316e9712019-04-12 16:22:15 -070064
Kelvin Zhang73549942024-01-04 16:43:53 -080065 int result = format_volume(volume, "", new_fstype);
xunchang316e9712019-04-12 16:22:15 -070066
67 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070068 RestoreLogFilesAfterFormat(log_files);
xunchang316e9712019-04-12 16:22:15 -070069 }
70
71 return (result == 0);
72}
73
Kelvin Zhang73549942024-01-04 16:43:53 -080074bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func,
75 std::string_view new_fstype) {
xunchang316e9712019-04-12 16:22:15 -070076 bool has_cache = volume_for_mount_point("/cache") != nullptr;
77 if (!has_cache) {
78 ui->Print("No /cache partition found.\n");
79 return false;
80 }
81
82 if (confirm_func && !confirm_func()) {
83 return false;
84 }
85
86 ui->Print("\n-- Wiping cache...\n");
Tianjie32b4e722021-03-02 16:42:07 -080087 ui->SetBackground(RecoveryUI::ERASING);
88 ui->SetProgressType(RecoveryUI::INDETERMINATE);
89
Kelvin Zhang73549942024-01-04 16:43:53 -080090 bool success = EraseVolume("/cache", ui, new_fstype);
xunchang316e9712019-04-12 16:22:15 -070091 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
92 return success;
93}
94
Kelvin Zhang73549942024-01-04 16:43:53 -080095bool WipeData(Device* device, bool keep_memtag_mode, std::string_view data_fstype) {
xunchang316e9712019-04-12 16:22:15 -070096 RecoveryUI* ui = device->GetUI();
Kelvin Zhang73549942024-01-04 16:43:53 -080097 ui->Print("\n-- Wiping data %.*s...\n", static_cast<int>(data_fstype.size()), data_fstype.data());
Tianjie32b4e722021-03-02 16:42:07 -080098 ui->SetBackground(RecoveryUI::ERASING);
99 ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -0700100
101 if (!FinishPendingSnapshotMerges(device)) {
102 ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
103 return false;
104 }
105
xunchang316e9712019-04-12 16:22:15 -0700106 bool success = device->PreWipeData();
107 if (success) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800108 success &= EraseVolume(DATA_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700109 bool has_cache = volume_for_mount_point("/cache") != nullptr;
110 if (has_cache) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800111 success &= EraseVolume(CACHE_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700112 }
113 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800114 success &= EraseVolume(METADATA_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700115 }
116 }
Florian Mayer0feef552023-10-30 17:40:56 +0000117 if (keep_memtag_mode) {
118 ui->Print("NOT resetting memtag message as per request...\n");
119 } else {
120 ui->Print("Resetting memtag message...\n");
121 std::string err;
122 if (!WriteMiscMemtagMessage({}, &err)) {
123 ui->Print("Failed to reset memtag message: %s\n", err.c_str());
124 success = false;
125 }
Florian Mayer21d50b22022-06-23 13:36:21 -0700126 }
xunchang316e9712019-04-12 16:22:15 -0700127 if (success) {
128 success &= device->PostWipeData();
129 }
130 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
131 return success;
Tao Baoe3f09a72019-10-01 11:55:36 -0700132}
Michael Bestas87091fe2019-09-27 19:52:13 +0300133
134bool WipeSystem(RecoveryUI* ui, const std::function<bool()>& confirm_func,
135 std::string_view new_fstype) {
136 if (confirm_func && !confirm_func()) {
137 return false;
138 }
139
140 ui->Print("\n-- Wiping system...\n");
141 bool success = EraseVolume(android::fs_mgr::GetSystemRoot().c_str(), ui, new_fstype);
142 ui->Print("System wipe %s.\n", success ? "complete" : "failed");
143 return success;
144}