The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <limits.h> |
Elliott Hughes | 26dbad2 | 2015-01-28 12:09:05 -0800 | [diff] [blame] | 21 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 23 | #include <sys/wait.h> |
| 24 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 26 | #include <chrono> |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 27 | #include <string> |
Tao Bao | 71e3e09 | 2016-02-02 14:02:27 -0800 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Tianjie Xu | fe16b5c | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 30 | #include <android-base/file.h> |
| 31 | #include <android-base/logging.h> |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 32 | #include <android-base/parseint.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
| 34 | #include <android-base/strings.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 35 | #include <ziparchive/zip_archive.h> |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 36 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | #include "common.h" |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 38 | #include "error_code.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 39 | #include "install.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | #include "minui/minui.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 41 | #include "otautil/SysUtil.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | #include "roots.h" |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 43 | #include "ui.h" |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 44 | #include "verifier.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 46 | #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 47 | #define PUBLIC_KEYS_FILE "/res/keys" |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 48 | static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; |
Tianjie Xu | fe16b5c | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 49 | 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] | 50 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 51 | // Default allocation of progress bar segments to operations |
| 52 | static const int VERIFICATION_PROGRESS_TIME = 60; |
| 53 | static const float VERIFICATION_PROGRESS_FRACTION = 0.25; |
| 54 | static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4; |
| 55 | static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1; |
| 56 | |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 57 | // This function parses and returns the build.version.incremental |
Chih-Hung Hsieh | 8b23811 | 2016-08-26 14:54:29 -0700 | [diff] [blame] | 58 | static int parse_build_number(const std::string& str) { |
| 59 | size_t pos = str.find('='); |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 60 | if (pos != std::string::npos) { |
| 61 | std::string num_string = android::base::Trim(str.substr(pos+1)); |
| 62 | int build_number; |
| 63 | if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) { |
| 64 | return build_number; |
| 65 | } |
| 66 | } |
| 67 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 68 | LOG(ERROR) << "Failed to parse build number in " << str; |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 72 | bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) { |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 73 | ZipString metadata_path(METADATA_PATH); |
| 74 | ZipEntry meta_entry; |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 75 | if (meta_data == nullptr) { |
| 76 | LOG(ERROR) << "string* meta_data can't be nullptr"; |
| 77 | return false; |
| 78 | } |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 79 | if (FindEntry(zip, metadata_path, &meta_entry) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 80 | LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package"; |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 81 | return false; |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 84 | meta_data->resize(meta_entry.uncompressed_length, '\0'); |
| 85 | if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]), |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 86 | meta_entry.uncompressed_length) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 87 | LOG(ERROR) << "Failed to read metadata in update package"; |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. |
| 94 | static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) { |
| 95 | std::string meta_data; |
| 96 | if (!read_metadata_from_package(zip, &meta_data)) { |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 99 | // Examples of the pre-build and post-build strings in metadata: |
| 100 | // pre-build-incremental=2943039 |
| 101 | // post-build-incremental=2951741 |
| 102 | std::vector<std::string> lines = android::base::Split(meta_data, "\n"); |
| 103 | for (const std::string& line : lines) { |
| 104 | std::string str = android::base::Trim(line); |
| 105 | if (android::base::StartsWith(str, "pre-build-incremental")){ |
| 106 | int source_build = parse_build_number(str); |
| 107 | if (source_build != -1) { |
| 108 | log_buffer.push_back(android::base::StringPrintf("source_build: %d", |
| 109 | source_build)); |
| 110 | } |
| 111 | } else if (android::base::StartsWith(str, "post-build-incremental")) { |
| 112 | int target_build = parse_build_number(str); |
| 113 | if (target_build != -1) { |
| 114 | log_buffer.push_back(android::base::StringPrintf("target_build: %d", |
| 115 | target_build)); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 121 | // If the package contains an update binary, extract it and run it. |
| 122 | static int |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 123 | try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache, |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 124 | std::vector<std::string>& log_buffer, int retry_count) |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 125 | { |
Tianjie Xu | b0ddae5 | 2016-06-08 14:30:04 -0700 | [diff] [blame] | 126 | read_source_target_build(zip, log_buffer); |
| 127 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 128 | ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME); |
| 129 | ZipEntry binary_entry; |
| 130 | if (FindEntry(zip, binary_name, &binary_entry) != 0) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 131 | return INSTALL_CORRUPT; |
| 132 | } |
| 133 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 134 | const char* binary = "/tmp/update_binary"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 135 | unlink(binary); |
| 136 | int fd = creat(binary, 0755); |
| 137 | if (fd < 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 138 | PLOG(ERROR) << "Can't make " << binary; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 139 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 140 | } |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 141 | int error = ExtractEntryToFile(zip, &binary_entry, fd); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 142 | close(fd); |
| 143 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 144 | if (error != 0) { |
| 145 | LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME |
| 146 | << " : " << ErrorCodeString(error); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 147 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int pipefd[2]; |
| 151 | pipe(pipefd); |
| 152 | |
| 153 | // When executing the update binary contained in the package, the |
| 154 | // arguments passed are: |
| 155 | // |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 156 | // - the version number for this interface |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 157 | // |
| 158 | // - an fd to which the program can write in order to update the |
| 159 | // progress bar. The program can write single-line commands: |
| 160 | // |
| 161 | // progress <frac> <secs> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 162 | // fill up the next <frac> part of of the progress bar |
| 163 | // over <secs> seconds. If <secs> is zero, use |
| 164 | // set_progress commands to manually control the |
Tao Bao | b07e1f3 | 2015-04-10 16:14:52 -0700 | [diff] [blame] | 165 | // progress of this segment of the bar. |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 166 | // |
| 167 | // set_progress <frac> |
| 168 | // <frac> should be between 0.0 and 1.0; sets the |
| 169 | // progress bar within the segment defined by the most |
| 170 | // recent progress command. |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 171 | // |
| 172 | // firmware <"hboot"|"radio"> <filename> |
| 173 | // arrange to install the contents of <filename> in the |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 174 | // given partition on reboot. |
| 175 | // |
| 176 | // (API v2: <filename> may start with "PACKAGE:" to |
| 177 | // indicate taking a file from the OTA package.) |
| 178 | // |
| 179 | // (API v3: this command no longer exists.) |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 180 | // |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 181 | // ui_print <string> |
| 182 | // display <string> on the screen. |
| 183 | // |
Tao Bao | b07e1f3 | 2015-04-10 16:14:52 -0700 | [diff] [blame] | 184 | // wipe_cache |
| 185 | // a wipe of cache will be performed following a successful |
| 186 | // installation. |
| 187 | // |
| 188 | // clear_display |
| 189 | // turn off the text display. |
| 190 | // |
| 191 | // enable_reboot |
| 192 | // packages can explicitly request that they want the user |
| 193 | // to be able to reboot during installation (useful for |
| 194 | // debugging packages that don't exit). |
| 195 | // |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 196 | // - the name of the package zip file. |
| 197 | // |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 198 | // - an optional argument "retry" if this update is a retry of a failed |
| 199 | // update attempt. |
| 200 | // |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 201 | |
Tianjie Xu | 64f46fb | 2016-06-03 15:44:52 -0700 | [diff] [blame] | 202 | const char* args[6]; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 203 | args[0] = binary; |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 204 | args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 205 | char temp[16]; |
| 206 | snprintf(temp, sizeof(temp), "%d", pipefd[1]); |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 207 | args[2] = temp; |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 208 | args[3] = path; |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 209 | args[4] = retry_count > 0 ? "retry" : NULL; |
| 210 | args[5] = NULL; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 211 | |
| 212 | pid_t pid = fork(); |
| 213 | if (pid == 0) { |
Alistair Strachan | 027429a | 2013-07-17 10:41:49 -0700 | [diff] [blame] | 214 | umask(022); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 215 | close(pipefd[0]); |
Yabin Cui | 4425c1d | 2016-02-10 13:47:32 -0800 | [diff] [blame] | 216 | execv(binary, const_cast<char**>(args)); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 217 | fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 218 | _exit(-1); |
| 219 | } |
| 220 | close(pipefd[1]); |
| 221 | |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 222 | *wipe_cache = false; |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 223 | bool retry_update = false; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 224 | |
Doug Zongker | 64893cc | 2009-07-14 16:31:56 -0700 | [diff] [blame] | 225 | char buffer[1024]; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 226 | FILE* from_child = fdopen(pipefd[0], "r"); |
| 227 | while (fgets(buffer, sizeof(buffer), from_child) != NULL) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 228 | char* command = strtok(buffer, " \n"); |
| 229 | if (command == NULL) { |
| 230 | continue; |
| 231 | } else if (strcmp(command, "progress") == 0) { |
| 232 | char* fraction_s = strtok(NULL, " \n"); |
| 233 | char* seconds_s = strtok(NULL, " \n"); |
| 234 | |
| 235 | float fraction = strtof(fraction_s, NULL); |
| 236 | int seconds = strtol(seconds_s, NULL, 10); |
| 237 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 238 | ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 239 | } else if (strcmp(command, "set_progress") == 0) { |
| 240 | char* fraction_s = strtok(NULL, " \n"); |
| 241 | float fraction = strtof(fraction_s, NULL); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 242 | ui->SetProgress(fraction); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 243 | } else if (strcmp(command, "ui_print") == 0) { |
| 244 | char* str = strtok(NULL, "\n"); |
| 245 | if (str) { |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 246 | ui->PrintOnScreenOnly("%s", str); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 247 | } else { |
Tao Bao | b6918c7 | 2015-05-19 17:02:16 -0700 | [diff] [blame] | 248 | ui->PrintOnScreenOnly("\n"); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 249 | } |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 250 | fflush(stdout); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 251 | } else if (strcmp(command, "wipe_cache") == 0) { |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 252 | *wipe_cache = true; |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 253 | } else if (strcmp(command, "clear_display") == 0) { |
| 254 | ui->SetBackground(RecoveryUI::NONE); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 255 | } else if (strcmp(command, "enable_reboot") == 0) { |
| 256 | // packages can explicitly request that they want the user |
| 257 | // to be able to reboot during installation (useful for |
| 258 | // debugging packages that don't exit). |
| 259 | ui->SetEnableReboot(true); |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 260 | } else if (strcmp(command, "retry_update") == 0) { |
| 261 | retry_update = true; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 262 | } else if (strcmp(command, "log") == 0) { |
| 263 | // Save the logging request from updater and write to |
| 264 | // last_install later. |
| 265 | log_buffer.push_back(std::string(strtok(NULL, "\n"))); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 266 | } else { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 267 | LOG(ERROR) << "unknown command [" << command << "]"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | fclose(from_child); |
| 271 | |
| 272 | int status; |
| 273 | waitpid(pid, &status, 0); |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 274 | if (retry_update) { |
| 275 | return INSTALL_RETRY; |
| 276 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 277 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 278 | LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 279 | return INSTALL_ERROR; |
| 280 | } |
| 281 | |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 282 | return INSTALL_SUCCESS; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 285 | static int |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 286 | really_install_package(const char *path, bool* wipe_cache, bool needs_mount, |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 287 | std::vector<std::string>& log_buffer, int retry_count) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 288 | { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 289 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 290 | ui->Print("Finding update package...\n"); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 291 | // Give verification half the progress bar... |
| 292 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 293 | ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME); |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 294 | LOG(INFO) << "Update location: " << path; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 295 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 296 | // Map the update package into memory. |
| 297 | ui->Print("Opening update package...\n"); |
| 298 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 299 | if (path && needs_mount) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 300 | if (path[0] == '@') { |
| 301 | ensure_path_mounted(path+1); |
| 302 | } else { |
| 303 | ensure_path_mounted(path); |
| 304 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 307 | MemMapping map; |
| 308 | if (sysMapFile(path, &map) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 309 | LOG(ERROR) << "failed to map file"; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 310 | return INSTALL_CORRUPT; |
| 311 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 312 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 313 | // Verify package. |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 314 | if (!verify_package(map.addr, map.length)) { |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 315 | log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure)); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 316 | sysReleaseMap(&map); |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 317 | return INSTALL_CORRUPT; |
| 318 | } |
| 319 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 320 | // Try to open the package. |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 321 | ZipArchiveHandle zip; |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 322 | int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 323 | if (err != 0) { |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 324 | LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err); |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 325 | log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); |
| 326 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 327 | sysReleaseMap(&map); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 328 | CloseArchive(zip); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 329 | return INSTALL_CORRUPT; |
| 330 | } |
| 331 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 332 | // Verify and install the contents of the package. |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 333 | ui->Print("Installing update...\n"); |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 334 | if (retry_count > 0) { |
| 335 | ui->Print("Retry attempt: %d\n", retry_count); |
| 336 | } |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 337 | ui->SetEnableReboot(false); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 338 | int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 339 | ui->SetEnableReboot(true); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 340 | ui->Print("\n"); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 341 | |
| 342 | sysReleaseMap(&map); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 343 | CloseArchive(zip); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 344 | return result; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 345 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 346 | |
| 347 | int |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 348 | install_package(const char* path, bool* wipe_cache, const char* install_file, |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 349 | bool needs_mount, int retry_count) |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 350 | { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 351 | modified_flash = true; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 352 | auto start = std::chrono::system_clock::now(); |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 353 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 354 | int result; |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 355 | std::vector<std::string> log_buffer; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 356 | if (setup_install_mounts() != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 357 | LOG(ERROR) << "failed to set up expected mounts for install; aborting"; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 358 | result = INSTALL_ERROR; |
| 359 | } else { |
Tianjie Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 360 | result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 361 | } |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 362 | |
Tao Bao | f4885ad | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 363 | // Measure the time spent to apply OTA update in seconds. |
| 364 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - start; |
| 365 | int time_total = static_cast<int>(duration.count()); |
Tianjie Xu | dd874b1 | 2016-05-13 12:13:15 -0700 | [diff] [blame] | 366 | |
Tao Bao | f4885ad | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 367 | if (ensure_path_mounted(UNCRYPT_STATUS) != 0) { |
| 368 | LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS; |
| 369 | } else { |
| 370 | std::string uncrypt_status; |
| 371 | if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) { |
| 372 | PLOG(WARNING) << "failed to read uncrypt status"; |
Tao Bao | ee9b955 | 2016-10-13 16:04:07 -0700 | [diff] [blame] | 373 | } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) { |
| 374 | LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status; |
Tianjie Xu | fe16b5c | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 375 | } else { |
Tao Bao | f4885ad | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 376 | log_buffer.push_back(android::base::Trim(uncrypt_status)); |
Tianjie Xu | fe16b5c | 2016-09-09 10:55:44 -0700 | [diff] [blame] | 377 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 378 | } |
Tao Bao | f4885ad | 2016-09-26 11:39:14 -0700 | [diff] [blame] | 379 | |
| 380 | // The first two lines need to be the package name and install result. |
| 381 | std::vector<std::string> log_header = { |
| 382 | path, |
| 383 | result == INSTALL_SUCCESS ? "1" : "0", |
| 384 | "time_total: " + std::to_string(time_total), |
| 385 | "retry: " + std::to_string(retry_count), |
| 386 | }; |
| 387 | std::string log_content = android::base::Join(log_header, "\n") + "\n" + |
| 388 | android::base::Join(log_buffer, "\n"); |
| 389 | if (!android::base::WriteStringToFile(log_content, install_file)) { |
| 390 | PLOG(ERROR) << "failed to write " << install_file; |
| 391 | } |
| 392 | |
| 393 | // Write a copy into last_log. |
| 394 | LOG(INFO) << log_content; |
| 395 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 396 | return result; |
| 397 | } |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 398 | |
| 399 | bool verify_package(const unsigned char* package_data, size_t package_size) { |
| 400 | std::vector<Certificate> loadedKeys; |
| 401 | if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) { |
| 402 | LOG(ERROR) << "Failed to load keys"; |
| 403 | return false; |
| 404 | } |
| 405 | LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE; |
| 406 | |
| 407 | // Verify package. |
| 408 | ui->Print("Verifying update package...\n"); |
| 409 | auto t0 = std::chrono::system_clock::now(); |
| 410 | int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys); |
| 411 | std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0; |
| 412 | ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err); |
| 413 | if (err != VERIFY_SUCCESS) { |
| 414 | LOG(ERROR) << "Signature verification failed"; |
| 415 | LOG(ERROR) << "error: " << kZipVerificationFailure; |
| 416 | return false; |
| 417 | } |
| 418 | return true; |
| 419 | } |