blob: 5e28472708e8e576b4ada338e7999924031c490f [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
bigbiffd58ba182020-03-23 10:02:29 -040040#include "install/install.h"
Ethan Yonker941a8992016-12-05 09:04:30 -060041#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];
mauronofrioc79f86e2020-05-18 16:08:12 -0400124 char propmodel[PROPERTY_VALUE_MAX];
125 char propname[PROPERTY_VALUE_MAX];
Ethan Yonker941a8992016-12-05 09:04:30 -0600126
127 property_get("ro.product.device", value, "");
mauronofrioc79f86e2020-05-18 16:08:12 -0400128 property_get("ro.product.model", propmodel, "");
129 property_get("ro.product.name", propname, "");
Ethan Yonker941a8992016-12-05 09:04:30 -0600130 const std::string& pkg_device = metadata["pre-device"];
mauronofrioc79f86e2020-05-18 16:08:12 -0400131
132 std::vector<std::string> assertResults = android::base::Split(pkg_device, ",");
133
134 bool deviceExists = false;
135
136 for(const std::string& deviceAssert : assertResults)
137 {
138 std::string assertName = android::base::Trim(deviceAssert);
139 if ((assertName == value || assertName == propmodel || assertName == propname ) && !assertName.empty()) {
140 deviceExists = true;
141 break;
142 }
143 }
144
145 if (!deviceExists) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600146 printf("Package is for product %s but expected %s\n",
mauronofrioc79f86e2020-05-18 16:08:12 -0400147 pkg_device.c_str(), value);
Ethan Yonker941a8992016-12-05 09:04:30 -0600148 return INSTALL_ERROR;
149 }
150
151 // We allow the package to not have any serialno, but if it has a non-empty
152 // value it should match.
153 property_get("ro.serialno", value, "");
154 const std::string& pkg_serial_no = metadata["serialno"];
155 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
156 printf("Package is for serial %s\n", pkg_serial_no.c_str());
157 return INSTALL_ERROR;
158 }
159
160 if (metadata["ota-type"] != "AB") {
161 printf("Package is not A/B\n");
162 return INSTALL_ERROR;
163 }
164
165 // Incremental updates should match the current build.
166 property_get("ro.build.version.incremental", value, "");
167 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
168 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
169 printf("Package is for source build %s but expected %s\n",
170 pkg_pre_build.c_str(), value);
171 return INSTALL_ERROR;
172 }
173 property_get("ro.build.fingerprint", value, "");
174 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
175 if (!pkg_pre_build_fingerprint.empty() &&
176 pkg_pre_build_fingerprint != value) {
177 printf("Package is for source build %s but expected %s\n",
178 pkg_pre_build_fingerprint.c_str(), value);
179 return INSTALL_ERROR;
180 }
181
Ethan Yonker941a8992016-12-05 09:04:30 -0600182 return 0;
183}
184
185int
Ethan Yonker58f21322018-08-24 11:17:36 -0500186abupdate_binary_command(const char* path, ZipWrap* zip, int retry_count __unused,
Ethan Yonker941a8992016-12-05 09:04:30 -0600187 int status_fd, std::vector<std::string>* cmd)
188{
Ethan Yonker58f21322018-08-24 11:17:36 -0500189 read_source_target_build(zip);
Ethan Yonker941a8992016-12-05 09:04:30 -0600190 int ret = check_newer_ab_build(zip);
191 if (ret) {
192 return ret;
193 }
194
195 // For A/B updates we extract the payload properties to a buffer and obtain
196 // the RAW payload offset in the zip file.
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500197 if (!zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600198 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
199 return INSTALL_CORRUPT;
200 }
201 std::vector<unsigned char> payload_properties(
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500202 zip->GetUncompressedSize(AB_OTA_PAYLOAD_PROPERTIES));
203 if (!zip->ExtractToBuffer(AB_OTA_PAYLOAD_PROPERTIES, payload_properties.data())) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600204 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
205 return INSTALL_CORRUPT;
206 }
207
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500208 if (!zip->EntryExists(AB_OTA_PAYLOAD)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600209 printf("Can't find %s\n", AB_OTA_PAYLOAD);
210 return INSTALL_CORRUPT;
211 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500212 long payload_offset = zip->GetEntryOffset(AB_OTA_PAYLOAD);
Ethan Yonker941a8992016-12-05 09:04:30 -0600213 *cmd = {
214 "/sbin/update_engine_sideload",
215 android::base::StringPrintf("--payload=file://%s", path),
216 android::base::StringPrintf("--offset=%ld", payload_offset),
217 "--headers=" + std::string(payload_properties.begin(),
218 payload_properties.end()),
219 android::base::StringPrintf("--status_fd=%d", status_fd),
220 };
221 return INSTALL_SUCCESS;
222}
223
224#else
225
Ethan Yonker58f21322018-08-24 11:17:36 -0500226void read_source_target_build(ZipWrap* zip __unused /*, std::vector<std::string>& log_buffer*/) {return;}
227
Ethan Yonker941a8992016-12-05 09:04:30 -0600228int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500229abupdate_binary_command(__unused const char* path, __unused ZipWrap* zip, __unused int retry_count,
230 __unused int status_fd, __unused std::vector<std::string>* cmd)
Ethan Yonker941a8992016-12-05 09:04:30 -0600231{
232 printf("No support for AB OTA zips included\n");
233 return INSTALL_CORRUPT;
234}
235
236#endif
237
238int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500239update_binary_command(const char* path, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600240 int status_fd, std::vector<std::string>* cmd)
241{
242 char charfd[16];
243 sprintf(charfd, "%i", status_fd);
244 cmd->push_back(TMP_UPDATER_BINARY_PATH);
245 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
246 cmd->push_back(charfd);
247 cmd->push_back(path);
248 /**cmd = {
249 TMP_UPDATER_BINARY_PATH,
250 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
251 charfd,
252 path,
253 };*/
254 if (retry_count > 0)
255 cmd->push_back("retry");
256 return 0;
257}
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500258
259#ifdef USE_MINZIP
260bool verify_package_compatibility(ZipWrap *package_zip) {
261 if (package_zip->EntryExists("compatibility.zip"))
262 printf("Cannot verify treble package compatibility, must build TWRP in Oreo tree or higher.\n");
263 return true;
264}
265#else
266// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
267// entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA
268// package.
269bool verify_package_compatibility(ZipWrap *zw) {
270 ZipArchiveHandle package_zip = zw->GetZipArchiveHandle();
271 printf("Verifying package compatibility...\n");
272
273 static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip";
274 ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY);
275 ZipEntry compatibility_entry;
276 if (FindEntry(package_zip, compatibility_entry_name, &compatibility_entry) != 0) {
277 printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY);
278 return true;
279 }
280
281 std::string zip_content(compatibility_entry.uncompressed_length, '\0');
282 int32_t ret;
283 if ((ret = ExtractToMemory(package_zip, &compatibility_entry,
284 reinterpret_cast<uint8_t*>(&zip_content[0]),
285 compatibility_entry.uncompressed_length)) != 0) {
286 printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret));
287 return false;
288 }
289
290 ZipArchiveHandle zip_handle;
291 ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())),
292 zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle);
293 if (ret != 0) {
294 printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret));
295 return false;
296 }
297
298 // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents.
299 void* cookie;
300 ret = StartIteration(zip_handle, &cookie, nullptr, nullptr);
301 if (ret != 0) {
302 printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret));
303 CloseArchive(zip_handle);
304 return false;
305 }
306 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
307
308 std::vector<std::string> compatibility_info;
309 ZipEntry info_entry;
310 ZipString info_name;
311 while (Next(cookie, &info_entry, &info_name) == 0) {
312 std::string content(info_entry.uncompressed_length, '\0');
313 int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]),
314 info_entry.uncompressed_length);
315 if (ret != 0) {
316 printf("Failed to read %s: %s\n", info_name.name, ErrorCodeString(ret));
317 CloseArchive(zip_handle);
318 return false;
319 }
320 compatibility_info.emplace_back(std::move(content));
321 }
322 CloseArchive(zip_handle);
323
Ethan Yonker58f21322018-08-24 11:17:36 -0500324 // VintfObjectRecovery::CheckCompatibility returns zero on success. TODO THIS CAUSES A WEIRD COMPILE ERROR
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500325 std::string err;
326 int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err);
327 if (result == 0) {
328 return true;
329 }
330
331 printf("Failed to verify package compatibility (result %i): %s\n", result, err.c_str());
332 return false;
333}
334#endif