2019-04-13 01:22:15 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "install/wipe_data.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
|
2022-06-23 22:36:21 +02:00
|
|
|
#include "bootloader_message/bootloader_message.h"
|
2019-10-15 22:22:20 +02:00
|
|
|
#include "install/snapshot_utils.h"
|
2019-04-13 01:22:15 +02:00
|
|
|
#include "otautil/dirutil.h"
|
|
|
|
#include "recovery_ui/ui.h"
|
2019-10-01 20:55:36 +02:00
|
|
|
#include "recovery_utils/logging.h"
|
|
|
|
#include "recovery_utils/roots.h"
|
2019-04-13 01:22:15 +02:00
|
|
|
|
|
|
|
constexpr const char* CACHE_ROOT = "/cache";
|
|
|
|
constexpr const char* DATA_ROOT = "/data";
|
|
|
|
constexpr const char* METADATA_ROOT = "/metadata";
|
|
|
|
|
2022-03-10 23:13:39 +01:00
|
|
|
static bool EraseVolume(const char* volume, RecoveryUI* ui) {
|
2019-04-13 01:22:15 +02:00
|
|
|
bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
|
|
|
|
|
|
|
|
std::vector<saved_log_file> log_files;
|
|
|
|
if (is_cache) {
|
2019-04-16 00:24:24 +02:00
|
|
|
// If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
|
|
|
|
// current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
|
|
|
|
log_files = ReadLogFilesToMemory();
|
2019-04-13 01:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ui->Print("Formatting %s...\n", volume);
|
|
|
|
|
|
|
|
ensure_path_unmounted(volume);
|
|
|
|
|
2022-03-10 23:13:39 +01:00
|
|
|
int result = format_volume(volume);
|
2019-04-13 01:22:15 +02:00
|
|
|
|
|
|
|
if (is_cache) {
|
2019-04-16 00:24:24 +02:00
|
|
|
RestoreLogFilesAfterFormat(log_files);
|
2019-04-13 01:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (result == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func) {
|
|
|
|
bool has_cache = volume_for_mount_point("/cache") != nullptr;
|
|
|
|
if (!has_cache) {
|
|
|
|
ui->Print("No /cache partition found.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (confirm_func && !confirm_func()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->Print("\n-- Wiping cache...\n");
|
2021-03-03 01:42:07 +01:00
|
|
|
ui->SetBackground(RecoveryUI::ERASING);
|
|
|
|
ui->SetProgressType(RecoveryUI::INDETERMINATE);
|
|
|
|
|
2022-03-10 23:13:39 +01:00
|
|
|
bool success = EraseVolume("/cache", ui);
|
2019-04-13 01:22:15 +02:00
|
|
|
ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2023-10-30 18:40:56 +01:00
|
|
|
bool WipeData(Device* device, bool keep_memtag_mode) {
|
2019-04-13 01:22:15 +02:00
|
|
|
RecoveryUI* ui = device->GetUI();
|
|
|
|
ui->Print("\n-- Wiping data...\n");
|
2021-03-03 01:42:07 +01:00
|
|
|
ui->SetBackground(RecoveryUI::ERASING);
|
|
|
|
ui->SetProgressType(RecoveryUI::INDETERMINATE);
|
2019-10-15 22:22:20 +02:00
|
|
|
|
|
|
|
if (!FinishPendingSnapshotMerges(device)) {
|
|
|
|
ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-13 01:22:15 +02:00
|
|
|
bool success = device->PreWipeData();
|
|
|
|
if (success) {
|
2022-03-10 23:13:39 +01:00
|
|
|
success &= EraseVolume(DATA_ROOT, ui);
|
2019-04-13 01:22:15 +02:00
|
|
|
bool has_cache = volume_for_mount_point("/cache") != nullptr;
|
|
|
|
if (has_cache) {
|
2022-03-10 23:13:39 +01:00
|
|
|
success &= EraseVolume(CACHE_ROOT, ui);
|
2019-04-13 01:22:15 +02:00
|
|
|
}
|
|
|
|
if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
|
2022-03-10 23:13:39 +01:00
|
|
|
success &= EraseVolume(METADATA_ROOT, ui);
|
2019-04-13 01:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-30 18:40:56 +01:00
|
|
|
if (keep_memtag_mode) {
|
|
|
|
ui->Print("NOT resetting memtag message as per request...\n");
|
|
|
|
} else {
|
|
|
|
ui->Print("Resetting memtag message...\n");
|
|
|
|
std::string err;
|
|
|
|
if (!WriteMiscMemtagMessage({}, &err)) {
|
|
|
|
ui->Print("Failed to reset memtag message: %s\n", err.c_str());
|
|
|
|
success = false;
|
|
|
|
}
|
2022-06-23 22:36:21 +02:00
|
|
|
}
|
2019-04-13 01:22:15 +02:00
|
|
|
if (success) {
|
|
|
|
success &= device->PostWipeData();
|
|
|
|
}
|
|
|
|
ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
|
|
|
|
return success;
|
2019-10-01 20:55:36 +02:00
|
|
|
}
|