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 | |
| 25 | #include "amend/amend.h" |
| 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 | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 36 | #include "firmware.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 38 | #define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script" |
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 | |
| 42 | static const ZipEntry * |
| 43 | find_update_script(ZipArchive *zip) |
| 44 | { |
| 45 | //TODO: Get the location of this script from the MANIFEST.MF file |
| 46 | return mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME); |
| 47 | } |
| 48 | |
| 49 | static int read_data(ZipArchive *zip, const ZipEntry *entry, |
| 50 | char** ppData, int* pLength) { |
| 51 | int len = (int)mzGetZipEntryUncompLen(entry); |
| 52 | if (len <= 0) { |
| 53 | LOGE("Bad data length %d\n", len); |
| 54 | return -1; |
| 55 | } |
| 56 | char *data = malloc(len + 1); |
| 57 | if (data == NULL) { |
| 58 | LOGE("Can't allocate %d bytes for data\n", len + 1); |
| 59 | return -2; |
| 60 | } |
| 61 | bool ok = mzReadZipEntry(zip, entry, data, len); |
| 62 | if (!ok) { |
| 63 | LOGE("Error while reading data\n"); |
| 64 | free(data); |
| 65 | return -3; |
| 66 | } |
| 67 | data[len] = '\0'; // not necessary, but just to be safe |
| 68 | *ppData = data; |
| 69 | if (pLength) { |
| 70 | *pLength = len; |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int |
| 76 | handle_update_script(ZipArchive *zip, const ZipEntry *update_script_entry) |
| 77 | { |
| 78 | /* Read the entire script into a buffer. |
| 79 | */ |
| 80 | int script_len; |
| 81 | char* script_data; |
| 82 | if (read_data(zip, update_script_entry, &script_data, &script_len) < 0) { |
| 83 | LOGE("Can't read update script\n"); |
| 84 | return INSTALL_ERROR; |
| 85 | } |
| 86 | |
| 87 | /* Parse the script. Note that the script and parse tree are never freed. |
| 88 | */ |
| 89 | const AmCommandList *commands = parseAmendScript(script_data, script_len); |
| 90 | if (commands == NULL) { |
| 91 | LOGE("Syntax error in update script\n"); |
| 92 | return INSTALL_ERROR; |
| 93 | } else { |
| 94 | UnterminatedString name = mzGetZipEntryFileName(update_script_entry); |
| 95 | LOGI("Parsed %.*s\n", name.len, name.str); |
| 96 | } |
| 97 | |
| 98 | /* Execute the script. |
| 99 | */ |
| 100 | int ret = execCommandList((ExecContext *)1, commands); |
| 101 | if (ret != 0) { |
| 102 | int num = ret; |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 103 | char *line = NULL, *next = script_data; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 104 | while (next != NULL && ret-- > 0) { |
| 105 | line = next; |
| 106 | next = memchr(line, '\n', script_data + script_len - line); |
| 107 | if (next != NULL) *next++ = '\0'; |
| 108 | } |
| 109 | LOGE("Failure at line %d:\n%s\n", num, next ? line : "(not found)"); |
| 110 | return INSTALL_ERROR; |
| 111 | } |
| 112 | |
Doug Zongker | 07e1dca | 2009-05-28 19:02:45 -0700 | [diff] [blame] | 113 | LOGI("Installation complete.\n"); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 114 | return INSTALL_SUCCESS; |
| 115 | } |
| 116 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 117 | // The update binary ask us to install a firmware file on reboot. Set |
| 118 | // that up. Takes ownership of type and filename. |
| 119 | static int |
| 120 | handle_firmware_update(char* type, char* filename) { |
| 121 | struct stat st_data; |
| 122 | if (stat(filename, &st_data) < 0) { |
| 123 | LOGE("Error stat'ing %s: %s\n", filename, strerror(errno)); |
| 124 | return INSTALL_ERROR; |
| 125 | } |
| 126 | |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 127 | LOGI("type is %s; size is %d; file is %s\n", |
| 128 | type, (int)st_data.st_size, filename); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 129 | |
| 130 | char* data = malloc(st_data.st_size); |
| 131 | if (data == NULL) { |
| 132 | LOGE("Can't allocate %d bytes for firmware data\n", st_data.st_size); |
| 133 | return INSTALL_ERROR; |
| 134 | } |
| 135 | |
| 136 | FILE* f = fopen(filename, "rb"); |
| 137 | if (f == NULL) { |
| 138 | LOGE("Failed to open %s: %s\n", filename, strerror(errno)); |
| 139 | return INSTALL_ERROR; |
| 140 | } |
| 141 | if (fread(data, 1, st_data.st_size, f) != st_data.st_size) { |
| 142 | LOGE("Failed to read firmware data: %s\n", strerror(errno)); |
| 143 | return INSTALL_ERROR; |
| 144 | } |
| 145 | fclose(f); |
| 146 | |
| 147 | if (remember_firmware_update(type, data, st_data.st_size)) { |
| 148 | LOGE("Can't store %s image\n", type); |
| 149 | free(data); |
| 150 | return INSTALL_ERROR; |
| 151 | } |
| 152 | free(filename); |
| 153 | |
| 154 | return INSTALL_SUCCESS; |
| 155 | } |
| 156 | |
| 157 | // If the package contains an update binary, extract it and run it. |
| 158 | static int |
| 159 | try_update_binary(const char *path, ZipArchive *zip) { |
| 160 | const ZipEntry* binary_entry = |
| 161 | mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); |
| 162 | if (binary_entry == NULL) { |
| 163 | return INSTALL_CORRUPT; |
| 164 | } |
| 165 | |
| 166 | char* binary = "/tmp/update_binary"; |
| 167 | unlink(binary); |
| 168 | int fd = creat(binary, 0755); |
| 169 | if (fd < 0) { |
| 170 | LOGE("Can't make %s\n", binary); |
| 171 | return 1; |
| 172 | } |
| 173 | bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); |
| 174 | close(fd); |
| 175 | |
| 176 | if (!ok) { |
| 177 | LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME); |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | int pipefd[2]; |
| 182 | pipe(pipefd); |
| 183 | |
| 184 | // When executing the update binary contained in the package, the |
| 185 | // arguments passed are: |
| 186 | // |
| 187 | // - the version number for this interface (currently 1) |
| 188 | // |
| 189 | // - an fd to which the program can write in order to update the |
| 190 | // progress bar. The program can write single-line commands: |
| 191 | // |
| 192 | // progress <frac> <secs> |
| 193 | // fill up <frac> of the progress bar over <secs> seconds. |
| 194 | // |
| 195 | // firmware <"hboot"|"radio"> <filename> |
| 196 | // arrange to install the contents of <filename> in the |
| 197 | // given partition on reboot. |
| 198 | // |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 199 | // ui_print <string> |
| 200 | // display <string> on the screen. |
| 201 | // |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 202 | // - the name of the package zip file. |
| 203 | // |
| 204 | |
| 205 | char** args = malloc(sizeof(char*) * 5); |
| 206 | args[0] = binary; |
| 207 | args[1] = "1"; |
| 208 | args[2] = malloc(10); |
| 209 | sprintf(args[2], "%d", pipefd[1]); |
| 210 | args[3] = (char*)path; |
| 211 | args[4] = NULL; |
| 212 | |
| 213 | pid_t pid = fork(); |
| 214 | if (pid == 0) { |
| 215 | close(pipefd[0]); |
| 216 | execv(binary, args); |
| 217 | fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno)); |
| 218 | _exit(-1); |
| 219 | } |
| 220 | close(pipefd[1]); |
| 221 | |
| 222 | char* firmware_type = NULL; |
| 223 | char* firmware_filename = NULL; |
| 224 | |
| 225 | char buffer[81]; |
| 226 | FILE* from_child = fdopen(pipefd[0], "r"); |
| 227 | while (fgets(buffer, sizeof(buffer), from_child) != NULL) { |
| 228 | LOGI("read: %s", buffer); |
| 229 | |
| 230 | char* command = strtok(buffer, " \n"); |
| 231 | if (command == NULL) { |
| 232 | continue; |
| 233 | } else if (strcmp(command, "progress") == 0) { |
| 234 | char* fraction_s = strtok(NULL, " \n"); |
| 235 | char* seconds_s = strtok(NULL, " \n"); |
| 236 | |
| 237 | float fraction = strtof(fraction_s, NULL); |
| 238 | int seconds = strtol(seconds_s, NULL, 10); |
| 239 | |
| 240 | ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), |
| 241 | seconds); |
| 242 | } else if (strcmp(command, "firmware") == 0) { |
| 243 | char* type = strtok(NULL, " \n"); |
| 244 | char* filename = strtok(NULL, " \n"); |
| 245 | |
| 246 | if (type != NULL && filename != NULL) { |
| 247 | if (firmware_type != NULL) { |
| 248 | LOGE("ignoring attempt to do multiple firmware updates"); |
| 249 | } else { |
| 250 | firmware_type = strdup(type); |
| 251 | firmware_filename = strdup(filename); |
| 252 | } |
| 253 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 254 | } else if (strcmp(command, "ui_print") == 0) { |
| 255 | char* str = strtok(NULL, "\n"); |
| 256 | if (str) { |
| 257 | ui_print(str); |
| 258 | } else { |
| 259 | ui_print("\n"); |
| 260 | } |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 261 | } else { |
| 262 | LOGE("unknown command [%s]\n", command); |
| 263 | } |
| 264 | } |
| 265 | fclose(from_child); |
| 266 | |
| 267 | int status; |
| 268 | waitpid(pid, &status, 0); |
| 269 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 270 | LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status)); |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 271 | return INSTALL_ERROR; |
| 272 | } |
| 273 | |
| 274 | if (firmware_type != NULL) { |
| 275 | return handle_firmware_update(firmware_type, firmware_filename); |
| 276 | } else { |
| 277 | return INSTALL_SUCCESS; |
| 278 | } |
| 279 | } |
| 280 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 281 | static int |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 282 | handle_update_package(const char *path, ZipArchive *zip, |
| 283 | const RSAPublicKey *keys, int numKeys) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 284 | { |
| 285 | // Give verification half the progress bar... |
| 286 | ui_print("Verifying update package...\n"); |
| 287 | ui_show_progress( |
| 288 | VERIFICATION_PROGRESS_FRACTION, |
| 289 | VERIFICATION_PROGRESS_TIME); |
| 290 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 291 | if (!verify_jar_signature(zip, keys, numKeys)) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 292 | LOGE("Verification failed\n"); |
| 293 | return INSTALL_CORRUPT; |
| 294 | } |
| 295 | |
| 296 | // Update should take the rest of the progress bar. |
| 297 | ui_print("Installing update...\n"); |
| 298 | |
Doug Zongker | b2ee920 | 2009-06-04 10:24:53 -0700 | [diff] [blame] | 299 | int result = try_update_binary(path, zip); |
| 300 | if (result == INSTALL_SUCCESS || result == INSTALL_ERROR) { |
| 301 | register_package_root(NULL, NULL); // Unregister package root |
| 302 | return result; |
| 303 | } |
| 304 | |
| 305 | // if INSTALL_CORRUPT is returned, this package doesn't have an |
| 306 | // update binary. Fall back to the older mechanism of looking for |
| 307 | // an update script. |
| 308 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 309 | const ZipEntry *script_entry; |
| 310 | script_entry = find_update_script(zip); |
| 311 | if (script_entry == NULL) { |
| 312 | LOGE("Can't find update script\n"); |
| 313 | return INSTALL_CORRUPT; |
| 314 | } |
| 315 | |
| 316 | if (register_package_root(zip, path) < 0) { |
| 317 | LOGE("Can't register package root\n"); |
| 318 | return INSTALL_ERROR; |
| 319 | } |
| 320 | |
| 321 | int ret = handle_update_script(zip, script_entry); |
| 322 | register_package_root(NULL, NULL); // Unregister package root |
| 323 | return ret; |
| 324 | } |
| 325 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 326 | // Reads a file containing one or more public keys as produced by |
| 327 | // DumpPublicKey: this is an RSAPublicKey struct as it would appear |
| 328 | // as a C source literal, eg: |
| 329 | // |
| 330 | // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" |
| 331 | // |
| 332 | // (Note that the braces and commas in this example are actual |
| 333 | // characters the parser expects to find in the file; the ellipses |
| 334 | // indicate more numbers omitted from this example.) |
| 335 | // |
| 336 | // The file may contain multiple keys in this format, separated by |
| 337 | // commas. The last key must not be followed by a comma. |
| 338 | // |
| 339 | // Returns NULL if the file failed to parse, or if it contain zero keys. |
| 340 | static RSAPublicKey* |
| 341 | load_keys(const char* filename, int* numKeys) { |
| 342 | RSAPublicKey* out = NULL; |
| 343 | *numKeys = 0; |
| 344 | |
| 345 | FILE* f = fopen(filename, "r"); |
| 346 | if (f == NULL) { |
| 347 | LOGE("opening %s: %s\n", filename, strerror(errno)); |
| 348 | goto exit; |
| 349 | } |
| 350 | |
| 351 | int i; |
| 352 | bool done = false; |
| 353 | while (!done) { |
| 354 | ++*numKeys; |
| 355 | out = realloc(out, *numKeys * sizeof(RSAPublicKey)); |
| 356 | RSAPublicKey* key = out + (*numKeys - 1); |
| 357 | if (fscanf(f, " { %i , %i , { %i", |
| 358 | &(key->len), &(key->n0inv), &(key->n[0])) != 3) { |
| 359 | goto exit; |
| 360 | } |
| 361 | if (key->len != RSANUMWORDS) { |
| 362 | LOGE("key length (%d) does not match expected size\n", key->len); |
| 363 | goto exit; |
| 364 | } |
| 365 | for (i = 1; i < key->len; ++i) { |
| 366 | if (fscanf(f, " , %i", &(key->n[i])) != 1) goto exit; |
| 367 | } |
| 368 | if (fscanf(f, " } , { %i", &(key->rr[0])) != 1) goto exit; |
| 369 | for (i = 1; i < key->len; ++i) { |
| 370 | if (fscanf(f, " , %i", &(key->rr[i])) != 1) goto exit; |
| 371 | } |
| 372 | fscanf(f, " } } "); |
| 373 | |
| 374 | // if the line ends in a comma, this file has more keys. |
| 375 | switch (fgetc(f)) { |
| 376 | case ',': |
| 377 | // more keys to come. |
| 378 | break; |
| 379 | |
| 380 | case EOF: |
| 381 | done = true; |
| 382 | break; |
| 383 | |
| 384 | default: |
| 385 | LOGE("unexpected character between keys\n"); |
| 386 | goto exit; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | fclose(f); |
| 391 | return out; |
| 392 | |
| 393 | exit: |
| 394 | if (f) fclose(f); |
| 395 | free(out); |
| 396 | *numKeys = 0; |
| 397 | return NULL; |
| 398 | } |
| 399 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 400 | int |
| 401 | install_package(const char *root_path) |
| 402 | { |
| 403 | ui_set_background(BACKGROUND_ICON_INSTALLING); |
| 404 | ui_print("Finding update package...\n"); |
| 405 | ui_show_indeterminate_progress(); |
| 406 | LOGI("Update location: %s\n", root_path); |
| 407 | |
| 408 | if (ensure_root_path_mounted(root_path) != 0) { |
| 409 | LOGE("Can't mount %s\n", root_path); |
| 410 | return INSTALL_CORRUPT; |
| 411 | } |
| 412 | |
| 413 | char path[PATH_MAX] = ""; |
| 414 | if (translate_root_path(root_path, path, sizeof(path)) == NULL) { |
| 415 | LOGE("Bad path %s\n", root_path); |
| 416 | return INSTALL_CORRUPT; |
| 417 | } |
| 418 | |
| 419 | ui_print("Opening update package...\n"); |
| 420 | LOGI("Update file path: %s\n", path); |
| 421 | |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 422 | int numKeys; |
| 423 | RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); |
| 424 | if (loadedKeys == NULL) { |
| 425 | LOGE("Failed to load keys\n"); |
| 426 | return INSTALL_CORRUPT; |
| 427 | } |
| 428 | LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); |
| 429 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 430 | /* Try to open the package. |
| 431 | */ |
| 432 | ZipArchive zip; |
| 433 | int err = mzOpenZipArchive(path, &zip); |
| 434 | if (err != 0) { |
| 435 | LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad"); |
| 436 | return INSTALL_CORRUPT; |
| 437 | } |
| 438 | |
| 439 | /* Verify and install the contents of the package. |
| 440 | */ |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 441 | int status = handle_update_package(path, &zip, loadedKeys, numKeys); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 442 | mzCloseZipArchive(&zip); |
Doug Zongker | d1b19b9 | 2009-04-01 15:48:46 -0700 | [diff] [blame] | 443 | free(loadedKeys); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 444 | return status; |
| 445 | } |