blob: 02cf467aaa9bfcd7a716ccec51dc2fcbd4a097ca [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 "otautil/dirutil.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
Eric Biggersfde69fb2022-03-10 22:13:39 +000039static bool EraseVolume(const char* volume, RecoveryUI* ui) {
xunchang316e9712019-04-12 16:22:15 -070040 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
xunchang316e9712019-04-12 16:22:15 -070041
bigbiffd58ba182020-03-23 10:02:29 -040042 // ui->SetBackground(RecoveryUI::ERASING);
43 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
xunchang388d2532019-04-12 16:22:15 -070044
xunchang316e9712019-04-12 16:22:15 -070045 std::vector<saved_log_file> log_files;
xunchang316e9712019-04-12 16:22:15 -070046 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070047 // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
48 // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
49 log_files = ReadLogFilesToMemory();
xunchang316e9712019-04-12 16:22:15 -070050 }
51
bigbiffd58ba182020-03-23 10:02:29 -040052 // ui->Print("Formatting %s...\n", volume);
xunchang316e9712019-04-12 16:22:15 -070053
54 ensure_path_unmounted(volume);
55
Eric Biggersfde69fb2022-03-10 22:13:39 +000056 int result = format_volume(volume);
xunchang316e9712019-04-12 16:22:15 -070057
58 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070059 RestoreLogFilesAfterFormat(log_files);
xunchang316e9712019-04-12 16:22:15 -070060 }
61
62 return (result == 0);
63}
64
bigbiffd58ba182020-03-23 10:02:29 -040065bool WipeCache(const std::function<bool()>& confirm_func) {
xunchang316e9712019-04-12 16:22:15 -070066 bool has_cache = volume_for_mount_point("/cache") != nullptr;
67 if (!has_cache) {
bigbiffd58ba182020-03-23 10:02:29 -040068 // ui->Print("No /cache partition found.\n");
xunchang316e9712019-04-12 16:22:15 -070069 return false;
70 }
71
72 if (confirm_func && !confirm_func()) {
73 return false;
74 }
75
bigbiffd58ba182020-03-23 10:02:29 -040076 // ui->Print("\n-- Wiping cache...\n");
bigbiff75b94232021-11-11 17:34:34 -050077 // ui->SetBackground(RecoveryUI::ERASING);
78 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
Tianjie32b4e722021-03-02 16:42:07 -080079
bigbiff984fa152021-11-21 14:48:57 -050080 bool success = EraseVolume("/cache", false);
bigbiff8b83b2e2021-11-20 15:56:23 -050081 // ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
xunchang316e9712019-04-12 16:22:15 -070082 return success;
83}
84
xunchang388d2532019-04-12 16:22:15 -070085bool WipeData(Device* device, bool convert_fbe) {
bigbiffd58ba182020-03-23 10:02:29 -040086 // RecoveryUI* ui = device->GetUI();
87 // ui->Print("\n-- Wiping data...\n");
bigbiff75b94232021-11-11 17:34:34 -050088 // ui->SetBackground(RecoveryUI::ERASING);
89 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -070090
bigbiff673c7ae2020-12-02 19:44:56 -050091 // if (!FinishPendingSnapshotMerges(device)) {
92 // ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
93 // return false;
94 // }
David Anderson89d2d052019-10-15 13:22:20 -070095
xunchang316e9712019-04-12 16:22:15 -070096 bool success = device->PreWipeData();
97 if (success) {
bigbiff49551a22023-12-24 15:24:13 -050098 success &= EraseVolume(DATA_ROOT);
xunchang316e9712019-04-12 16:22:15 -070099 bool has_cache = volume_for_mount_point("/cache") != nullptr;
100 if (has_cache) {
bigbiff49551a22023-12-24 15:24:13 -0500101 success &= EraseVolume(CACHE_ROOT);
xunchang316e9712019-04-12 16:22:15 -0700102 }
103 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
bigbiff49551a22023-12-24 15:24:13 -0500104 success &= EraseVolume(METADATA_ROOT);
xunchang316e9712019-04-12 16:22:15 -0700105 }
106 }
Florian Mayer21d50b22022-06-23 13:36:21 -0700107 ui->Print("Resetting memtag message...\n");
108 std::string err;
109 if (!WriteMiscMemtagMessage({}, &err)) {
110 ui->Print("Failed to reset memtag message: %s\n", err.c_str());
111 success = false;
112 }
xunchang316e9712019-04-12 16:22:15 -0700113 if (success) {
114 success &= device->PostWipeData();
115 }
bigbiffd58ba182020-03-23 10:02:29 -0400116 // ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
xunchang316e9712019-04-12 16:22:15 -0700117 return success;
Tao Baoe3f09a72019-10-01 11:55:36 -0700118}