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> |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 33 | #include "twinstall/install.h" |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 34 | |
| 35 | #ifdef AB_OTA_UPDATER |
| 36 | |
| 37 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
| 38 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
| 39 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
| 40 | |
| 41 | // This function parses and returns the build.version.incremental |
| 42 | static int parse_build_number(std::string str) { |
| 43 | size_t pos = str.find("="); |
| 44 | if (pos != std::string::npos) { |
| 45 | std::string num_string = android::base::Trim(str.substr(pos+1)); |
| 46 | int build_number; |
| 47 | if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) { |
| 48 | return build_number; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | printf("Failed to parse build number in %s.\n", str.c_str()); |
| 53 | return -1; |
| 54 | } |
| 55 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 56 | bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) { |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 57 | std::string binary_name(METADATA_PATH); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 58 | ZipEntry binary_entry; |
| 59 | if (FindEntry(zip, binary_name, &binary_entry) == 0) { |
| 60 | long size = binary_entry.uncompressed_length; |
| 61 | if (size <= 0) |
| 62 | return false; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 63 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 64 | meta_data->resize(size, '\0'); |
| 65 | int32_t ret = ExtractToMemory(zip, &binary_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]), |
| 66 | size); |
| 67 | if (ret != 0) { |
| 68 | printf("Failed to read metadata in update package.\n"); |
| 69 | CloseArchive(zip); |
| 70 | return false; |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // 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] | 78 | 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] | 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. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 110 | static int check_newer_ab_build(ZipArchiveHandle 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]; |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 124 | char propmodel[PROPERTY_VALUE_MAX]; |
| 125 | char propname[PROPERTY_VALUE_MAX]; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 126 | |
| 127 | property_get("ro.product.device", value, ""); |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 128 | property_get("ro.product.model", propmodel, ""); |
| 129 | property_get("ro.product.name", propname, ""); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 130 | const std::string& pkg_device = metadata["pre-device"]; |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 131 | |
| 132 | std::vector<std::string> assertResults = android::base::Split(pkg_device, ","); |
| 133 | |
| 134 | bool deviceExists = false; |
| 135 | |
| 136 | for(const std::string& deviceAssert : assertResults) |
| 137 | { |
| 138 | std::string assertName = android::base::Trim(deviceAssert); |
| 139 | if ((assertName == value || assertName == propmodel || assertName == propname ) && !assertName.empty()) { |
| 140 | deviceExists = true; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (!deviceExists) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 146 | printf("Package is for product %s but expected %s\n", |
mauronofrio | c79f86e | 2020-05-18 16:08:12 -0400 | [diff] [blame] | 147 | pkg_device.c_str(), value); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 148 | return INSTALL_ERROR; |
| 149 | } |
| 150 | |
| 151 | // We allow the package to not have any serialno, but if it has a non-empty |
| 152 | // value it should match. |
| 153 | property_get("ro.serialno", value, ""); |
| 154 | const std::string& pkg_serial_no = metadata["serialno"]; |
| 155 | if (!pkg_serial_no.empty() && pkg_serial_no != value) { |
| 156 | printf("Package is for serial %s\n", pkg_serial_no.c_str()); |
| 157 | return INSTALL_ERROR; |
| 158 | } |
| 159 | |
| 160 | if (metadata["ota-type"] != "AB") { |
| 161 | printf("Package is not A/B\n"); |
| 162 | return INSTALL_ERROR; |
| 163 | } |
| 164 | |
| 165 | // Incremental updates should match the current build. |
| 166 | property_get("ro.build.version.incremental", value, ""); |
| 167 | const std::string& pkg_pre_build = metadata["pre-build-incremental"]; |
| 168 | if (!pkg_pre_build.empty() && pkg_pre_build != value) { |
| 169 | printf("Package is for source build %s but expected %s\n", |
| 170 | pkg_pre_build.c_str(), value); |
| 171 | return INSTALL_ERROR; |
| 172 | } |
| 173 | property_get("ro.build.fingerprint", value, ""); |
| 174 | const std::string& pkg_pre_build_fingerprint = metadata["pre-build"]; |
| 175 | if (!pkg_pre_build_fingerprint.empty() && |
| 176 | pkg_pre_build_fingerprint != value) { |
| 177 | printf("Package is for source build %s but expected %s\n", |
| 178 | pkg_pre_build_fingerprint.c_str(), value); |
| 179 | return INSTALL_ERROR; |
| 180 | } |
| 181 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 186 | abupdate_binary_command(const char* path, int retry_count __unused, |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 187 | int status_fd, std::vector<std::string>* cmd) |
| 188 | { |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 189 | auto package = Package::CreateMemoryPackage(path); |
| 190 | if (!package) { |
| 191 | return INSTALL_CORRUPT; |
| 192 | } |
| 193 | |
| 194 | ZipArchiveHandle Zip = package->GetZipArchiveHandle(); |
| 195 | read_source_target_build(Zip); |
| 196 | int ret = check_newer_ab_build(Zip); |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 197 | if (ret) { |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | // For A/B updates we extract the payload properties to a buffer and obtain |
| 202 | // the RAW payload offset in the zip file. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 203 | // if (!Zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) { |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 204 | std::string binary_name(AB_OTA_PAYLOAD_PROPERTIES); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 205 | ZipEntry binary_entry; |
| 206 | if (FindEntry(Zip, binary_name, &binary_entry) != 0) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 207 | printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 208 | return INSTALL_CORRUPT; |
| 209 | } |
| 210 | std::vector<unsigned char> payload_properties( |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 211 | binary_entry.uncompressed_length); |
| 212 | int32_t extract_ret = ExtractToMemory(Zip, &binary_entry, reinterpret_cast<uint8_t*>(payload_properties.data()), |
| 213 | binary_entry.uncompressed_length); |
| 214 | if (extract_ret != 0) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 215 | printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 216 | CloseArchive(Zip); |
| 217 | return false; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 218 | } |
| 219 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 220 | std::string ab_ota_payload(AB_OTA_PAYLOAD); |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 221 | ZipEntry ab_ota_payload_entry; |
| 222 | if (FindEntry(Zip, ab_ota_payload, &ab_ota_payload_entry) != 0) { |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 223 | printf("Can't find %s\n", AB_OTA_PAYLOAD); |
| 224 | return INSTALL_CORRUPT; |
| 225 | } |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 226 | // long payload_offset = Zip->GetEntryOffset(AB_OTA_PAYLOAD); |
| 227 | long payload_offset = ab_ota_payload_entry.offset; |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 228 | *cmd = { |
bigbiff | ad58e1b | 2020-07-06 20:24:34 -0400 | [diff] [blame] | 229 | "/system/bin/update_engine_sideload", |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 230 | android::base::StringPrintf("--payload=file://%s", path), |
| 231 | android::base::StringPrintf("--offset=%ld", payload_offset), |
| 232 | "--headers=" + std::string(payload_properties.begin(), |
| 233 | payload_properties.end()), |
| 234 | android::base::StringPrintf("--status_fd=%d", status_fd), |
| 235 | }; |
| 236 | return INSTALL_SUCCESS; |
| 237 | } |
| 238 | |
| 239 | #else |
| 240 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 241 | 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] | 242 | |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 243 | int |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 244 | abupdate_binary_command(__unused const char* path, __unused int retry_count, |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 245 | __unused int status_fd, __unused std::vector<std::string>* cmd) |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 246 | { |
| 247 | printf("No support for AB OTA zips included\n"); |
| 248 | return INSTALL_CORRUPT; |
| 249 | } |
| 250 | |
| 251 | #endif |
| 252 | |
| 253 | int |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 254 | update_binary_command(const char* path, int retry_count, |
Ethan Yonker | 941a899 | 2016-12-05 09:04:30 -0600 | [diff] [blame] | 255 | int status_fd, std::vector<std::string>* cmd) |
| 256 | { |
| 257 | char charfd[16]; |
| 258 | sprintf(charfd, "%i", status_fd); |
| 259 | cmd->push_back(TMP_UPDATER_BINARY_PATH); |
| 260 | cmd->push_back(EXPAND(RECOVERY_API_VERSION)); |
| 261 | cmd->push_back(charfd); |
| 262 | cmd->push_back(path); |
| 263 | /**cmd = { |
| 264 | TMP_UPDATER_BINARY_PATH, |
| 265 | EXPAND(RECOVERY_API_VERSION), // defined in Android.mk |
| 266 | charfd, |
| 267 | path, |
| 268 | };*/ |
| 269 | if (retry_count > 0) |
| 270 | cmd->push_back("retry"); |
| 271 | return 0; |
| 272 | } |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 273 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 274 | // Verifes the compatibility info in a Treble-compatible package. Returns true directly if the |
| 275 | // entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA |
| 276 | // package. |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 277 | bool verify_package_compatibility(ZipArchiveHandle zw) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 278 | printf("Verifying package compatibility...\n"); |
| 279 | |
| 280 | static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip"; |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 281 | std::string compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 282 | ZipEntry compatibility_entry; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 283 | if (FindEntry(zw, compatibility_entry_name, &compatibility_entry) != 0) { |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 284 | printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY); |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | std::string zip_content(compatibility_entry.uncompressed_length, '\0'); |
| 289 | int32_t ret; |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 290 | if ((ret = ExtractToMemory(zw, &compatibility_entry, |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 291 | reinterpret_cast<uint8_t*>(&zip_content[0]), |
| 292 | compatibility_entry.uncompressed_length)) != 0) { |
| 293 | printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret)); |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | ZipArchiveHandle zip_handle; |
| 298 | ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())), |
| 299 | zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle); |
| 300 | if (ret != 0) { |
| 301 | printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret)); |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents. |
| 306 | void* cookie; |
| 307 | ret = StartIteration(zip_handle, &cookie, nullptr, nullptr); |
| 308 | if (ret != 0) { |
| 309 | printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret)); |
| 310 | CloseArchive(zip_handle); |
| 311 | return false; |
| 312 | } |
| 313 | std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); |
| 314 | |
| 315 | std::vector<std::string> compatibility_info; |
| 316 | ZipEntry info_entry; |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 317 | std::string info_name; |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 318 | while (Next(cookie, &info_entry, &info_name) == 0) { |
| 319 | std::string content(info_entry.uncompressed_length, '\0'); |
| 320 | int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]), |
| 321 | info_entry.uncompressed_length); |
| 322 | if (ret != 0) { |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 323 | printf("Failed to read %s: %s\n", info_name.c_str(), ErrorCodeString(ret)); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 324 | CloseArchive(zip_handle); |
| 325 | return false; |
| 326 | } |
| 327 | compatibility_info.emplace_back(std::move(content)); |
| 328 | } |
| 329 | CloseArchive(zip_handle); |
| 330 | |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 331 | return true; |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 332 | } |