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 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | #include "common.h" |
| 27 | #include "install.h" |
| 28 | #include "mincrypt/rsa.h" |
| 29 | #include "minui/minui.h" |
| 30 | #include "minzip/SysUtil.h" |
| 31 | #include "minzip/Zip.h" |
| 32 | #include "mtdutils/mounts.h" |
| 33 | #include "mtdutils/mtdutils.h" |
| 34 | #include "roots.h" |
| 35 | #include "verifier.h" |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 36 | #include "ui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 38 | extern RecoveryUI* ui; |
| 39 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 40 | #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 41 | #define PUBLIC_KEYS_FILE "/res/keys" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 43 | // Default allocation of progress bar segments to operations |
| 44 | static const int VERIFICATION_PROGRESS_TIME = 60; |
| 45 | static const float VERIFICATION_PROGRESS_FRACTION = 0.25; |
| 46 | static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4; |
| 47 | static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1; |
| 48 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 49 | // If the package contains an update binary, extract it and run it. |
| 50 | static int |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 51 | try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 52 | const ZipEntry* binary_entry = |
| 53 | mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); |
| 54 | if (binary_entry == NULL) { |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 55 | mzCloseZipArchive(zip); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 56 | return INSTALL_CORRUPT; |
| 57 | } |
| 58 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 59 | const char* binary = "/tmp/update_binary"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 60 | unlink(binary); |
| 61 | int fd = creat(binary, 0755); |
| 62 | if (fd < 0) { |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 63 | mzCloseZipArchive(zip); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 64 | LOGE("Can't make %s\n", binary); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 65 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 66 | } |
| 67 | bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); |
| 68 | close(fd); |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 69 | mzCloseZipArchive(zip); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 70 | |
| 71 | if (!ok) { |
| 72 | LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 73 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | int pipefd[2]; |
| 77 | pipe(pipefd); |
| 78 | |
| 79 | // When executing the update binary contained in the package, the |
| 80 | // arguments passed are: |
| 81 | // |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 82 | // - the version number for this interface |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 83 | // |
| 84 | // - an fd to which the program can write in order to update the |
| 85 | // progress bar. The program can write single-line commands: |
| 86 | // |
| 87 | // progress <frac> <secs> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 88 | // fill up the next <frac> part of of the progress bar |
| 89 | // over <secs> seconds. If <secs> is zero, use |
| 90 | // set_progress commands to manually control the |
| 91 | // progress of this segment of the bar |
| 92 | // |
| 93 | // set_progress <frac> |
| 94 | // <frac> should be between 0.0 and 1.0; sets the |
| 95 | // progress bar within the segment defined by the most |
| 96 | // recent progress command. |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 97 | // |
| 98 | // firmware <"hboot"|"radio"> <filename> |
| 99 | // arrange to install the contents of <filename> in the |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 100 | // given partition on reboot. |
| 101 | // |
| 102 | // (API v2: <filename> may start with "PACKAGE:" to |
| 103 | // indicate taking a file from the OTA package.) |
| 104 | // |
| 105 | // (API v3: this command no longer exists.) |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 106 | // |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 107 | // ui_print <string> |
| 108 | // display <string> on the screen. |
| 109 | // |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 110 | // - the name of the package zip file. |
| 111 | // |
| 112 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 113 | const char** args = (const char**)malloc(sizeof(char*) * 5); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 114 | args[0] = binary; |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 115 | args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 116 | char* temp = (char*)malloc(10); |
| 117 | sprintf(temp, "%d", pipefd[1]); |
| 118 | args[2] = temp; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 119 | args[3] = (char*)path; |
| 120 | args[4] = NULL; |
| 121 | |
| 122 | pid_t pid = fork(); |
| 123 | if (pid == 0) { |
Alistair Strachan | 027429a | 2013-07-17 10:41:49 -0700 | [diff] [blame] | 124 | umask(022); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 125 | close(pipefd[0]); |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 126 | execv(binary, (char* const*)args); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 127 | fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 128 | _exit(-1); |
| 129 | } |
| 130 | close(pipefd[1]); |
| 131 | |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 132 | *wipe_cache = false; |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 133 | |
Doug Zongker | 64893cc | 2009-07-14 16:31:56 -0700 | [diff] [blame] | 134 | char buffer[1024]; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 135 | FILE* from_child = fdopen(pipefd[0], "r"); |
| 136 | while (fgets(buffer, sizeof(buffer), from_child) != NULL) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 137 | char* command = strtok(buffer, " \n"); |
| 138 | if (command == NULL) { |
| 139 | continue; |
| 140 | } else if (strcmp(command, "progress") == 0) { |
| 141 | char* fraction_s = strtok(NULL, " \n"); |
| 142 | char* seconds_s = strtok(NULL, " \n"); |
| 143 | |
| 144 | float fraction = strtof(fraction_s, NULL); |
| 145 | int seconds = strtol(seconds_s, NULL, 10); |
| 146 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 147 | ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 148 | } else if (strcmp(command, "set_progress") == 0) { |
| 149 | char* fraction_s = strtok(NULL, " \n"); |
| 150 | float fraction = strtof(fraction_s, NULL); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 151 | ui->SetProgress(fraction); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 152 | } else if (strcmp(command, "ui_print") == 0) { |
| 153 | char* str = strtok(NULL, "\n"); |
| 154 | if (str) { |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 155 | ui->Print("%s", str); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 156 | } else { |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 157 | ui->Print("\n"); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 158 | } |
Doug Zongker | fafc85b | 2013-07-09 12:29:45 -0700 | [diff] [blame] | 159 | fflush(stdout); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 160 | } else if (strcmp(command, "wipe_cache") == 0) { |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 161 | *wipe_cache = true; |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 162 | } else if (strcmp(command, "clear_display") == 0) { |
| 163 | ui->SetBackground(RecoveryUI::NONE); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 164 | } else if (strcmp(command, "enable_reboot") == 0) { |
| 165 | // packages can explicitly request that they want the user |
| 166 | // to be able to reboot during installation (useful for |
| 167 | // debugging packages that don't exit). |
| 168 | ui->SetEnableReboot(true); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 169 | } else { |
| 170 | LOGE("unknown command [%s]\n", command); |
| 171 | } |
| 172 | } |
| 173 | fclose(from_child); |
| 174 | |
| 175 | int status; |
| 176 | waitpid(pid, &status, 0); |
| 177 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 178 | LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 179 | return INSTALL_ERROR; |
| 180 | } |
| 181 | |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 182 | return INSTALL_SUCCESS; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 185 | static int |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 186 | really_install_package(const char *path, bool* wipe_cache, bool needs_mount) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 187 | { |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 188 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 189 | ui->Print("Finding update package...\n"); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 190 | // Give verification half the progress bar... |
| 191 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 192 | ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 193 | LOGI("Update location: %s\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 194 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 195 | // Map the update package into memory. |
| 196 | ui->Print("Opening update package...\n"); |
| 197 | |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 198 | if (path && needs_mount) { |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 199 | if (path[0] == '@') { |
| 200 | ensure_path_mounted(path+1); |
| 201 | } else { |
| 202 | ensure_path_mounted(path); |
| 203 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 206 | MemMapping map; |
| 207 | if (sysMapFile(path, &map) != 0) { |
| 208 | LOGE("failed to map file\n"); |
| 209 | return INSTALL_CORRUPT; |
| 210 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 211 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 212 | int numKeys; |
Doug Zongker | bac7fba | 2013-04-10 11:32:17 -0700 | [diff] [blame] | 213 | Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 214 | if (loadedKeys == NULL) { |
| 215 | LOGE("Failed to load keys\n"); |
| 216 | return INSTALL_CORRUPT; |
| 217 | } |
| 218 | LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); |
| 219 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 220 | ui->Print("Verifying update package...\n"); |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 221 | |
| 222 | int err; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 223 | err = verify_file(map.addr, map.length, loadedKeys, numKeys); |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 224 | free(loadedKeys); |
| 225 | LOGI("verify_file returned %d\n", err); |
| 226 | if (err != VERIFY_SUCCESS) { |
| 227 | LOGE("signature verification failed\n"); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 228 | sysReleaseMap(&map); |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 229 | return INSTALL_CORRUPT; |
| 230 | } |
| 231 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 232 | /* Try to open the package. |
| 233 | */ |
| 234 | ZipArchive zip; |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 235 | err = mzOpenZipArchive(map.addr, map.length, &zip); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 236 | if (err != 0) { |
| 237 | LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad"); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 238 | sysReleaseMap(&map); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 239 | return INSTALL_CORRUPT; |
| 240 | } |
| 241 | |
| 242 | /* Verify and install the contents of the package. |
| 243 | */ |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 244 | ui->Print("Installing update...\n"); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 245 | ui->SetEnableReboot(false); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 246 | int result = try_update_binary(path, &zip, wipe_cache); |
Doug Zongker | c704e06 | 2014-05-23 08:40:35 -0700 | [diff] [blame] | 247 | ui->SetEnableReboot(true); |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 248 | ui->Print("\n"); |
Doug Zongker | 99916f0 | 2014-01-13 14:16:58 -0800 | [diff] [blame] | 249 | |
| 250 | sysReleaseMap(&map); |
| 251 | |
| 252 | return result; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 253 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 254 | |
| 255 | int |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 256 | install_package(const char* path, bool* wipe_cache, const char* install_file, |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 257 | bool needs_mount) |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 258 | { |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 259 | FILE* install_log = fopen_path(install_file, "w"); |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 260 | if (install_log) { |
| 261 | fputs(path, install_log); |
| 262 | fputc('\n', install_log); |
| 263 | } else { |
| 264 | LOGE("failed to open last_install: %s\n", strerror(errno)); |
| 265 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 266 | int result; |
| 267 | if (setup_install_mounts() != 0) { |
| 268 | LOGE("failed to set up expected mounts for install; aborting\n"); |
| 269 | result = INSTALL_ERROR; |
| 270 | } else { |
Doug Zongker | 075ad80 | 2014-06-26 15:35:51 -0700 | [diff] [blame] | 271 | result = really_install_package(path, wipe_cache, needs_mount); |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 272 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 273 | if (install_log) { |
| 274 | fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log); |
| 275 | fputc('\n', install_log); |
| 276 | fclose(install_log); |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 277 | } |
| 278 | return result; |
| 279 | } |