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