blob: fa079ea2d9e47bdae34c9ef1c7830f30413f8fb2 [file] [log] [blame]
Ethan Yonker941a8992016-12-05 09:04:30 -06001/*
2 * Copyright (C) 2007 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 <stdlib.h>
18#include <string>
19#include <vector>
20
21#ifdef AB_OTA_UPDATER
Ethan Yonkerd9918b72017-09-15 08:17:42 -050022#include <inttypes.h>
Ethan Yonker941a8992016-12-05 09:04:30 -060023#include <map>
24#include <android-base/parseint.h>
25#include <android-base/stringprintf.h>
26#include <android-base/strings.h>
27#endif
28#include <cutils/properties.h>
29
30#include "common.h"
31#include "installcommand.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050032#include "zipwrap.hpp"
33#ifndef USE_MINZIP
34#include <ziparchive/zip_archive.h>
35#include <vintf/VintfObjectRecovery.h>
36#endif
Ethan Yonker941a8992016-12-05 09:04:30 -060037#ifdef USE_OLD_VERIFIER
38#include "verifier24/verifier.h"
39#else
40#include "verifier.h"
41#endif
42
43#ifdef AB_OTA_UPDATER
44
45static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
46static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
47static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
48
49// This function parses and returns the build.version.incremental
50static int parse_build_number(std::string str) {
51 size_t pos = str.find("=");
52 if (pos != std::string::npos) {
53 std::string num_string = android::base::Trim(str.substr(pos+1));
54 int build_number;
55 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
56 return build_number;
57 }
58 }
59
60 printf("Failed to parse build number in %s.\n", str.c_str());
61 return -1;
62}
63
Ethan Yonker8373cfe2017-09-08 06:50:54 -050064bool read_metadata_from_package(ZipWrap* zip, std::string* meta_data) {
65 long size = zip->GetUncompressedSize(METADATA_PATH);
66 if (size <= 0)
67 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -060068
Ethan Yonker8373cfe2017-09-08 06:50:54 -050069 meta_data->resize(size, '\0');
70 if (!zip->ExtractToBuffer(METADATA_PATH, reinterpret_cast<uint8_t*>(&(*meta_data)[0]))) {
Ethan Yonker941a8992016-12-05 09:04:30 -060071 printf("Failed to read metadata in update package.\n");
72 return false;
73 }
74 return true;
75}
76
77// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
Ethan Yonker58f21322018-08-24 11:17:36 -050078void read_source_target_build(ZipWrap* zip/*, std::vector<std::string>& log_buffer*/) {
Ethan Yonker941a8992016-12-05 09:04:30 -060079 std::string meta_data;
80 if (!read_metadata_from_package(zip, &meta_data)) {
81 return;
82 }
83 // Examples of the pre-build and post-build strings in metadata:
84 // pre-build-incremental=2943039
85 // post-build-incremental=2951741
86 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
87 for (const std::string& line : lines) {
88 std::string str = android::base::Trim(line);
89 if (android::base::StartsWith(str, "pre-build-incremental")){
90 int source_build = parse_build_number(str);
91 if (source_build != -1) {
Ethan Yonker58f21322018-08-24 11:17:36 -050092 printf("source_build: %d\n", source_build);
93 /*log_buffer.push_back(android::base::StringPrintf("source_build: %d",
94 source_build));*/
Ethan Yonker941a8992016-12-05 09:04:30 -060095 }
96 } else if (android::base::StartsWith(str, "post-build-incremental")) {
97 int target_build = parse_build_number(str);
98 if (target_build != -1) {
Ethan Yonker58f21322018-08-24 11:17:36 -050099 printf("target_build: %d\n", target_build);
100 /*log_buffer.push_back(android::base::StringPrintf("target_build: %d",
101 target_build));*/
Ethan Yonker941a8992016-12-05 09:04:30 -0600102 }
103 }
104 }
105}
106
107// Parses the metadata of the OTA package in |zip| and checks whether we are
108// allowed to accept this A/B package. Downgrading is not allowed unless
109// explicitly enabled in the package and only for incremental packages.
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500110static int check_newer_ab_build(ZipWrap* zip)
Ethan Yonker941a8992016-12-05 09:04:30 -0600111{
112 std::string metadata_str;
113 if (!read_metadata_from_package(zip, &metadata_str)) {
114 return INSTALL_CORRUPT;
115 }
116 std::map<std::string, std::string> metadata;
117 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
118 size_t eq = line.find('=');
119 if (eq != std::string::npos) {
120 metadata[line.substr(0, eq)] = line.substr(eq + 1);
121 }
122 }
123 char value[PROPERTY_VALUE_MAX];
124
125 property_get("ro.product.device", value, "");
126 const std::string& pkg_device = metadata["pre-device"];
Hernán Castañón990168b2019-02-27 12:28:49 +0100127
128 std::vector<std::string> assertResults = android::base::Split(pkg_device, ",");
129
130 bool deviceExists = false;
131
132 for(const std::string& deviceAssert : assertResults)
133 {
134 std::string assertName = android::base::Trim(deviceAssert);
135 if (assertName == value && !assertName.empty()) {
136 deviceExists = true;
137 break;
138 }
139 }
140
141 if (!deviceExists) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600142 printf("Package is for product %s but expected %s\n",
Hernán Castañón990168b2019-02-27 12:28:49 +0100143 pkg_device.c_str(), value);
Ethan Yonker941a8992016-12-05 09:04:30 -0600144 return INSTALL_ERROR;
145 }
146
147 // We allow the package to not have any serialno, but if it has a non-empty
148 // value it should match.
149 property_get("ro.serialno", value, "");
150 const std::string& pkg_serial_no = metadata["serialno"];
151 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
152 printf("Package is for serial %s\n", pkg_serial_no.c_str());
153 return INSTALL_ERROR;
154 }
155
156 if (metadata["ota-type"] != "AB") {
157 printf("Package is not A/B\n");
158 return INSTALL_ERROR;
159 }
160
161 // Incremental updates should match the current build.
162 property_get("ro.build.version.incremental", value, "");
163 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
164 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
165 printf("Package is for source build %s but expected %s\n",
166 pkg_pre_build.c_str(), value);
167 return INSTALL_ERROR;
168 }
169 property_get("ro.build.fingerprint", value, "");
170 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
171 if (!pkg_pre_build_fingerprint.empty() &&
172 pkg_pre_build_fingerprint != value) {
173 printf("Package is for source build %s but expected %s\n",
174 pkg_pre_build_fingerprint.c_str(), value);
175 return INSTALL_ERROR;
176 }
177
Ethan Yonker941a8992016-12-05 09:04:30 -0600178 return 0;
179}
180
181int
Ethan Yonker58f21322018-08-24 11:17:36 -0500182abupdate_binary_command(const char* path, ZipWrap* zip, int retry_count __unused,
Ethan Yonker941a8992016-12-05 09:04:30 -0600183 int status_fd, std::vector<std::string>* cmd)
184{
Ethan Yonker58f21322018-08-24 11:17:36 -0500185 read_source_target_build(zip);
Ethan Yonker941a8992016-12-05 09:04:30 -0600186 int ret = check_newer_ab_build(zip);
187 if (ret) {
188 return ret;
189 }
190
191 // For A/B updates we extract the payload properties to a buffer and obtain
192 // the RAW payload offset in the zip file.
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500193 if (!zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600194 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
195 return INSTALL_CORRUPT;
196 }
197 std::vector<unsigned char> payload_properties(
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500198 zip->GetUncompressedSize(AB_OTA_PAYLOAD_PROPERTIES));
199 if (!zip->ExtractToBuffer(AB_OTA_PAYLOAD_PROPERTIES, payload_properties.data())) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600200 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
201 return INSTALL_CORRUPT;
202 }
203
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500204 if (!zip->EntryExists(AB_OTA_PAYLOAD)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600205 printf("Can't find %s\n", AB_OTA_PAYLOAD);
206 return INSTALL_CORRUPT;
207 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500208 long payload_offset = zip->GetEntryOffset(AB_OTA_PAYLOAD);
Ethan Yonker941a8992016-12-05 09:04:30 -0600209 *cmd = {
210 "/sbin/update_engine_sideload",
211 android::base::StringPrintf("--payload=file://%s", path),
212 android::base::StringPrintf("--offset=%ld", payload_offset),
213 "--headers=" + std::string(payload_properties.begin(),
214 payload_properties.end()),
215 android::base::StringPrintf("--status_fd=%d", status_fd),
216 };
217 return INSTALL_SUCCESS;
218}
219
220#else
221
Ethan Yonker58f21322018-08-24 11:17:36 -0500222void read_source_target_build(ZipWrap* zip __unused /*, std::vector<std::string>& log_buffer*/) {return;}
223
Ethan Yonker941a8992016-12-05 09:04:30 -0600224int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500225abupdate_binary_command(__unused const char* path, __unused ZipWrap* zip, __unused int retry_count,
226 __unused int status_fd, __unused std::vector<std::string>* cmd)
Ethan Yonker941a8992016-12-05 09:04:30 -0600227{
228 printf("No support for AB OTA zips included\n");
229 return INSTALL_CORRUPT;
230}
231
232#endif
233
234int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500235update_binary_command(const char* path, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600236 int status_fd, std::vector<std::string>* cmd)
237{
238 char charfd[16];
239 sprintf(charfd, "%i", status_fd);
240 cmd->push_back(TMP_UPDATER_BINARY_PATH);
241 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
242 cmd->push_back(charfd);
243 cmd->push_back(path);
244 /**cmd = {
245 TMP_UPDATER_BINARY_PATH,
246 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
247 charfd,
248 path,
249 };*/
250 if (retry_count > 0)
251 cmd->push_back("retry");
252 return 0;
253}
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500254
255#ifdef USE_MINZIP
256bool verify_package_compatibility(ZipWrap *package_zip) {
257 if (package_zip->EntryExists("compatibility.zip"))
258 printf("Cannot verify treble package compatibility, must build TWRP in Oreo tree or higher.\n");
259 return true;
260}
261#else
262// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
263// entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA
264// package.
265bool verify_package_compatibility(ZipWrap *zw) {
266 ZipArchiveHandle package_zip = zw->GetZipArchiveHandle();
267 printf("Verifying package compatibility...\n");
268
269 static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip";
270 ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY);
271 ZipEntry compatibility_entry;
272 if (FindEntry(package_zip, compatibility_entry_name, &compatibility_entry) != 0) {
273 printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY);
274 return true;
275 }
276
277 std::string zip_content(compatibility_entry.uncompressed_length, '\0');
278 int32_t ret;
279 if ((ret = ExtractToMemory(package_zip, &compatibility_entry,
280 reinterpret_cast<uint8_t*>(&zip_content[0]),
281 compatibility_entry.uncompressed_length)) != 0) {
282 printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret));
283 return false;
284 }
285
286 ZipArchiveHandle zip_handle;
287 ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())),
288 zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle);
289 if (ret != 0) {
290 printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret));
291 return false;
292 }
293
294 // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents.
295 void* cookie;
296 ret = StartIteration(zip_handle, &cookie, nullptr, nullptr);
297 if (ret != 0) {
298 printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret));
299 CloseArchive(zip_handle);
300 return false;
301 }
302 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
303
304 std::vector<std::string> compatibility_info;
305 ZipEntry info_entry;
306 ZipString info_name;
307 while (Next(cookie, &info_entry, &info_name) == 0) {
308 std::string content(info_entry.uncompressed_length, '\0');
309 int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]),
310 info_entry.uncompressed_length);
311 if (ret != 0) {
312 printf("Failed to read %s: %s\n", info_name.name, ErrorCodeString(ret));
313 CloseArchive(zip_handle);
314 return false;
315 }
316 compatibility_info.emplace_back(std::move(content));
317 }
318 CloseArchive(zip_handle);
319
Ethan Yonker58f21322018-08-24 11:17:36 -0500320 // VintfObjectRecovery::CheckCompatibility returns zero on success. TODO THIS CAUSES A WEIRD COMPILE ERROR
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500321 std::string err;
322 int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err);
323 if (result == 0) {
324 return true;
325 }
326
327 printf("Failed to verify package compatibility (result %i): %s\n", result, err.c_str());
328 return false;
329}
330#endif