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