blob: ab19478cf7625e0b92ef8c80ae0574255cf8e9ea [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
25#include "amend/amend.h"
26#include "common.h"
27#include "install.h"
28#include "mincrypt/rsa.h"
29#include "minui/minui.h"
30#include "minzip/SysUtil.h"
31#include "minzip/Zip.h"
32#include "mtdutils/mounts.h"
33#include "mtdutils/mtdutils.h"
34#include "roots.h"
35#include "verifier.h"
Doug Zongkerb2ee9202009-06-04 10:24:53 -070036#include "firmware.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script"
Doug Zongkerb2ee9202009-06-04 10:24:53 -070039#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070040#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
42static const ZipEntry *
43find_update_script(ZipArchive *zip)
44{
45//TODO: Get the location of this script from the MANIFEST.MF file
46 return mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME);
47}
48
49static int read_data(ZipArchive *zip, const ZipEntry *entry,
50 char** ppData, int* pLength) {
51 int len = (int)mzGetZipEntryUncompLen(entry);
52 if (len <= 0) {
53 LOGE("Bad data length %d\n", len);
54 return -1;
55 }
56 char *data = malloc(len + 1);
57 if (data == NULL) {
58 LOGE("Can't allocate %d bytes for data\n", len + 1);
59 return -2;
60 }
61 bool ok = mzReadZipEntry(zip, entry, data, len);
62 if (!ok) {
63 LOGE("Error while reading data\n");
64 free(data);
65 return -3;
66 }
67 data[len] = '\0'; // not necessary, but just to be safe
68 *ppData = data;
69 if (pLength) {
70 *pLength = len;
71 }
72 return 0;
73}
74
75static int
76handle_update_script(ZipArchive *zip, const ZipEntry *update_script_entry)
77{
78 /* Read the entire script into a buffer.
79 */
80 int script_len;
81 char* script_data;
82 if (read_data(zip, update_script_entry, &script_data, &script_len) < 0) {
83 LOGE("Can't read update script\n");
84 return INSTALL_ERROR;
85 }
86
87 /* Parse the script. Note that the script and parse tree are never freed.
88 */
89 const AmCommandList *commands = parseAmendScript(script_data, script_len);
90 if (commands == NULL) {
91 LOGE("Syntax error in update script\n");
92 return INSTALL_ERROR;
93 } else {
94 UnterminatedString name = mzGetZipEntryFileName(update_script_entry);
95 LOGI("Parsed %.*s\n", name.len, name.str);
96 }
97
98 /* Execute the script.
99 */
100 int ret = execCommandList((ExecContext *)1, commands);
101 if (ret != 0) {
102 int num = ret;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700103 char *line = NULL, *next = script_data;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 while (next != NULL && ret-- > 0) {
105 line = next;
106 next = memchr(line, '\n', script_data + script_len - line);
107 if (next != NULL) *next++ = '\0';
108 }
109 LOGE("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
110 return INSTALL_ERROR;
111 }
112
Doug Zongker07e1dca2009-05-28 19:02:45 -0700113 LOGI("Installation complete.\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114 return INSTALL_SUCCESS;
115}
116
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700117// The update binary ask us to install a firmware file on reboot. Set
118// that up. Takes ownership of type and filename.
119static int
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700120handle_firmware_update(char* type, char* filename, ZipArchive* zip) {
121 unsigned int data_size;
122 const ZipEntry* entry = NULL;
123
124 if (strncmp(filename, "PACKAGE:", 8) == 0) {
125 entry = mzFindZipEntry(zip, filename+8);
126 if (entry == NULL) {
127 LOGE("Failed to find \"%s\" in package", filename+8);
128 return INSTALL_ERROR;
129 }
130 data_size = entry->uncompLen;
131 } else {
132 struct stat st_data;
133 if (stat(filename, &st_data) < 0) {
134 LOGE("Error stat'ing %s: %s\n", filename, strerror(errno));
135 return INSTALL_ERROR;
136 }
137 data_size = st_data.st_size;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700138 }
139
Doug Zongker8edb00c2009-06-11 17:21:44 -0700140 LOGI("type is %s; size is %d; file is %s\n",
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700141 type, data_size, filename);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700142
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700143 char* data = malloc(data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700144 if (data == NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700145 LOGI("Can't allocate %d bytes for firmware data\n", data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700146 return INSTALL_ERROR;
147 }
148
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700149 if (entry) {
150 if (mzReadZipEntry(zip, entry, data, data_size) == false) {
151 LOGE("Failed to read \"%s\" from package", filename+8);
152 return INSTALL_ERROR;
153 }
154 } else {
155 FILE* f = fopen(filename, "rb");
156 if (f == NULL) {
157 LOGE("Failed to open %s: %s\n", filename, strerror(errno));
158 return INSTALL_ERROR;
159 }
160 if (fread(data, 1, data_size, f) != data_size) {
161 LOGE("Failed to read firmware data: %s\n", strerror(errno));
162 return INSTALL_ERROR;
163 }
164 fclose(f);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700165 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700166
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700167 if (remember_firmware_update(type, data, data_size)) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700168 LOGE("Can't store %s image\n", type);
169 free(data);
170 return INSTALL_ERROR;
171 }
172 free(filename);
173
174 return INSTALL_SUCCESS;
175}
176
177// If the package contains an update binary, extract it and run it.
178static int
179try_update_binary(const char *path, ZipArchive *zip) {
180 const ZipEntry* binary_entry =
181 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
182 if (binary_entry == NULL) {
183 return INSTALL_CORRUPT;
184 }
185
186 char* binary = "/tmp/update_binary";
187 unlink(binary);
188 int fd = creat(binary, 0755);
189 if (fd < 0) {
190 LOGE("Can't make %s\n", binary);
191 return 1;
192 }
193 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
194 close(fd);
195
196 if (!ok) {
197 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
198 return 1;
199 }
200
201 int pipefd[2];
202 pipe(pipefd);
203
204 // When executing the update binary contained in the package, the
205 // arguments passed are:
206 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700207 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700208 //
209 // - an fd to which the program can write in order to update the
210 // progress bar. The program can write single-line commands:
211 //
212 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700213 // fill up the next <frac> part of of the progress bar
214 // over <secs> seconds. If <secs> is zero, use
215 // set_progress commands to manually control the
216 // progress of this segment of the bar
217 //
218 // set_progress <frac>
219 // <frac> should be between 0.0 and 1.0; sets the
220 // progress bar within the segment defined by the most
221 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700222 //
223 // firmware <"hboot"|"radio"> <filename>
224 // arrange to install the contents of <filename> in the
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700225 // given partition on reboot. (API v2: <filename> may
226 // start with "PACKAGE:" to indicate taking a file from
227 // the OTA package.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700228 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 // ui_print <string>
230 // display <string> on the screen.
231 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700232 // - the name of the package zip file.
233 //
234
235 char** args = malloc(sizeof(char*) * 5);
236 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700237 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700238 args[2] = malloc(10);
239 sprintf(args[2], "%d", pipefd[1]);
240 args[3] = (char*)path;
241 args[4] = NULL;
242
243 pid_t pid = fork();
244 if (pid == 0) {
245 close(pipefd[0]);
246 execv(binary, args);
247 fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno));
248 _exit(-1);
249 }
250 close(pipefd[1]);
251
252 char* firmware_type = NULL;
253 char* firmware_filename = NULL;
254
255 char buffer[81];
256 FILE* from_child = fdopen(pipefd[0], "r");
257 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
258 LOGI("read: %s", buffer);
259
260 char* command = strtok(buffer, " \n");
261 if (command == NULL) {
262 continue;
263 } else if (strcmp(command, "progress") == 0) {
264 char* fraction_s = strtok(NULL, " \n");
265 char* seconds_s = strtok(NULL, " \n");
266
267 float fraction = strtof(fraction_s, NULL);
268 int seconds = strtol(seconds_s, NULL, 10);
269
270 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
271 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700272 } else if (strcmp(command, "set_progress") == 0) {
273 char* fraction_s = strtok(NULL, " \n");
274 float fraction = strtof(fraction_s, NULL);
275 ui_set_progress(fraction);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700276 } else if (strcmp(command, "firmware") == 0) {
277 char* type = strtok(NULL, " \n");
278 char* filename = strtok(NULL, " \n");
279
280 if (type != NULL && filename != NULL) {
281 if (firmware_type != NULL) {
282 LOGE("ignoring attempt to do multiple firmware updates");
283 } else {
284 firmware_type = strdup(type);
285 firmware_filename = strdup(filename);
286 }
287 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700288 } else if (strcmp(command, "ui_print") == 0) {
289 char* str = strtok(NULL, "\n");
290 if (str) {
291 ui_print(str);
292 } else {
293 ui_print("\n");
294 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700295 } else {
296 LOGE("unknown command [%s]\n", command);
297 }
298 }
299 fclose(from_child);
300
301 int status;
302 waitpid(pid, &status, 0);
303 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700305 return INSTALL_ERROR;
306 }
307
308 if (firmware_type != NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700309 return handle_firmware_update(firmware_type, firmware_filename, zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700310 } else {
311 return INSTALL_SUCCESS;
312 }
313}
314
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800315static int
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700316handle_update_package(const char *path, ZipArchive *zip,
317 const RSAPublicKey *keys, int numKeys)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800318{
319 // Give verification half the progress bar...
320 ui_print("Verifying update package...\n");
321 ui_show_progress(
322 VERIFICATION_PROGRESS_FRACTION,
323 VERIFICATION_PROGRESS_TIME);
324
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700325 if (!verify_jar_signature(zip, keys, numKeys)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800326 LOGE("Verification failed\n");
327 return INSTALL_CORRUPT;
328 }
329
330 // Update should take the rest of the progress bar.
331 ui_print("Installing update...\n");
332
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700333 int result = try_update_binary(path, zip);
334 if (result == INSTALL_SUCCESS || result == INSTALL_ERROR) {
335 register_package_root(NULL, NULL); // Unregister package root
336 return result;
337 }
338
339 // if INSTALL_CORRUPT is returned, this package doesn't have an
340 // update binary. Fall back to the older mechanism of looking for
341 // an update script.
342
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800343 const ZipEntry *script_entry;
344 script_entry = find_update_script(zip);
345 if (script_entry == NULL) {
346 LOGE("Can't find update script\n");
347 return INSTALL_CORRUPT;
348 }
349
350 if (register_package_root(zip, path) < 0) {
351 LOGE("Can't register package root\n");
352 return INSTALL_ERROR;
353 }
354
355 int ret = handle_update_script(zip, script_entry);
356 register_package_root(NULL, NULL); // Unregister package root
357 return ret;
358}
359
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700360// Reads a file containing one or more public keys as produced by
361// DumpPublicKey: this is an RSAPublicKey struct as it would appear
362// as a C source literal, eg:
363//
364// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
365//
366// (Note that the braces and commas in this example are actual
367// characters the parser expects to find in the file; the ellipses
368// indicate more numbers omitted from this example.)
369//
370// The file may contain multiple keys in this format, separated by
371// commas. The last key must not be followed by a comma.
372//
373// Returns NULL if the file failed to parse, or if it contain zero keys.
374static RSAPublicKey*
375load_keys(const char* filename, int* numKeys) {
376 RSAPublicKey* out = NULL;
377 *numKeys = 0;
378
379 FILE* f = fopen(filename, "r");
380 if (f == NULL) {
381 LOGE("opening %s: %s\n", filename, strerror(errno));
382 goto exit;
383 }
384
385 int i;
386 bool done = false;
387 while (!done) {
388 ++*numKeys;
389 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
390 RSAPublicKey* key = out + (*numKeys - 1);
391 if (fscanf(f, " { %i , %i , { %i",
392 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
393 goto exit;
394 }
395 if (key->len != RSANUMWORDS) {
396 LOGE("key length (%d) does not match expected size\n", key->len);
397 goto exit;
398 }
399 for (i = 1; i < key->len; ++i) {
400 if (fscanf(f, " , %i", &(key->n[i])) != 1) goto exit;
401 }
402 if (fscanf(f, " } , { %i", &(key->rr[0])) != 1) goto exit;
403 for (i = 1; i < key->len; ++i) {
404 if (fscanf(f, " , %i", &(key->rr[i])) != 1) goto exit;
405 }
406 fscanf(f, " } } ");
407
408 // if the line ends in a comma, this file has more keys.
409 switch (fgetc(f)) {
410 case ',':
411 // more keys to come.
412 break;
413
414 case EOF:
415 done = true;
416 break;
417
418 default:
419 LOGE("unexpected character between keys\n");
420 goto exit;
421 }
422 }
423
424 fclose(f);
425 return out;
426
427exit:
428 if (f) fclose(f);
429 free(out);
430 *numKeys = 0;
431 return NULL;
432}
433
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800434int
435install_package(const char *root_path)
436{
437 ui_set_background(BACKGROUND_ICON_INSTALLING);
438 ui_print("Finding update package...\n");
439 ui_show_indeterminate_progress();
440 LOGI("Update location: %s\n", root_path);
441
442 if (ensure_root_path_mounted(root_path) != 0) {
443 LOGE("Can't mount %s\n", root_path);
444 return INSTALL_CORRUPT;
445 }
446
447 char path[PATH_MAX] = "";
448 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
449 LOGE("Bad path %s\n", root_path);
450 return INSTALL_CORRUPT;
451 }
452
453 ui_print("Opening update package...\n");
454 LOGI("Update file path: %s\n", path);
455
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700456 int numKeys;
457 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
458 if (loadedKeys == NULL) {
459 LOGE("Failed to load keys\n");
460 return INSTALL_CORRUPT;
461 }
462 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
463
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464 /* Try to open the package.
465 */
466 ZipArchive zip;
467 int err = mzOpenZipArchive(path, &zip);
468 if (err != 0) {
469 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
470 return INSTALL_CORRUPT;
471 }
472
473 /* Verify and install the contents of the package.
474 */
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700475 int status = handle_update_package(path, &zip, loadedKeys, numKeys);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800476 mzCloseZipArchive(&zip);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700477 free(loadedKeys);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800478 return status;
479}