xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 19 | #include <string.h> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 20 | |
| 21 | #include <functional> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/stringprintf.h> |
| 27 | |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 28 | #include "install/snapshot_utils.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 29 | #include "otautil/dirutil.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 30 | #include "recovery_ui/ui.h" |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 31 | #include "recovery_utils/logging.h" |
| 32 | #include "recovery_utils/roots.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 33 | |
| 34 | constexpr const char* CACHE_ROOT = "/cache"; |
| 35 | constexpr const char* DATA_ROOT = "/data"; |
| 36 | constexpr const char* METADATA_ROOT = "/metadata"; |
| 37 | |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 38 | static bool EraseVolume(const char* volume, RecoveryUI* ui) { |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 39 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 40 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 41 | std::vector<saved_log_file> log_files; |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 42 | if (is_cache) { |
xunchang | 2239b9e | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 43 | // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the |
| 44 | // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat. |
| 45 | log_files = ReadLogFilesToMemory(); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ui->Print("Formatting %s...\n", volume); |
| 49 | |
| 50 | ensure_path_unmounted(volume); |
| 51 | |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 52 | int result = format_volume(volume); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 53 | |
| 54 | if (is_cache) { |
xunchang | 2239b9e | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 55 | RestoreLogFilesAfterFormat(log_files); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return (result == 0); |
| 59 | } |
| 60 | |
| 61 | bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func) { |
| 62 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 63 | if (!has_cache) { |
| 64 | ui->Print("No /cache partition found.\n"); |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if (confirm_func && !confirm_func()) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | ui->Print("\n-- Wiping cache...\n"); |
Tianjie | 32b4e72 | 2021-03-02 16:42:07 -0800 | [diff] [blame] | 73 | ui->SetBackground(RecoveryUI::ERASING); |
| 74 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
| 75 | |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 76 | bool success = EraseVolume("/cache", ui); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 77 | ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); |
| 78 | return success; |
| 79 | } |
| 80 | |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 81 | bool WipeData(Device* device) { |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 82 | RecoveryUI* ui = device->GetUI(); |
| 83 | ui->Print("\n-- Wiping data...\n"); |
Tianjie | 32b4e72 | 2021-03-02 16:42:07 -0800 | [diff] [blame] | 84 | ui->SetBackground(RecoveryUI::ERASING); |
| 85 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 86 | |
| 87 | if (!FinishPendingSnapshotMerges(device)) { |
| 88 | ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n"); |
| 89 | return false; |
| 90 | } |
| 91 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 92 | bool success = device->PreWipeData(); |
| 93 | if (success) { |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 94 | success &= EraseVolume(DATA_ROOT, ui); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 95 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 96 | if (has_cache) { |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 97 | success &= EraseVolume(CACHE_ROOT, ui); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 98 | } |
| 99 | if (volume_for_mount_point(METADATA_ROOT) != nullptr) { |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 100 | success &= EraseVolume(METADATA_ROOT, ui); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | if (success) { |
| 104 | success &= device->PostWipeData(); |
| 105 | } |
| 106 | ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); |
| 107 | return success; |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 108 | } |