blob: 8b319a035fd55f5eede378d47455151c334082d4 [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"
Koushik Dutta19447c02010-11-10 10:40:44 -080031#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032#include "mtdutils/mtdutils.h"
33#include "roots.h"
34#include "verifier.h"
35
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#include "firmware.h"
Koushik K. Dutta4c1eed22010-02-11 18:59:58 -080037#include "legacy.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080039#include "extendedcommands.h"
40
Koushik Dutta0eb14b32010-06-23 17:38:05 -070041
Doug Zongkerb2ee9202009-06-04 10:24:53 -070042#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070043#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Doug Zongkerb2ee9202009-06-04 10:24:53 -070045// The update binary ask us to install a firmware file on reboot. Set
46// that up. Takes ownership of type and filename.
47static int
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070048handle_firmware_update(char* type, char* filename, ZipArchive* zip) {
49 unsigned int data_size;
50 const ZipEntry* entry = NULL;
51
52 if (strncmp(filename, "PACKAGE:", 8) == 0) {
53 entry = mzFindZipEntry(zip, filename+8);
54 if (entry == NULL) {
55 LOGE("Failed to find \"%s\" in package", filename+8);
56 return INSTALL_ERROR;
57 }
58 data_size = entry->uncompLen;
59 } else {
60 struct stat st_data;
61 if (stat(filename, &st_data) < 0) {
62 LOGE("Error stat'ing %s: %s\n", filename, strerror(errno));
63 return INSTALL_ERROR;
64 }
65 data_size = st_data.st_size;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070066 }
67
Doug Zongker8edb00c2009-06-11 17:21:44 -070068 LOGI("type is %s; size is %d; file is %s\n",
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070069 type, data_size, filename);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070070
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070071 char* data = malloc(data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070072 if (data == NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070073 LOGI("Can't allocate %d bytes for firmware data\n", data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070074 return INSTALL_ERROR;
75 }
76
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070077 if (entry) {
78 if (mzReadZipEntry(zip, entry, data, data_size) == false) {
79 LOGE("Failed to read \"%s\" from package", filename+8);
80 return INSTALL_ERROR;
81 }
82 } else {
83 FILE* f = fopen(filename, "rb");
84 if (f == NULL) {
85 LOGE("Failed to open %s: %s\n", filename, strerror(errno));
86 return INSTALL_ERROR;
87 }
88 if (fread(data, 1, data_size, f) != data_size) {
89 LOGE("Failed to read firmware data: %s\n", strerror(errno));
90 return INSTALL_ERROR;
91 }
92 fclose(f);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070093 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -070094
Koushik Dutta5aaa8232010-07-20 16:23:18 -070095#ifndef BOARD_HAS_NO_MISC_PARTITION
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070096 if (remember_firmware_update(type, data, data_size)) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070097 LOGE("Can't store %s image\n", type);
98 free(data);
99 return INSTALL_ERROR;
100 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -0700101#endif
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700102 free(filename);
103
104 return INSTALL_SUCCESS;
105}
106
107// If the package contains an update binary, extract it and run it.
108static int
109try_update_binary(const char *path, ZipArchive *zip) {
110 const ZipEntry* binary_entry =
111 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
112 if (binary_entry == NULL) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700113 return INSTALL_UPDATE_BINARY_MISSING;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700114 }
115
116 char* binary = "/tmp/update_binary";
117 unlink(binary);
118 int fd = creat(binary, 0755);
119 if (fd < 0) {
120 LOGE("Can't make %s\n", binary);
121 return 1;
122 }
123 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
124 close(fd);
125
126 if (!ok) {
127 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
128 return 1;
129 }
130
131 int pipefd[2];
132 pipe(pipefd);
133
134 // When executing the update binary contained in the package, the
135 // arguments passed are:
136 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700137 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700138 //
139 // - an fd to which the program can write in order to update the
140 // progress bar. The program can write single-line commands:
141 //
142 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700143 // fill up the next <frac> part of of the progress bar
144 // over <secs> seconds. If <secs> is zero, use
145 // set_progress commands to manually control the
146 // progress of this segment of the bar
147 //
148 // set_progress <frac>
149 // <frac> should be between 0.0 and 1.0; sets the
150 // progress bar within the segment defined by the most
151 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700152 //
153 // firmware <"hboot"|"radio"> <filename>
154 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800155 // given partition on reboot.
156 //
157 // (API v2: <filename> may start with "PACKAGE:" to
158 // indicate taking a file from the OTA package.)
159 //
160 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700161 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700162 // ui_print <string>
163 // display <string> on the screen.
164 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700165 // - the name of the package zip file.
166 //
167
168 char** args = malloc(sizeof(char*) * 5);
169 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700170 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700171 args[2] = malloc(10);
172 sprintf(args[2], "%d", pipefd[1]);
173 args[3] = (char*)path;
174 args[4] = NULL;
175
176 pid_t pid = fork();
177 if (pid == 0) {
178 close(pipefd[0]);
179 execv(binary, args);
180 fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno));
181 _exit(-1);
182 }
183 close(pipefd[1]);
184
Koushik Dutta19447c02010-11-10 10:40:44 -0800185 char* firmware_type = NULL;
186 char* firmware_filename = NULL;
187
Doug Zongker64893cc2009-07-14 16:31:56 -0700188 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700189 FILE* from_child = fdopen(pipefd[0], "r");
190 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700191 char* command = strtok(buffer, " \n");
192 if (command == NULL) {
193 continue;
194 } else if (strcmp(command, "progress") == 0) {
195 char* fraction_s = strtok(NULL, " \n");
196 char* seconds_s = strtok(NULL, " \n");
197
198 float fraction = strtof(fraction_s, NULL);
199 int seconds = strtol(seconds_s, NULL, 10);
200
201 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
202 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700203 } else if (strcmp(command, "set_progress") == 0) {
204 char* fraction_s = strtok(NULL, " \n");
205 float fraction = strtof(fraction_s, NULL);
206 ui_set_progress(fraction);
Koushik Dutta19447c02010-11-10 10:40:44 -0800207 } else if (strcmp(command, "firmware") == 0) {
208 char* type = strtok(NULL, " \n");
209 char* filename = strtok(NULL, " \n");
210
211 if (type != NULL && filename != NULL) {
212 if (firmware_type != NULL) {
213 LOGE("ignoring attempt to do multiple firmware updates");
214 } else {
215 firmware_type = strdup(type);
216 firmware_filename = strdup(filename);
217 }
218 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700219 } else if (strcmp(command, "ui_print") == 0) {
220 char* str = strtok(NULL, "\n");
221 if (str) {
222 ui_print(str);
223 } else {
224 ui_print("\n");
225 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700226 } else {
227 LOGE("unknown command [%s]\n", command);
228 }
229 }
230 fclose(from_child);
231
232 int status;
233 waitpid(pid, &status, 0);
234 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700235 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700236 return INSTALL_ERROR;
237 }
238
Koushik Dutta19447c02010-11-10 10:40:44 -0800239 if (firmware_type != NULL) {
240 return handle_firmware_update(firmware_type, firmware_filename, zip);
241 } else {
242 return INSTALL_SUCCESS;
243 }
Doug Zongkere08991e2010-02-02 13:09:52 -0800244 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700245}
246
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800247static int
Doug Zongker54e2e862009-08-17 13:21:04 -0700248handle_update_package(const char *path, ZipArchive *zip)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800249{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250 // Update should take the rest of the progress bar.
251 ui_print("Installing update...\n");
252
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700253 LOGI("Trying update-binary.\n");
254 int result = try_update_binary(path, zip);
255
256 if (result == INSTALL_UPDATE_BINARY_MISSING)
Koushik K. Duttae9234872010-02-12 00:43:24 -0800257 {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700258 register_package_root(NULL, NULL); // Unregister package root
259 if (register_package_root(zip, path) < 0) {
260 LOGE("Can't register package root\n");
261 return INSTALL_ERROR;
262 }
263 const ZipEntry *script_entry;
264 script_entry = find_update_script(zip);
265 LOGI("Trying update-script.\n");
266 result = handle_update_script(zip, script_entry);
267 if (result == INSTALL_UPDATE_SCRIPT_MISSING)
268 result = INSTALL_ERROR;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800269 }
270
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 register_package_root(NULL, NULL); // Unregister package root
Doug Zongker64893cc2009-07-14 16:31:56 -0700272 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800273}
274
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700275// Reads a file containing one or more public keys as produced by
276// DumpPublicKey: this is an RSAPublicKey struct as it would appear
277// as a C source literal, eg:
278//
279// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
280//
281// (Note that the braces and commas in this example are actual
282// characters the parser expects to find in the file; the ellipses
283// indicate more numbers omitted from this example.)
284//
285// The file may contain multiple keys in this format, separated by
286// commas. The last key must not be followed by a comma.
287//
288// Returns NULL if the file failed to parse, or if it contain zero keys.
289static RSAPublicKey*
290load_keys(const char* filename, int* numKeys) {
291 RSAPublicKey* out = NULL;
292 *numKeys = 0;
293
294 FILE* f = fopen(filename, "r");
295 if (f == NULL) {
296 LOGE("opening %s: %s\n", filename, strerror(errno));
297 goto exit;
298 }
299
300 int i;
301 bool done = false;
302 while (!done) {
303 ++*numKeys;
304 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
305 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800306 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700307 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
308 goto exit;
309 }
310 if (key->len != RSANUMWORDS) {
311 LOGE("key length (%d) does not match expected size\n", key->len);
312 goto exit;
313 }
314 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800315 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700316 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800317 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700318 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800319 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700320 }
321 fscanf(f, " } } ");
322
323 // if the line ends in a comma, this file has more keys.
324 switch (fgetc(f)) {
325 case ',':
326 // more keys to come.
327 break;
328
329 case EOF:
330 done = true;
331 break;
332
333 default:
334 LOGE("unexpected character between keys\n");
335 goto exit;
336 }
337 }
338
339 fclose(f);
340 return out;
341
342exit:
343 if (f) fclose(f);
344 free(out);
345 *numKeys = 0;
346 return NULL;
347}
348
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800349int
350install_package(const char *root_path)
351{
352 ui_set_background(BACKGROUND_ICON_INSTALLING);
353 ui_print("Finding update package...\n");
354 ui_show_indeterminate_progress();
355 LOGI("Update location: %s\n", root_path);
356
357 if (ensure_root_path_mounted(root_path) != 0) {
358 LOGE("Can't mount %s\n", root_path);
359 return INSTALL_CORRUPT;
360 }
361
362 char path[PATH_MAX] = "";
363 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
364 LOGE("Bad path %s\n", root_path);
365 return INSTALL_CORRUPT;
366 }
367
368 ui_print("Opening update package...\n");
369 LOGI("Update file path: %s\n", path);
370
Doug Zongker54e2e862009-08-17 13:21:04 -0700371 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800372
373 if (signature_check_enabled) {
374 int numKeys;
375 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
376 if (loadedKeys == NULL) {
377 LOGE("Failed to load keys\n");
378 return INSTALL_CORRUPT;
379 }
380 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
381
382 // Give verification half the progress bar...
383 ui_print("Verifying update package...\n");
384 ui_show_progress(
385 VERIFICATION_PROGRESS_FRACTION,
386 VERIFICATION_PROGRESS_TIME);
387
388 err = verify_file(path, loadedKeys, numKeys);
389 free(loadedKeys);
390 LOGI("verify_file returned %d\n", err);
391 if (err != VERIFY_SUCCESS) {
392 LOGE("signature verification failed\n");
393 return INSTALL_CORRUPT;
394 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700395 }
396
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800397 /* Try to open the package.
398 */
399 ZipArchive zip;
Doug Zongker54e2e862009-08-17 13:21:04 -0700400 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800401 if (err != 0) {
402 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
403 return INSTALL_CORRUPT;
404 }
405
406 /* Verify and install the contents of the package.
407 */
Doug Zongker54e2e862009-08-17 13:21:04 -0700408 int status = handle_update_package(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800409 mzCloseZipArchive(&zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800410 return status;
411}