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 <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/stat.h> |
| 22 | |
| 23 | #include <functional> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/file.h> |
| 27 | #include <android-base/logging.h> |
| 28 | #include <android-base/stringprintf.h> |
Michael Bestas | bd1cda0 | 2019-09-27 19:52:13 +0300 | [diff] [blame] | 29 | #include <fs_mgr/roots.h> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 30 | |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 31 | #include "install/snapshot_utils.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 32 | #include "otautil/dirutil.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 33 | #include "recovery_ui/ui.h" |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 34 | #include "recovery_utils/logging.h" |
| 35 | #include "recovery_utils/roots.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 36 | |
| 37 | constexpr const char* CACHE_ROOT = "/cache"; |
| 38 | constexpr const char* DATA_ROOT = "/data"; |
| 39 | constexpr const char* METADATA_ROOT = "/metadata"; |
| 40 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 41 | static bool EraseVolume(const char* volume, RecoveryUI* ui, bool convert_fbe) { |
| 42 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
| 43 | bool is_data = (strcmp(volume, DATA_ROOT) == 0); |
| 44 | |
| 45 | ui->SetBackground(RecoveryUI::ERASING); |
| 46 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
| 47 | |
| 48 | std::vector<saved_log_file> log_files; |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 49 | if (is_cache) { |
xunchang | 2239b9e | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 50 | // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the |
| 51 | // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat. |
| 52 | log_files = ReadLogFilesToMemory(); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ui->Print("Formatting %s...\n", volume); |
| 56 | |
Simon Shields | e14a8d4 | 2019-10-02 00:21:45 +1000 | [diff] [blame] | 57 | Volume* vol = volume_for_mount_point(volume); |
| 58 | if (ensure_volume_unmounted(vol->blk_device) == -1) { |
| 59 | PLOG(ERROR) << "Failed to unmount volume!"; |
| 60 | return false; |
| 61 | } |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 62 | |
| 63 | int result; |
| 64 | if (is_data && convert_fbe) { |
| 65 | constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe"; |
| 66 | constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe"; |
| 67 | // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not |
| 68 | // full disk encryption. |
| 69 | if (mkdir(CONVERT_FBE_DIR, 0700) != 0) { |
| 70 | PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR; |
| 71 | return false; |
| 72 | } |
| 73 | FILE* f = fopen(CONVERT_FBE_FILE, "wbe"); |
| 74 | if (!f) { |
| 75 | PLOG(ERROR) << "Failed to convert to file encryption"; |
| 76 | return false; |
| 77 | } |
| 78 | fclose(f); |
| 79 | result = format_volume(volume, CONVERT_FBE_DIR); |
| 80 | remove(CONVERT_FBE_FILE); |
| 81 | rmdir(CONVERT_FBE_DIR); |
| 82 | } else { |
| 83 | result = format_volume(volume); |
| 84 | } |
| 85 | |
| 86 | if (is_cache) { |
xunchang | 2239b9e | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 87 | RestoreLogFilesAfterFormat(log_files); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return (result == 0); |
| 91 | } |
| 92 | |
| 93 | bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func) { |
| 94 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 95 | if (!has_cache) { |
| 96 | ui->Print("No /cache partition found.\n"); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (confirm_func && !confirm_func()) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | ui->Print("\n-- Wiping cache...\n"); |
| 105 | bool success = EraseVolume("/cache", ui, false); |
| 106 | ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); |
| 107 | return success; |
| 108 | } |
| 109 | |
| 110 | bool WipeData(Device* device, bool convert_fbe) { |
| 111 | RecoveryUI* ui = device->GetUI(); |
| 112 | ui->Print("\n-- Wiping data...\n"); |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 113 | |
| 114 | if (!FinishPendingSnapshotMerges(device)) { |
| 115 | ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n"); |
| 116 | return false; |
| 117 | } |
| 118 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 119 | bool success = device->PreWipeData(); |
| 120 | if (success) { |
| 121 | success &= EraseVolume(DATA_ROOT, ui, convert_fbe); |
| 122 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 123 | if (has_cache) { |
| 124 | success &= EraseVolume(CACHE_ROOT, ui, false); |
| 125 | } |
| 126 | if (volume_for_mount_point(METADATA_ROOT) != nullptr) { |
| 127 | success &= EraseVolume(METADATA_ROOT, ui, false); |
| 128 | } |
| 129 | } |
| 130 | if (success) { |
| 131 | success &= device->PostWipeData(); |
| 132 | } |
| 133 | ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); |
| 134 | return success; |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 135 | } |
Michael Bestas | bd1cda0 | 2019-09-27 19:52:13 +0300 | [diff] [blame] | 136 | |
| 137 | bool WipeSystem(RecoveryUI* ui, const std::function<bool()>& confirm_func) { |
| 138 | if (confirm_func && !confirm_func()) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | ui->Print("\n-- Wiping system...\n"); |
| 143 | bool success = EraseVolume(android::fs_mgr::GetSystemRoot().c_str(), ui, false); |
| 144 | ui->Print("System wipe %s.\n", success ? "complete" : "failed"); |
| 145 | return success; |
| 146 | } |