blob: 0f896c43bc14da92304aa2102a6e3cd7d63eb6f2 [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) {
Tao Bao7f19d102019-04-26 22:56:56 -070054 uint32_t length = entry.uncompressed_length;
55 partition_list_content = std::string(length, '\0');
56 if (auto err = ExtractToMemory(
57 zip, &entry, reinterpret_cast<uint8_t*>(partition_list_content.data()), length);
58 err != 0) {
59 LOG(ERROR) << "Failed to extract " << RECOVERY_WIPE_ENTRY_NAME << ": "
60 << ErrorCodeString(err);
61 return {};
62 }
63 } else {
64 LOG(INFO) << "Failed to find " << RECOVERY_WIPE_ENTRY_NAME
65 << ", falling back to use the partition list on device.";
66
67 constexpr char RECOVERY_WIPE_ON_DEVICE[] = "/etc/recovery.wipe";
68 if (!android::base::ReadFileToString(RECOVERY_WIPE_ON_DEVICE, &partition_list_content)) {
69 PLOG(ERROR) << "failed to read \"" << RECOVERY_WIPE_ON_DEVICE << "\"";
70 return {};
71 }
72 }
73
74 std::vector<std::string> result;
75 auto lines = android::base::Split(partition_list_content, "\n");
76 for (const auto& line : lines) {
77 auto partition = android::base::Trim(line);
78 // Ignore '#' comment or empty lines.
79 if (android::base::StartsWith(partition, "#") || partition.empty()) {
80 continue;
81 }
82 result.push_back(line);
83 }
84
85 return result;
86}
87
88// Secure-wipes a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with
89// BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT.
90static bool SecureWipePartition(const std::string& partition) {
91 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY)));
92 if (fd == -1) {
93 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
94 return false;
95 }
96
97 uint64_t range[2] = { 0, 0 };
98 if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) {
99 PLOG(ERROR) << "Failed to get partition size";
100 return false;
101 }
102 LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1];
103
104 LOG(INFO) << " Trying BLKSECDISCARD...";
105 if (ioctl(fd, BLKSECDISCARD, &range) == -1) {
106 PLOG(WARNING) << " Failed";
107
108 // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT.
109 unsigned int zeroes;
110 if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) {
111 LOG(INFO) << " Trying BLKDISCARD...";
112 if (ioctl(fd, BLKDISCARD, &range) == -1) {
113 PLOG(ERROR) << " Failed";
114 return false;
115 }
116 } else {
117 LOG(INFO) << " Trying BLKZEROOUT...";
118 if (ioctl(fd, BLKZEROOUT, &range) == -1) {
119 PLOG(ERROR) << " Failed";
120 return false;
121 }
122 }
123 }
124
125 LOG(INFO) << " Done";
126 return true;
127}
128
129static std::unique_ptr<Package> ReadWipePackage(size_t wipe_package_size) {
130 if (wipe_package_size == 0) {
131 LOG(ERROR) << "wipe_package_size is zero";
132 return nullptr;
133 }
134
135 std::string wipe_package;
136 if (std::string err_str; !read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
137 PLOG(ERROR) << "Failed to read wipe package" << err_str;
138 return nullptr;
139 }
140
141 return Package::CreateMemoryPackage(
142 std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr);
143}
144
145// Checks if the wipe package matches expectation. If the check passes, reads the list of
146// partitions to wipe from the package. Checks include
147// 1. verify the package.
148// 2. check metadata (ota-type, pre-device and serial number if having one).
149static bool CheckWipePackage(Package* wipe_package, RecoveryUI* ui) {
150 if (!verify_package(wipe_package, ui)) {
151 LOG(ERROR) << "Failed to verify package";
152 return false;
153 }
154
155 ZipArchiveHandle zip = wipe_package->GetZipArchiveHandle();
156 if (!zip) {
157 LOG(ERROR) << "Failed to get ZipArchiveHandle";
158 return false;
159 }
160
161 std::map<std::string, std::string> metadata;
162 if (!ReadMetadataFromPackage(zip, &metadata)) {
163 LOG(ERROR) << "Failed to parse metadata in the zip file";
164 return false;
165 }
166
Tao Bao36c72762019-04-30 00:25:41 -0700167 return CheckPackageMetadata(metadata, OtaType::BRICK);
Tao Bao7f19d102019-04-26 22:56:56 -0700168}
169
170bool WipeAbDevice(Device* device, size_t wipe_package_size) {
171 auto ui = device->GetUI();
172 ui->SetBackground(RecoveryUI::ERASING);
173 ui->SetProgressType(RecoveryUI::INDETERMINATE);
174
175 auto wipe_package = ReadWipePackage(wipe_package_size);
176 if (!wipe_package) {
177 LOG(ERROR) << "Failed to open wipe package";
178 return false;
179 }
180
181 if (!CheckWipePackage(wipe_package.get(), ui)) {
182 LOG(ERROR) << "Failed to verify wipe package";
183 return false;
184 }
185
186 auto partition_list = GetWipePartitionList(wipe_package.get());
187 if (partition_list.empty()) {
188 LOG(ERROR) << "Empty wipe ab partition list";
189 return false;
190 }
191
192 for (const auto& partition : partition_list) {
193 // Proceed anyway even if it fails to wipe some partition.
194 SecureWipePartition(partition);
195 }
196 return true;
197}