blob: 0f57384a91b2d3e72980eae6ff202f0f5c9acba6 [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>
27
Florian Mayer21d50b22022-06-23 13:36:21 -070028#include "bootloader_message/bootloader_message.h"
David Anderson89d2d052019-10-15 13:22:20 -070029#include "install/snapshot_utils.h"
xunchang316e9712019-04-12 16:22:15 -070030#include "recovery_ui/ui.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070031#include "recovery_utils/logging.h"
32#include "recovery_utils/roots.h"
xunchang316e9712019-04-12 16:22:15 -070033
34constexpr const char* CACHE_ROOT = "/cache";
35constexpr const char* DATA_ROOT = "/data";
36constexpr const char* METADATA_ROOT = "/metadata";
37
Kelvin Zhang73549942024-01-04 16:43:53 -080038static bool EraseVolume(const char* volume, RecoveryUI* ui, std::string_view new_fstype) {
39 LOG(INFO) << "Erasing volume " << volume << " with new filesystem type " << new_fstype;
xunchang316e9712019-04-12 16:22:15 -070040 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
xunchang316e9712019-04-12 16:22:15 -070041
xunchang316e9712019-04-12 16:22:15 -070042 std::vector<saved_log_file> log_files;
xunchang316e9712019-04-12 16:22:15 -070043 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070044 // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
45 // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
46 log_files = ReadLogFilesToMemory();
xunchang316e9712019-04-12 16:22:15 -070047 }
48
49 ui->Print("Formatting %s...\n", volume);
50
51 ensure_path_unmounted(volume);
52
Kelvin Zhang73549942024-01-04 16:43:53 -080053 int result = format_volume(volume, "", new_fstype);
xunchang316e9712019-04-12 16:22:15 -070054
55 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070056 RestoreLogFilesAfterFormat(log_files);
xunchang316e9712019-04-12 16:22:15 -070057 }
58
59 return (result == 0);
60}
61
Kelvin Zhang73549942024-01-04 16:43:53 -080062bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func,
63 std::string_view new_fstype) {
xunchang316e9712019-04-12 16:22:15 -070064 bool has_cache = volume_for_mount_point("/cache") != nullptr;
65 if (!has_cache) {
66 ui->Print("No /cache partition found.\n");
67 return false;
68 }
69
70 if (confirm_func && !confirm_func()) {
71 return false;
72 }
73
74 ui->Print("\n-- Wiping cache...\n");
Tianjie32b4e722021-03-02 16:42:07 -080075 ui->SetBackground(RecoveryUI::ERASING);
76 ui->SetProgressType(RecoveryUI::INDETERMINATE);
77
Kelvin Zhang73549942024-01-04 16:43:53 -080078 bool success = EraseVolume("/cache", ui, new_fstype);
xunchang316e9712019-04-12 16:22:15 -070079 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
80 return success;
81}
82
Kelvin Zhang73549942024-01-04 16:43:53 -080083bool WipeData(Device* device, bool keep_memtag_mode, std::string_view data_fstype) {
xunchang316e9712019-04-12 16:22:15 -070084 RecoveryUI* ui = device->GetUI();
Kelvin Zhang73549942024-01-04 16:43:53 -080085 ui->Print("\n-- Wiping data %.*s...\n", static_cast<int>(data_fstype.size()), data_fstype.data());
Tianjie32b4e722021-03-02 16:42:07 -080086 ui->SetBackground(RecoveryUI::ERASING);
87 ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -070088
89 if (!FinishPendingSnapshotMerges(device)) {
90 ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
91 return false;
92 }
93
xunchang316e9712019-04-12 16:22:15 -070094 bool success = device->PreWipeData();
95 if (success) {
Kelvin Zhang73549942024-01-04 16:43:53 -080096 success &= EraseVolume(DATA_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -070097 bool has_cache = volume_for_mount_point("/cache") != nullptr;
98 if (has_cache) {
Kelvin Zhang73549942024-01-04 16:43:53 -080099 success &= EraseVolume(CACHE_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700100 }
101 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800102 success &= EraseVolume(METADATA_ROOT, ui, data_fstype);
xunchang316e9712019-04-12 16:22:15 -0700103 }
104 }
Florian Mayer0feef552023-10-30 17:40:56 +0000105 if (keep_memtag_mode) {
106 ui->Print("NOT resetting memtag message as per request...\n");
107 } else {
108 ui->Print("Resetting memtag message...\n");
109 std::string err;
110 if (!WriteMiscMemtagMessage({}, &err)) {
111 ui->Print("Failed to reset memtag message: %s\n", err.c_str());
112 success = false;
113 }
Florian Mayer21d50b22022-06-23 13:36:21 -0700114 }
xunchang316e9712019-04-12 16:22:15 -0700115 if (success) {
116 success &= device->PostWipeData();
117 }
118 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
119 return success;
Tao Baoe3f09a72019-10-01 11:55:36 -0700120}