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> |
| 21 | #include <sys/stat.h> |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 22 | #include <sys/wait.h> |
| 23 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include "common.h" |
| 26 | #include "install.h" |
| 27 | #include "mincrypt/rsa.h" |
| 28 | #include "minui/minui.h" |
| 29 | #include "minzip/SysUtil.h" |
| 30 | #include "minzip/Zip.h" |
| 31 | #include "mtdutils/mounts.h" |
| 32 | #include "mtdutils/mtdutils.h" |
| 33 | #include "roots.h" |
| 34 | #include "verifier.h" |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 35 | #include "ui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 36 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 37 | extern RecoveryUI* ui; |
| 38 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 39 | #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 40 | #define PUBLIC_KEYS_FILE "/res/keys" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 41 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 42 | // Default allocation of progress bar segments to operations |
| 43 | static const int VERIFICATION_PROGRESS_TIME = 60; |
| 44 | static const float VERIFICATION_PROGRESS_FRACTION = 0.25; |
| 45 | static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4; |
| 46 | static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1; |
| 47 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 48 | // If the package contains an update binary, extract it and run it. |
| 49 | static int |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 50 | try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 51 | const ZipEntry* binary_entry = |
| 52 | mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); |
| 53 | if (binary_entry == NULL) { |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 54 | mzCloseZipArchive(zip); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 55 | return INSTALL_CORRUPT; |
| 56 | } |
| 57 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 58 | const char* binary = "/tmp/update_binary"; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 59 | unlink(binary); |
| 60 | int fd = creat(binary, 0755); |
| 61 | if (fd < 0) { |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 62 | mzCloseZipArchive(zip); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 63 | LOGE("Can't make %s\n", binary); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 64 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 65 | } |
| 66 | bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); |
| 67 | close(fd); |
Doug Zongker | 8e5e4da | 2010-09-14 18:06:55 -0700 | [diff] [blame] | 68 | mzCloseZipArchive(zip); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 69 | |
| 70 | if (!ok) { |
| 71 | LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 72 | return INSTALL_ERROR; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int pipefd[2]; |
| 76 | pipe(pipefd); |
| 77 | |
| 78 | // When executing the update binary contained in the package, the |
| 79 | // arguments passed are: |
| 80 | // |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 81 | // - the version number for this interface |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 82 | // |
| 83 | // - an fd to which the program can write in order to update the |
| 84 | // progress bar. The program can write single-line commands: |
| 85 | // |
| 86 | // progress <frac> <secs> |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 87 | // fill up the next <frac> part of of the progress bar |
| 88 | // over <secs> seconds. If <secs> is zero, use |
| 89 | // set_progress commands to manually control the |
| 90 | // progress of this segment of the bar |
| 91 | // |
| 92 | // set_progress <frac> |
| 93 | // <frac> should be between 0.0 and 1.0; sets the |
| 94 | // progress bar within the segment defined by the most |
| 95 | // recent progress command. |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 96 | // |
| 97 | // firmware <"hboot"|"radio"> <filename> |
| 98 | // arrange to install the contents of <filename> in the |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 99 | // given partition on reboot. |
| 100 | // |
| 101 | // (API v2: <filename> may start with "PACKAGE:" to |
| 102 | // indicate taking a file from the OTA package.) |
| 103 | // |
| 104 | // (API v3: this command no longer exists.) |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 105 | // |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 106 | // ui_print <string> |
| 107 | // display <string> on the screen. |
| 108 | // |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 109 | // - the name of the package zip file. |
| 110 | // |
| 111 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 112 | const char** args = (const char**)malloc(sizeof(char*) * 5); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 113 | args[0] = binary; |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 114 | args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 115 | char* temp = (char*)malloc(10); |
| 116 | sprintf(temp, "%d", pipefd[1]); |
| 117 | args[2] = temp; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 118 | args[3] = (char*)path; |
| 119 | args[4] = NULL; |
| 120 | |
| 121 | pid_t pid = fork(); |
| 122 | if (pid == 0) { |
| 123 | close(pipefd[0]); |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 124 | execv(binary, (char* const*)args); |
Doug Zongker | 56c5105 | 2010-07-01 09:18:44 -0700 | [diff] [blame] | 125 | fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 126 | _exit(-1); |
| 127 | } |
| 128 | close(pipefd[1]); |
| 129 | |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 130 | *wipe_cache = 0; |
| 131 | |
Doug Zongker | 64893cc | 2009-07-14 16:31:56 -0700 | [diff] [blame] | 132 | char buffer[1024]; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 133 | FILE* from_child = fdopen(pipefd[0], "r"); |
| 134 | while (fgets(buffer, sizeof(buffer), from_child) != NULL) { |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 135 | char* command = strtok(buffer, " \n"); |
| 136 | if (command == NULL) { |
| 137 | continue; |
| 138 | } else if (strcmp(command, "progress") == 0) { |
| 139 | char* fraction_s = strtok(NULL, " \n"); |
| 140 | char* seconds_s = strtok(NULL, " \n"); |
| 141 | |
| 142 | float fraction = strtof(fraction_s, NULL); |
| 143 | int seconds = strtol(seconds_s, NULL, 10); |
| 144 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 145 | ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds); |
Doug Zongker | fbf3c10 | 2009-06-24 09:36:20 -0700 | [diff] [blame] | 146 | } else if (strcmp(command, "set_progress") == 0) { |
| 147 | char* fraction_s = strtok(NULL, " \n"); |
| 148 | float fraction = strtof(fraction_s, NULL); |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 149 | ui->SetProgress(fraction); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 150 | } else if (strcmp(command, "ui_print") == 0) { |
| 151 | char* str = strtok(NULL, "\n"); |
| 152 | if (str) { |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 153 | ui->Print("%s", str); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 154 | } else { |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 155 | ui->Print("\n"); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 156 | } |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 157 | } else if (strcmp(command, "wipe_cache") == 0) { |
| 158 | *wipe_cache = 1; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 159 | } else { |
| 160 | LOGE("unknown command [%s]\n", command); |
| 161 | } |
| 162 | } |
| 163 | fclose(from_child); |
| 164 | |
| 165 | int status; |
| 166 | waitpid(pid, &status, 0); |
| 167 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 168 | LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 169 | return INSTALL_ERROR; |
| 170 | } |
| 171 | |
Doug Zongker | e08991e | 2010-02-02 13:09:52 -0800 | [diff] [blame] | 172 | return INSTALL_SUCCESS; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 175 | // Reads a file containing one or more public keys as produced by |
| 176 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 177 | // as a C source literal, eg: |
| 178 | // |
| 179 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 180 | // |
| 181 | // (Note that the braces and commas in this example are actual |
| 182 | // characters the parser expects to find in the file; the ellipses |
| 183 | // indicate more numbers omitted from this example.) |
| 184 | // |
| 185 | // The file may contain multiple keys in this format, separated by |
| 186 | // commas. The last key must not be followed by a comma. |
| 187 | // |
| 188 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
| 189 | static RSAPublicKey* |
| 190 | load_keys(const char* filename, int* numKeys) { |
| 191 | RSAPublicKey* out = NULL; |
| 192 | *numKeys = 0; |
| 193 | |
| 194 | FILE* f = fopen(filename, "r"); |
| 195 | if (f == NULL) { |
| 196 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
| 197 | goto exit; |
| 198 | } |
| 199 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 200 | { |
| 201 | int i; |
| 202 | bool done = false; |
| 203 | while (!done) { |
| 204 | ++*numKeys; |
| 205 | out = (RSAPublicKey*)realloc(out, *numKeys * sizeof(RSAPublicKey)); |
| 206 | RSAPublicKey* key = out + (*numKeys - 1); |
| 207 | if (fscanf(f, " { %i , 0x%x , { %u", |
| 208 | &(key->len), &(key->n0inv), &(key->n[0])) != 3) { |
| 209 | goto exit; |
| 210 | } |
| 211 | if (key->len != RSANUMWORDS) { |
| 212 | LOGE("key length (%d) does not match expected size\n", key->len); |
| 213 | goto exit; |
| 214 | } |
| 215 | for (i = 1; i < key->len; ++i) { |
| 216 | if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit; |
| 217 | } |
| 218 | if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit; |
| 219 | for (i = 1; i < key->len; ++i) { |
| 220 | if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit; |
| 221 | } |
| 222 | fscanf(f, " } } "); |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 223 | |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 224 | // if the line ends in a comma, this file has more keys. |
| 225 | switch (fgetc(f)) { |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 226 | case ',': |
| 227 | // more keys to come. |
| 228 | break; |
| 229 | |
| 230 | case EOF: |
| 231 | done = true; |
| 232 | break; |
| 233 | |
| 234 | default: |
| 235 | LOGE("unexpected character between keys\n"); |
| 236 | goto exit; |
Doug Zongker | 10e418d | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 237 | } |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | fclose(f); |
| 242 | return out; |
| 243 | |
| 244 | exit: |
| 245 | if (f) fclose(f); |
| 246 | free(out); |
| 247 | *numKeys = 0; |
| 248 | return NULL; |
| 249 | } |
| 250 | |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 251 | static int |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 252 | really_install_package(const char *path, int* wipe_cache) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 253 | { |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 254 | ui->SetBackground(RecoveryUI::INSTALLING); |
| 255 | ui->Print("Finding update package...\n"); |
| 256 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 257 | LOGI("Update location: %s\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 258 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 259 | if (ensure_path_mounted(path) != 0) { |
| 260 | LOGE("Can't mount %s\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 261 | return INSTALL_CORRUPT; |
| 262 | } |
| 263 | |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 264 | ui->Print("Opening update package...\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 265 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 266 | int numKeys; |
| 267 | RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); |
| 268 | if (loadedKeys == NULL) { |
| 269 | LOGE("Failed to load keys\n"); |
| 270 | return INSTALL_CORRUPT; |
| 271 | } |
| 272 | LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); |
| 273 | |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 274 | // Give verification half the progress bar... |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 275 | ui->Print("Verifying update package...\n"); |
| 276 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 277 | ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME); |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 278 | |
| 279 | int err; |
| 280 | err = verify_file(path, loadedKeys, numKeys); |
| 281 | free(loadedKeys); |
| 282 | LOGI("verify_file returned %d\n", err); |
| 283 | if (err != VERIFY_SUCCESS) { |
| 284 | LOGE("signature verification failed\n"); |
| 285 | return INSTALL_CORRUPT; |
| 286 | } |
| 287 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 288 | /* Try to open the package. |
| 289 | */ |
| 290 | ZipArchive zip; |
Doug Zongker | 60151a2 | 2009-08-12 18:30:03 -0700 | [diff] [blame] | 291 | err = mzOpenZipArchive(path, &zip); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 292 | if (err != 0) { |
| 293 | LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad"); |
| 294 | return INSTALL_CORRUPT; |
| 295 | } |
| 296 | |
| 297 | /* Verify and install the contents of the package. |
| 298 | */ |
Doug Zongker | 7440630 | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 299 | ui->Print("Installing update...\n"); |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 300 | return try_update_binary(path, &zip, wipe_cache); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 301 | } |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 302 | |
| 303 | int |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 304 | install_package(const char* path, int* wipe_cache, const char* install_file) |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 305 | { |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 306 | FILE* install_log = fopen_path(install_file, "w"); |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 307 | if (install_log) { |
| 308 | fputs(path, install_log); |
| 309 | fputc('\n', install_log); |
| 310 | } else { |
| 311 | LOGE("failed to open last_install: %s\n", strerror(errno)); |
| 312 | } |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 313 | int result = really_install_package(path, wipe_cache); |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 314 | if (install_log) { |
| 315 | fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log); |
| 316 | fputc('\n', install_log); |
| 317 | fclose(install_log); |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 318 | } |
| 319 | return result; |
| 320 | } |