blob: 845cc9567c271c5d5cfb803e1dca2b415d11e630 [file] [log] [blame]
xunchang388d2532019-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
xunchang388d2532019-04-12 16:22:15 -070019#include <stdio.h>
20#include <string.h>
21#include <sys/stat.h>
22
23#include <functional>
xunchang388d2532019-04-12 16:22:15 -070024#include <vector>
25
26#include <android-base/file.h>
27#include <android-base/logging.h>
28#include <android-base/stringprintf.h>
29
David Anderson89d2d052019-10-15 13:22:20 -070030#include "install/snapshot_utils.h"
xunchang388d2532019-04-12 16:22:15 -070031#include "otautil/dirutil.h"
xunchang388d2532019-04-12 16:22:15 -070032#include "recovery_ui/ui.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070033#include "recovery_utils/logging.h"
34#include "recovery_utils/roots.h"
xunchang388d2532019-04-12 16:22:15 -070035
36constexpr const char* CACHE_ROOT = "/cache";
37constexpr const char* DATA_ROOT = "/data";
38constexpr const char* METADATA_ROOT = "/metadata";
39
bigbiffd58ba182020-03-23 10:02:29 -040040static bool EraseVolume(const char* volume, bool convert_fbe) {
xunchang388d2532019-04-12 16:22:15 -070041 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
42 bool is_data = (strcmp(volume, DATA_ROOT) == 0);
43
bigbiffd58ba182020-03-23 10:02:29 -040044 // ui->SetBackground(RecoveryUI::ERASING);
45 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
xunchang388d2532019-04-12 16:22:15 -070046
47 std::vector<saved_log_file> log_files;
xunchang388d2532019-04-12 16:22:15 -070048 if (is_cache) {
xunchangcd780b42019-04-15 15:24:24 -070049 // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
50 // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
51 log_files = ReadLogFilesToMemory();
xunchang388d2532019-04-12 16:22:15 -070052 }
53
bigbiffd58ba182020-03-23 10:02:29 -040054 // ui->Print("Formatting %s...\n", volume);
xunchang388d2532019-04-12 16:22:15 -070055
56 ensure_path_unmounted(volume);
57
58 int result;
59 if (is_data && convert_fbe) {
60 constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe";
61 constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
62 // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not
63 // full disk encryption.
64 if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
65 PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR;
66 return false;
67 }
68 FILE* f = fopen(CONVERT_FBE_FILE, "wbe");
69 if (!f) {
70 PLOG(ERROR) << "Failed to convert to file encryption";
71 return false;
72 }
73 fclose(f);
74 result = format_volume(volume, CONVERT_FBE_DIR);
75 remove(CONVERT_FBE_FILE);
76 rmdir(CONVERT_FBE_DIR);
77 } else {
78 result = format_volume(volume);
79 }
80
81 if (is_cache) {
xunchangcd780b42019-04-15 15:24:24 -070082 RestoreLogFilesAfterFormat(log_files);
xunchang388d2532019-04-12 16:22:15 -070083 }
84
85 return (result == 0);
86}
87
bigbiffd58ba182020-03-23 10:02:29 -040088bool WipeCache(const std::function<bool()>& confirm_func) {
xunchang388d2532019-04-12 16:22:15 -070089 bool has_cache = volume_for_mount_point("/cache") != nullptr;
90 if (!has_cache) {
bigbiffd58ba182020-03-23 10:02:29 -040091 // ui->Print("No /cache partition found.\n");
xunchang388d2532019-04-12 16:22:15 -070092 return false;
93 }
94
95 if (confirm_func && !confirm_func()) {
96 return false;
97 }
98
bigbiffd58ba182020-03-23 10:02:29 -040099 // ui->Print("\n-- Wiping cache...\n");
bigbiff75b94232021-11-11 17:34:34 -0500100 // ui->SetBackground(RecoveryUI::ERASING);
101 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
Tianjie32b4e722021-03-02 16:42:07 -0800102
xunchang316e9712019-04-12 16:22:15 -0700103 bool success = EraseVolume("/cache", ui, false);
104 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
xunchang388d2532019-04-12 16:22:15 -0700105 return success;
106}
107
108bool WipeData(Device* device, bool convert_fbe) {
bigbiffd58ba182020-03-23 10:02:29 -0400109 // RecoveryUI* ui = device->GetUI();
110 // ui->Print("\n-- Wiping data...\n");
bigbiff75b94232021-11-11 17:34:34 -0500111 // ui->SetBackground(RecoveryUI::ERASING);
112 // ui->SetProgressType(RecoveryUI::INDETERMINATE);
David Anderson89d2d052019-10-15 13:22:20 -0700113
bigbiff673c7ae2020-12-02 19:44:56 -0500114 // if (!FinishPendingSnapshotMerges(device)) {
115 // ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
116 // return false;
117 // }
David Anderson89d2d052019-10-15 13:22:20 -0700118
xunchang388d2532019-04-12 16:22:15 -0700119 bool success = device->PreWipeData();
120 if (success) {
bigbiffd58ba182020-03-23 10:02:29 -0400121 success &= EraseVolume(DATA_ROOT, convert_fbe);
xunchang388d2532019-04-12 16:22:15 -0700122 bool has_cache = volume_for_mount_point("/cache") != nullptr;
123 if (has_cache) {
bigbiffd58ba182020-03-23 10:02:29 -0400124 success &= EraseVolume(CACHE_ROOT, false);
xunchang388d2532019-04-12 16:22:15 -0700125 }
126 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
bigbiffd58ba182020-03-23 10:02:29 -0400127 success &= EraseVolume(METADATA_ROOT, false);
xunchang388d2532019-04-12 16:22:15 -0700128 }
129 }
130 if (success) {
131 success &= device->PostWipeData();
132 }
bigbiffd58ba182020-03-23 10:02:29 -0400133 // ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
xunchang388d2532019-04-12 16:22:15 -0700134 return success;
Tao Baoe3f09a72019-10-01 11:55:36 -0700135}