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