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