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 | |
Tim Zimmermann | 68dd30e | 2021-06-08 16:14:55 +0200 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <linux/fs.h> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <string.h> |
Tim Zimmermann | 68dd30e | 2021-06-08 16:14:55 +0200 | [diff] [blame] | 23 | #include <sys/ioctl.h> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | |
| 26 | #include <functional> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
| 31 | #include <android-base/stringprintf.h> |
Michael Bestas | 9b1b410 | 2019-09-27 19:52:13 +0300 | [diff] [blame] | 32 | #include <fs_mgr/roots.h> |
Tim Zimmermann | 68dd30e | 2021-06-08 16:14:55 +0200 | [diff] [blame] | 33 | #include <libdm/dm.h> |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 34 | |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 35 | #include "install/snapshot_utils.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 36 | #include "otautil/dirutil.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 37 | #include "recovery_ui/ui.h" |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 38 | #include "recovery_utils/logging.h" |
| 39 | #include "recovery_utils/roots.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 40 | |
| 41 | constexpr const char* CACHE_ROOT = "/cache"; |
| 42 | constexpr const char* DATA_ROOT = "/data"; |
| 43 | constexpr const char* METADATA_ROOT = "/metadata"; |
| 44 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 45 | static bool EraseVolume(const char* volume, RecoveryUI* ui, bool convert_fbe) { |
| 46 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
| 47 | bool is_data = (strcmp(volume, DATA_ROOT) == 0); |
| 48 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 49 | std::vector<saved_log_file> log_files; |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 50 | if (is_cache) { |
xunchang | 2239b9e | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 51 | // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the |
| 52 | // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat. |
| 53 | log_files = ReadLogFilesToMemory(); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | ui->Print("Formatting %s...\n", volume); |
| 57 | |
Simon Shields | dbc4d66 | 2019-10-02 00:21:45 +1000 | [diff] [blame] | 58 | Volume* vol = volume_for_mount_point(volume); |
Tim Zimmermann | 68dd30e | 2021-06-08 16:14:55 +0200 | [diff] [blame] | 59 | if (vol->fs_mgr_flags.logical) { |
| 60 | android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance(); |
| 61 | |
| 62 | map_logical_partitions(); |
| 63 | // map_logical_partitions is non-blocking, so check for some limited time |
| 64 | // if it succeeded |
| 65 | for (int i = 0; i < 500; i++) { |
| 66 | if (vol->blk_device[0] == '/' || |
| 67 | dm.GetState(vol->blk_device) == android::dm::DmDeviceState::ACTIVE) |
| 68 | break; |
| 69 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 70 | } |
| 71 | |
| 72 | if (vol->blk_device[0] != '/' && !dm.GetDmDevicePathByName(vol->blk_device, &vol->blk_device)) { |
| 73 | PLOG(ERROR) << "Failed to find dm device path for " << vol->blk_device; |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | int fd = open(vol->blk_device.c_str(), O_RDWR); |
| 78 | if (fd < 0) { |
| 79 | PLOG(ERROR) << "Failed to open " << vol->blk_device; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | int val = 0; |
| 84 | if (ioctl(fd, BLKROSET, &val) != 0) { |
| 85 | PLOG(ERROR) << "Failed to set " << vol->blk_device << " rw"; |
| 86 | close(fd); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | close(fd); |
| 91 | } |
| 92 | |
Simon Shields | dbc4d66 | 2019-10-02 00:21:45 +1000 | [diff] [blame] | 93 | if (ensure_volume_unmounted(vol->blk_device) == -1) { |
| 94 | PLOG(ERROR) << "Failed to unmount volume!"; |
| 95 | return false; |
| 96 | } |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 97 | |
| 98 | int result; |
| 99 | if (is_data && convert_fbe) { |
| 100 | constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe"; |
| 101 | constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe"; |
| 102 | // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not |
| 103 | // full disk encryption. |
| 104 | if (mkdir(CONVERT_FBE_DIR, 0700) != 0) { |
| 105 | PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR; |
| 106 | return false; |
| 107 | } |
| 108 | FILE* f = fopen(CONVERT_FBE_FILE, "wbe"); |
| 109 | if (!f) { |
| 110 | PLOG(ERROR) << "Failed to convert to file encryption"; |
| 111 | return false; |
| 112 | } |
| 113 | fclose(f); |
| 114 | result = format_volume(volume, CONVERT_FBE_DIR); |
| 115 | remove(CONVERT_FBE_FILE); |
| 116 | rmdir(CONVERT_FBE_DIR); |
| 117 | } else { |
| 118 | result = format_volume(volume); |
| 119 | } |
| 120 | |
| 121 | if (is_cache) { |
xunchang | 2239b9e | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 122 | RestoreLogFilesAfterFormat(log_files); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return (result == 0); |
| 126 | } |
| 127 | |
| 128 | bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func) { |
| 129 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 130 | if (!has_cache) { |
| 131 | ui->Print("No /cache partition found.\n"); |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | if (confirm_func && !confirm_func()) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | ui->Print("\n-- Wiping cache...\n"); |
Tianjie | 32b4e72 | 2021-03-02 16:42:07 -0800 | [diff] [blame] | 140 | ui->SetBackground(RecoveryUI::ERASING); |
| 141 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
| 142 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 143 | bool success = EraseVolume("/cache", ui, false); |
| 144 | ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); |
| 145 | return success; |
| 146 | } |
| 147 | |
| 148 | bool WipeData(Device* device, bool convert_fbe) { |
| 149 | RecoveryUI* ui = device->GetUI(); |
| 150 | ui->Print("\n-- Wiping data...\n"); |
Tianjie | 32b4e72 | 2021-03-02 16:42:07 -0800 | [diff] [blame] | 151 | ui->SetBackground(RecoveryUI::ERASING); |
| 152 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
David Anderson | 89d2d05 | 2019-10-15 13:22:20 -0700 | [diff] [blame] | 153 | |
| 154 | if (!FinishPendingSnapshotMerges(device)) { |
| 155 | ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n"); |
| 156 | return false; |
| 157 | } |
| 158 | |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 159 | bool success = device->PreWipeData(); |
| 160 | if (success) { |
| 161 | success &= EraseVolume(DATA_ROOT, ui, convert_fbe); |
| 162 | bool has_cache = volume_for_mount_point("/cache") != nullptr; |
| 163 | if (has_cache) { |
| 164 | success &= EraseVolume(CACHE_ROOT, ui, false); |
| 165 | } |
| 166 | if (volume_for_mount_point(METADATA_ROOT) != nullptr) { |
| 167 | success &= EraseVolume(METADATA_ROOT, ui, false); |
| 168 | } |
| 169 | } |
| 170 | if (success) { |
| 171 | success &= device->PostWipeData(); |
| 172 | } |
| 173 | ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); |
| 174 | return success; |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 175 | } |
Michael Bestas | 9b1b410 | 2019-09-27 19:52:13 +0300 | [diff] [blame] | 176 | |
| 177 | bool WipeSystem(RecoveryUI* ui, const std::function<bool()>& confirm_func) { |
| 178 | if (confirm_func && !confirm_func()) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | ui->Print("\n-- Wiping system...\n"); |
| 183 | bool success = EraseVolume(android::fs_mgr::GetSystemRoot().c_str(), ui, false); |
| 184 | ui->Print("System wipe %s.\n", success ? "complete" : "failed"); |
| 185 | return success; |
| 186 | } |