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 <ziparchive/zip_archive.h> |
| 33 | #include <vintf/VintfObjectRecovery.h> |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 34 | #include "twinstall/install.h" |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 35 | |
| 36 | #ifdef AB_OTA_UPDATER |
| 37 | |
| 38 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
| 39 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
| 40 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
| 41 | |
| 42 | // This function parses and returns the build.version.incremental |
| 43 | static 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 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 57 | bool 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 64 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 65 | 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 79 | void read_source_target_build(ZipArchiveHandle zip/*, std::vector<std::string>& log_buffer*/) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 80 | 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 Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 93 | printf("source_build: %d\n", source_build); |
| 94 | /*log_buffer.push_back(android::base::StringPrintf("source_build: %d", |
| 95 | source_build));*/ |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 96 | } |
| 97 | } else if (android::base::StartsWith(str, "post-build-incremental")) { |
| 98 | int target_build = parse_build_number(str); |
| 99 | if (target_build != -1) { |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 100 | printf("target_build: %d\n", target_build); |
| 101 | /*log_buffer.push_back(android::base::StringPrintf("target_build: %d", |
| 102 | target_build));*/ |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 103 | } |
| 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. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 111 | static int check_newer_ab_build(ZipArchiveHandle zip) |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 112 | { |
| 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]; |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 125 | char propmodel[PROPERTY_VALUE_MAX]; |
| 126 | char propname[PROPERTY_VALUE_MAX]; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 127 | |
| 128 | property_get("ro.product.device", value, ""); |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 129 | property_get("ro.product.model", propmodel, ""); |
| 130 | property_get("ro.product.name", propname, ""); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 131 | const std::string& pkg_device = metadata["pre-device"]; |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 132 | |
| 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 147 | printf("Package is for product %s but expected %s\n", |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 148 | pkg_device.c_str(), value); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 149 | 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 187 | abupdate_binary_command(const char* path, int retry_count __unused, |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 188 | int status_fd, std::vector<std::string>* cmd) |
| 189 | { |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 190 | 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 198 | 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. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 204 | // 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 208 | printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 209 | return INSTALL_CORRUPT; |
| 210 | } |
| 211 | std::vector<unsigned char> payload_properties( |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 212 | 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 216 | printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 217 | CloseArchive(Zip); |
| 218 | return false; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 219 | } |
| 220 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 221 | 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 Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 224 | printf("Can't find %s\n", AB_OTA_PAYLOAD); |
| 225 | return INSTALL_CORRUPT; |
| 226 | } |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 227 | // long payload_offset = Zip->GetEntryOffset(AB_OTA_PAYLOAD); |
| 228 | long payload_offset = ab_ota_payload_entry.offset; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 229 | *cmd = { |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 230 | "/system/bin/update_engine_sideload", |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 231 | 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 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 242 | void read_source_target_build(ZipArchiveHandle zip __unused /*, std::vector<std::string>& log_buffer*/) {return;} |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 243 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 244 | int |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 245 | abupdate_binary_command(__unused const char* path, __unused int retry_count, |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 246 | __unused int status_fd, __unused std::vector<std::string>* cmd) |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 247 | { |
| 248 | printf("No support for AB OTA zips included\n"); |
| 249 | return INSTALL_CORRUPT; |
| 250 | } |
| 251 | |
| 252 | #endif |
| 253 | |
| 254 | int |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 255 | update_binary_command(const char* path, int retry_count, |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 256 | 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 Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 274 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 275 | // 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. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 278 | bool verify_package_compatibility(ZipArchiveHandle zw) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 279 | 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; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 284 | if (FindEntry(zw, compatibility_entry_name, &compatibility_entry) != 0) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 285 | 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; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame^] | 291 | if ((ret = ExtractToMemory(zw, &compatibility_entry, |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 292 | 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 Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 332 | // 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] | 333 | 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 | } |