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