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