blob: f9978f27bf57f19dcbf9fd514efcfbe38e06ea56 [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
162 // Check for downgrade version.
163 int64_t build_timestampt = property_get_int64(
164 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
165 int64_t pkg_post_timespampt = 0;
166 // We allow to full update to the same version we are running, in case there
167 // is a problem with the current copy of that version.
168 if (metadata["post-timestamp"].empty() ||
169 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
170 &pkg_post_timespampt) ||
171 pkg_post_timespampt < build_timestampt) {
172 if (metadata["ota-downgrade"] != "yes") {
173 printf("Update package is older than the current build, expected a "
174 "build newer than timestamp %" PRIu64 " but package has "
175 "timestamp %" PRIu64 " and downgrade not allowed.\n",
176 build_timestampt, pkg_post_timespampt);
177 return INSTALL_ERROR;
178 }
179 if (pkg_pre_build_fingerprint.empty()) {
180 printf("Downgrade package must have a pre-build version set, not "
181 "allowed.\n");
182 return INSTALL_ERROR;
183 }
184 }
185
186 return 0;
187}
188
189int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500190abupdate_binary_command(const char* path, ZipWrap* zip, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600191 int status_fd, std::vector<std::string>* cmd)
192{
193 int ret = check_newer_ab_build(zip);
194 if (ret) {
195 return ret;
196 }
197
198 // For A/B updates we extract the payload properties to a buffer and obtain
199 // the RAW payload offset in the zip file.
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500200 if (!zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600201 printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
202 return INSTALL_CORRUPT;
203 }
204 std::vector<unsigned char> payload_properties(
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500205 zip->GetUncompressedSize(AB_OTA_PAYLOAD_PROPERTIES));
206 if (!zip->ExtractToBuffer(AB_OTA_PAYLOAD_PROPERTIES, payload_properties.data())) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600207 printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
208 return INSTALL_CORRUPT;
209 }
210
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500211 if (!zip->EntryExists(AB_OTA_PAYLOAD)) {
Ethan Yonker941a8992016-12-05 09:04:30 -0600212 printf("Can't find %s\n", AB_OTA_PAYLOAD);
213 return INSTALL_CORRUPT;
214 }
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500215 long payload_offset = zip->GetEntryOffset(AB_OTA_PAYLOAD);
Ethan Yonker941a8992016-12-05 09:04:30 -0600216 *cmd = {
217 "/sbin/update_engine_sideload",
218 android::base::StringPrintf("--payload=file://%s", path),
219 android::base::StringPrintf("--offset=%ld", payload_offset),
220 "--headers=" + std::string(payload_properties.begin(),
221 payload_properties.end()),
222 android::base::StringPrintf("--status_fd=%d", status_fd),
223 };
224 return INSTALL_SUCCESS;
225}
226
227#else
228
229int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500230abupdate_binary_command(__unused const char* path, __unused ZipWrap* zip, __unused int retry_count,
231 __unused int status_fd, __unused std::vector<std::string>* cmd)
Ethan Yonker941a8992016-12-05 09:04:30 -0600232{
233 printf("No support for AB OTA zips included\n");
234 return INSTALL_CORRUPT;
235}
236
237#endif
238
239int
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500240update_binary_command(const char* path, int retry_count,
Ethan Yonker941a8992016-12-05 09:04:30 -0600241 int status_fd, std::vector<std::string>* cmd)
242{
243 char charfd[16];
244 sprintf(charfd, "%i", status_fd);
245 cmd->push_back(TMP_UPDATER_BINARY_PATH);
246 cmd->push_back(EXPAND(RECOVERY_API_VERSION));
247 cmd->push_back(charfd);
248 cmd->push_back(path);
249 /**cmd = {
250 TMP_UPDATER_BINARY_PATH,
251 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
252 charfd,
253 path,
254 };*/
255 if (retry_count > 0)
256 cmd->push_back("retry");
257 return 0;
258}
Ethan Yonker8373cfe2017-09-08 06:50:54 -0500259
260#ifdef USE_MINZIP
261bool verify_package_compatibility(ZipWrap *package_zip) {
262 if (package_zip->EntryExists("compatibility.zip"))
263 printf("Cannot verify treble package compatibility, must build TWRP in Oreo tree or higher.\n");
264 return true;
265}
266#else
267// Verifes the compatibility info in a Treble-compatible package. Returns true directly if the
268// entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA
269// package.
270bool verify_package_compatibility(ZipWrap *zw) {
271 ZipArchiveHandle package_zip = zw->GetZipArchiveHandle();
272 printf("Verifying package compatibility...\n");
273
274 static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip";
275 ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY);
276 ZipEntry compatibility_entry;
277 if (FindEntry(package_zip, compatibility_entry_name, &compatibility_entry) != 0) {
278 printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY);
279 return true;
280 }
281
282 std::string zip_content(compatibility_entry.uncompressed_length, '\0');
283 int32_t ret;
284 if ((ret = ExtractToMemory(package_zip, &compatibility_entry,
285 reinterpret_cast<uint8_t*>(&zip_content[0]),
286 compatibility_entry.uncompressed_length)) != 0) {
287 printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret));
288 return false;
289 }
290
291 ZipArchiveHandle zip_handle;
292 ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())),
293 zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle);
294 if (ret != 0) {
295 printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret));
296 return false;
297 }
298
299 // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents.
300 void* cookie;
301 ret = StartIteration(zip_handle, &cookie, nullptr, nullptr);
302 if (ret != 0) {
303 printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret));
304 CloseArchive(zip_handle);
305 return false;
306 }
307 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
308
309 std::vector<std::string> compatibility_info;
310 ZipEntry info_entry;
311 ZipString info_name;
312 while (Next(cookie, &info_entry, &info_name) == 0) {
313 std::string content(info_entry.uncompressed_length, '\0');
314 int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]),
315 info_entry.uncompressed_length);
316 if (ret != 0) {
317 printf("Failed to read %s: %s\n", info_name.name, ErrorCodeString(ret));
318 CloseArchive(zip_handle);
319 return false;
320 }
321 compatibility_info.emplace_back(std::move(content));
322 }
323 CloseArchive(zip_handle);
324
325 // VintfObjectRecovery::CheckCompatibility returns zero on success.
326 std::string err;
327 int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err);
328 if (result == 0) {
329 return true;
330 }
331
332 printf("Failed to verify package compatibility (result %i): %s\n", result, err.c_str());
333 return false;
334}
335#endif