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