blob: 3a37b4a76e5d1df05f83a23538bae6df66696fa9 [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
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070093 if (remember_firmware_update(type, data, data_size)) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070094 LOGE("Can't store %s image\n", type);
95 free(data);
96 return INSTALL_ERROR;
97 }
98 free(filename);
99
100 return INSTALL_SUCCESS;
101}
102
103// If the package contains an update binary, extract it and run it.
104static int
105try_update_binary(const char *path, ZipArchive *zip) {
106 const ZipEntry* binary_entry =
107 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
108 if (binary_entry == NULL) {
Koushik K. Dutta4c1eed22010-02-11 18:59:58 -0800109 return INSTALL_UPDATE_BINARY_MISSING;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700110 }
111
112 char* binary = "/tmp/update_binary";
113 unlink(binary);
114 int fd = creat(binary, 0755);
115 if (fd < 0) {
116 LOGE("Can't make %s\n", binary);
117 return 1;
118 }
119 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
120 close(fd);
121
122 if (!ok) {
123 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
124 return 1;
125 }
126
127 int pipefd[2];
128 pipe(pipefd);
129
130 // When executing the update binary contained in the package, the
131 // arguments passed are:
132 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700133 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700134 //
135 // - an fd to which the program can write in order to update the
136 // progress bar. The program can write single-line commands:
137 //
138 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700139 // fill up the next <frac> part of of the progress bar
140 // over <secs> seconds. If <secs> is zero, use
141 // set_progress commands to manually control the
142 // progress of this segment of the bar
143 //
144 // set_progress <frac>
145 // <frac> should be between 0.0 and 1.0; sets the
146 // progress bar within the segment defined by the most
147 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700148 //
149 // firmware <"hboot"|"radio"> <filename>
150 // arrange to install the contents of <filename> in the
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700151 // given partition on reboot. (API v2: <filename> may
152 // start with "PACKAGE:" to indicate taking a file from
153 // the OTA package.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700154 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700155 // ui_print <string>
156 // display <string> on the screen.
157 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700158 // - the name of the package zip file.
159 //
160
161 char** args = malloc(sizeof(char*) * 5);
162 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700163 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700164 args[2] = malloc(10);
165 sprintf(args[2], "%d", pipefd[1]);
166 args[3] = (char*)path;
167 args[4] = NULL;
168
169 pid_t pid = fork();
170 if (pid == 0) {
171 close(pipefd[0]);
172 execv(binary, args);
173 fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno));
174 _exit(-1);
175 }
176 close(pipefd[1]);
177
178 char* firmware_type = NULL;
179 char* firmware_filename = NULL;
180
Doug Zongker64893cc2009-07-14 16:31:56 -0700181 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700182 FILE* from_child = fdopen(pipefd[0], "r");
183 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700184 char* command = strtok(buffer, " \n");
185 if (command == NULL) {
186 continue;
187 } else if (strcmp(command, "progress") == 0) {
188 char* fraction_s = strtok(NULL, " \n");
189 char* seconds_s = strtok(NULL, " \n");
190
191 float fraction = strtof(fraction_s, NULL);
192 int seconds = strtol(seconds_s, NULL, 10);
193
194 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
195 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700196 } else if (strcmp(command, "set_progress") == 0) {
197 char* fraction_s = strtok(NULL, " \n");
198 float fraction = strtof(fraction_s, NULL);
199 ui_set_progress(fraction);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700200 } else if (strcmp(command, "firmware") == 0) {
201 char* type = strtok(NULL, " \n");
202 char* filename = strtok(NULL, " \n");
203
204 if (type != NULL && filename != NULL) {
205 if (firmware_type != NULL) {
206 LOGE("ignoring attempt to do multiple firmware updates");
207 } else {
208 firmware_type = strdup(type);
209 firmware_filename = strdup(filename);
210 }
211 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700212 } else if (strcmp(command, "ui_print") == 0) {
213 char* str = strtok(NULL, "\n");
214 if (str) {
215 ui_print(str);
216 } else {
217 ui_print("\n");
218 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700219 } else {
220 LOGE("unknown command [%s]\n", command);
221 }
222 }
223 fclose(from_child);
224
225 int status;
226 waitpid(pid, &status, 0);
227 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700228 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700229 return INSTALL_ERROR;
230 }
231
232 if (firmware_type != NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700233 return handle_firmware_update(firmware_type, firmware_filename, zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700234 } else {
235 return INSTALL_SUCCESS;
236 }
237}
238
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239static int
Doug Zongker54e2e862009-08-17 13:21:04 -0700240handle_update_package(const char *path, ZipArchive *zip)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800241{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800242 // Update should take the rest of the progress bar.
243 ui_print("Installing update...\n");
244
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700245 int result = try_update_binary(path, zip);
Koushik K. Dutta1f14c9a2010-02-11 23:08:23 -0800246 if (result == INSTALL_UPDATE_BINARY_MISSING)
247 {
248 if (register_package_root(zip, path) < 0) {
249 LOGE("Can't register package root\n");
250 return INSTALL_ERROR;
251 }
252 const ZipEntry *script_entry;
253 script_entry = find_update_script(zip);
254 result = handle_update_script(zip, script_entry);
255 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800256 register_package_root(NULL, NULL); // Unregister package root
Doug Zongker64893cc2009-07-14 16:31:56 -0700257 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800258}
259
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700260// Reads a file containing one or more public keys as produced by
261// DumpPublicKey: this is an RSAPublicKey struct as it would appear
262// as a C source literal, eg:
263//
264// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
265//
266// (Note that the braces and commas in this example are actual
267// characters the parser expects to find in the file; the ellipses
268// indicate more numbers omitted from this example.)
269//
270// The file may contain multiple keys in this format, separated by
271// commas. The last key must not be followed by a comma.
272//
273// Returns NULL if the file failed to parse, or if it contain zero keys.
274static RSAPublicKey*
275load_keys(const char* filename, int* numKeys) {
276 RSAPublicKey* out = NULL;
277 *numKeys = 0;
278
279 FILE* f = fopen(filename, "r");
280 if (f == NULL) {
281 LOGE("opening %s: %s\n", filename, strerror(errno));
282 goto exit;
283 }
284
285 int i;
286 bool done = false;
287 while (!done) {
288 ++*numKeys;
289 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
290 RSAPublicKey* key = out + (*numKeys - 1);
291 if (fscanf(f, " { %i , %i , { %i",
292 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
293 goto exit;
294 }
295 if (key->len != RSANUMWORDS) {
296 LOGE("key length (%d) does not match expected size\n", key->len);
297 goto exit;
298 }
299 for (i = 1; i < key->len; ++i) {
300 if (fscanf(f, " , %i", &(key->n[i])) != 1) goto exit;
301 }
302 if (fscanf(f, " } , { %i", &(key->rr[0])) != 1) goto exit;
303 for (i = 1; i < key->len; ++i) {
304 if (fscanf(f, " , %i", &(key->rr[i])) != 1) goto exit;
305 }
306 fscanf(f, " } } ");
307
308 // if the line ends in a comma, this file has more keys.
309 switch (fgetc(f)) {
310 case ',':
311 // more keys to come.
312 break;
313
314 case EOF:
315 done = true;
316 break;
317
318 default:
319 LOGE("unexpected character between keys\n");
320 goto exit;
321 }
322 }
323
324 fclose(f);
325 return out;
326
327exit:
328 if (f) fclose(f);
329 free(out);
330 *numKeys = 0;
331 return NULL;
332}
333
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800334int
335install_package(const char *root_path)
336{
337 ui_set_background(BACKGROUND_ICON_INSTALLING);
338 ui_print("Finding update package...\n");
339 ui_show_indeterminate_progress();
340 LOGI("Update location: %s\n", root_path);
341
342 if (ensure_root_path_mounted(root_path) != 0) {
343 LOGE("Can't mount %s\n", root_path);
344 return INSTALL_CORRUPT;
345 }
346
347 char path[PATH_MAX] = "";
348 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
349 LOGE("Bad path %s\n", root_path);
350 return INSTALL_CORRUPT;
351 }
352
353 ui_print("Opening update package...\n");
354 LOGI("Update file path: %s\n", path);
355
Doug Zongker54e2e862009-08-17 13:21:04 -0700356 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800357
358 if (signature_check_enabled) {
359 int numKeys;
360 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
361 if (loadedKeys == NULL) {
362 LOGE("Failed to load keys\n");
363 return INSTALL_CORRUPT;
364 }
365 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
366
367 // Give verification half the progress bar...
368 ui_print("Verifying update package...\n");
369 ui_show_progress(
370 VERIFICATION_PROGRESS_FRACTION,
371 VERIFICATION_PROGRESS_TIME);
372
373 err = verify_file(path, loadedKeys, numKeys);
374 free(loadedKeys);
375 LOGI("verify_file returned %d\n", err);
376 if (err != VERIFY_SUCCESS) {
377 LOGE("signature verification failed\n");
378 return INSTALL_CORRUPT;
379 }
Doug Zongker54e2e862009-08-17 13:21:04 -0700380 }
381
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800382 /* Try to open the package.
383 */
384 ZipArchive zip;
Doug Zongker54e2e862009-08-17 13:21:04 -0700385 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800386 if (err != 0) {
387 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
388 return INSTALL_CORRUPT;
389 }
390
391 /* Verify and install the contents of the package.
392 */
Doug Zongker54e2e862009-08-17 13:21:04 -0700393 int status = handle_update_package(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800394 mzCloseZipArchive(&zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800395 return status;
396}