blob: b306e872fd9dfb2d970a0382a3da1d881151bcc6 [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 Yonker8373cfe2017-09-08 06:50:54 -050078static void 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) {
92 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
93 source_build));
94 }
95 } else if (android::base::StartsWith(str, "post-build-incremental")) {
96 int target_build = parse_build_number(str);
97 if (target_build != -1) {
98 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
99 target_build));
100 }
101 }
102 }
103}
104
105// Parses the metadata of the OTA package in |zip| and checks whether we are
106// allowed to accept this A/B package. Downgrading is not allowed unless
107// explicitly enabled in the package and only for incremental packages.
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500108static int check_newer_ab_build(ZipWrap* zip)
Ethan Yonker941a8992016-12-05 09:04:30 -0600109{
110 std::string metadata_str;
111 if (!read_metadata_from_package(zip, &metadata_str)) {
112 return INSTALL_CORRUPT;
113 }
114 std::map<std::string, std::string> metadata;
115 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
116 size_t eq = line.find('=');
117 if (eq != std::string::npos) {
118 metadata[line.substr(0, eq)] = line.substr(eq + 1);
119 }
120 }
121 char value[PROPERTY_VALUE_MAX];
122
123 property_get("ro.product.device", value, "");
124 const std::string& pkg_device = metadata["pre-device"];
125 if (pkg_device != value || pkg_device.empty()) {
126 printf("Package is for product %s but expected %s\n",
127 pkg_device.c_str(), value);
128 return INSTALL_ERROR;
129 }
130
131 // We allow the package to not have any serialno, but if it has a non-empty
132 // value it should match.
133 property_get("ro.serialno", value, "");
134 const std::string& pkg_serial_no = metadata["serialno"];
135 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
136 printf("Package is for serial %s\n", pkg_serial_no.c_str());
137 return INSTALL_ERROR;
138 }
139
140 if (metadata["ota-type"] != "AB") {
141 printf("Package is not A/B\n");
142 return INSTALL_ERROR;
143 }
144
145 // Incremental updates should match the current build.
146 property_get("ro.build.version.incremental", value, "");
147 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
148 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
149 printf("Package is for source build %s but expected %s\n",
150 pkg_pre_build.c_str(), value);
151 return INSTALL_ERROR;
152 }
153 property_get("ro.build.fingerprint", value, "");
154 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
155 if (!pkg_pre_build_fingerprint.empty() &&
156 pkg_pre_build_fingerprint != value) {
157 printf("Package is for source build %s but expected %s\n",
158 pkg_pre_build_fingerprint.c_str(), value);
159 return INSTALL_ERROR;
160 }
161
Ethan Yonker941a8992016-12-05 09:04:30 -0600162 return 0;
163}
164
165int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500166abupdate_binary_command(const char* path, ZipWrap* zip, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600167 int status_fd, std::vector<std::string>* cmd)
168{
169 int ret = check_newer_ab_build(zip);
170 if (ret) {
171 return ret;
172 }
173
174 // For A/B updates we extract the payload properties to a buffer and obtain
175 // the RAW payload offset in the zip file.
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500176 if (!zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600177 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
178 return INSTALL_CORRUPT;
179 }
180 std::vector<unsigned char> payload_properties(
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500181 zip->GetUncompressedSize(AB_OTA_PAYLOAD_PROPERTIES));
182 if (!zip->ExtractToBuffer(AB_OTA_PAYLOAD_PROPERTIES, payload_properties.data())) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600183 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
184 return INSTALL_CORRUPT;
185 }
186
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500187 if (!zip->EntryExists(AB_OTA_PAYLOAD)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600188 printf("Can't find %s\n", AB_OTA_PAYLOAD);
189 return INSTALL_CORRUPT;
190 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500191 long payload_offset = zip->GetEntryOffset(AB_OTA_PAYLOAD);
Ethan Yonker941a8992016-12-05 09:04:30 -0600192 *cmd = {
193 "/sbin/update_engine_sideload",
194 android::base::StringPrintf("--payload=file://%s", path),
195 android::base::StringPrintf("--offset=%ld", payload_offset),
196 "--headers=" + std::string(payload_properties.begin(),
197 payload_properties.end()),
198 android::base::StringPrintf("--status_fd=%d", status_fd),
199 };
200 return INSTALL_SUCCESS;
201}
202
203#else
204
205int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500206abupdate_binary_command(__unused const char* path, __unused ZipWrap* zip, __unused int retry_count,
207 __unused int status_fd, __unused std::vector<std::string>* cmd)
Ethan Yonker941a8992016-12-05 09:04:30 -0600208{
209 printf("No support for AB OTA zips included\n");
210 return INSTALL_CORRUPT;
211}
212
213#endif
214
215int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500216update_binary_command(const char* path, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600217 int status_fd, std::vector<std::string>* cmd)
218{
219 char charfd[16];
220 sprintf(charfd, "%i", status_fd);
221 cmd->push_back(TMP_UPDATER_BINARY_PATH);
222 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
223 cmd->push_back(charfd);
224 cmd->push_back(path);
225 /**cmd = {
226 TMP_UPDATER_BINARY_PATH,
227 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
228 charfd,
229 path,
230 };*/
231 if (retry_count > 0)
232 cmd->push_back("retry");
233 return 0;
234}
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500235
236#ifdef USE_MINZIP
237bool verify_package_compatibility(ZipWrap *package_zip) {
238 if (package_zip->EntryExists("compatibility.zip"))
239 printf("Cannot verify treble package compatibility, must build TWRP in Oreo tree or higher.\n");
240 return true;
241}
242#else
243// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
244// entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA
245// package.
246bool verify_package_compatibility(ZipWrap *zw) {
247 ZipArchiveHandle package_zip = zw->GetZipArchiveHandle();
248 printf("Verifying package compatibility...\n");
249
250 static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip";
251 ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY);
252 ZipEntry compatibility_entry;
253 if (FindEntry(package_zip, compatibility_entry_name, &compatibility_entry) != 0) {
254 printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY);
255 return true;
256 }
257
258 std::string zip_content(compatibility_entry.uncompressed_length, '\0');
259 int32_t ret;
260 if ((ret = ExtractToMemory(package_zip, &compatibility_entry,
261 reinterpret_cast<uint8_t*>(&zip_content[0]),
262 compatibility_entry.uncompressed_length)) != 0) {
263 printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret));
264 return false;
265 }
266
267 ZipArchiveHandle zip_handle;
268 ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())),
269 zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle);
270 if (ret != 0) {
271 printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret));
272 return false;
273 }
274
275 // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents.
276 void* cookie;
277 ret = StartIteration(zip_handle, &cookie, nullptr, nullptr);
278 if (ret != 0) {
279 printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret));
280 CloseArchive(zip_handle);
281 return false;
282 }
283 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
284
285 std::vector<std::string> compatibility_info;
286 ZipEntry info_entry;
287 ZipString info_name;
288 while (Next(cookie, &info_entry, &info_name) == 0) {
289 std::string content(info_entry.uncompressed_length, '\0');
290 int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]),
291 info_entry.uncompressed_length);
292 if (ret != 0) {
293 printf("Failed to read %s: %s\n", info_name.name, ErrorCodeString(ret));
294 CloseArchive(zip_handle);
295 return false;
296 }
297 compatibility_info.emplace_back(std::move(content));
298 }
299 CloseArchive(zip_handle);
300
301 // VintfObjectRecovery::CheckCompatibility returns zero on success.
302 std::string err;
303 int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err);
304 if (result == 0) {
305 return true;
306 }
307
308 printf("Failed to verify package compatibility (result %i): %s\n", result, err.c_str());
309 return false;
310}
311#endif