The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [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 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 17 | #include "install.h" |
| 18 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
Alex Deymo | 4e29ce0 | 2016-08-12 13:43:04 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <limits.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 24 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <sys/stat.h> |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 26 | #include <sys/wait.h> |
| 27 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 29 | #include <chrono> |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 30 | #include <functional> |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 31 | #include <limits> |
| 32 | #include <map> |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 33 | #include <string> |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 34 | #include <vector> |
| 35 | |
Tianjie Xu | e16e799 | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 36 | #include <android-base/file.h> |
| 37 | #include <android-base/logging.h> |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 38 | #include <android-base/parsedouble.h> |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 39 | #include <android-base/parseint.h> |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 40 | #include <android-base/properties.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 41 | #include <android-base/stringprintf.h> |
| 42 | #include <android-base/strings.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 43 | #include <ziparchive/zip_archive.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | #include "common.h" |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 46 | #include "error_code.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 47 | #include "minui/minui.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 48 | #include "otautil/SysUtil.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | #include "roots.h" |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 50 | #include "ui.h" |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 51 | #include "verifier.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 52 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 53 | #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 54 | static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; |
| 55 | static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 56 | #define PUBLIC_KEYS_FILE "/res/keys" |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 57 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
Tianjie Xu | e16e799 | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 58 | static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 59 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 60 | // Default allocation of progress bar segments to operations |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 61 | static constexpr int VERIFICATION_PROGRESS_TIME = 60; |
| 62 | static constexpr float VERIFICATION_PROGRESS_FRACTION = 0.25; |
| 63 | static constexpr float DEFAULT_FILES_PROGRESS_FRACTION = 0.4; |
| 64 | static constexpr float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1; |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 65 | |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 66 | // This function parses and returns the build.version.incremental |
Chih-Hung Hsieh | 8b23811 | 2016-08-26 14:54:29 -0700 | [diff] [blame] | 67 | static int parse_build_number(const std::string& str) { |
| 68 | size_t pos = str.find('='); |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 69 | if (pos != std::string::npos) { |
| 70 | std::string num_string = android::base::Trim(str.substr(pos+1)); |
| 71 | int build_number; |
| 72 | if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) { |
| 73 | return build_number; |
| 74 | } |
| 75 | } |
| 76 | |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 77 | LOG(ERROR) << "Failed to parse build number in " << str; |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 81 | bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) { |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 82 | ZipString metadata_path(METADATA_PATH); |
| 83 | ZipEntry meta_entry; |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 84 | if (meta_data == nullptr) { |
| 85 | LOG(ERROR) << "string* meta_data can't be nullptr"; |
| 86 | return false; |
| 87 | } |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 88 | if (FindEntry(zip, metadata_path, &meta_entry) != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 89 | LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package"; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 90 | return false; |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 93 | meta_data->resize(meta_entry.uncompressed_length, '\0'); |
| 94 | if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]), |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 95 | meta_entry.uncompressed_length) != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 96 | LOG(ERROR) << "Failed to read metadata in update package"; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 103 | static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) { |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 104 | std::string meta_data; |
| 105 | if (!read_metadata_from_package(zip, &meta_data)) { |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 106 | return; |
| 107 | } |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 108 | // Examples of the pre-build and post-build strings in metadata: |
| 109 | // pre-build-incremental=2943039 |
| 110 | // post-build-incremental=2951741 |
| 111 | std::vector<std::string> lines = android::base::Split(meta_data, "\n"); |
| 112 | for (const std::string& line : lines) { |
| 113 | std::string str = android::base::Trim(line); |
| 114 | if (android::base::StartsWith(str, "pre-build-incremental")){ |
| 115 | int source_build = parse_build_number(str); |
| 116 | if (source_build != -1) { |
| 117 | log_buffer.push_back(android::base::StringPrintf("source_build: %d", |
| 118 | source_build)); |
| 119 | } |
| 120 | } else if (android::base::StartsWith(str, "post-build-incremental")) { |
| 121 | int target_build = parse_build_number(str); |
| 122 | if (target_build != -1) { |
| 123 | log_buffer.push_back(android::base::StringPrintf("target_build: %d", |
| 124 | target_build)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 130 | // Extract the update binary from the open zip archive |zip| located at |path| |
| 131 | // and store into |cmd| the command line that should be called. The |status_fd| |
| 132 | // is the file descriptor the child process should use to report back the |
| 133 | // progress of the update. |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 134 | static int |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 135 | update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count, |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 136 | int status_fd, std::vector<std::string>* cmd); |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 137 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 138 | #ifdef AB_OTA_UPDATER |
| 139 | |
| 140 | // Parses the metadata of the OTA package in |zip| and checks whether we are |
| 141 | // allowed to accept this A/B package. Downgrading is not allowed unless |
| 142 | // explicitly enabled in the package and only for incremental packages. |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 143 | static int check_newer_ab_build(ZipArchiveHandle zip) { |
| 144 | std::string metadata_str; |
| 145 | if (!read_metadata_from_package(zip, &metadata_str)) { |
| 146 | return INSTALL_CORRUPT; |
| 147 | } |
| 148 | std::map<std::string, std::string> metadata; |
| 149 | for (const std::string& line : android::base::Split(metadata_str, "\n")) { |
| 150 | size_t eq = line.find('='); |
| 151 | if (eq != std::string::npos) { |
| 152 | metadata[line.substr(0, eq)] = line.substr(eq + 1); |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 153 | } |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 154 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 155 | |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 156 | std::string value = android::base::GetProperty("ro.product.device", ""); |
| 157 | const std::string& pkg_device = metadata["pre-device"]; |
| 158 | if (pkg_device != value || pkg_device.empty()) { |
| 159 | LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value; |
| 160 | return INSTALL_ERROR; |
| 161 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 162 | |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 163 | // We allow the package to not have any serialno, but if it has a non-empty |
| 164 | // value it should match. |
| 165 | value = android::base::GetProperty("ro.serialno", ""); |
| 166 | const std::string& pkg_serial_no = metadata["serialno"]; |
| 167 | if (!pkg_serial_no.empty() && pkg_serial_no != value) { |
| 168 | LOG(ERROR) << "Package is for serial " << pkg_serial_no; |
| 169 | return INSTALL_ERROR; |
| 170 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 171 | |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 172 | if (metadata["ota-type"] != "AB") { |
| 173 | LOG(ERROR) << "Package is not A/B"; |
| 174 | return INSTALL_ERROR; |
| 175 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 176 | |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 177 | // Incremental updates should match the current build. |
| 178 | value = android::base::GetProperty("ro.build.version.incremental", ""); |
| 179 | const std::string& pkg_pre_build = metadata["pre-build-incremental"]; |
| 180 | if (!pkg_pre_build.empty() && pkg_pre_build != value) { |
| 181 | LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value; |
| 182 | return INSTALL_ERROR; |
| 183 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 184 | |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 185 | value = android::base::GetProperty("ro.build.fingerprint", ""); |
| 186 | const std::string& pkg_pre_build_fingerprint = metadata["pre-build"]; |
| 187 | if (!pkg_pre_build_fingerprint.empty() && pkg_pre_build_fingerprint != value) { |
| 188 | LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint << " but expected " |
| 189 | << value; |
| 190 | return INSTALL_ERROR; |
| 191 | } |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 192 | |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 193 | // Check for downgrade version. |
| 194 | int64_t build_timestamp = |
| 195 | android::base::GetIntProperty("ro.build.date.utc", std::numeric_limits<int64_t>::max()); |
| 196 | int64_t pkg_post_timestamp = 0; |
| 197 | // We allow to full update to the same version we are running, in case there |
| 198 | // is a problem with the current copy of that version. |
| 199 | if (metadata["post-timestamp"].empty() || |
| 200 | !android::base::ParseInt(metadata["post-timestamp"].c_str(), &pkg_post_timestamp) || |
| 201 | pkg_post_timestamp < build_timestamp) { |
| 202 | if (metadata["ota-downgrade"] != "yes") { |
| 203 | LOG(ERROR) << "Update package is older than the current build, expected a build " |
| 204 | "newer than timestamp " |
| 205 | << build_timestamp << " but package has timestamp " << pkg_post_timestamp |
| 206 | << " and downgrade not allowed."; |
| 207 | return INSTALL_ERROR; |
| 208 | } |
| 209 | if (pkg_pre_build_fingerprint.empty()) { |
| 210 | LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed."; |
| 211 | return INSTALL_ERROR; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return 0; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 219 | update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count, |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 220 | int status_fd, std::vector<std::string>* cmd) |
| 221 | { |
| 222 | int ret = check_newer_ab_build(zip); |
| 223 | if (ret) { |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | // For A/B updates we extract the payload properties to a buffer and obtain |
| 228 | // the RAW payload offset in the zip file. |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 229 | ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES); |
| 230 | ZipEntry properties_entry; |
| 231 | if (FindEntry(zip, property_name, &properties_entry) != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 232 | LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 233 | return INSTALL_CORRUPT; |
| 234 | } |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 235 | std::vector<uint8_t> payload_properties( |
| 236 | properties_entry.uncompressed_length); |
| 237 | if (ExtractToMemory(zip, &properties_entry, payload_properties.data(), |
| 238 | properties_entry.uncompressed_length) != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 239 | LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 240 | return INSTALL_CORRUPT; |
| 241 | } |
| 242 | |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 243 | ZipString payload_name(AB_OTA_PAYLOAD); |
| 244 | ZipEntry payload_entry; |
| 245 | if (FindEntry(zip, payload_name, &payload_entry) != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 246 | LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 247 | return INSTALL_CORRUPT; |
| 248 | } |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 249 | long payload_offset = payload_entry.offset; |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 250 | *cmd = { |
| 251 | "/sbin/update_engine_sideload", |
| 252 | android::base::StringPrintf("--payload=file://%s", path), |
| 253 | android::base::StringPrintf("--offset=%ld", payload_offset), |
| 254 | "--headers=" + std::string(payload_properties.begin(), |
| 255 | payload_properties.end()), |
| 256 | android::base::StringPrintf("--status_fd=%d", status_fd), |
| 257 | }; |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | #else // !AB_OTA_UPDATER |
| 262 | |
| 263 | static int |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 264 | update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count, |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 265 | int status_fd, std::vector<std::string>* cmd) |
| 266 | { |
| 267 | // On traditional updates we extract the update binary from the package. |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 268 | ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME); |
| 269 | ZipEntry binary_entry; |
| 270 | if (FindEntry(zip, binary_name, &binary_entry) != 0) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 271 | return INSTALL_CORRUPT; |
| 272 | } |
| 273 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 274 | const char* binary = "/tmp/update_binary"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 275 | unlink(binary); |
| 276 | int fd = creat(binary, 0755); |
| 277 | if (fd < 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 278 | PLOG(ERROR) << "Can't make " << binary; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 279 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 280 | } |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 281 | int error = ExtractEntryToFile(zip, &binary_entry, fd); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 282 | close(fd); |
| 283 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 284 | if (error != 0) { |
| 285 | LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME |
| 286 | << " : " << ErrorCodeString(error); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 287 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 290 | *cmd = { |
| 291 | binary, |
| 292 | EXPAND(RECOVERY_API_VERSION), // defined in Android.mk |
| 293 | std::to_string(status_fd), |
| 294 | path, |
| 295 | }; |
| 296 | if (retry_count > 0) |
| 297 | cmd->push_back("retry"); |
| 298 | return 0; |
| 299 | } |
| 300 | #endif // !AB_OTA_UPDATER |
| 301 | |
| 302 | // If the package contains an update binary, extract it and run it. |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 303 | static int try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache, |
| 304 | std::vector<std::string>& log_buffer, int retry_count) { |
| 305 | read_source_target_build(zip, log_buffer); |
Alex Deymo | 4344d63 | 2016-08-03 21:03:53 -0700 | [diff] [blame] | 306 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 307 | int pipefd[2]; |
| 308 | pipe(pipefd); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 309 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 310 | std::vector<std::string> args; |
| 311 | int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args); |
| 312 | if (ret) { |
| 313 | close(pipefd[0]); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 314 | close(pipefd[1]); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 315 | return ret; |
| 316 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 317 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 318 | // When executing the update binary contained in the package, the |
| 319 | // arguments passed are: |
| 320 | // |
| 321 | // - the version number for this interface |
| 322 | // |
| 323 | // - an FD to which the program can write in order to update the |
| 324 | // progress bar. The program can write single-line commands: |
| 325 | // |
| 326 | // progress <frac> <secs> |
| 327 | // fill up the next <frac> part of of the progress bar |
| 328 | // over <secs> seconds. If <secs> is zero, use |
| 329 | // set_progress commands to manually control the |
| 330 | // progress of this segment of the bar. |
| 331 | // |
| 332 | // set_progress <frac> |
| 333 | // <frac> should be between 0.0 and 1.0; sets the |
| 334 | // progress bar within the segment defined by the most |
| 335 | // recent progress command. |
| 336 | // |
| 337 | // ui_print <string> |
| 338 | // display <string> on the screen. |
| 339 | // |
| 340 | // wipe_cache |
| 341 | // a wipe of cache will be performed following a successful |
| 342 | // installation. |
| 343 | // |
| 344 | // clear_display |
| 345 | // turn off the text display. |
| 346 | // |
| 347 | // enable_reboot |
| 348 | // packages can explicitly request that they want the user |
| 349 | // to be able to reboot during installation (useful for |
| 350 | // debugging packages that don't exit). |
| 351 | // |
| 352 | // retry_update |
| 353 | // updater encounters some issue during the update. It requests |
| 354 | // a reboot to retry the same package automatically. |
| 355 | // |
| 356 | // log <string> |
| 357 | // updater requests logging the string (e.g. cause of the |
| 358 | // failure). |
| 359 | // |
| 360 | // - the name of the package zip file. |
| 361 | // |
| 362 | // - an optional argument "retry" if this update is a retry of a failed |
| 363 | // update attempt. |
| 364 | // |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 365 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 366 | // Convert the vector to a NULL-terminated char* array suitable for execv. |
| 367 | const char* chr_args[args.size() + 1]; |
| 368 | chr_args[args.size()] = nullptr; |
| 369 | for (size_t i = 0; i < args.size(); i++) { |
| 370 | chr_args[i] = args[i].c_str(); |
| 371 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 372 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 373 | pid_t pid = fork(); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 374 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 375 | if (pid == -1) { |
| 376 | close(pipefd[0]); |
| 377 | close(pipefd[1]); |
| 378 | PLOG(ERROR) << "Failed to fork update binary"; |
| 379 | return INSTALL_ERROR; |
| 380 | } |
| 381 | |
| 382 | if (pid == 0) { |
| 383 | umask(022); |
| 384 | close(pipefd[0]); |
| 385 | execv(chr_args[0], const_cast<char**>(chr_args)); |
Tianjie Xu | ab1abae | 2017-01-30 16:48:52 -0800 | [diff] [blame] | 386 | // Bug: 34769056 |
| 387 | // We shouldn't use LOG/PLOG in the forked process, since they may cause |
| 388 | // the child process to hang. This deadlock results from an improperly |
| 389 | // copied mutex in the ui functions. |
| 390 | fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno)); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 391 | _exit(EXIT_FAILURE); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 392 | } |
| 393 | close(pipefd[1]); |
| 394 | |
| 395 | *wipe_cache = false; |
| 396 | bool retry_update = false; |
| 397 | |
| 398 | char buffer[1024]; |
| 399 | FILE* from_child = fdopen(pipefd[0], "r"); |
| 400 | while (fgets(buffer, sizeof(buffer), from_child) != nullptr) { |
| 401 | std::string line(buffer); |
| 402 | size_t space = line.find_first_of(" \n"); |
| 403 | std::string command(line.substr(0, space)); |
| 404 | if (command.empty()) continue; |
| 405 | |
| 406 | // Get rid of the leading and trailing space and/or newline. |
| 407 | std::string args = space == std::string::npos ? "" : android::base::Trim(line.substr(space)); |
| 408 | |
| 409 | if (command == "progress") { |
| 410 | std::vector<std::string> tokens = android::base::Split(args, " "); |
| 411 | double fraction; |
| 412 | int seconds; |
| 413 | if (tokens.size() == 2 && android::base::ParseDouble(tokens[0].c_str(), &fraction) && |
| 414 | android::base::ParseInt(tokens[1], &seconds)) { |
| 415 | ui->ShowProgress(fraction * (1 - VERIFICATION_PROGRESS_FRACTION), seconds); |
| 416 | } else { |
| 417 | LOG(ERROR) << "invalid \"progress\" parameters: " << line; |
| 418 | } |
| 419 | } else if (command == "set_progress") { |
| 420 | std::vector<std::string> tokens = android::base::Split(args, " "); |
| 421 | double fraction; |
| 422 | if (tokens.size() == 1 && android::base::ParseDouble(tokens[0].c_str(), &fraction)) { |
| 423 | ui->SetProgress(fraction); |
| 424 | } else { |
| 425 | LOG(ERROR) << "invalid \"set_progress\" parameters: " << line; |
| 426 | } |
| 427 | } else if (command == "ui_print") { |
Tao Bao | f013642 | 2017-01-21 13:03:25 -0800 | [diff] [blame] | 428 | ui->PrintOnScreenOnly("%s\n", args.c_str()); |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 429 | fflush(stdout); |
| 430 | } else if (command == "wipe_cache") { |
| 431 | *wipe_cache = true; |
| 432 | } else if (command == "clear_display") { |
| 433 | ui->SetBackground(RecoveryUI::NONE); |
| 434 | } else if (command == "enable_reboot") { |
| 435 | // packages can explicitly request that they want the user |
| 436 | // to be able to reboot during installation (useful for |
| 437 | // debugging packages that don't exit). |
| 438 | ui->SetEnableReboot(true); |
| 439 | } else if (command == "retry_update") { |
| 440 | retry_update = true; |
| 441 | } else if (command == "log") { |
| 442 | if (!args.empty()) { |
| 443 | // Save the logging request from updater and write to last_install later. |
| 444 | log_buffer.push_back(args); |
| 445 | } else { |
| 446 | LOG(ERROR) << "invalid \"log\" parameters: " << line; |
| 447 | } |
| 448 | } else { |
| 449 | LOG(ERROR) << "unknown command [" << command << "]"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 450 | } |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 451 | } |
| 452 | fclose(from_child); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 453 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 454 | int status; |
| 455 | waitpid(pid, &status, 0); |
| 456 | if (retry_update) { |
| 457 | return INSTALL_RETRY; |
| 458 | } |
| 459 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 460 | LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")"; |
| 461 | return INSTALL_ERROR; |
| 462 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 463 | |
Tao Bao | 20c581e | 2016-12-28 20:55:51 -0800 | [diff] [blame] | 464 | return INSTALL_SUCCESS; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 467 | static int |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 468 | really_install_package(const char *path, bool* wipe_cache, bool needs_mount, |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 469 | std::vector<std::string>& log_buffer, int retry_count) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 470 | { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 471 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 472 | ui->Print("Finding update package...\n"); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 473 | // Give verification half the progress bar... |
| 474 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 475 | ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME); |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 476 | LOG(INFO) << "Update location: " << path; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 477 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 478 | // Map the update package into memory. |
| 479 | ui->Print("Opening update package...\n"); |
| 480 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 481 | if (path && needs_mount) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 482 | if (path[0] == '@') { |
| 483 | ensure_path_mounted(path+1); |
| 484 | } else { |
| 485 | ensure_path_mounted(path); |
| 486 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 489 | MemMapping map; |
| 490 | if (sysMapFile(path, &map) != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 491 | LOG(ERROR) << "failed to map file"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 492 | return INSTALL_CORRUPT; |
| 493 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 494 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 495 | // Verify package. |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 496 | if (!verify_package(map.addr, map.length)) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 497 | log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 498 | sysReleaseMap(&map); |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 499 | return INSTALL_CORRUPT; |
| 500 | } |
| 501 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 502 | // Try to open the package. |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 503 | ZipArchiveHandle zip; |
Tianjie Xu | 8176cf2 | 2016-10-18 14:13:07 -0700 | [diff] [blame] | 504 | int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 505 | if (err != 0) { |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 506 | LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 507 | log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); |
| 508 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 509 | sysReleaseMap(&map); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 510 | CloseArchive(zip); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 511 | return INSTALL_CORRUPT; |
| 512 | } |
| 513 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 514 | // Verify and install the contents of the package. |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 515 | ui->Print("Installing update...\n"); |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 516 | if (retry_count > 0) { |
| 517 | ui->Print("Retry attempt: %d\n", retry_count); |
| 518 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 519 | ui->SetEnableReboot(false); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 520 | int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 521 | ui->SetEnableReboot(true); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 522 | ui->Print("\n"); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 523 | |
| 524 | sysReleaseMap(&map); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 525 | CloseArchive(zip); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 526 | return result; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 527 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 528 | |
| 529 | int |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 530 | install_package(const char* path, bool* wipe_cache, const char* install_file, |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 531 | bool needs_mount, int retry_count) |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 532 | { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 533 | modified_flash = true; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 534 | auto start = std::chrono::system_clock::now(); |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 535 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 536 | int result; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 537 | std::vector<std::string> log_buffer; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 538 | if (setup_install_mounts() != 0) { |
Tianjie Xu | c21edd4 | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 539 | LOG(ERROR) << "failed to set up expected mounts for install; aborting"; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 540 | result = INSTALL_ERROR; |
| 541 | } else { |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 542 | result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 543 | } |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 544 | |
Tao Bao | badaac4 | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 545 | // Measure the time spent to apply OTA update in seconds. |
| 546 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - start; |
| 547 | int time_total = static_cast<int>(duration.count()); |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 548 | |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame^] | 549 | bool has_cache = volume_for_path("/cache") != nullptr; |
| 550 | // Skip logging the uncrypt_status on devices without /cache. |
| 551 | if (has_cache) { |
| 552 | if (ensure_path_mounted(UNCRYPT_STATUS) != 0) { |
Tao Bao | badaac4 | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 553 | LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS; |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame^] | 554 | } else { |
Tao Bao | badaac4 | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 555 | std::string uncrypt_status; |
| 556 | if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) { |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame^] | 557 | PLOG(WARNING) << "failed to read uncrypt status"; |
Tianjie Xu | 68fc81e | 2016-09-24 15:31:34 -0700 | [diff] [blame] | 558 | } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) { |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame^] | 559 | LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status; |
Tianjie Xu | e16e799 | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 560 | } else { |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame^] | 561 | log_buffer.push_back(android::base::Trim(uncrypt_status)); |
Tianjie Xu | e16e799 | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 562 | } |
Tianjie Xu | a286778 | 2017-03-24 14:13:56 -0700 | [diff] [blame^] | 563 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 564 | } |
Tao Bao | badaac4 | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 565 | |
| 566 | // The first two lines need to be the package name and install result. |
| 567 | std::vector<std::string> log_header = { |
| 568 | path, |
| 569 | result == INSTALL_SUCCESS ? "1" : "0", |
| 570 | "time_total: " + std::to_string(time_total), |
| 571 | "retry: " + std::to_string(retry_count), |
| 572 | }; |
| 573 | std::string log_content = android::base::Join(log_header, "\n") + "\n" + |
| 574 | android::base::Join(log_buffer, "\n"); |
| 575 | if (!android::base::WriteStringToFile(log_content, install_file)) { |
| 576 | PLOG(ERROR) << "failed to write " << install_file; |
| 577 | } |
| 578 | |
| 579 | // Write a copy into last_log. |
| 580 | LOG(INFO) << log_content; |
| 581 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 582 | return result; |
| 583 | } |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 584 | |
| 585 | bool verify_package(const unsigned char* package_data, size_t package_size) { |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 586 | std::vector<Certificate> loadedKeys; |
| 587 | if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) { |
| 588 | LOG(ERROR) << "Failed to load keys"; |
| 589 | return false; |
| 590 | } |
| 591 | LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 592 | |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 593 | // Verify package. |
| 594 | ui->Print("Verifying update package...\n"); |
| 595 | auto t0 = std::chrono::system_clock::now(); |
Tao Bao | 76fdb24 | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 596 | int err = verify_file(package_data, package_size, loadedKeys, |
Tao Bao | 5e53501 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 597 | std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1)); |
| 598 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; |
| 599 | ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); |
| 600 | if (err != VERIFY_SUCCESS) { |
| 601 | LOG(ERROR) << "Signature verification failed"; |
| 602 | LOG(ERROR) << "error: " << kZipVerificationFailure; |
| 603 | return false; |
| 604 | } |
| 605 | return true; |
Yabin Cui | 6faf026 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 606 | } |