• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <string.h>
20 
21 #include <functional>
22 #include <vector>
23 
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <android-base/stringprintf.h>
27 
28 #include "bootloader_message/bootloader_message.h"
29 #include "install/snapshot_utils.h"
30 #include "otautil/dirutil.h"
31 #include "recovery_ui/ui.h"
32 #include "recovery_utils/logging.h"
33 #include "recovery_utils/roots.h"
34 
35 constexpr const char* CACHE_ROOT = "/cache";
36 constexpr const char* DATA_ROOT = "/data";
37 constexpr const char* METADATA_ROOT = "/metadata";
38 
EraseVolume(const char * volume,RecoveryUI * ui)39 static bool EraseVolume(const char* volume, RecoveryUI* ui) {
40   bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
41 
42   std::vector<saved_log_file> log_files;
43   if (is_cache) {
44     // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
45     // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
46     log_files = ReadLogFilesToMemory();
47   }
48 
49   ui->Print("Formatting %s...\n", volume);
50 
51   ensure_path_unmounted(volume);
52 
53   int result = format_volume(volume);
54 
55   if (is_cache) {
56     RestoreLogFilesAfterFormat(log_files);
57   }
58 
59   return (result == 0);
60 }
61 
WipeCache(RecoveryUI * ui,const std::function<bool ()> & confirm_func)62 bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func) {
63   bool has_cache = volume_for_mount_point("/cache") != nullptr;
64   if (!has_cache) {
65     ui->Print("No /cache partition found.\n");
66     return false;
67   }
68 
69   if (confirm_func && !confirm_func()) {
70     return false;
71   }
72 
73   ui->Print("\n-- Wiping cache...\n");
74   ui->SetBackground(RecoveryUI::ERASING);
75   ui->SetProgressType(RecoveryUI::INDETERMINATE);
76 
77   bool success = EraseVolume("/cache", ui);
78   ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
79   return success;
80 }
81 
WipeData(Device * device)82 bool WipeData(Device* device) {
83   RecoveryUI* ui = device->GetUI();
84   ui->Print("\n-- Wiping data...\n");
85   ui->SetBackground(RecoveryUI::ERASING);
86   ui->SetProgressType(RecoveryUI::INDETERMINATE);
87 
88   if (!FinishPendingSnapshotMerges(device)) {
89     ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
90     return false;
91   }
92 
93   bool success = device->PreWipeData();
94   if (success) {
95     success &= EraseVolume(DATA_ROOT, ui);
96     bool has_cache = volume_for_mount_point("/cache") != nullptr;
97     if (has_cache) {
98       success &= EraseVolume(CACHE_ROOT, ui);
99     }
100     if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
101       success &= EraseVolume(METADATA_ROOT, ui);
102     }
103   }
104   ui->Print("Resetting memtag message...\n");
105   std::string err;
106   if (!WriteMiscMemtagMessage({}, &err)) {
107     ui->Print("Failed to reset memtag message: %s\n", err.c_str());
108     success = false;
109   }
110   if (success) {
111     success &= device->PostWipeData();
112   }
113   ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
114   return success;
115 }
116