blob: de3ec73c84383e147f175edf027bc6916edbb648 [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"
Doug Zongkerb2ee9202009-06-04 10:24:53 -070035#include "firmware.h"
Koushik K. Dutta4c1eed22010-02-11 18:59:58 -080036#include "legacy.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080038#include "extendedcommands.h"
39
Doug Zongkerb2ee9202009-06-04 10:24:53 -070040#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070041#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Doug Zongkerb2ee9202009-06-04 10:24:53 -070043// The update binary ask us to install a firmware file on reboot. Set
44// that up. Takes ownership of type and filename.
45static int
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070046handle_firmware_update(char* type, char* filename, ZipArchive* zip) {
47 unsigned int data_size;
48 const ZipEntry* entry = NULL;
49
50 if (strncmp(filename, "PACKAGE:", 8) == 0) {
51 entry = mzFindZipEntry(zip, filename+8);
52 if (entry == NULL) {
53 LOGE("Failed to find \"%s\" in package", filename+8);
54 return INSTALL_ERROR;
55 }
56 data_size = entry->uncompLen;
57 } else {
58 struct stat st_data;
59 if (stat(filename, &st_data) < 0) {
60 LOGE("Error stat'ing %s: %s\n", filename, strerror(errno));
61 return INSTALL_ERROR;
62 }
63 data_size = st_data.st_size;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070064 }
65
Doug Zongker8edb00c2009-06-11 17:21:44 -070066 LOGI("type is %s; size is %d; file is %s\n",
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070067 type, data_size, filename);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070068
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070069 char* data = malloc(data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070070 if (data == NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070071 LOGI("Can't allocate %d bytes for firmware data\n", data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070072 return INSTALL_ERROR;
73 }
74
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070075 if (entry) {
76 if (mzReadZipEntry(zip, entry, data, data_size) == false) {
77 LOGE("Failed to read \"%s\" from package", filename+8);
78 return INSTALL_ERROR;
79 }
80 } else {
81 FILE* f = fopen(filename, "rb");
82 if (f == NULL) {
83 LOGE("Failed to open %s: %s\n", filename, strerror(errno));
84 return INSTALL_ERROR;
85 }
86 if (fread(data, 1, data_size, f) != data_size) {
87 LOGE("Failed to read firmware data: %s\n", strerror(errno));
88 return INSTALL_ERROR;
89 }
90 fclose(f);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070091 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -070092
Koushik Dutta5aaa8232010-07-20 16:23:18 -070093#ifndef BOARD_HAS_NO_MISC_PARTITION
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070094 if (remember_firmware_update(type, data, data_size)) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070095 LOGE("Can't store %s image\n", type);
96 free(data);
97 return INSTALL_ERROR;
98 }
Koushik Dutta5aaa8232010-07-20 16:23:18 -070099#endif
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700100 free(filename);
101
102 return INSTALL_SUCCESS;
103}
104
105// If the package contains an update binary, extract it and run it.
106static int
107try_update_binary(const char *path, ZipArchive *zip) {
108 const ZipEntry* binary_entry =
109 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
110 if (binary_entry == NULL) {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700111 return INSTALL_UPDATE_BINARY_MISSING;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700112 }
113
114 char* binary = "/tmp/update_binary";
115 unlink(binary);
116 int fd = creat(binary, 0755);
117 if (fd < 0) {
118 LOGE("Can't make %s\n", binary);
119 return 1;
120 }
121 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
122 close(fd);
123
124 if (!ok) {
125 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
126 return 1;
127 }
128
129 int pipefd[2];
130 pipe(pipefd);
131
132 // When executing the update binary contained in the package, the
133 // arguments passed are:
134 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700135 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700136 //
137 // - an fd to which the program can write in order to update the
138 // progress bar. The program can write single-line commands:
139 //
140 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700141 // fill up the next <frac> part of of the progress bar
142 // over <secs> seconds. If <secs> is zero, use
143 // set_progress commands to manually control the
144 // progress of this segment of the bar
145 //
146 // set_progress <frac>
147 // <frac> should be between 0.0 and 1.0; sets the
148 // progress bar within the segment defined by the most
149 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700150 //
151 // firmware <"hboot"|"radio"> <filename>
152 // arrange to install the contents of <filename> in the
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700153 // given partition on reboot. (API v2: <filename> may
154 // start with "PACKAGE:" to indicate taking a file from
155 // the OTA package.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700156 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700157 // ui_print <string>
158 // display <string> on the screen.
159 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700160 // - the name of the package zip file.
161 //
162
163 char** args = malloc(sizeof(char*) * 5);
164 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700165 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700166 args[2] = malloc(10);
167 sprintf(args[2], "%d", pipefd[1]);
168 args[3] = (char*)path;
169 args[4] = NULL;
170
171 pid_t pid = fork();
172 if (pid == 0) {
173 close(pipefd[0]);
174 execv(binary, args);
175 fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno));
176 _exit(-1);
177 }
178 close(pipefd[1]);
179
180 char* firmware_type = NULL;
181 char* firmware_filename = NULL;
182
Doug Zongker64893cc2009-07-14 16:31:56 -0700183 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700184 FILE* from_child = fdopen(pipefd[0], "r");
185 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700186 char* command = strtok(buffer, " \n");
187 if (command == NULL) {
188 continue;
189 } else if (strcmp(command, "progress") == 0) {
190 char* fraction_s = strtok(NULL, " \n");
191 char* seconds_s = strtok(NULL, " \n");
192
193 float fraction = strtof(fraction_s, NULL);
194 int seconds = strtol(seconds_s, NULL, 10);
195
196 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
197 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700198 } else if (strcmp(command, "set_progress") == 0) {
199 char* fraction_s = strtok(NULL, " \n");
200 float fraction = strtof(fraction_s, NULL);
201 ui_set_progress(fraction);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700202 } else if (strcmp(command, "firmware") == 0) {
203 char* type = strtok(NULL, " \n");
204 char* filename = strtok(NULL, " \n");
205
206 if (type != NULL && filename != NULL) {
207 if (firmware_type != NULL) {
208 LOGE("ignoring attempt to do multiple firmware updates");
209 } else {
210 firmware_type = strdup(type);
211 firmware_filename = strdup(filename);
212 }
213 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700214 } else if (strcmp(command, "ui_print") == 0) {
215 char* str = strtok(NULL, "\n");
216 if (str) {
217 ui_print(str);
218 } else {
219 ui_print("\n");
220 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700221 } else {
222 LOGE("unknown command [%s]\n", command);
223 }
224 }
225 fclose(from_child);
226
227 int status;
228 waitpid(pid, &status, 0);
229 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700231 return INSTALL_ERROR;
232 }
233
234 if (firmware_type != NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700235 return handle_firmware_update(firmware_type, firmware_filename, zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700236 } else {
237 return INSTALL_SUCCESS;
238 }
239}
240
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241static int
Doug Zongker54e2e862009-08-17 13:21:04 -0700242handle_update_package(const char *path, ZipArchive *zip)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244 // Update should take the rest of the progress bar.
245 ui_print("Installing update...\n");
246
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700247 LOGI("Trying update-binary.\n");
248 int result = try_update_binary(path, zip);
249
250 if (result == INSTALL_UPDATE_BINARY_MISSING)
Koushik K. Duttae9234872010-02-12 00:43:24 -0800251 {
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700252 register_package_root(NULL, NULL); // Unregister package root
253 if (register_package_root(zip, path) < 0) {
254 LOGE("Can't register package root\n");
255 return INSTALL_ERROR;
256 }
257 const ZipEntry *script_entry;
258 script_entry = find_update_script(zip);
259 LOGI("Trying update-script.\n");
260 result = handle_update_script(zip, script_entry);
261 if (result == INSTALL_UPDATE_SCRIPT_MISSING)
262 result = INSTALL_ERROR;
Koushik K. Duttae9234872010-02-12 00:43:24 -0800263 }
264
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265 register_package_root(NULL, NULL); // Unregister package root
Doug Zongker64893cc2009-07-14 16:31:56 -0700266 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800267}
268
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700269// Reads a file containing one or more public keys as produced by
270// DumpPublicKey: this is an RSAPublicKey struct as it would appear
271// as a C source literal, eg:
272//
273// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
274//
275// (Note that the braces and commas in this example are actual
276// characters the parser expects to find in the file; the ellipses
277// indicate more numbers omitted from this example.)
278//
279// The file may contain multiple keys in this format, separated by
280// commas. The last key must not be followed by a comma.
281//
282// Returns NULL if the file failed to parse, or if it contain zero keys.
283static RSAPublicKey*
284load_keys(const char* filename, int* numKeys) {
285 RSAPublicKey* out = NULL;
286 *numKeys = 0;
287
288 FILE* f = fopen(filename, "r");
289 if (f == NULL) {
290 LOGE("opening %s: %s\n", filename, strerror(errno));
291 goto exit;
292 }
293
294 int i;
295 bool done = false;
296 while (!done) {
297 ++*numKeys;
298 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
299 RSAPublicKey* key = out + (*numKeys - 1);
300 if (fscanf(f, " { %i , %i , { %i",
301 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
302 goto exit;
303 }
304 if (key->len != RSANUMWORDS) {
305 LOGE("key length (%d) does not match expected size\n", key->len);
306 goto exit;
307 }
308 for (i = 1; i < key->len; ++i) {
309 if (fscanf(f, " , %i", &(key->n[i])) != 1) goto exit;
310 }
311 if (fscanf(f, " } , { %i", &(key->rr[0])) != 1) goto exit;
312 for (i = 1; i < key->len; ++i) {
313 if (fscanf(f, " , %i", &(key->rr[i])) != 1) goto exit;
314 }
315 fscanf(f, " } } ");
316
317 // if the line ends in a comma, this file has more keys.
318 switch (fgetc(f)) {
319 case ',':
320 // more keys to come.
321 break;
322
323 case EOF:
324 done = true;
325 break;
326
327 default:
328 LOGE("unexpected character between keys\n");
329 goto exit;
330 }
331 }
332
333 fclose(f);
334 return out;
335
336exit:
337 if (f) fclose(f);
338 free(out);
339 *numKeys = 0;
340 return NULL;
341}
342
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800343int
344install_package(const char *root_path)
345{
346 ui_set_background(BACKGROUND_ICON_INSTALLING);
347 ui_print("Finding update package...\n");
348 ui_show_indeterminate_progress();
349 LOGI("Update location: %s\n", root_path);
350
351 if (ensure_root_path_mounted(root_path) != 0) {
352 LOGE("Can't mount %s\n", root_path);
353 return INSTALL_CORRUPT;
354 }
355
356 char path[PATH_MAX] = "";
357 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
358 LOGE("Bad path %s\n", root_path);
359 return INSTALL_CORRUPT;
360 }
361
362 ui_print("Opening update package...\n");
363 LOGI("Update file path: %s\n", path);
364
Doug Zongker54e2e862009-08-17 13:21:04 -0700365 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800366
367 if (signature_check_enabled) {
368 int numKeys;
369 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
370 if (loadedKeys == NULL) {
371 LOGE("Failed to load keys\n");
372 return INSTALL_CORRUPT;
373 }
374 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
375
376 // Give verification half the progress bar...
377 ui_print("Verifying update package...\n");
378 ui_show_progress(
379 VERIFICATION_PROGRESS_FRACTION,
380 VERIFICATION_PROGRESS_TIME);
381
382 err = verify_file(path, loadedKeys, numKeys);
383 free(loadedKeys);
384 LOGI("verify_file returned %d\n", err);
385 if (err != VERIFY_SUCCESS) {
386 LOGE("signature verification failed\n");
387 return INSTALL_CORRUPT;
388 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700389 }
390
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800391 /* Try to open the package.
392 */
393 ZipArchive zip;
Doug Zongker54e2e862009-08-17 13:21:04 -0700394 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800395 if (err != 0) {
396 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
397 return INSTALL_CORRUPT;
398 }
399
400 /* Verify and install the contents of the package.
401 */
Doug Zongker54e2e862009-08-17 13:21:04 -0700402 int status = handle_update_package(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800403 mzCloseZipArchive(&zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800404 return status;
405}