blob: 2d8a4cde7f5dd69fb19967d03a5aa92925acfb94 [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"
37
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080038#include "extendedcommands.h"
39
Koushik Dutta0eb14b32010-06-23 17:38:05 -070040
Doug Zongkerb2ee9202009-06-04 10:24:53 -070041#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070042#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Doug Zongkerb2ee9202009-06-04 10:24:53 -070044// The update binary ask us to install a firmware file on reboot. Set
45// that up. Takes ownership of type and filename.
46static int
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070047handle_firmware_update(char* type, char* filename, ZipArchive* zip) {
48 unsigned int data_size;
49 const ZipEntry* entry = NULL;
50
51 if (strncmp(filename, "PACKAGE:", 8) == 0) {
52 entry = mzFindZipEntry(zip, filename+8);
53 if (entry == NULL) {
54 LOGE("Failed to find \"%s\" in package", filename+8);
55 return INSTALL_ERROR;
56 }
57 data_size = entry->uncompLen;
58 } else {
59 struct stat st_data;
60 if (stat(filename, &st_data) < 0) {
61 LOGE("Error stat'ing %s: %s\n", filename, strerror(errno));
62 return INSTALL_ERROR;
63 }
64 data_size = st_data.st_size;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070065 }
66
Doug Zongker8edb00c2009-06-11 17:21:44 -070067 LOGI("type is %s; size is %d; file is %s\n",
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070068 type, data_size, filename);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070069
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070070 char* data = malloc(data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070071 if (data == NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070072 LOGI("Can't allocate %d bytes for firmware data\n", data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070073 return INSTALL_ERROR;
74 }
75
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070076 if (entry) {
77 if (mzReadZipEntry(zip, entry, data, data_size) == false) {
78 LOGE("Failed to read \"%s\" from package", filename+8);
79 return INSTALL_ERROR;
80 }
81 } else {
82 FILE* f = fopen(filename, "rb");
83 if (f == NULL) {
84 LOGE("Failed to open %s: %s\n", filename, strerror(errno));
85 return INSTALL_ERROR;
86 }
87 if (fread(data, 1, data_size, f) != data_size) {
88 LOGE("Failed to read firmware data: %s\n", strerror(errno));
89 return INSTALL_ERROR;
90 }
91 fclose(f);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070092 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -070093
Doug Zongkerb2ee9202009-06-04 10:24:53 -070094 free(filename);
95
96 return INSTALL_SUCCESS;
97}
98
99// If the package contains an update binary, extract it and run it.
100static int
101try_update_binary(const char *path, ZipArchive *zip) {
102 const ZipEntry* binary_entry =
103 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
104 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700105 mzCloseZipArchive(zip);
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700106 return INSTALL_UPDATE_BINARY_MISSING;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700107 }
108
109 char* binary = "/tmp/update_binary";
110 unlink(binary);
111 int fd = creat(binary, 0755);
112 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700113 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700114 LOGE("Can't make %s\n", binary);
115 return 1;
116 }
117 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
118 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700119 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700120
121 if (!ok) {
122 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
123 return 1;
124 }
125
126 int pipefd[2];
127 pipe(pipefd);
128
129 // When executing the update binary contained in the package, the
130 // arguments passed are:
131 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700132 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700133 //
134 // - an fd to which the program can write in order to update the
135 // progress bar. The program can write single-line commands:
136 //
137 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700138 // fill up the next <frac> part of of the progress bar
139 // over <secs> seconds. If <secs> is zero, use
140 // set_progress commands to manually control the
141 // progress of this segment of the bar
142 //
143 // set_progress <frac>
144 // <frac> should be between 0.0 and 1.0; sets the
145 // progress bar within the segment defined by the most
146 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700147 //
148 // firmware <"hboot"|"radio"> <filename>
149 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800150 // given partition on reboot.
151 //
152 // (API v2: <filename> may start with "PACKAGE:" to
153 // indicate taking a file from the OTA package.)
154 //
155 // (API v3: this command no longer exists.)
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);
Doug Zongker56c51052010-07-01 09:18:44 -0700175 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700176 _exit(-1);
177 }
178 close(pipefd[1]);
179
Koushik Dutta19447c02010-11-10 10:40:44 -0800180 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);
Koushik Dutta19447c02010-11-10 10:40:44 -0800202 } 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) {
Nick Kralevich21b97ed2010-06-24 16:11:17 -0700217 ui_print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700218 } 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
Koushik Dutta19447c02010-11-10 10:40:44 -0800234 if (firmware_type != NULL) {
235 return handle_firmware_update(firmware_type, firmware_filename, zip);
236 } else {
237 return INSTALL_SUCCESS;
238 }
Doug Zongkere08991e2010-02-02 13:09:52 -0800239 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700240}
241
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700242// Reads a file containing one or more public keys as produced by
243// DumpPublicKey: this is an RSAPublicKey struct as it would appear
244// as a C source literal, eg:
245//
246// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
247//
248// (Note that the braces and commas in this example are actual
249// characters the parser expects to find in the file; the ellipses
250// indicate more numbers omitted from this example.)
251//
252// The file may contain multiple keys in this format, separated by
253// commas. The last key must not be followed by a comma.
254//
255// Returns NULL if the file failed to parse, or if it contain zero keys.
256static RSAPublicKey*
257load_keys(const char* filename, int* numKeys) {
258 RSAPublicKey* out = NULL;
259 *numKeys = 0;
260
261 FILE* f = fopen(filename, "r");
262 if (f == NULL) {
263 LOGE("opening %s: %s\n", filename, strerror(errno));
264 goto exit;
265 }
266
267 int i;
268 bool done = false;
269 while (!done) {
270 ++*numKeys;
271 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
272 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800273 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700274 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
275 goto exit;
276 }
277 if (key->len != RSANUMWORDS) {
278 LOGE("key length (%d) does not match expected size\n", key->len);
279 goto exit;
280 }
281 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800282 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700283 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800284 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700285 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800286 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700287 }
288 fscanf(f, " } } ");
289
290 // if the line ends in a comma, this file has more keys.
291 switch (fgetc(f)) {
292 case ',':
293 // more keys to come.
294 break;
295
296 case EOF:
297 done = true;
298 break;
299
300 default:
301 LOGE("unexpected character between keys\n");
302 goto exit;
303 }
304 }
305
306 fclose(f);
307 return out;
308
309exit:
310 if (f) fclose(f);
311 free(out);
312 *numKeys = 0;
313 return NULL;
314}
315
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800316int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700317install_package(const char *path)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800318{
319 ui_set_background(BACKGROUND_ICON_INSTALLING);
320 ui_print("Finding update package...\n");
321 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700322 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800323
Doug Zongkerd4208f92010-09-20 12:16:13 -0700324 if (ensure_path_mounted(path) != 0) {
325 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800326 return INSTALL_CORRUPT;
327 }
328
329 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800330
Doug Zongker60151a22009-08-12 18:30:03 -0700331 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800332
333 if (signature_check_enabled) {
334 int numKeys;
335 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
336 if (loadedKeys == NULL) {
337 LOGE("Failed to load keys\n");
338 return INSTALL_CORRUPT;
339 }
340 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
341
342 // Give verification half the progress bar...
343 ui_print("Verifying update package...\n");
344 ui_show_progress(
345 VERIFICATION_PROGRESS_FRACTION,
346 VERIFICATION_PROGRESS_TIME);
347
348 err = verify_file(path, loadedKeys, numKeys);
349 free(loadedKeys);
350 LOGI("verify_file returned %d\n", err);
351 if (err != VERIFY_SUCCESS) {
352 LOGE("signature verification failed\n");
353 return INSTALL_CORRUPT;
354 }
Doug Zongker60151a22009-08-12 18:30:03 -0700355 }
356
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800357 /* Try to open the package.
358 */
359 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700360 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800361 if (err != 0) {
362 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
363 return INSTALL_CORRUPT;
364 }
365
366 /* Verify and install the contents of the package.
367 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700368 ui_print("Installing update...\n");
369 return try_update_binary(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800370}