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