blob: 915c87b4591b36bb47b40f5cbc3b08fcb4e8ea13 [file] [log] [blame]
Tao Bao7f19d102019-04-26 22:56:56 -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_device.h"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <linux/fs.h>
22#include <stdint.h>
23#include <sys/ioctl.h>
24
25#include <map>
26#include <memory>
27#include <string>
28#include <vector>
29
30#include <android-base/file.h>
31#include <android-base/logging.h>
32#include <android-base/strings.h>
33#include <android-base/unique_fd.h>
34#include <ziparchive/zip_archive.h>
35
36#include "bootloader_message/bootloader_message.h"
37#include "install/install.h"
38#include "install/package.h"
39#include "recovery_ui/device.h"
40#include "recovery_ui/ui.h"
41
42std::vector<std::string> GetWipePartitionList(Package* wipe_package) {
43 ZipArchiveHandle zip = wipe_package->GetZipArchiveHandle();
44 if (!zip) {
45 LOG(ERROR) << "Failed to get ZipArchiveHandle";
46 return {};
47 }
48
49 constexpr char RECOVERY_WIPE_ENTRY_NAME[] = "recovery.wipe";
50
51 std::string partition_list_content;
Kelvin Zhang4f811302020-09-16 14:06:12 -040052 ZipEntry64 entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -070053 if (FindEntry(zip, RECOVERY_WIPE_ENTRY_NAME, &entry) == 0) {
Kelvin Zhangd1ba38f2020-09-17 11:32:29 -040054 auto length = entry.uncompressed_length;
55 if (length > std::numeric_limits<size_t>::max()) {
56 LOG(ERROR) << "Failed to extract " << RECOVERY_WIPE_ENTRY_NAME
57 << " because's uncompressed size exceeds size of address space. " << length;
58 return {};
59 }
Tao Bao7f19d102019-04-26 22:56:56 -070060 partition_list_content = std::string(length, '\0');
61 if (auto err = ExtractToMemory(
62 zip, &entry, reinterpret_cast<uint8_t*>(partition_list_content.data()), length);
63 err != 0) {
64 LOG(ERROR) << "Failed to extract " << RECOVERY_WIPE_ENTRY_NAME << ": "
65 << ErrorCodeString(err);
66 return {};
67 }
68 } else {
69 LOG(INFO) << "Failed to find " << RECOVERY_WIPE_ENTRY_NAME
70 << ", falling back to use the partition list on device.";
71
72 constexpr char RECOVERY_WIPE_ON_DEVICE[] = "/etc/recovery.wipe";
73 if (!android::base::ReadFileToString(RECOVERY_WIPE_ON_DEVICE, &partition_list_content)) {
74 PLOG(ERROR) << "failed to read \"" << RECOVERY_WIPE_ON_DEVICE << "\"";
75 return {};
76 }
77 }
78
79 std::vector<std::string> result;
80 auto lines = android::base::Split(partition_list_content, "\n");
81 for (const auto& line : lines) {
82 auto partition = android::base::Trim(line);
83 // Ignore '#' comment or empty lines.
84 if (android::base::StartsWith(partition, "#") || partition.empty()) {
85 continue;
86 }
87 result.push_back(line);
88 }
89
90 return result;
91}
92
93// Secure-wipes a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with
94// BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT.
95static bool SecureWipePartition(const std::string& partition) {
96 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY)));
97 if (fd == -1) {
98 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
99 return false;
100 }
101
102 uint64_t range[2] = { 0, 0 };
103 if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) {
104 PLOG(ERROR) << "Failed to get partition size";
105 return false;
106 }
107 LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1];
108
109 LOG(INFO) << " Trying BLKSECDISCARD...";
110 if (ioctl(fd, BLKSECDISCARD, &range) == -1) {
111 PLOG(WARNING) << " Failed";
112
113 // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT.
114 unsigned int zeroes;
115 if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) {
116 LOG(INFO) << " Trying BLKDISCARD...";
117 if (ioctl(fd, BLKDISCARD, &range) == -1) {
118 PLOG(ERROR) << " Failed";
119 return false;
120 }
121 } else {
122 LOG(INFO) << " Trying BLKZEROOUT...";
123 if (ioctl(fd, BLKZEROOUT, &range) == -1) {
124 PLOG(ERROR) << " Failed";
125 return false;
126 }
127 }
128 }
129
130 LOG(INFO) << " Done";
131 return true;
132}
133
134static std::unique_ptr<Package> ReadWipePackage(size_t wipe_package_size) {
135 if (wipe_package_size == 0) {
136 LOG(ERROR) << "wipe_package_size is zero";
137 return nullptr;
138 }
139
140 std::string wipe_package;
141 if (std::string err_str; !read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
142 PLOG(ERROR) << "Failed to read wipe package" << err_str;
143 return nullptr;
144 }
145
146 return Package::CreateMemoryPackage(
147 std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr);
148}
149
150// Checks if the wipe package matches expectation. If the check passes, reads the list of
151// partitions to wipe from the package. Checks include
152// 1. verify the package.
153// 2. check metadata (ota-type, pre-device and serial number if having one).
154static bool CheckWipePackage(Package* wipe_package, RecoveryUI* ui) {
155 if (!verify_package(wipe_package, ui)) {
156 LOG(ERROR) << "Failed to verify package";
157 return false;
158 }
159
160 ZipArchiveHandle zip = wipe_package->GetZipArchiveHandle();
161 if (!zip) {
162 LOG(ERROR) << "Failed to get ZipArchiveHandle";
163 return false;
164 }
165
166 std::map<std::string, std::string> metadata;
167 if (!ReadMetadataFromPackage(zip, &metadata)) {
168 LOG(ERROR) << "Failed to parse metadata in the zip file";
169 return false;
170 }
171
Tao Bao36c72762019-04-30 00:25:41 -0700172 return CheckPackageMetadata(metadata, OtaType::BRICK);
Tao Bao7f19d102019-04-26 22:56:56 -0700173}
174
175bool WipeAbDevice(Device* device, size_t wipe_package_size) {
176 auto ui = device->GetUI();
177 ui->SetBackground(RecoveryUI::ERASING);
178 ui->SetProgressType(RecoveryUI::INDETERMINATE);
179
180 auto wipe_package = ReadWipePackage(wipe_package_size);
181 if (!wipe_package) {
182 LOG(ERROR) << "Failed to open wipe package";
183 return false;
184 }
185
186 if (!CheckWipePackage(wipe_package.get(), ui)) {
187 LOG(ERROR) << "Failed to verify wipe package";
188 return false;
189 }
190
191 auto partition_list = GetWipePartitionList(wipe_package.get());
192 if (partition_list.empty()) {
193 LOG(ERROR) << "Empty wipe ab partition list";
194 return false;
195 }
196
197 for (const auto& partition : partition_list) {
198 // Proceed anyway even if it fails to wipe some partition.
199 SecureWipePartition(partition);
200 }
201 return true;
202}