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