blob: ed672073ea8a337fb176d5bde585a9491955c22c [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
30#include "otautil/dirutil.h"
31#include "otautil/logging.h"
32#include "otautil/roots.h"
33#include "recovery_ui/ui.h"
34
35constexpr const char* CACHE_ROOT = "/cache";
36constexpr const char* DATA_ROOT = "/data";
37constexpr const char* METADATA_ROOT = "/metadata";
38
xunchang388d2532019-04-12 16:22:15 -070039static bool EraseVolume(const char* volume, RecoveryUI* ui, bool convert_fbe) {
40 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
41 bool is_data = (strcmp(volume, DATA_ROOT) == 0);
42
43 ui->SetBackground(RecoveryUI::ERASING);
44 ui->SetProgressType(RecoveryUI::INDETERMINATE);
45
46 std::vector<saved_log_file> log_files;
xunchang388d2532019-04-12 16:22:15 -070047 if (is_cache) {
xunchangcd780b42019-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();
xunchang388d2532019-04-12 16:22:15 -070051 }
52
53 ui->Print("Formatting %s...\n", volume);
54
Simon Shields9fe59e42019-10-02 00:21:45 +100055 Volume* vol = volume_for_mount_point(volume);
56 if (ensure_volume_unmounted(vol->blk_device) == -1) {
57 PLOG(ERROR) << "Failed to unmount volume!";
58 return false;
59 }
xunchang388d2532019-04-12 16:22:15 -070060
61 int result;
62 if (is_data && convert_fbe) {
63 constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe";
64 constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
65 // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not
66 // full disk encryption.
67 if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
68 PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR;
69 return false;
70 }
71 FILE* f = fopen(CONVERT_FBE_FILE, "wbe");
72 if (!f) {
73 PLOG(ERROR) << "Failed to convert to file encryption";
74 return false;
75 }
76 fclose(f);
77 result = format_volume(volume, CONVERT_FBE_DIR);
78 remove(CONVERT_FBE_FILE);
79 rmdir(CONVERT_FBE_DIR);
80 } else {
81 result = format_volume(volume);
82 }
83
84 if (is_cache) {
xunchangcd780b42019-04-15 15:24:24 -070085 RestoreLogFilesAfterFormat(log_files);
xunchang388d2532019-04-12 16:22:15 -070086 }
87
88 return (result == 0);
89}
90
91bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func) {
92 bool has_cache = volume_for_mount_point("/cache") != nullptr;
93 if (!has_cache) {
94 ui->Print("No /cache partition found.\n");
95 return false;
96 }
97
98 if (confirm_func && !confirm_func()) {
99 return false;
100 }
101
102 ui->Print("\n-- Wiping cache...\n");
103 bool success = EraseVolume("/cache", ui, false);
104 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
105 return success;
106}
107
108bool WipeData(Device* device, bool convert_fbe) {
109 RecoveryUI* ui = device->GetUI();
110 ui->Print("\n-- Wiping data...\n");
111 bool success = device->PreWipeData();
112 if (success) {
113 success &= EraseVolume(DATA_ROOT, ui, convert_fbe);
114 bool has_cache = volume_for_mount_point("/cache") != nullptr;
115 if (has_cache) {
116 success &= EraseVolume(CACHE_ROOT, ui, false);
117 }
118 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
119 success &= EraseVolume(METADATA_ROOT, ui, false);
120 }
121 }
122 if (success) {
123 success &= device->PostWipeData();
124 }
125 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
126 return success;
127}