blob: 9836014184ffd68b6e0ba004d294f9f84c868782 [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 <ziparchive/zip_archive.h>
bigbiff1f9e4842020-10-31 11:33:15 -040033#include "twinstall/install.h"
Ethan Yonker941a8992016-12-05 09:04:30 -060034
35#ifdef AB_OTA_UPDATER
36
37static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
38static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
39static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
40
41// This function parses and returns the build.version.incremental
42static int parse_build_number(std::string str) {
43 size_t pos = str.find("=");
44 if (pos != std::string::npos) {
45 std::string num_string = android::base::Trim(str.substr(pos+1));
46 int build_number;
47 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
48 return build_number;
49 }
50 }
51
52 printf("Failed to parse build number in %s.\n", str.c_str());
53 return -1;
54}
55
bigbiff1f9e4842020-10-31 11:33:15 -040056bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) {
bigbiff673c7ae2020-12-02 19:44:56 -050057 std::string binary_name(METADATA_PATH);
bigbiff1f9e4842020-10-31 11:33:15 -040058 ZipEntry binary_entry;
59 if (FindEntry(zip, binary_name, &binary_entry) == 0) {
60 long size = binary_entry.uncompressed_length;
61 if (size <= 0)
62 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -060063
bigbiff1f9e4842020-10-31 11:33:15 -040064 meta_data->resize(size, '\0');
65 int32_t ret = ExtractToMemory(zip, &binary_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]),
66 size);
67 if (ret != 0) {
68 printf("Failed to read metadata in update package.\n");
bigbiff1f9e4842020-10-31 11:33:15 -040069 return false;
70 }
71 return true;
72 }
73 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -060074}
75
76// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
bigbiff1f9e4842020-10-31 11:33:15 -040077void read_source_target_build(ZipArchiveHandle zip/*, std::vector<std::string>& log_buffer*/) {
Ethan Yonker941a8992016-12-05 09:04:30 -060078 std::string meta_data;
79 if (!read_metadata_from_package(zip, &meta_data)) {
80 return;
81 }
82 // Examples of the pre-build and post-build strings in metadata:
83 // pre-build-incremental=2943039
84 // post-build-incremental=2951741
85 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
86 for (const std::string& line : lines) {
87 std::string str = android::base::Trim(line);
88 if (android::base::StartsWith(str, "pre-build-incremental")){
89 int source_build = parse_build_number(str);
90 if (source_build != -1) {
Ethan Yonker58f21322018-08-24 11:17:36 -050091 printf("source_build: %d\n", source_build);
92 /*log_buffer.push_back(android::base::StringPrintf("source_build: %d",
93 source_build));*/
Ethan Yonker941a8992016-12-05 09:04:30 -060094 }
95 } else if (android::base::StartsWith(str, "post-build-incremental")) {
96 int target_build = parse_build_number(str);
97 if (target_build != -1) {
Ethan Yonker58f21322018-08-24 11:17:36 -050098 printf("target_build: %d\n", target_build);
99 /*log_buffer.push_back(android::base::StringPrintf("target_build: %d",
100 target_build));*/
Ethan Yonker941a8992016-12-05 09:04:30 -0600101 }
102 }
103 }
104}
105
106// Parses the metadata of the OTA package in |zip| and checks whether we are
107// allowed to accept this A/B package. Downgrading is not allowed unless
108// explicitly enabled in the package and only for incremental packages.
bigbiff1f9e4842020-10-31 11:33:15 -0400109static int check_newer_ab_build(ZipArchiveHandle zip)
Ethan Yonker941a8992016-12-05 09:04:30 -0600110{
111 std::string metadata_str;
112 if (!read_metadata_from_package(zip, &metadata_str)) {
113 return INSTALL_CORRUPT;
114 }
115 std::map<std::string, std::string> metadata;
116 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
117 size_t eq = line.find('=');
118 if (eq != std::string::npos) {
119 metadata[line.substr(0, eq)] = line.substr(eq + 1);
120 }
121 }
122 char value[PROPERTY_VALUE_MAX];
mauronofrioc79f86e2020-05-18 16:08:12 -0400123 char propmodel[PROPERTY_VALUE_MAX];
124 char propname[PROPERTY_VALUE_MAX];
Ethan Yonker941a8992016-12-05 09:04:30 -0600125
126 property_get("ro.product.device", value, "");
mauronofrioc79f86e2020-05-18 16:08:12 -0400127 property_get("ro.product.model", propmodel, "");
128 property_get("ro.product.name", propname, "");
Ethan Yonker941a8992016-12-05 09:04:30 -0600129 const std::string& pkg_device = metadata["pre-device"];
mauronofrioc79f86e2020-05-18 16:08:12 -0400130
131 std::vector<std::string> assertResults = android::base::Split(pkg_device, ",");
132
133 bool deviceExists = false;
134
135 for(const std::string& deviceAssert : assertResults)
136 {
137 std::string assertName = android::base::Trim(deviceAssert);
138 if ((assertName == value || assertName == propmodel || assertName == propname ) && !assertName.empty()) {
139 deviceExists = true;
140 break;
141 }
142 }
143
144 if (!deviceExists) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600145 printf("Package is for product %s but expected %s\n",
mauronofrioc79f86e2020-05-18 16:08:12 -0400146 pkg_device.c_str(), value);
Ethan Yonker941a8992016-12-05 09:04:30 -0600147 return INSTALL_ERROR;
148 }
149
150 // We allow the package to not have any serialno, but if it has a non-empty
151 // value it should match.
152 property_get("ro.serialno", value, "");
153 const std::string& pkg_serial_no = metadata["serialno"];
154 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
155 printf("Package is for serial %s\n", pkg_serial_no.c_str());
156 return INSTALL_ERROR;
157 }
158
159 if (metadata["ota-type"] != "AB") {
160 printf("Package is not A/B\n");
161 return INSTALL_ERROR;
162 }
163
164 // Incremental updates should match the current build.
165 property_get("ro.build.version.incremental", value, "");
166 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
167 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
168 printf("Package is for source build %s but expected %s\n",
169 pkg_pre_build.c_str(), value);
170 return INSTALL_ERROR;
171 }
172 property_get("ro.build.fingerprint", value, "");
173 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
174 if (!pkg_pre_build_fingerprint.empty() &&
175 pkg_pre_build_fingerprint != value) {
176 printf("Package is for source build %s but expected %s\n",
177 pkg_pre_build_fingerprint.c_str(), value);
178 return INSTALL_ERROR;
179 }
180
Ethan Yonker941a8992016-12-05 09:04:30 -0600181 return 0;
182}
183
184int
bigbiff1f9e4842020-10-31 11:33:15 -0400185abupdate_binary_command(const char* path, int retry_count __unused,
Ethan Yonker941a8992016-12-05 09:04:30 -0600186 int status_fd, std::vector<std::string>* cmd)
187{
bigbiff1f9e4842020-10-31 11:33:15 -0400188 auto package = Package::CreateMemoryPackage(path);
189 if (!package) {
190 return INSTALL_CORRUPT;
191 }
192
193 ZipArchiveHandle Zip = package->GetZipArchiveHandle();
194 read_source_target_build(Zip);
195 int ret = check_newer_ab_build(Zip);
Ethan Yonker941a8992016-12-05 09:04:30 -0600196 if (ret) {
197 return ret;
198 }
199
200 // For A/B updates we extract the payload properties to a buffer and obtain
201 // the RAW payload offset in the zip file.
bigbiff1f9e4842020-10-31 11:33:15 -0400202 // if (!Zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) {
bigbiff673c7ae2020-12-02 19:44:56 -0500203 std::string binary_name(AB_OTA_PAYLOAD_PROPERTIES);
bigbiff1f9e4842020-10-31 11:33:15 -0400204 ZipEntry binary_entry;
205 if (FindEntry(Zip, binary_name, &binary_entry) != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600206 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
207 return INSTALL_CORRUPT;
208 }
209 std::vector<unsigned char> payload_properties(
bigbiff1f9e4842020-10-31 11:33:15 -0400210 binary_entry.uncompressed_length);
211 int32_t extract_ret = ExtractToMemory(Zip, &binary_entry, reinterpret_cast<uint8_t*>(payload_properties.data()),
212 binary_entry.uncompressed_length);
213 if (extract_ret != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600214 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
bigbiff1f9e4842020-10-31 11:33:15 -0400215 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -0600216 }
217
bigbiff673c7ae2020-12-02 19:44:56 -0500218 std::string ab_ota_payload(AB_OTA_PAYLOAD);
bigbiff1f9e4842020-10-31 11:33:15 -0400219 ZipEntry ab_ota_payload_entry;
220 if (FindEntry(Zip, ab_ota_payload, &ab_ota_payload_entry) != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600221 printf("Can't find %s\n", AB_OTA_PAYLOAD);
222 return INSTALL_CORRUPT;
223 }
bigbiff1f9e4842020-10-31 11:33:15 -0400224 // long payload_offset = Zip->GetEntryOffset(AB_OTA_PAYLOAD);
225 long payload_offset = ab_ota_payload_entry.offset;
Ethan Yonker941a8992016-12-05 09:04:30 -0600226 *cmd = {
bigbiffad58e1b2020-07-06 20:24:34 -0400227 "/system/bin/update_engine_sideload",
Ethan Yonker941a8992016-12-05 09:04:30 -0600228 android::base::StringPrintf("--payload=file://%s", path),
229 android::base::StringPrintf("--offset=%ld", payload_offset),
230 "--headers=" + std::string(payload_properties.begin(),
231 payload_properties.end()),
232 android::base::StringPrintf("--status_fd=%d", status_fd),
233 };
234 return INSTALL_SUCCESS;
235}
236
237#else
238
bigbiff1f9e4842020-10-31 11:33:15 -0400239void read_source_target_build(ZipArchiveHandle zip __unused /*, std::vector<std::string>& log_buffer*/) {return;}
Ethan Yonker58f21322018-08-24 11:17:36 -0500240
Ethan Yonker941a8992016-12-05 09:04:30 -0600241int
bigbiff1f9e4842020-10-31 11:33:15 -0400242abupdate_binary_command(__unused const char* path, __unused int retry_count,
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500243 __unused int status_fd, __unused std::vector<std::string>* cmd)
Ethan Yonker941a8992016-12-05 09:04:30 -0600244{
245 printf("No support for AB OTA zips included\n");
246 return INSTALL_CORRUPT;
247}
248
249#endif
250
251int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500252update_binary_command(const char* path, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600253 int status_fd, std::vector<std::string>* cmd)
254{
255 char charfd[16];
256 sprintf(charfd, "%i", status_fd);
257 cmd->push_back(TMP_UPDATER_BINARY_PATH);
258 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
259 cmd->push_back(charfd);
260 cmd->push_back(path);
261 /**cmd = {
262 TMP_UPDATER_BINARY_PATH,
263 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
264 charfd,
265 path,
266 };*/
267 if (retry_count > 0)
268 cmd->push_back("retry");
269 return 0;
270}
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500271
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500272// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
273// entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA
274// package.
bigbiff1f9e4842020-10-31 11:33:15 -0400275bool verify_package_compatibility(ZipArchiveHandle zw) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500276 printf("Verifying package compatibility...\n");
277
278 static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip";
bigbiff673c7ae2020-12-02 19:44:56 -0500279 std::string compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY);
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500280 ZipEntry compatibility_entry;
bigbiff1f9e4842020-10-31 11:33:15 -0400281 if (FindEntry(zw, compatibility_entry_name, &compatibility_entry) != 0) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500282 printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY);
283 return true;
284 }
285
286 std::string zip_content(compatibility_entry.uncompressed_length, '\0');
287 int32_t ret;
bigbiff1f9e4842020-10-31 11:33:15 -0400288 if ((ret = ExtractToMemory(zw, &compatibility_entry,
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500289 reinterpret_cast<uint8_t*>(&zip_content[0]),
290 compatibility_entry.uncompressed_length)) != 0) {
291 printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret));
292 return false;
293 }
294
295 ZipArchiveHandle zip_handle;
296 ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())),
297 zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle);
298 if (ret != 0) {
299 printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret));
300 return false;
301 }
302
303 // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents.
304 void* cookie;
305 ret = StartIteration(zip_handle, &cookie, nullptr, nullptr);
306 if (ret != 0) {
307 printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret));
308 CloseArchive(zip_handle);
309 return false;
310 }
311 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
312
313 std::vector<std::string> compatibility_info;
314 ZipEntry info_entry;
bigbiff673c7ae2020-12-02 19:44:56 -0500315 std::string info_name;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500316 while (Next(cookie, &info_entry, &info_name) == 0) {
317 std::string content(info_entry.uncompressed_length, '\0');
318 int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]),
319 info_entry.uncompressed_length);
320 if (ret != 0) {
bigbiff673c7ae2020-12-02 19:44:56 -0500321 printf("Failed to read %s: %s\n", info_name.c_str(), ErrorCodeString(ret));
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500322 CloseArchive(zip_handle);
323 return false;
324 }
325 compatibility_info.emplace_back(std::move(content));
326 }
327 CloseArchive(zip_handle);
328
bigbiff673c7ae2020-12-02 19:44:56 -0500329 return true;
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500330}