blob: 707cda43890b71dafda5206c8503fcb742f1ada9 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Zongkerb2ee9202009-06-04 10:24:53 -070017#include <ctype.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018#include <errno.h>
19#include <fcntl.h>
20#include <limits.h>
21#include <sys/stat.h>
Doug Zongkerb2ee9202009-06-04 10:24:53 -070022#include <sys/wait.h>
23#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#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 Zongkerb2ee9202009-06-04 10:24:53 -070036#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070037#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Doug Zongker469243e2011-04-12 09:28:10 -070039static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
40
Doug Zongkerb2ee9202009-06-04 10:24:53 -070041// If the package contains an update binary, extract it and run it.
42static int
43try_update_binary(const char *path, ZipArchive *zip) {
44 const ZipEntry* binary_entry =
45 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
46 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070047 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070048 return INSTALL_CORRUPT;
49 }
50
51 char* binary = "/tmp/update_binary";
52 unlink(binary);
53 int fd = creat(binary, 0755);
54 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070055 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070056 LOGE("Can't make %s\n", binary);
57 return 1;
58 }
59 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
60 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070061 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070062
63 if (!ok) {
64 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
65 return 1;
66 }
67
68 int pipefd[2];
69 pipe(pipefd);
70
71 // When executing the update binary contained in the package, the
72 // arguments passed are:
73 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070074 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070075 //
76 // - an fd to which the program can write in order to update the
77 // progress bar. The program can write single-line commands:
78 //
79 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070080 // fill up the next <frac> part of of the progress bar
81 // over <secs> seconds. If <secs> is zero, use
82 // set_progress commands to manually control the
83 // progress of this segment of the bar
84 //
85 // set_progress <frac>
86 // <frac> should be between 0.0 and 1.0; sets the
87 // progress bar within the segment defined by the most
88 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -070089 //
90 // firmware <"hboot"|"radio"> <filename>
91 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -080092 // given partition on reboot.
93 //
94 // (API v2: <filename> may start with "PACKAGE:" to
95 // indicate taking a file from the OTA package.)
96 //
97 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -070098 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -070099 // ui_print <string>
100 // display <string> on the screen.
101 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700102 // - the name of the package zip file.
103 //
104
105 char** args = malloc(sizeof(char*) * 5);
106 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700107 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700108 args[2] = malloc(10);
109 sprintf(args[2], "%d", pipefd[1]);
110 args[3] = (char*)path;
111 args[4] = NULL;
112
113 pid_t pid = fork();
114 if (pid == 0) {
115 close(pipefd[0]);
116 execv(binary, args);
Doug Zongker56c51052010-07-01 09:18:44 -0700117 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700118 _exit(-1);
119 }
120 close(pipefd[1]);
121
Doug Zongker64893cc2009-07-14 16:31:56 -0700122 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700123 FILE* from_child = fdopen(pipefd[0], "r");
124 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700125 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);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700137 } 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);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700141 } else if (strcmp(command, "ui_print") == 0) {
142 char* str = strtok(NULL, "\n");
143 if (str) {
Nick Kralevich21b97ed2010-06-24 16:11:17 -0700144 ui_print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700145 } else {
146 ui_print("\n");
147 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700148 } else {
149 LOGE("unknown command [%s]\n", command);
150 }
151 }
152 fclose(from_child);
153
154 int status;
155 waitpid(pid, &status, 0);
156 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700158 return INSTALL_ERROR;
159 }
160
Doug Zongkere08991e2010-02-02 13:09:52 -0800161 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700162}
163
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700164// Reads a file containing one or more public keys as produced by
165// DumpPublicKey: this is an RSAPublicKey struct as it would appear
166// as a C source literal, eg:
167//
168// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
169//
170// (Note that the braces and commas in this example are actual
171// characters the parser expects to find in the file; the ellipses
172// indicate more numbers omitted from this example.)
173//
174// The file may contain multiple keys in this format, separated by
175// commas. The last key must not be followed by a comma.
176//
177// Returns NULL if the file failed to parse, or if it contain zero keys.
178static RSAPublicKey*
179load_keys(const char* filename, int* numKeys) {
180 RSAPublicKey* out = NULL;
181 *numKeys = 0;
182
183 FILE* f = fopen(filename, "r");
184 if (f == NULL) {
185 LOGE("opening %s: %s\n", filename, strerror(errno));
186 goto exit;
187 }
188
189 int i;
190 bool done = false;
191 while (!done) {
192 ++*numKeys;
193 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
194 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800195 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700196 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
197 goto exit;
198 }
199 if (key->len != RSANUMWORDS) {
200 LOGE("key length (%d) does not match expected size\n", key->len);
201 goto exit;
202 }
203 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800204 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700205 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800206 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700207 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800208 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700209 }
210 fscanf(f, " } } ");
211
212 // if the line ends in a comma, this file has more keys.
213 switch (fgetc(f)) {
214 case ',':
215 // more keys to come.
216 break;
217
218 case EOF:
219 done = true;
220 break;
221
222 default:
223 LOGE("unexpected character between keys\n");
224 goto exit;
225 }
226 }
227
228 fclose(f);
229 return out;
230
231exit:
232 if (f) fclose(f);
233 free(out);
234 *numKeys = 0;
235 return NULL;
236}
237
Doug Zongker469243e2011-04-12 09:28:10 -0700238static int
239really_install_package(const char *path)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800240{
241 ui_set_background(BACKGROUND_ICON_INSTALLING);
242 ui_print("Finding update package...\n");
243 ui_show_indeterminate_progress();
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700244 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700246 if (ensure_path_mounted(path) != 0) {
247 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 return INSTALL_CORRUPT;
249 }
250
251 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700253 int numKeys;
254 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
255 if (loadedKeys == NULL) {
256 LOGE("Failed to load keys\n");
257 return INSTALL_CORRUPT;
258 }
259 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
260
Doug Zongker60151a22009-08-12 18:30:03 -0700261 // Give verification half the progress bar...
262 ui_print("Verifying update package...\n");
263 ui_show_progress(
264 VERIFICATION_PROGRESS_FRACTION,
265 VERIFICATION_PROGRESS_TIME);
266
267 int err;
268 err = verify_file(path, loadedKeys, numKeys);
269 free(loadedKeys);
270 LOGI("verify_file returned %d\n", err);
271 if (err != VERIFY_SUCCESS) {
272 LOGE("signature verification failed\n");
273 return INSTALL_CORRUPT;
274 }
275
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800276 /* Try to open the package.
277 */
278 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700279 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800280 if (err != 0) {
281 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
282 return INSTALL_CORRUPT;
283 }
284
285 /* Verify and install the contents of the package.
286 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700287 ui_print("Installing update...\n");
288 return try_update_binary(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800289}
Doug Zongker469243e2011-04-12 09:28:10 -0700290
291int
292install_package(const char* path)
293{
294 FILE* install_log = fopen_path(LAST_INSTALL_FILE, "w");
295 if (install_log) {
296 fputs(path, install_log);
297 fputc('\n', install_log);
298 } else {
299 LOGE("failed to open last_install: %s\n", strerror(errno));
300 }
301 int result = really_install_package(path);
302 if (install_log) {
303 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
304 fputc('\n', install_log);
305 fclose(install_log);
306 chmod(LAST_INSTALL_FILE, 0644);
307 }
308 return result;
309}