blob: 3d788ceab974e22fbdbf5b7b37012960459727ad [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 Zimmermann68dd30e2021-06-08 16:14:55 +020019#include <fcntl.h>
20#include <linux/fs.h>
xunchang316e9712019-04-12 16:22:15 -070021#include <stdio.h>
22#include <string.h>
Tim Zimmermann68dd30e2021-06-08 16:14:55 +020023#include <sys/ioctl.h>
xunchang316e9712019-04-12 16:22:15 -070024#include <sys/stat.h>
25
26#include <functional>
xunchang316e9712019-04-12 16:22:15 -070027#include <vector>
28
29#include <android-base/file.h>
30#include <android-base/logging.h>
31#include <android-base/stringprintf.h>
Michael Bestas9b1b4102019-09-27 19:52:13 +030032#include <fs_mgr/roots.h>
Tim Zimmermann68dd30e2021-06-08 16:14:55 +020033#include <libdm/dm.h>
xunchang316e9712019-04-12 16:22:15 -070034
David Anderson89d2d052019-10-15 13:22:20 -070035#include "install/snapshot_utils.h"
xunchang316e9712019-04-12 16:22:15 -070036#include "otautil/dirutil.h"
xunchang316e9712019-04-12 16:22:15 -070037#include "recovery_ui/ui.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070038#include "recovery_utils/logging.h"
39#include "recovery_utils/roots.h"
xunchang316e9712019-04-12 16:22:15 -070040
41constexpr const char* CACHE_ROOT = "/cache";
42constexpr const char* DATA_ROOT = "/data";
43constexpr const char* METADATA_ROOT = "/metadata";
44
xunchang316e9712019-04-12 16:22:15 -070045static 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
xunchang316e9712019-04-12 16:22:15 -070049 std::vector<saved_log_file> log_files;
xunchang316e9712019-04-12 16:22:15 -070050 if (is_cache) {
xunchang2239b9e2019-04-15 15:24:24 -070051 // 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();
xunchang316e9712019-04-12 16:22:15 -070054 }
55
56 ui->Print("Formatting %s...\n", volume);
57
Simon Shieldsdbc4d662019-10-02 00:21:45 +100058 Volume* vol = volume_for_mount_point(volume);
Tim Zimmermann68dd30e2021-06-08 16:14:55 +020059 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 Shieldsdbc4d662019-10-02 00:21:45 +100093 if (ensure_volume_unmounted(vol->blk_device) == -1) {
94 PLOG(ERROR) << "Failed to unmount volume!";
95 return false;
96 }
xunchang316e9712019-04-12 16:22:15 -070097
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) {
xunchang2239b9e2019-04-15 15:24:24 -0700122 RestoreLogFilesAfterFormat(log_files);
xunchang316e9712019-04-12 16:22:15 -0700123 }
124
125 return (result == 0);
126}
127
128bool 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");
Tianjie32b4e722021-03-02 16:42:07 -0800140 ui->SetBackground(RecoveryUI::ERASING);
141 ui->SetProgressType(RecoveryUI::INDETERMINATE);
142
xunchang316e9712019-04-12 16:22:15 -0700143 bool success = EraseVolume("/cache", ui, false);
144 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
145 return success;
146}
147
148bool WipeData(Device* device, bool convert_fbe) {
149 RecoveryUI* ui = device->GetUI();
150 ui->Print("\n-- Wiping data...\n");
Tianjie32b4e722021-03-02 16:42:07 -0800151 ui->SetBackground(RecoveryUI::ERASING);
152 ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -0700153
154 if (!FinishPendingSnapshotMerges(device)) {
155 ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
156 return false;
157 }
158
xunchang316e9712019-04-12 16:22:15 -0700159 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 Baoe3f09a72019-10-01 11:55:36 -0700175}
Michael Bestas9b1b4102019-09-27 19:52:13 +0300176
177bool 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}