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 <stdio.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 
23 #include <functional>
24 #include <vector>
25 
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28 #include <android-base/stringprintf.h>
29 
30 #include "install/snapshot_utils.h"
31 #include "otautil/dirutil.h"
32 #include "recovery_ui/ui.h"
33 #include "recovery_utils/logging.h"
34 #include "recovery_utils/roots.h"
35 
36 constexpr const char* CACHE_ROOT = "/cache";
37 constexpr const char* DATA_ROOT = "/data";
38 constexpr const char* METADATA_ROOT = "/metadata";
39 
EraseVolume(const char * volume,RecoveryUI * ui,bool convert_fbe)40 static bool EraseVolume(const char* volume, RecoveryUI* ui, bool convert_fbe) {
41   bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
42   bool is_data = (strcmp(volume, DATA_ROOT) == 0);
43 
44   std::vector<saved_log_file> log_files;
45   if (is_cache) {
46     // If we're reformatting /cache, we load any past logs (i.e. "/cache/recovery/last_*") and the
47     // current log ("/cache/recovery/log") into memory, so we can restore them after the reformat.
48     log_files = ReadLogFilesToMemory();
49   }
50 
51   ui->Print("Formatting %s...\n", volume);
52 
53   ensure_path_unmounted(volume);
54 
55   int result;
56   if (is_data && convert_fbe) {
57     constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe";
58     constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
59     // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not
60     // full disk encryption.
61     if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
62       PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR;
63       return false;
64     }
65     FILE* f = fopen(CONVERT_FBE_FILE, "wbe");
66     if (!f) {
67       PLOG(ERROR) << "Failed to convert to file encryption";
68       return false;
69     }
70     fclose(f);
71     result = format_volume(volume, CONVERT_FBE_DIR);
72     remove(CONVERT_FBE_FILE);
73     rmdir(CONVERT_FBE_DIR);
74   } else {
75     result = format_volume(volume);
76   }
77 
78   if (is_cache) {
79     RestoreLogFilesAfterFormat(log_files);
80   }
81 
82   return (result == 0);
83 }
84 
WipeCache(RecoveryUI * ui,const std::function<bool ()> & confirm_func)85 bool WipeCache(RecoveryUI* ui, const std::function<bool()>& confirm_func) {
86   bool has_cache = volume_for_mount_point("/cache") != nullptr;
87   if (!has_cache) {
88     ui->Print("No /cache partition found.\n");
89     return false;
90   }
91 
92   if (confirm_func && !confirm_func()) {
93     return false;
94   }
95 
96   ui->Print("\n-- Wiping cache...\n");
97   ui->SetBackground(RecoveryUI::ERASING);
98   ui->SetProgressType(RecoveryUI::INDETERMINATE);
99 
100   bool success = EraseVolume("/cache", ui, false);
101   ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
102   return success;
103 }
104 
WipeData(Device * device,bool convert_fbe)105 bool WipeData(Device* device, bool convert_fbe) {
106   RecoveryUI* ui = device->GetUI();
107   ui->Print("\n-- Wiping data...\n");
108   ui->SetBackground(RecoveryUI::ERASING);
109   ui->SetProgressType(RecoveryUI::INDETERMINATE);
110 
111   if (!FinishPendingSnapshotMerges(device)) {
112     ui->Print("Unable to check update status or complete merge, cannot wipe partitions.\n");
113     return false;
114   }
115 
116   bool success = device->PreWipeData();
117   if (success) {
118     success &= EraseVolume(DATA_ROOT, ui, convert_fbe);
119     bool has_cache = volume_for_mount_point("/cache") != nullptr;
120     if (has_cache) {
121       success &= EraseVolume(CACHE_ROOT, ui, false);
122     }
123     if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
124       success &= EraseVolume(METADATA_ROOT, ui, false);
125     }
126   }
127   if (success) {
128     success &= device->PostWipeData();
129   }
130   ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
131   return success;
132 }
133