xunchang | 388d253 | 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 | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 19 | #include <string.h> |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 20 | |
| 21 | #include <functional> |
xunchang | 388d253 | 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 | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 29 | #include "otautil/dirutil.h" |
xunchang | 388d253 | 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 | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 33 | |
| 34 | constexpr const char* CACHE_ROOT = "/cache"; |
bigbiff | 7df8747 | 2022-09-24 16:45:03 -0400 | [diff] [blame] | 35 | //constexpr const char* DATA_ROOT = "/data"; |
| 36 | //constexpr const char* METADATA_ROOT = "/metadata"; |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 37 | |
bigbiff | 7df8747 | 2022-09-24 16:45:03 -0400 | [diff] [blame] | 38 | static bool EraseVolume(const char* volume) { |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 39 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 40 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 41 | // ui->SetBackground(RecoveryUI::ERASING); |
| 42 | // ui->SetProgressType(RecoveryUI::INDETERMINATE); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 43 | |
| 44 | std::vector<saved_log_file> log_files; |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 45 | if (is_cache) { |
xunchang | cd780b4 | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 46 | // 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(); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 49 | } |
| 50 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 51 | // ui->Print("Formatting %s...\n", volume); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 52 | |
| 53 | ensure_path_unmounted(volume); |
| 54 | |
Eric Biggers | fde69fb | 2022-03-10 22:13:39 +0000 | [diff] [blame] | 55 | int result = format_volume(volume); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 56 | |
| 57 | if (is_cache) { |
xunchang | cd780b4 | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 58 | RestoreLogFilesAfterFormat(log_files); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return (result == 0); |
| 62 | } |
| 63 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 64 | bool WipeCache(const std::function<bool()>& confirm_func) { |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 65 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 66 | if (!has_cache) { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 67 | // ui->Print("No /cache partition found.\n"); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if (confirm_func && !confirm_func()) { |
| 72 | return false; |
| 73 | } |
| 74 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 75 | // ui->Print("\n-- Wiping cache...\n"); |
bigbiff | 75b9423 | 2021-11-11 17:34:34 -0500 | [diff] [blame] | 76 | // ui->SetBackground(RecoveryUI::ERASING); |
| 77 | // ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Tianjie | 32b4e72 | 2021-03-02 16:42:07 -0800 | [diff] [blame] | 78 | |
bigbiff | 7df8747 | 2022-09-24 16:45:03 -0400 | [diff] [blame] | 79 | bool success = EraseVolume("/cache"); |
| 80 | //ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 81 | return success; |
| 82 | } |
| 83 | |
bigbiff | 7df8747 | 2022-09-24 16:45:03 -0400 | [diff] [blame] | 84 | bool WipeData() { |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 85 | // RecoveryUI* ui = device->GetUI(); |
| 86 | // ui->Print("\n-- Wiping data...\n"); |
bigbiff | 75b9423 | 2021-11-11 17:34:34 -0500 | [diff] [blame] | 87 | // ui->SetBackground(RecoveryUI::ERASING); |
| 88 | // ui->SetProgressType(RecoveryUI::INDETERMINATE); |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 89 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 90 | // if (!FinishPendingSnapshotMerges(device)) { |
| 91 | // ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n"); |
| 92 | // return false; |
| 93 | // } |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 94 | |
bigbiff | 7df8747 | 2022-09-24 16:45:03 -0400 | [diff] [blame] | 95 | //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 | //} |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 110 | // ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); |
xunchang | 388d253 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 111 | return success; |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 112 | } |