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