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, int* wipe_cache) { 42 const ZipEntry* binary_entry = 43 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); 44 if (binary_entry == NULL) { 45 mzCloseZipArchive(zip); 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) { 53 mzCloseZipArchive(zip); 54 LOGE("Can't make %s\n", binary); 55 return INSTALL_ERROR; 56 } 57 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); 58 close(fd); 59 mzCloseZipArchive(zip); 60 61 if (!ok) { 62 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME); 63 return INSTALL_ERROR; 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 // 72 // - the version number for this interface 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> 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. 87 // 88 // firmware <"hboot"|"radio"> <filename> 89 // arrange to install the contents of <filename> in the 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.) 96 // 97 // ui_print <string> 98 // display <string> on the screen. 99 // 100 // - the name of the package zip file. 101 // 102 103 char** args = malloc(sizeof(char*) * 5); 104 args[0] = binary; 105 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk 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(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno)); 116 _exit(-1); 117 } 118 close(pipefd[1]); 119 120 *wipe_cache = 0; 121 122 char buffer[1024]; 123 FILE* from_child = fdopen(pipefd[0], "r"); 124 while (fgets(buffer, sizeof(buffer), from_child) != NULL) { 125 char* command = strtok(buffer, " \n"); 126 if (command == NULL) { 127 continue; 128 } else if (strcmp(command, "progress") == 0) { 129 char* fraction_s = strtok(NULL, " \n"); 130 char* seconds_s = strtok(NULL, " \n"); 131 132 float fraction = strtof(fraction_s, NULL); 133 int seconds = strtol(seconds_s, NULL, 10); 134 135 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), 136 seconds); 137 } else if (strcmp(command, "set_progress") == 0) { 138 char* fraction_s = strtok(NULL, " \n"); 139 float fraction = strtof(fraction_s, NULL); 140 ui_set_progress(fraction); 141 } else if (strcmp(command, "ui_print") == 0) { 142 char* str = strtok(NULL, "\n"); 143 if (str) { 144 ui_print("%s", str); 145 } else { 146 ui_print("\n"); 147 } 148 } else if (strcmp(command, "wipe_cache") == 0) { 149 *wipe_cache = 1; 150 } else { 151 LOGE("unknown command [%s]\n", command); 152 } 153 } 154 fclose(from_child); 155 156 int status; 157 waitpid(pid, &status, 0); 158 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { 159 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status)); 160 return INSTALL_ERROR; 161 } 162 163 return INSTALL_SUCCESS; 164 } 165 166 // Reads a file containing one or more public keys as produced by 167 // DumpPublicKey: this is an RSAPublicKey struct as it would appear 168 // as a C source literal, eg: 169 // 170 // "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}" 171 // 172 // (Note that the braces and commas in this example are actual 173 // characters the parser expects to find in the file; the ellipses 174 // indicate more numbers omitted from this example.) 175 // 176 // The file may contain multiple keys in this format, separated by 177 // commas. The last key must not be followed by a comma. 178 // 179 // Returns NULL if the file failed to parse, or if it contain zero keys. 180 static RSAPublicKey* 181 load_keys(const char* filename, int* numKeys) { 182 RSAPublicKey* out = NULL; 183 *numKeys = 0; 184 185 FILE* f = fopen(filename, "r"); 186 if (f == NULL) { 187 LOGE("opening %s: %s\n", filename, strerror(errno)); 188 goto exit; 189 } 190 191 int i; 192 bool done = false; 193 while (!done) { 194 ++*numKeys; 195 out = realloc(out, *numKeys * sizeof(RSAPublicKey)); 196 RSAPublicKey* key = out + (*numKeys - 1); 197 if (fscanf(f, " { %i , 0x%x , { %u", 198 &(key->len), &(key->n0inv), &(key->n[0])) != 3) { 199 goto exit; 200 } 201 if (key->len != RSANUMWORDS) { 202 LOGE("key length (%d) does not match expected size\n", key->len); 203 goto exit; 204 } 205 for (i = 1; i < key->len; ++i) { 206 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit; 207 } 208 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit; 209 for (i = 1; i < key->len; ++i) { 210 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit; 211 } 212 fscanf(f, " } } "); 213 214 // if the line ends in a comma, this file has more keys. 215 switch (fgetc(f)) { 216 case ',': 217 // more keys to come. 218 break; 219 220 case EOF: 221 done = true; 222 break; 223 224 default: 225 LOGE("unexpected character between keys\n"); 226 goto exit; 227 } 228 } 229 230 fclose(f); 231 return out; 232 233 exit: 234 if (f) fclose(f); 235 free(out); 236 *numKeys = 0; 237 return NULL; 238 } 239 240 static int 241 really_install_package(const char *path, int* wipe_cache) 242 { 243 ui_set_background(BACKGROUND_ICON_INSTALLING); 244 ui_print("Finding update package...\n"); 245 ui_show_indeterminate_progress(); 246 LOGI("Update location: %s\n", path); 247 248 if (ensure_path_mounted(path) != 0) { 249 LOGE("Can't mount %s\n", path); 250 return INSTALL_CORRUPT; 251 } 252 253 ui_print("Opening update package...\n"); 254 255 int numKeys; 256 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys); 257 if (loadedKeys == NULL) { 258 LOGE("Failed to load keys\n"); 259 return INSTALL_CORRUPT; 260 } 261 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE); 262 263 // Give verification half the progress bar... 264 ui_print("Verifying update package...\n"); 265 ui_show_progress( 266 VERIFICATION_PROGRESS_FRACTION, 267 VERIFICATION_PROGRESS_TIME); 268 269 int err; 270 err = verify_file(path, loadedKeys, numKeys); 271 free(loadedKeys); 272 LOGI("verify_file returned %d\n", err); 273 if (err != VERIFY_SUCCESS) { 274 LOGE("signature verification failed\n"); 275 return INSTALL_CORRUPT; 276 } 277 278 /* Try to open the package. 279 */ 280 ZipArchive zip; 281 err = mzOpenZipArchive(path, &zip); 282 if (err != 0) { 283 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad"); 284 return INSTALL_CORRUPT; 285 } 286 287 /* Verify and install the contents of the package. 288 */ 289 ui_print("Installing update...\n"); 290 return try_update_binary(path, &zip, wipe_cache); 291 } 292 293 int 294 install_package(const char* path, int* wipe_cache, const char* install_file) 295 { 296 FILE* install_log = fopen_path(install_file, "w"); 297 if (install_log) { 298 fputs(path, install_log); 299 fputc('\n', install_log); 300 } else { 301 LOGE("failed to open last_install: %s\n", strerror(errno)); 302 } 303 int result = really_install_package(path, wipe_cache); 304 if (install_log) { 305 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log); 306 fputc('\n', install_log); 307 fclose(install_log); 308 } 309 return result; 310 } 311