blob: 6b7cc25cb67829fd3a947acb5caf85006eb58395 [file] [log] [blame]
xunchang388d2532019-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
xunchang388d2532019-04-12 16:22:15 -070019#include <string.h>
xunchang388d2532019-04-12 16:22:15 -070020
21#include <functional>
xunchang388d2532019-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
David Anderson89d2d052019-10-15 13:22:20 -070028#include "install/snapshot_utils.h"
xunchang388d2532019-04-12 16:22:15 -070029#include "otautil/dirutil.h"
xunchang388d2532019-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"
xunchang388d2532019-04-12 16:22:15 -070033
34constexpr const char* CACHE_ROOT = "/cache";
bigbiff7df87472022-09-24 16:45:03 -040035//constexpr const char* DATA_ROOT = "/data";
36//constexpr const char* METADATA_ROOT = "/metadata";
xunchang388d2532019-04-12 16:22:15 -070037
bigbiff7df87472022-09-24 16:45:03 -040038static bool EraseVolume(const char* volume) {
xunchang388d2532019-04-12 16:22:15 -070039 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
xunchang388d2532019-04-12 16:22:15 -070040
bigbiffd58ba182020-03-23 10:02:29 -040041 // ui->SetBackground(RecoveryUI::ERASING);
42 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
xunchang388d2532019-04-12 16:22:15 -070043
44 std::vector<saved_log_file> log_files;
xunchang388d2532019-04-12 16:22:15 -070045 if (is_cache) {
xunchangcd780b42019-04-15 15:24:24 -070046 // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
47 // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
48 log_files = ReadLogFilesToMemory();
xunchang388d2532019-04-12 16:22:15 -070049 }
50
bigbiffd58ba182020-03-23 10:02:29 -040051 // ui->Print("Formatting %s...\n", volume);
xunchang388d2532019-04-12 16:22:15 -070052
53 ensure_path_unmounted(volume);
54
Eric Biggersfde69fb2022-03-10 22:13:39 +000055 int result = format_volume(volume);
xunchang388d2532019-04-12 16:22:15 -070056
57 if (is_cache) {
xunchangcd780b42019-04-15 15:24:24 -070058 RestoreLogFilesAfterFormat(log_files);
xunchang388d2532019-04-12 16:22:15 -070059 }
60
61 return (result == 0);
62}
63
bigbiffd58ba182020-03-23 10:02:29 -040064bool WipeCache(const std::function<bool()>& confirm_func) {
xunchang388d2532019-04-12 16:22:15 -070065 bool has_cache = volume_for_mount_point("/cache") != nullptr;
66 if (!has_cache) {
bigbiffd58ba182020-03-23 10:02:29 -040067 // ui->Print("No /cache partition found.\n");
xunchang388d2532019-04-12 16:22:15 -070068 return false;
69 }
70
71 if (confirm_func && !confirm_func()) {
72 return false;
73 }
74
bigbiffd58ba182020-03-23 10:02:29 -040075 // ui->Print("\n-- Wiping cache...\n");
bigbiff75b94232021-11-11 17:34:34 -050076 // ui->SetBackground(RecoveryUI::ERASING);
77 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
Tianjie32b4e722021-03-02 16:42:07 -080078
bigbiff7df87472022-09-24 16:45:03 -040079 bool success = EraseVolume("/cache");
80 //ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
xunchang388d2532019-04-12 16:22:15 -070081 return success;
82}
83
bigbiff7df87472022-09-24 16:45:03 -040084bool WipeData() {
bigbiffd58ba182020-03-23 10:02:29 -040085 // RecoveryUI* ui = device->GetUI();
86 // ui->Print("\n-- Wiping data...\n");
bigbiff75b94232021-11-11 17:34:34 -050087 // ui->SetBackground(RecoveryUI::ERASING);
88 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -070089
bigbiff673c7ae2020-12-02 19:44:56 -050090 // if (!FinishPendingSnapshotMerges(device)) {
91 // ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
92 // return false;
93 // }
David Anderson89d2d052019-10-15 13:22:20 -070094
bigbiff7df87472022-09-24 16:45:03 -040095 //bool success = device->PreWipeData();
96 bool success = true;
97 //if (success) {
98 //success &= EraseVolume(DATA_ROOT);
99 //bool has_cache = volume_for_mount_point("/cache") != nullptr;
100 //if (has_cache) {
101 // success &= EraseVolume(CACHE_ROOT);
102 // }
103 //if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
104 //success &= EraseVolume(METADATA_ROOT);
105 //}
106 //}
107 //if (success) {
108 //success &= device->PostWipeData();
109 //}
bigbiffd58ba182020-03-23 10:02:29 -0400110 // ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
xunchang388d2532019-04-12 16:22:15 -0700111 return success;
Tao Baoe3f09a72019-10-01 11:55:36 -0700112}