blob: cbb35805c88f4cc9986df42d4f77665b82fcce45 [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>
213 // fill up <frac> of the progress bar over <secs> seconds.
214 //
215 // firmware <"hboot"|"radio"> <filename>
216 // arrange to install the contents of <filename> in the
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700217 // given partition on reboot. (API v2: <filename> may
218 // start with "PACKAGE:" to indicate taking a file from
219 // the OTA package.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700220 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700221 // ui_print <string>
222 // display <string> on the screen.
223 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700224 // - the name of the package zip file.
225 //
226
227 char** args = malloc(sizeof(char*) * 5);
228 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700229 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700230 args[2] = malloc(10);
231 sprintf(args[2], "%d", pipefd[1]);
232 args[3] = (char*)path;
233 args[4] = NULL;
234
235 pid_t pid = fork();
236 if (pid == 0) {
237 close(pipefd[0]);
238 execv(binary, args);
239 fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno));
240 _exit(-1);
241 }
242 close(pipefd[1]);
243
244 char* firmware_type = NULL;
245 char* firmware_filename = NULL;
246
247 char buffer[81];
248 FILE* from_child = fdopen(pipefd[0], "r");
249 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
250 LOGI("read: %s", buffer);
251
252 char* command = strtok(buffer, " \n");
253 if (command == NULL) {
254 continue;
255 } else if (strcmp(command, "progress") == 0) {
256 char* fraction_s = strtok(NULL, " \n");
257 char* seconds_s = strtok(NULL, " \n");
258
259 float fraction = strtof(fraction_s, NULL);
260 int seconds = strtol(seconds_s, NULL, 10);
261
262 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
263 seconds);
264 } else if (strcmp(command, "firmware") == 0) {
265 char* type = strtok(NULL, " \n");
266 char* filename = strtok(NULL, " \n");
267
268 if (type != NULL && filename != NULL) {
269 if (firmware_type != NULL) {
270 LOGE("ignoring attempt to do multiple firmware updates");
271 } else {
272 firmware_type = strdup(type);
273 firmware_filename = strdup(filename);
274 }
275 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700276 } else if (strcmp(command, "ui_print") == 0) {
277 char* str = strtok(NULL, "\n");
278 if (str) {
279 ui_print(str);
280 } else {
281 ui_print("\n");
282 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700283 } else {
284 LOGE("unknown command [%s]\n", command);
285 }
286 }
287 fclose(from_child);
288
289 int status;
290 waitpid(pid, &status, 0);
291 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700293 return INSTALL_ERROR;
294 }
295
296 if (firmware_type != NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700297 return handle_firmware_update(firmware_type, firmware_filename, zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700298 } else {
299 return INSTALL_SUCCESS;
300 }
301}
302
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303static int
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700304handle_update_package(const char *path, ZipArchive *zip,
305 const RSAPublicKey *keys, int numKeys)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306{
307 // Give verification half the progress bar...
308 ui_print("Verifying update package...\n");
309 ui_show_progress(
310 VERIFICATION_PROGRESS_FRACTION,
311 VERIFICATION_PROGRESS_TIME);
312
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700313 if (!verify_jar_signature(zip, keys, numKeys)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800314 LOGE("Verification failed\n");
315 return INSTALL_CORRUPT;
316 }
317
318 // Update should take the rest of the progress bar.
319 ui_print("Installing update...\n");
320
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700321 int result = try_update_binary(path, zip);
322 if (result == INSTALL_SUCCESS || result == INSTALL_ERROR) {
323 register_package_root(NULL, NULL); // Unregister package root
324 return result;
325 }
326
327 // if INSTALL_CORRUPT is returned, this package doesn't have an
328 // update binary. Fall back to the older mechanism of looking for
329 // an update script.
330
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331 const ZipEntry *script_entry;
332 script_entry = find_update_script(zip);
333 if (script_entry == NULL) {
334 LOGE("Can't find update script\n");
335 return INSTALL_CORRUPT;
336 }
337
338 if (register_package_root(zip, path) < 0) {
339 LOGE("Can't register package root\n");
340 return INSTALL_ERROR;
341 }
342
343 int ret = handle_update_script(zip, script_entry);
344 register_package_root(NULL, NULL); // Unregister package root
345 return ret;
346}
347
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700348// Reads a file containing one or more public keys as produced by
349// DumpPublicKey: this is an RSAPublicKey struct as it would appear
350// as a C source literal, eg:
351//
352// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
353//
354// (Note that the braces and commas in this example are actual
355// characters the parser expects to find in the file; the ellipses
356// indicate more numbers omitted from this example.)
357//
358// The file may contain multiple keys in this format, separated by
359// commas. The last key must not be followed by a comma.
360//
361// Returns NULL if the file failed to parse, or if it contain zero keys.
362static RSAPublicKey*
363load_keys(const char* filename, int* numKeys) {
364 RSAPublicKey* out = NULL;
365 *numKeys = 0;
366
367 FILE* f = fopen(filename, "r");
368 if (f == NULL) {
369 LOGE("opening %s: %s\n", filename, strerror(errno));
370 goto exit;
371 }
372
373 int i;
374 bool done = false;
375 while (!done) {
376 ++*numKeys;
377 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
378 RSAPublicKey* key = out + (*numKeys - 1);
379 if (fscanf(f, " { %i , %i , { %i",
380 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
381 goto exit;
382 }
383 if (key->len != RSANUMWORDS) {
384 LOGE("key length (%d) does not match expected size\n", key->len);
385 goto exit;
386 }
387 for (i = 1; i < key->len; ++i) {
388 if (fscanf(f, " , %i", &(key->n[i])) != 1) goto exit;
389 }
390 if (fscanf(f, " } , { %i", &(key->rr[0])) != 1) goto exit;
391 for (i = 1; i < key->len; ++i) {
392 if (fscanf(f, " , %i", &(key->rr[i])) != 1) goto exit;
393 }
394 fscanf(f, " } } ");
395
396 // if the line ends in a comma, this file has more keys.
397 switch (fgetc(f)) {
398 case ',':
399 // more keys to come.
400 break;
401
402 case EOF:
403 done = true;
404 break;
405
406 default:
407 LOGE("unexpected character between keys\n");
408 goto exit;
409 }
410 }
411
412 fclose(f);
413 return out;
414
415exit:
416 if (f) fclose(f);
417 free(out);
418 *numKeys = 0;
419 return NULL;
420}
421
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800422int
423install_package(const char *root_path)
424{
425 ui_set_background(BACKGROUND_ICON_INSTALLING);
426 ui_print("Finding update package...\n");
427 ui_show_indeterminate_progress();
428 LOGI("Update location: %s\n", root_path);
429
430 if (ensure_root_path_mounted(root_path) != 0) {
431 LOGE("Can't mount %s\n", root_path);
432 return INSTALL_CORRUPT;
433 }
434
435 char path[PATH_MAX] = "";
436 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
437 LOGE("Bad path %s\n", root_path);
438 return INSTALL_CORRUPT;
439 }
440
441 ui_print("Opening update package...\n");
442 LOGI("Update file path: %s\n", path);
443
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700444 int numKeys;
445 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
446 if (loadedKeys == NULL) {
447 LOGE("Failed to load keys\n");
448 return INSTALL_CORRUPT;
449 }
450 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
451
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800452 /* Try to open the package.
453 */
454 ZipArchive zip;
455 int err = mzOpenZipArchive(path, &zip);
456 if (err != 0) {
457 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
458 return INSTALL_CORRUPT;
459 }
460
461 /* Verify and install the contents of the package.
462 */
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700463 int status = handle_update_package(path, &zip, loadedKeys, numKeys);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464 mzCloseZipArchive(&zip);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700465 free(loadedKeys);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800466 return status;
467}