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