mauronofrio | d8ee9dd | 2019-11-14 20:20:05 +0100 | [diff] [blame^] | 1 | <<<<<<< HEAD (fc79aa Encryption: don't try wrapped key if not needed) |
| 2 | ======= |
| 3 | /* |
| 4 | * Copyright (C) 2007 The Android Open Source Project |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #ifdef AB_OTA_UPDATER |
| 24 | #include <inttypes.h> |
| 25 | #include <map> |
| 26 | #include <android-base/parseint.h> |
| 27 | #include <android-base/stringprintf.h> |
| 28 | #include <android-base/strings.h> |
| 29 | #endif |
| 30 | #include <cutils/properties.h> |
| 31 | |
| 32 | #include "common.h" |
| 33 | #include "installcommand.h" |
| 34 | #include "zipwrap.hpp" |
| 35 | #ifndef USE_MINZIP |
| 36 | #include <ziparchive/zip_archive.h> |
| 37 | #include <vintf/VintfObjectRecovery.h> |
| 38 | #endif |
| 39 | #ifdef USE_OLD_VERIFIER |
| 40 | #include "verifier24/verifier.h" |
| 41 | #else |
| 42 | #include "verifier.h" |
| 43 | #endif |
| 44 | |
| 45 | #ifdef AB_OTA_UPDATER |
| 46 | |
| 47 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
| 48 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
| 49 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
| 50 | |
| 51 | // This function parses and returns the build.version.incremental |
| 52 | static int parse_build_number(std::string str) { |
| 53 | size_t pos = str.find("="); |
| 54 | if (pos != std::string::npos) { |
| 55 | std::string num_string = android::base::Trim(str.substr(pos+1)); |
| 56 | int build_number; |
| 57 | if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) { |
| 58 | return build_number; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | printf("Failed to parse build number in %s.\n", str.c_str()); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | bool read_metadata_from_package(ZipWrap* zip, std::string* meta_data) { |
| 67 | long size = zip->GetUncompressedSize(METADATA_PATH); |
| 68 | if (size <= 0) |
| 69 | return false; |
| 70 | |
| 71 | meta_data->resize(size, '\0'); |
| 72 | if (!zip->ExtractToBuffer(METADATA_PATH, reinterpret_cast<uint8_t*>(&(*meta_data)[0]))) { |
| 73 | printf("Failed to read metadata in update package.\n"); |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. |
| 80 | void read_source_target_build(ZipWrap* zip/*, std::vector<std::string>& log_buffer*/) { |
| 81 | std::string meta_data; |
| 82 | if (!read_metadata_from_package(zip, &meta_data)) { |
| 83 | return; |
| 84 | } |
| 85 | // Examples of the pre-build and post-build strings in metadata: |
| 86 | // pre-build-incremental=2943039 |
| 87 | // post-build-incremental=2951741 |
| 88 | std::vector<std::string> lines = android::base::Split(meta_data, "\n"); |
| 89 | for (const std::string& line : lines) { |
| 90 | std::string str = android::base::Trim(line); |
| 91 | if (android::base::StartsWith(str, "pre-build-incremental")){ |
| 92 | int source_build = parse_build_number(str); |
| 93 | if (source_build != -1) { |
| 94 | printf("source_build: %d\n", source_build); |
| 95 | /*log_buffer.push_back(android::base::StringPrintf("source_build: %d", |
| 96 | source_build));*/ |
| 97 | } |
| 98 | } else if (android::base::StartsWith(str, "post-build-incremental")) { |
| 99 | int target_build = parse_build_number(str); |
| 100 | if (target_build != -1) { |
| 101 | printf("target_build: %d\n", target_build); |
| 102 | /*log_buffer.push_back(android::base::StringPrintf("target_build: %d", |
| 103 | target_build));*/ |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Parses the metadata of the OTA package in |zip| and checks whether we are |
| 110 | // allowed to accept this A/B package. Downgrading is not allowed unless |
| 111 | // explicitly enabled in the package and only for incremental packages. |
| 112 | static int check_newer_ab_build(ZipWrap* zip) |
| 113 | { |
| 114 | std::string metadata_str; |
| 115 | if (!read_metadata_from_package(zip, &metadata_str)) { |
| 116 | return INSTALL_CORRUPT; |
| 117 | } |
| 118 | std::map<std::string, std::string> metadata; |
| 119 | for (const std::string& line : android::base::Split(metadata_str, "\n")) { |
| 120 | size_t eq = line.find('='); |
| 121 | if (eq != std::string::npos) { |
| 122 | metadata[line.substr(0, eq)] = line.substr(eq + 1); |
| 123 | } |
| 124 | } |
| 125 | char value[PROPERTY_VALUE_MAX]; |
| 126 | char propmodel[PROPERTY_VALUE_MAX]; |
| 127 | char propname[PROPERTY_VALUE_MAX]; |
| 128 | |
| 129 | property_get("ro.product.device", value, ""); |
| 130 | property_get("ro.product.model", propmodel, ""); |
| 131 | property_get("ro.product.name", propname, ""); |
| 132 | const std::string& pkg_device = metadata["pre-device"]; |
| 133 | |
| 134 | std::vector<std::string> assertResults = android::base::Split(pkg_device, ","); |
| 135 | |
| 136 | bool deviceExists = false; |
| 137 | |
| 138 | for(const std::string& deviceAssert : assertResults) |
| 139 | { |
| 140 | std::string assertName = android::base::Trim(deviceAssert); |
| 141 | if ((assertName == value || assertName == propmodel || assertName == propname ) && !assertName.empty()) { |
| 142 | deviceExists = true; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (!deviceExists) { |
| 148 | printf("Package is for product %s but expected %s\n", |
| 149 | pkg_device.c_str(), value); |
| 150 | return INSTALL_ERROR; |
| 151 | } |
| 152 | |
| 153 | // We allow the package to not have any serialno, but if it has a non-empty |
| 154 | // value it should match. |
| 155 | property_get("ro.serialno", value, ""); |
| 156 | const std::string& pkg_serial_no = metadata["serialno"]; |
| 157 | if (!pkg_serial_no.empty() && pkg_serial_no != value) { |
| 158 | printf("Package is for serial %s\n", pkg_serial_no.c_str()); |
| 159 | return INSTALL_ERROR; |
| 160 | } |
| 161 | |
| 162 | if (metadata["ota-type"] != "AB") { |
| 163 | printf("Package is not A/B\n"); |
| 164 | return INSTALL_ERROR; |
| 165 | } |
| 166 | |
| 167 | // Incremental updates should match the current build. |
| 168 | property_get("ro.build.version.incremental", value, ""); |
| 169 | const std::string& pkg_pre_build = metadata["pre-build-incremental"]; |
| 170 | if (!pkg_pre_build.empty() && pkg_pre_build != value) { |
| 171 | printf("Package is for source build %s but expected %s\n", |
| 172 | pkg_pre_build.c_str(), value); |
| 173 | return INSTALL_ERROR; |
| 174 | } |
| 175 | property_get("ro.build.fingerprint", value, ""); |
| 176 | const std::string& pkg_pre_build_fingerprint = metadata["pre-build"]; |
| 177 | if (!pkg_pre_build_fingerprint.empty() && |
| 178 | pkg_pre_build_fingerprint != value) { |
| 179 | printf("Package is for source build %s but expected %s\n", |
| 180 | pkg_pre_build_fingerprint.c_str(), value); |
| 181 | return INSTALL_ERROR; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int |
| 188 | abupdate_binary_command(const char* path, ZipWrap* zip, int retry_count __unused, |
| 189 | int status_fd, std::vector<std::string>* cmd) |
| 190 | { |
| 191 | read_source_target_build(zip); |
| 192 | int ret = check_newer_ab_build(zip); |
| 193 | if (ret) { |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | // For A/B updates we extract the payload properties to a buffer and obtain |
| 198 | // the RAW payload offset in the zip file. |
| 199 | if (!zip->EntryExists(AB_OTA_PAYLOAD_PROPERTIES)) { |
| 200 | printf("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 201 | return INSTALL_CORRUPT; |
| 202 | } |
| 203 | std::vector<unsigned char> payload_properties( |
| 204 | zip->GetUncompressedSize(AB_OTA_PAYLOAD_PROPERTIES)); |
| 205 | if (!zip->ExtractToBuffer(AB_OTA_PAYLOAD_PROPERTIES, payload_properties.data())) { |
| 206 | printf("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES); |
| 207 | return INSTALL_CORRUPT; |
| 208 | } |
| 209 | |
| 210 | if (!zip->EntryExists(AB_OTA_PAYLOAD)) { |
| 211 | printf("Can't find %s\n", AB_OTA_PAYLOAD); |
| 212 | return INSTALL_CORRUPT; |
| 213 | } |
| 214 | long payload_offset = zip->GetEntryOffset(AB_OTA_PAYLOAD); |
| 215 | *cmd = { |
| 216 | "/sbin/update_engine_sideload", |
| 217 | android::base::StringPrintf("--payload=file://%s", path), |
| 218 | android::base::StringPrintf("--offset=%ld", payload_offset), |
| 219 | "--headers=" + std::string(payload_properties.begin(), |
| 220 | payload_properties.end()), |
| 221 | android::base::StringPrintf("--status_fd=%d", status_fd), |
| 222 | }; |
| 223 | return INSTALL_SUCCESS; |
| 224 | } |
| 225 | |
| 226 | #else |
| 227 | |
| 228 | void read_source_target_build(ZipWrap* zip __unused /*, std::vector<std::string>& log_buffer*/) {return;} |
| 229 | |
| 230 | int |
| 231 | abupdate_binary_command(__unused const char* path, __unused ZipWrap* zip, __unused int retry_count, |
| 232 | __unused int status_fd, __unused std::vector<std::string>* cmd) |
| 233 | { |
| 234 | printf("No support for AB OTA zips included\n"); |
| 235 | return INSTALL_CORRUPT; |
| 236 | } |
| 237 | |
| 238 | #endif |
| 239 | |
| 240 | int |
| 241 | update_binary_command(const char* path, int retry_count, |
| 242 | int status_fd, std::vector<std::string>* cmd) |
| 243 | { |
| 244 | char charfd[16]; |
| 245 | sprintf(charfd, "%i", status_fd); |
| 246 | cmd->push_back(TMP_UPDATER_BINARY_PATH); |
| 247 | cmd->push_back(EXPAND(RECOVERY_API_VERSION)); |
| 248 | cmd->push_back(charfd); |
| 249 | cmd->push_back(path); |
| 250 | /**cmd = { |
| 251 | TMP_UPDATER_BINARY_PATH, |
| 252 | EXPAND(RECOVERY_API_VERSION), // defined in Android.mk |
| 253 | charfd, |
| 254 | path, |
| 255 | };*/ |
| 256 | if (retry_count > 0) |
| 257 | cmd->push_back("retry"); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | #ifdef USE_MINZIP |
| 262 | bool verify_package_compatibility(ZipWrap *package_zip) { |
| 263 | if (package_zip->EntryExists("compatibility.zip")) |
| 264 | printf("Cannot verify treble package compatibility, must build TWRP in Oreo tree or higher.\n"); |
| 265 | return true; |
| 266 | } |
| 267 | #else |
| 268 | // Verifes the compatibility info in a Treble-compatible package. Returns true directly if the |
| 269 | // entry doesn't exist. Note that the compatibility info is packed in a zip file inside the OTA |
| 270 | // package. |
| 271 | bool verify_package_compatibility(ZipWrap *zw) { |
| 272 | ZipArchiveHandle package_zip = zw->GetZipArchiveHandle(); |
| 273 | printf("Verifying package compatibility...\n"); |
| 274 | |
| 275 | static constexpr const char* COMPATIBILITY_ZIP_ENTRY = "compatibility.zip"; |
| 276 | ZipString compatibility_entry_name(COMPATIBILITY_ZIP_ENTRY); |
| 277 | ZipEntry compatibility_entry; |
| 278 | if (FindEntry(package_zip, compatibility_entry_name, &compatibility_entry) != 0) { |
| 279 | printf("Package doesn't contain %s entry\n", COMPATIBILITY_ZIP_ENTRY); |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | std::string zip_content(compatibility_entry.uncompressed_length, '\0'); |
| 284 | int32_t ret; |
| 285 | if ((ret = ExtractToMemory(package_zip, &compatibility_entry, |
| 286 | reinterpret_cast<uint8_t*>(&zip_content[0]), |
| 287 | compatibility_entry.uncompressed_length)) != 0) { |
| 288 | printf("Failed to read %s: %s\n", COMPATIBILITY_ZIP_ENTRY, ErrorCodeString(ret)); |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | ZipArchiveHandle zip_handle; |
| 293 | ret = OpenArchiveFromMemory(static_cast<void*>(const_cast<char*>(zip_content.data())), |
| 294 | zip_content.size(), COMPATIBILITY_ZIP_ENTRY, &zip_handle); |
| 295 | if (ret != 0) { |
| 296 | printf("Failed to OpenArchiveFromMemory: %s\n", ErrorCodeString(ret)); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | // Iterate all the entries inside COMPATIBILITY_ZIP_ENTRY and read the contents. |
| 301 | void* cookie; |
| 302 | ret = StartIteration(zip_handle, &cookie, nullptr, nullptr); |
| 303 | if (ret != 0) { |
| 304 | printf("Failed to start iterating zip entries: %s\n", ErrorCodeString(ret)); |
| 305 | CloseArchive(zip_handle); |
| 306 | return false; |
| 307 | } |
| 308 | std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); |
| 309 | |
| 310 | std::vector<std::string> compatibility_info; |
| 311 | ZipEntry info_entry; |
| 312 | ZipString info_name; |
| 313 | while (Next(cookie, &info_entry, &info_name) == 0) { |
| 314 | std::string content(info_entry.uncompressed_length, '\0'); |
| 315 | int32_t ret = ExtractToMemory(zip_handle, &info_entry, reinterpret_cast<uint8_t*>(&content[0]), |
| 316 | info_entry.uncompressed_length); |
| 317 | if (ret != 0) { |
| 318 | printf("Failed to read %s: %s\n", info_name.name, ErrorCodeString(ret)); |
| 319 | CloseArchive(zip_handle); |
| 320 | return false; |
| 321 | } |
| 322 | compatibility_info.emplace_back(std::move(content)); |
| 323 | } |
| 324 | CloseArchive(zip_handle); |
| 325 | |
| 326 | // VintfObjectRecovery::CheckCompatibility returns zero on success. TODO THIS CAUSES A WEIRD COMPILE ERROR |
| 327 | std::string err; |
| 328 | int result = android::vintf::VintfObjectRecovery::CheckCompatibility(compatibility_info, &err); |
| 329 | if (result == 0) { |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | printf("Failed to verify package compatibility (result %i): %s\n", result, err.c_str()); |
| 334 | return false; |
| 335 | } |
| 336 | #endif |
| 337 | >>>>>>> CHANGE (1913bf Make the twrp verify the pre-device among several props) |