Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 1 | /* |
| 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 Yonker | d9918b7 | 2017-09-15 08:17:42 -0500 | [diff] [blame] | 22 | #include <inttypes.h> |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 23 | #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 Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 32 | #include "zipwrap.hpp" |
| 33 | #ifndef USE_MINZIP |
| 34 | #include <ziparchive/zip_archive.h> |
| 35 | #include <vintf/VintfObjectRecovery.h> |
| 36 | #endif |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 37 | #ifdef USE_OLD_VERIFIER |
| 38 | #include "verifier24/verifier.h" |
| 39 | #else |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 40 | #include "install/install.h" |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #ifdef AB_OTA_UPDATER |
| 44 | |
| 45 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
| 46 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
| 47 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
| 48 | |
| 49 | // This function parses and returns the build.version.incremental |
| 50 | static 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 Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 64 | bool 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 68 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 69 | meta_data->resize(size, '\0'); |
| 70 | if (!zip->ExtractToBuffer(METADATA_PATH, reinterpret_cast<uint8_t*>(&(*meta_data)[0]))) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 71 | 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 Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 78 | void read_source_target_build(ZipWrap* zip/*, std::vector<std::string>& log_buffer*/) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 79 | 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 Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 92 | printf("source_build: %d\n", source_build); |
| 93 | /*log_buffer.push_back(android::base::StringPrintf("source_build: %d", |
| 94 | source_build));*/ |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 95 | } |
| 96 | } else if (android::base::StartsWith(str, "post-build-incremental")) { |
| 97 | int target_build = parse_build_number(str); |
| 98 | if (target_build != -1) { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 99 | printf("target_build: %d\n", target_build); |
| 100 | /*log_buffer.push_back(android::base::StringPrintf("target_build: %d", |
| 101 | target_build));*/ |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 102 | } |
| 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. |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 110 | static int check_newer_ab_build(ZipWrap* zip) |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 111 | { |
| 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]; |
| 124 | |
| 125 | property_get("ro.product.device", value, ""); |
| 126 | const std::string& pkg_device = metadata["pre-device"]; |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 127 | if (pkg_device != value || pkg_device.empty()) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 128 | printf("Package is for product %s but expected %s\n", |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 129 | pkg_device.c_str(), value); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 130 | return INSTALL_ERROR; |
| 131 | } |
| 132 | |
| 133 | // We allow the package to not have any serialno, but if it has a non-empty |
| 134 | // value it should match. |
| 135 | property_get("ro.serialno", value, ""); |
| 136 | const std::string& pkg_serial_no = metadata["serialno"]; |
| 137 | if (!pkg_serial_no.empty() && pkg_serial_no != value) { |
| 138 | printf("Package is for serial %s\n", pkg_serial_no.c_str()); |
| 139 | return INSTALL_ERROR; |
| 140 | } |
| 141 | |
| 142 | if (metadata["ota-type"] != "AB") { |
| 143 | printf("Package is not A/B\n"); |
| 144 | return INSTALL_ERROR; |
| 145 | } |
| 146 | |
| 147 | // Incremental updates should match the current build. |
| 148 | property_get("ro.build.version.incremental", value, ""); |
| 149 | const std::string& pkg_pre_build = metadata["pre-build-incremental"]; |
| 150 | if (!pkg_pre_build.empty() && pkg_pre_build != value) { |
| 151 | printf("Package is for source build %s but expected %s\n", |
| 152 | pkg_pre_build.c_str(), value); |
| 153 | return INSTALL_ERROR; |
| 154 | } |
| 155 | property_get("ro.build.fingerprint", value, ""); |
| 156 | const std::string& pkg_pre_build_fingerprint = metadata["pre-build"]; |
| 157 | if (!pkg_pre_build_fingerprint.empty() && |
| 158 | pkg_pre_build_fingerprint != value) { |
| 159 | printf("Package is for source build %s but expected %s\n", |
| 160 | pkg_pre_build_fingerprint.c_str(), value); |
| 161 | return INSTALL_ERROR; |
| 162 | } |
| 163 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 168 | abupdate_binary_command(const char* path, ZipWrap* zip, int retry_count __unused, |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 169 | int status_fd, std::vector<std::string>* cmd) |
| 170 | { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 171 | read_source_target_build(zip); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 172 | int ret = check_newer_ab_build(zip); |
| 173 | if (ret) { |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | // For A/B updates we extract the payload properties to a buffer and obtain |
| 178 | // the RAW payload offset in the zip file. |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 179 | if (!zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 180 | printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 181 | return INSTALL_CORRUPT; |
| 182 | } |
| 183 | std::vector<unsigned char> payload_properties( |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 184 | zip->GetUncompressedSize(AB_OTA_PAYLOAD_PROPERTIES)); |
| 185 | if (!zip->ExtractToBuffer(AB_OTA_PAYLOAD_PROPERTIES, payload_properties.data())) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 186 | printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 187 | return INSTALL_CORRUPT; |
| 188 | } |
| 189 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 190 | if (!zip->EntryExists(AB_OTA_PAYLOAD)) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 191 | printf("Can't find %s\n", AB_OTA_PAYLOAD); |
| 192 | return INSTALL_CORRUPT; |
| 193 | } |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 194 | long payload_offset = zip->GetEntryOffset(AB_OTA_PAYLOAD); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 195 | *cmd = { |
| 196 | "/sbin/update_engine_sideload", |
| 197 | android::base::StringPrintf("--payload=file://%s", path), |
| 198 | android::base::StringPrintf("--offset=%ld", payload_offset), |
| 199 | "--headers=" + std::string(payload_properties.begin(), |
| 200 | payload_properties.end()), |
| 201 | android::base::StringPrintf("--status_fd=%d", status_fd), |
| 202 | }; |
| 203 | return INSTALL_SUCCESS; |
| 204 | } |
| 205 | |
| 206 | #else |
| 207 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 208 | void read_source_target_build(ZipWrap* zip __unused /*, std::vector<std::string>& log_buffer*/) {return;} |
| 209 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 210 | int |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 211 | abupdate_binary_command(__unused const char* path, __unused ZipWrap* zip, __unused int retry_count, |
| 212 | __unused int status_fd, __unused std::vector<std::string>* cmd) |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 213 | { |
| 214 | printf("No support for AB OTA zips included\n"); |
| 215 | return INSTALL_CORRUPT; |
| 216 | } |
| 217 | |
| 218 | #endif |
| 219 | |
| 220 | int |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 221 | update_binary_command(const char* path, int retry_count, |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 222 | int status_fd, std::vector<std::string>* cmd) |
| 223 | { |
| 224 | char charfd[16]; |
| 225 | sprintf(charfd, "%i", status_fd); |
| 226 | cmd->push_back(TMP_UPDATER_BINARY_PATH); |
| 227 | cmd->push_back(EXPAND(RECOVERY_API_VERSION)); |
| 228 | cmd->push_back(charfd); |
| 229 | cmd->push_back(path); |
| 230 | /**cmd = { |
| 231 | TMP_UPDATER_BINARY_PATH, |
| 232 | EXPAND(RECOVERY_API_VERSION), // defined in Android.mk |
| 233 | charfd, |
| 234 | path, |
| 235 | };*/ |
| 236 | if (retry_count > 0) |
| 237 | cmd->push_back("retry"); |
| 238 | return 0; |
| 239 | } |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 240 | |
| 241 | #ifdef USE_MINZIP |
| 242 | bool verify_package_compatibility(ZipWrap *package_zip) { |
| 243 | if (package_zip->EntryExists("compatibility.zip")) |
| 244 | printf("Cannot verify treble package compatibility, must build TWRP in Oreo tree or higher.\n"); |
| 245 | return true; |
| 246 | } |
| 247 | #else |
| 248 | // Verifes the compatibility info in a Treble-compatible package. Returns true directly if the |
| 249 | // entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA |
| 250 | // package. |
| 251 | bool verify_package_compatibility(ZipWrap *zw) { |
| 252 | ZipArchiveHandle package_zip = zw->GetZipArchiveHandle(); |
| 253 | printf("Verifying package compatibility...\n"); |
| 254 | |
| 255 | static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip"; |
| 256 | ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY); |
| 257 | ZipEntry compatibility_entry; |
| 258 | if (FindEntry(package_zip, compatibility_entry_name, &compatibility_entry) != 0) { |
| 259 | printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY); |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | std::string zip_content(compatibility_entry.uncompressed_length, '\0'); |
| 264 | int32_t ret; |
| 265 | if ((ret = ExtractToMemory(package_zip, &compatibility_entry, |
| 266 | reinterpret_cast<uint8_t*>(&zip_content[0]), |
| 267 | compatibility_entry.uncompressed_length)) != 0) { |
| 268 | printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret)); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | ZipArchiveHandle zip_handle; |
| 273 | ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())), |
| 274 | zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle); |
| 275 | if (ret != 0) { |
| 276 | printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret)); |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents. |
| 281 | void* cookie; |
| 282 | ret = StartIteration(zip_handle, &cookie, nullptr, nullptr); |
| 283 | if (ret != 0) { |
| 284 | printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret)); |
| 285 | CloseArchive(zip_handle); |
| 286 | return false; |
| 287 | } |
| 288 | std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); |
| 289 | |
| 290 | std::vector<std::string> compatibility_info; |
| 291 | ZipEntry info_entry; |
| 292 | ZipString info_name; |
| 293 | while (Next(cookie, &info_entry, &info_name) == 0) { |
| 294 | std::string content(info_entry.uncompressed_length, '\0'); |
| 295 | int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]), |
| 296 | info_entry.uncompressed_length); |
| 297 | if (ret != 0) { |
| 298 | printf("Failed to read %s: %s\n", info_name.name, ErrorCodeString(ret)); |
| 299 | CloseArchive(zip_handle); |
| 300 | return false; |
| 301 | } |
| 302 | compatibility_info.emplace_back(std::move(content)); |
| 303 | } |
| 304 | CloseArchive(zip_handle); |
| 305 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 306 | // VintfObjectRecovery::CheckCompatibility returns zero on success. TODO THIS CAUSES A WEIRD COMPILE ERROR |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 307 | std::string err; |
| 308 | int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err); |
| 309 | if (result == 0) { |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | printf("Failed to verify package compatibility (result %i): %s\n", result, err.c_str()); |
| 314 | return false; |
| 315 | } |
| 316 | #endif |