blob: 6b488a0e75fa0409fa2d9afa1edf990c969d561a [file] [log] [blame]
xunchang316e9712019-04-12 16:22:15 -07001/*
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 Zimmermann241ef2c2021-06-08 16:14:55 +020019#include <fcntl.h>
20#include <linux/fs.h>
xunchang316e9712019-04-12 16:22:15 -070021#include <string.h>
Tim Zimmermann241ef2c2021-06-08 16:14:55 +020022#include <sys/ioctl.h>
xunchang316e9712019-04-12 16:22:15 -070023
24#include <functional>
xunchang316e9712019-04-12 16:22:15 -070025#include <vector>
26
27#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/stringprintf.h>
Michael Bestasb7e71ff2019-09-27 19:52:13 +030030#include <fs_mgr/roots.h>
Tim Zimmermann241ef2c2021-06-08 16:14:55 +020031#include <libdm/dm.h>
xunchang316e9712019-04-12 16:22:15 -070032
David Anderson89d2d052019-10-15 13:22:20 -070033#include "install/snapshot_utils.h"
xunchang316e9712019-04-12 16:22:15 -070034#include "otautil/dirutil.h"
xunchang316e9712019-04-12 16:22:15 -070035#include "recovery_ui/ui.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070036#include "recovery_utils/logging.h"
37#include "recovery_utils/roots.h"
xunchang316e9712019-04-12 16:22:15 -070038
39constexpr const char* CACHE_ROOT = "/cache";
40constexpr const char* DATA_ROOT = "/data";
41constexpr const char* METADATA_ROOT = "/metadata";
42
Eric Biggersfde69fb2022-03-10 22:13:39 +000043static bool EraseVolume(const char* volume, RecoveryUI* ui) {
xunchang316e9712019-04-12 16:22:15 -070044 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
xunchang316e9712019-04-12 16:22:15 -070045
xunchang316e9712019-04-12 16:22:15 -070046 std::vector<saved_log_file> log_files;
xunchang316e9712019-04-12 16:22:15 -070047 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070048 // 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();
xunchang316e9712019-04-12 16:22:15 -070051 }
52
53 ui->Print("Formatting %s...\n", volume);
54
Simon Shields0242aa22019-10-02 00:21:45 +100055 Volume* vol = volume_for_mount_point(volume);
Tim Zimmermann241ef2c2021-06-08 16:14:55 +020056 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 Shields0242aa22019-10-02 00:21:45 +100090 if (ensure_volume_unmounted(vol->blk_device) == -1) {
91 PLOG(ERROR) << "Failed to unmount volume!";
92 return false;
93 }
xunchang316e9712019-04-12 16:22:15 -070094
Eric Biggersfde69fb2022-03-10 22:13:39 +000095 int result = format_volume(volume);
xunchang316e9712019-04-12 16:22:15 -070096
97 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070098 RestoreLogFilesAfterFormat(log_files);
xunchang316e9712019-04-12 16:22:15 -070099 }
100
101 return (result == 0);
102}
103
104bool 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");
Tianjie32b4e722021-03-02 16:42:07 -0800116 ui->SetBackground(RecoveryUI::ERASING);
117 ui->SetProgressType(RecoveryUI::INDETERMINATE);
118
Eric Biggersfde69fb2022-03-10 22:13:39 +0000119 bool success = EraseVolume("/cache", ui);
xunchang316e9712019-04-12 16:22:15 -0700120 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
121 return success;
122}
123
Eric Biggersfde69fb2022-03-10 22:13:39 +0000124bool WipeData(Device* device) {
xunchang316e9712019-04-12 16:22:15 -0700125 RecoveryUI* ui = device->GetUI();
126 ui->Print("\n-- Wiping data...\n");
Tianjie32b4e722021-03-02 16:42:07 -0800127 ui->SetBackground(RecoveryUI::ERASING);
128 ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -0700129
130 if (!FinishPendingSnapshotMerges(device)) {
131 ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
132 return false;
133 }
134
xunchang316e9712019-04-12 16:22:15 -0700135 bool success = device->PreWipeData();
136 if (success) {
Eric Biggersfde69fb2022-03-10 22:13:39 +0000137 success &= EraseVolume(DATA_ROOT, ui);
xunchang316e9712019-04-12 16:22:15 -0700138 bool has_cache = volume_for_mount_point("/cache") != nullptr;
139 if (has_cache) {
Eric Biggersfde69fb2022-03-10 22:13:39 +0000140 success &= EraseVolume(CACHE_ROOT, ui);
xunchang316e9712019-04-12 16:22:15 -0700141 }
142 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
Eric Biggersfde69fb2022-03-10 22:13:39 +0000143 success &= EraseVolume(METADATA_ROOT, ui);
xunchang316e9712019-04-12 16:22:15 -0700144 }
145 }
146 if (success) {
147 success &= device->PostWipeData();
148 }
149 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
150 return success;
Tao Baoe3f09a72019-10-01 11:55:36 -0700151}
Michael Bestasb7e71ff2019-09-27 19:52:13 +0300152
153bool 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}