blob: e4e5366cee0af8da3cc616ce7e6c69aa2d380641 [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"
bigbiff08eca092023-12-26 19:35:47 -050031#include "twcommon.h"
Ethan Yonker941a8992016-12-05 09:04:30 -060032#include "installcommand.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050033#include <ziparchive/zip_archive.h>
bigbiff1f9e4842020-10-31 11:33:15 -040034#include "twinstall/install.h"
Ethan Yonker941a8992016-12-05 09:04:30 -060035
36#ifdef AB_OTA_UPDATER
37
38static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
39static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
40static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
41
42// This function parses and returns the build.version.incremental
43static int parse_build_number(std::string str) {
44 size_t pos = str.find("=");
45 if (pos != std::string::npos) {
46 std::string num_string = android::base::Trim(str.substr(pos+1));
47 int build_number;
48 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
49 return build_number;
50 }
51 }
52
53 printf("Failed to parse build number in %s.\n", str.c_str());
54 return -1;
55}
56
bigbiff1f9e4842020-10-31 11:33:15 -040057bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) {
bigbiff673c7ae2020-12-02 19:44:56 -050058 std::string binary_name(METADATA_PATH);
GarfieldHand2161882021-11-29 00:04:53 +080059 ZipEntry64 binary_entry;
bigbiff1f9e4842020-10-31 11:33:15 -040060 if (FindEntry(zip, binary_name, &binary_entry) == 0) {
61 long size = binary_entry.uncompressed_length;
62 if (size <= 0)
63 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -060064
bigbiff1f9e4842020-10-31 11:33:15 -040065 meta_data->resize(size, '\0');
66 int32_t ret = ExtractToMemory(zip, &binary_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]),
67 size);
68 if (ret != 0) {
69 printf("Failed to read metadata in update package.\n");
bigbiff1f9e4842020-10-31 11:33:15 -040070 return false;
71 }
72 return true;
73 }
74 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -060075}
76
77// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
bigbiff1f9e4842020-10-31 11:33:15 -040078void read_source_target_build(ZipArchiveHandle 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.
bigbiff1f9e4842020-10-31 11:33:15 -0400110static int check_newer_ab_build(ZipArchiveHandle 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
Darth9728ca7e2022-04-28 08:37:06 +0100132 std::vector<std::string> assertResults = android::base::Split(pkg_device, "[,|]");
mauronofrioc79f86e2020-05-18 16:08:12 -0400133
134 bool deviceExists = false;
135
DarthJabba97cd3abb2021-11-28 01:09:44 +0530136 // twrp.target.devices
137 bool has_target_devices = false;
138 char tw_devices[PROPERTY_VALUE_MAX * 2];
139 property_get("ro.twrp.target.devices", tw_devices, "");
Darth9728ca7e2022-04-28 08:37:06 +0100140 std::vector<std::string> TWRP_devices = android::base::Split(tw_devices, "[,|]");
DarthJabba97cd3abb2021-11-28 01:09:44 +0530141 if (strlen(tw_devices) > 1) {
142 has_target_devices = true;
143 }
144
mauronofrioc79f86e2020-05-18 16:08:12 -0400145 for(const std::string& deviceAssert : assertResults)
146 {
147 std::string assertName = android::base::Trim(deviceAssert);
148 if ((assertName == value || assertName == propmodel || assertName == propname ) && !assertName.empty()) {
149 deviceExists = true;
150 break;
151 }
DarthJabba97cd3abb2021-11-28 01:09:44 +0530152 // twrp.target.devices
153 else if (has_target_devices) {
154 for(const std::string& twrpDevice_x : TWRP_devices) {
155 std::string twrpName = android::base::Trim(twrpDevice_x);
156 if (!twrpName.empty() && !assertName.empty() && assertName == twrpName) {
157 deviceExists = true;
158 printf("Package is for product %s. The selected TWRP target device is %s\n", pkg_device.c_str(), twrpName.c_str());
159 break;
160 }
161 }
162 }
mauronofrioc79f86e2020-05-18 16:08:12 -0400163 }
164
165 if (!deviceExists) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600166 printf("Package is for product %s but expected %s\n",
mauronofrioc79f86e2020-05-18 16:08:12 -0400167 pkg_device.c_str(), value);
Ethan Yonker941a8992016-12-05 09:04:30 -0600168 return INSTALL_ERROR;
169 }
170
171 // We allow the package to not have any serialno, but if it has a non-empty
172 // value it should match.
173 property_get("ro.serialno", value, "");
174 const std::string& pkg_serial_no = metadata["serialno"];
175 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
176 printf("Package is for serial %s\n", pkg_serial_no.c_str());
177 return INSTALL_ERROR;
178 }
179
180 if (metadata["ota-type"] != "AB") {
181 printf("Package is not A/B\n");
182 return INSTALL_ERROR;
183 }
184
185 // Incremental updates should match the current build.
186 property_get("ro.build.version.incremental", value, "");
187 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
188 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
189 printf("Package is for source build %s but expected %s\n",
190 pkg_pre_build.c_str(), value);
191 return INSTALL_ERROR;
192 }
193 property_get("ro.build.fingerprint", value, "");
194 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
195 if (!pkg_pre_build_fingerprint.empty() &&
196 pkg_pre_build_fingerprint != value) {
197 printf("Package is for source build %s but expected %s\n",
198 pkg_pre_build_fingerprint.c_str(), value);
199 return INSTALL_ERROR;
200 }
201
Ethan Yonker941a8992016-12-05 09:04:30 -0600202 return 0;
203}
204
205int
bigbiff1f9e4842020-10-31 11:33:15 -0400206abupdate_binary_command(const char* path, int retry_count __unused,
Ethan Yonker941a8992016-12-05 09:04:30 -0600207 int status_fd, std::vector<std::string>* cmd)
208{
bigbiff1f9e4842020-10-31 11:33:15 -0400209 auto package = Package::CreateMemoryPackage(path);
210 if (!package) {
211 return INSTALL_CORRUPT;
212 }
213
214 ZipArchiveHandle Zip = package->GetZipArchiveHandle();
215 read_source_target_build(Zip);
216 int ret = check_newer_ab_build(Zip);
Ethan Yonker941a8992016-12-05 09:04:30 -0600217 if (ret) {
218 return ret;
219 }
220
221 // For A/B updates we extract the payload properties to a buffer and obtain
222 // the RAW payload offset in the zip file.
bigbiff1f9e4842020-10-31 11:33:15 -0400223 // if (!Zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) {
bigbiff673c7ae2020-12-02 19:44:56 -0500224 std::string binary_name(AB_OTA_PAYLOAD_PROPERTIES);
GarfieldHand2161882021-11-29 00:04:53 +0800225 ZipEntry64 binary_entry;
bigbiff1f9e4842020-10-31 11:33:15 -0400226 if (FindEntry(Zip, binary_name, &binary_entry) != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600227 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
228 return INSTALL_CORRUPT;
229 }
230 std::vector<unsigned char> payload_properties(
bigbiff1f9e4842020-10-31 11:33:15 -0400231 binary_entry.uncompressed_length);
232 int32_t extract_ret = ExtractToMemory(Zip, &binary_entry, reinterpret_cast<uint8_t*>(payload_properties.data()),
233 binary_entry.uncompressed_length);
234 if (extract_ret != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600235 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
bigbiff1f9e4842020-10-31 11:33:15 -0400236 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -0600237 }
238
bigbiff673c7ae2020-12-02 19:44:56 -0500239 std::string ab_ota_payload(AB_OTA_PAYLOAD);
GarfieldHand2161882021-11-29 00:04:53 +0800240 ZipEntry64 ab_ota_payload_entry;
bigbiff1f9e4842020-10-31 11:33:15 -0400241 if (FindEntry(Zip, ab_ota_payload, &ab_ota_payload_entry) != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600242 printf("Can't find %s\n", AB_OTA_PAYLOAD);
243 return INSTALL_CORRUPT;
244 }
bigbiff1f9e4842020-10-31 11:33:15 -0400245 // long payload_offset = Zip->GetEntryOffset(AB_OTA_PAYLOAD);
246 long payload_offset = ab_ota_payload_entry.offset;
Ethan Yonker941a8992016-12-05 09:04:30 -0600247 *cmd = {
bigbiffad58e1b2020-07-06 20:24:34 -0400248 "/system/bin/update_engine_sideload",
Ethan Yonker941a8992016-12-05 09:04:30 -0600249 android::base::StringPrintf("--payload=file://%s", path),
250 android::base::StringPrintf("--offset=%ld", payload_offset),
251 "--headers=" + std::string(payload_properties.begin(),
252 payload_properties.end()),
253 android::base::StringPrintf("--status_fd=%d", status_fd),
254 };
255 return INSTALL_SUCCESS;
256}
257
258#else
259
bigbiff1f9e4842020-10-31 11:33:15 -0400260void read_source_target_build(ZipArchiveHandle zip __unused /*, std::vector<std::string>& log_buffer*/) {return;}
Ethan Yonker58f21322018-08-24 11:17:36 -0500261
Ethan Yonker941a8992016-12-05 09:04:30 -0600262int
bigbiff1f9e4842020-10-31 11:33:15 -0400263abupdate_binary_command(__unused const char* path, __unused int retry_count,
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500264 __unused int status_fd, __unused std::vector<std::string>* cmd)
Ethan Yonker941a8992016-12-05 09:04:30 -0600265{
266 printf("No support for AB OTA zips included\n");
267 return INSTALL_CORRUPT;
268}
269
270#endif
271
272int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500273update_binary_command(const char* path, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600274 int status_fd, std::vector<std::string>* cmd)
275{
276 char charfd[16];
277 sprintf(charfd, "%i", status_fd);
278 cmd->push_back(TMP_UPDATER_BINARY_PATH);
279 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
280 cmd->push_back(charfd);
281 cmd->push_back(path);
282 /**cmd = {
283 TMP_UPDATER_BINARY_PATH,
284 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
285 charfd,
286 path,
287 };*/
288 if (retry_count > 0)
289 cmd->push_back("retry");
290 return 0;
291}
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500292
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500293// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
294// entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA
295// package.
bigbiff1f9e4842020-10-31 11:33:15 -0400296bool verify_package_compatibility(ZipArchiveHandle zw) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500297 printf("Verifying package compatibility...\n");
298
299 static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip";
bigbiff673c7ae2020-12-02 19:44:56 -0500300 std::string compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY);
GarfieldHand2161882021-11-29 00:04:53 +0800301 ZipEntry64 compatibility_entry;
bigbiff1f9e4842020-10-31 11:33:15 -0400302 if (FindEntry(zw, compatibility_entry_name, &compatibility_entry) != 0) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500303 printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY);
304 return true;
305 }
306
307 std::string zip_content(compatibility_entry.uncompressed_length, '\0');
308 int32_t ret;
bigbiff1f9e4842020-10-31 11:33:15 -0400309 if ((ret = ExtractToMemory(zw, &compatibility_entry,
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500310 reinterpret_cast<uint8_t*>(&zip_content[0]),
311 compatibility_entry.uncompressed_length)) != 0) {
312 printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret));
313 return false;
314 }
315
316 ZipArchiveHandle zip_handle;
317 ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())),
318 zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle);
319 if (ret != 0) {
320 printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret));
321 return false;
322 }
323
324 // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents.
325 void* cookie;
sekaiacg837e5372021-12-11 09:28:55 +0800326 ret = StartIteration(zip_handle, &cookie);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500327 if (ret != 0) {
328 printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret));
329 CloseArchive(zip_handle);
330 return false;
331 }
332 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
333
334 std::vector<std::string> compatibility_info;
GarfieldHand2161882021-11-29 00:04:53 +0800335 ZipEntry64 info_entry;
bigbiff673c7ae2020-12-02 19:44:56 -0500336 std::string info_name;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500337 while (Next(cookie, &info_entry, &info_name) == 0) {
338 std::string content(info_entry.uncompressed_length, '\0');
339 int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]),
340 info_entry.uncompressed_length);
341 if (ret != 0) {
bigbiff673c7ae2020-12-02 19:44:56 -0500342 printf("Failed to read %s: %s\n", info_name.c_str(), ErrorCodeString(ret));
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500343 CloseArchive(zip_handle);
344 return false;
345 }
346 compatibility_info.emplace_back(std::move(content));
347 }
348 CloseArchive(zip_handle);
349
bigbiff673c7ae2020-12-02 19:44:56 -0500350 return true;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500351}