blob: a03cb01ba4c92b1954d1ffc44e652bcc5fea7bbd [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>
33#include <vintf/VintfObjectRecovery.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) {
58 ZipString binary_name(METADATA_PATH);
59 ZipEntry binary_entry;
60 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");
70 CloseArchive(zip);
71 return false;
72 }
73 return true;
74 }
75 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -060076}
77
78// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
bigbiff1f9e4842020-10-31 11:33:15 -040079void read_source_target_build(ZipArchiveHandle zip/*, std::vector<std::string>& log_buffer*/) {
Ethan Yonker941a8992016-12-05 09:04:30 -060080 std::string meta_data;
81 if (!read_metadata_from_package(zip, &meta_data)) {
82 return;
83 }
84 // Examples of the pre-build and post-build strings in metadata:
85 // pre-build-incremental=2943039
86 // post-build-incremental=2951741
87 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
88 for (const std::string& line : lines) {
89 std::string str = android::base::Trim(line);
90 if (android::base::StartsWith(str, "pre-build-incremental")){
91 int source_build = parse_build_number(str);
92 if (source_build != -1) {
Ethan Yonker58f21322018-08-24 11:17:36 -050093 printf("source_build: %d\n", source_build);
94 /*log_buffer.push_back(android::base::StringPrintf("source_build: %d",
95 source_build));*/
Ethan Yonker941a8992016-12-05 09:04:30 -060096 }
97 } else if (android::base::StartsWith(str, "post-build-incremental")) {
98 int target_build = parse_build_number(str);
99 if (target_build != -1) {
Ethan Yonker58f21322018-08-24 11:17:36 -0500100 printf("target_build: %d\n", target_build);
101 /*log_buffer.push_back(android::base::StringPrintf("target_build: %d",
102 target_build));*/
Ethan Yonker941a8992016-12-05 09:04:30 -0600103 }
104 }
105 }
106}
107
108// Parses the metadata of the OTA package in |zip| and checks whether we are
109// allowed to accept this A/B package. Downgrading is not allowed unless
110// explicitly enabled in the package and only for incremental packages.
bigbiff1f9e4842020-10-31 11:33:15 -0400111static int check_newer_ab_build(ZipArchiveHandle zip)
Ethan Yonker941a8992016-12-05 09:04:30 -0600112{
113 std::string metadata_str;
114 if (!read_metadata_from_package(zip, &metadata_str)) {
115 return INSTALL_CORRUPT;
116 }
117 std::map<std::string, std::string> metadata;
118 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
119 size_t eq = line.find('=');
120 if (eq != std::string::npos) {
121 metadata[line.substr(0, eq)] = line.substr(eq + 1);
122 }
123 }
124 char value[PROPERTY_VALUE_MAX];
mauronofrioc79f86e2020-05-18 16:08:12 -0400125 char propmodel[PROPERTY_VALUE_MAX];
126 char propname[PROPERTY_VALUE_MAX];
Ethan Yonker941a8992016-12-05 09:04:30 -0600127
128 property_get("ro.product.device", value, "");
mauronofrioc79f86e2020-05-18 16:08:12 -0400129 property_get("ro.product.model", propmodel, "");
130 property_get("ro.product.name", propname, "");
Ethan Yonker941a8992016-12-05 09:04:30 -0600131 const std::string& pkg_device = metadata["pre-device"];
mauronofrioc79f86e2020-05-18 16:08:12 -0400132
133 std::vector<std::string> assertResults = android::base::Split(pkg_device, ",");
134
135 bool deviceExists = false;
136
137 for(const std::string& deviceAssert : assertResults)
138 {
139 std::string assertName = android::base::Trim(deviceAssert);
140 if ((assertName == value || assertName == propmodel || assertName == propname ) && !assertName.empty()) {
141 deviceExists = true;
142 break;
143 }
144 }
145
146 if (!deviceExists) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600147 printf("Package is for product %s but expected %s\n",
mauronofrioc79f86e2020-05-18 16:08:12 -0400148 pkg_device.c_str(), value);
Ethan Yonker941a8992016-12-05 09:04:30 -0600149 return INSTALL_ERROR;
150 }
151
152 // We allow the package to not have any serialno, but if it has a non-empty
153 // value it should match.
154 property_get("ro.serialno", value, "");
155 const std::string& pkg_serial_no = metadata["serialno"];
156 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
157 printf("Package is for serial %s\n", pkg_serial_no.c_str());
158 return INSTALL_ERROR;
159 }
160
161 if (metadata["ota-type"] != "AB") {
162 printf("Package is not A/B\n");
163 return INSTALL_ERROR;
164 }
165
166 // Incremental updates should match the current build.
167 property_get("ro.build.version.incremental", value, "");
168 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
169 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
170 printf("Package is for source build %s but expected %s\n",
171 pkg_pre_build.c_str(), value);
172 return INSTALL_ERROR;
173 }
174 property_get("ro.build.fingerprint", value, "");
175 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
176 if (!pkg_pre_build_fingerprint.empty() &&
177 pkg_pre_build_fingerprint != value) {
178 printf("Package is for source build %s but expected %s\n",
179 pkg_pre_build_fingerprint.c_str(), value);
180 return INSTALL_ERROR;
181 }
182
Ethan Yonker941a8992016-12-05 09:04:30 -0600183 return 0;
184}
185
186int
bigbiff1f9e4842020-10-31 11:33:15 -0400187abupdate_binary_command(const char* path, int retry_count __unused,
Ethan Yonker941a8992016-12-05 09:04:30 -0600188 int status_fd, std::vector<std::string>* cmd)
189{
bigbiff1f9e4842020-10-31 11:33:15 -0400190 auto package = Package::CreateMemoryPackage(path);
191 if (!package) {
192 return INSTALL_CORRUPT;
193 }
194
195 ZipArchiveHandle Zip = package->GetZipArchiveHandle();
196 read_source_target_build(Zip);
197 int ret = check_newer_ab_build(Zip);
Ethan Yonker941a8992016-12-05 09:04:30 -0600198 if (ret) {
199 return ret;
200 }
201
202 // For A/B updates we extract the payload properties to a buffer and obtain
203 // the RAW payload offset in the zip file.
bigbiff1f9e4842020-10-31 11:33:15 -0400204 // if (!Zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) {
205 ZipString binary_name(AB_OTA_PAYLOAD_PROPERTIES);
206 ZipEntry binary_entry;
207 if (FindEntry(Zip, binary_name, &binary_entry) != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600208 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
209 return INSTALL_CORRUPT;
210 }
211 std::vector<unsigned char> payload_properties(
bigbiff1f9e4842020-10-31 11:33:15 -0400212 binary_entry.uncompressed_length);
213 int32_t extract_ret = ExtractToMemory(Zip, &binary_entry, reinterpret_cast<uint8_t*>(payload_properties.data()),
214 binary_entry.uncompressed_length);
215 if (extract_ret != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600216 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
bigbiff1f9e4842020-10-31 11:33:15 -0400217 CloseArchive(Zip);
218 return false;
Ethan Yonker941a8992016-12-05 09:04:30 -0600219 }
220
bigbiff1f9e4842020-10-31 11:33:15 -0400221 ZipString ab_ota_payload(AB_OTA_PAYLOAD);
222 ZipEntry ab_ota_payload_entry;
223 if (FindEntry(Zip, ab_ota_payload, &ab_ota_payload_entry) != 0) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600224 printf("Can't find %s\n", AB_OTA_PAYLOAD);
225 return INSTALL_CORRUPT;
226 }
bigbiff1f9e4842020-10-31 11:33:15 -0400227 // long payload_offset = Zip->GetEntryOffset(AB_OTA_PAYLOAD);
228 long payload_offset = ab_ota_payload_entry.offset;
Ethan Yonker941a8992016-12-05 09:04:30 -0600229 *cmd = {
bigbiffad58e1b2020-07-06 20:24:34 -0400230 "/system/bin/update_engine_sideload",
Ethan Yonker941a8992016-12-05 09:04:30 -0600231 android::base::StringPrintf("--payload=file://%s", path),
232 android::base::StringPrintf("--offset=%ld", payload_offset),
233 "--headers=" + std::string(payload_properties.begin(),
234 payload_properties.end()),
235 android::base::StringPrintf("--status_fd=%d", status_fd),
236 };
237 return INSTALL_SUCCESS;
238}
239
240#else
241
bigbiff1f9e4842020-10-31 11:33:15 -0400242void read_source_target_build(ZipArchiveHandle zip __unused /*, std::vector<std::string>& log_buffer*/) {return;}
Ethan Yonker58f21322018-08-24 11:17:36 -0500243
Ethan Yonker941a8992016-12-05 09:04:30 -0600244int
bigbiff1f9e4842020-10-31 11:33:15 -0400245abupdate_binary_command(__unused const char* path, __unused int retry_count,
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500246 __unused int status_fd, __unused std::vector<std::string>* cmd)
Ethan Yonker941a8992016-12-05 09:04:30 -0600247{
248 printf("No support for AB OTA zips included\n");
249 return INSTALL_CORRUPT;
250}
251
252#endif
253
254int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500255update_binary_command(const char* path, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600256 int status_fd, std::vector<std::string>* cmd)
257{
258 char charfd[16];
259 sprintf(charfd, "%i", status_fd);
260 cmd->push_back(TMP_UPDATER_BINARY_PATH);
261 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
262 cmd->push_back(charfd);
263 cmd->push_back(path);
264 /**cmd = {
265 TMP_UPDATER_BINARY_PATH,
266 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
267 charfd,
268 path,
269 };*/
270 if (retry_count > 0)
271 cmd->push_back("retry");
272 return 0;
273}
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500274
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500275// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
276// entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA
277// package.
bigbiff1f9e4842020-10-31 11:33:15 -0400278bool verify_package_compatibility(ZipArchiveHandle zw) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500279 printf("Verifying package compatibility...\n");
280
281 static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip";
282 ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY);
283 ZipEntry compatibility_entry;
bigbiff1f9e4842020-10-31 11:33:15 -0400284 if (FindEntry(zw, compatibility_entry_name, &compatibility_entry) != 0) {
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500285 printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY);
286 return true;
287 }
288
289 std::string zip_content(compatibility_entry.uncompressed_length, '\0');
290 int32_t ret;
bigbiff1f9e4842020-10-31 11:33:15 -0400291 if ((ret = ExtractToMemory(zw, &compatibility_entry,
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500292 reinterpret_cast<uint8_t*>(&zip_content[0]),
293 compatibility_entry.uncompressed_length)) != 0) {
294 printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret));
295 return false;
296 }
297
298 ZipArchiveHandle zip_handle;
299 ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())),
300 zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle);
301 if (ret != 0) {
302 printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret));
303 return false;
304 }
305
306 // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents.
307 void* cookie;
308 ret = StartIteration(zip_handle, &cookie, nullptr, nullptr);
309 if (ret != 0) {
310 printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret));
311 CloseArchive(zip_handle);
312 return false;
313 }
314 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
315
316 std::vector<std::string> compatibility_info;
317 ZipEntry info_entry;
318 ZipString info_name;
319 while (Next(cookie, &info_entry, &info_name) == 0) {
320 std::string content(info_entry.uncompressed_length, '\0');
321 int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]),
322 info_entry.uncompressed_length);
323 if (ret != 0) {
324 printf("Failed to read %s: %s\n", info_name.name, ErrorCodeString(ret));
325 CloseArchive(zip_handle);
326 return false;
327 }
328 compatibility_info.emplace_back(std::move(content));
329 }
330 CloseArchive(zip_handle);
331
Ethan Yonker58f21322018-08-24 11:17:36 -0500332 // VintfObjectRecovery::CheckCompatibility returns zero on success. TODO THIS CAUSES A WEIRD COMPILE ERROR
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500333 std::string err;
334 int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err);
335 if (result == 0) {
336 return true;
337 }
338
339 printf("Failed to verify package compatibility (result %i): %s\n", result, err.c_str());
340 return false;
341}