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