blob: cca9400219d1ad9763b711f6c5a67094796332f3 [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
120handle_firmware_update(char* type, char* filename) {
121 struct stat st_data;
122 if (stat(filename, &st_data) < 0) {
123 LOGE("Error stat'ing %s: %s\n", filename, strerror(errno));
124 return INSTALL_ERROR;
125 }
126
Doug Zongker8edb00c2009-06-11 17:21:44 -0700127 LOGI("type is %s; size is %d; file is %s\n",
128 type, (int)st_data.st_size, filename);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700129
130 char* data = malloc(st_data.st_size);
131 if (data == NULL) {
132 LOGE("Can't allocate %d bytes for firmware data\n", st_data.st_size);
133 return INSTALL_ERROR;
134 }
135
136 FILE* f = fopen(filename, "rb");
137 if (f == NULL) {
138 LOGE("Failed to open %s: %s\n", filename, strerror(errno));
139 return INSTALL_ERROR;
140 }
141 if (fread(data, 1, st_data.st_size, f) != st_data.st_size) {
142 LOGE("Failed to read firmware data: %s\n", strerror(errno));
143 return INSTALL_ERROR;
144 }
145 fclose(f);
146
147 if (remember_firmware_update(type, data, st_data.st_size)) {
148 LOGE("Can't store %s image\n", type);
149 free(data);
150 return INSTALL_ERROR;
151 }
152 free(filename);
153
154 return INSTALL_SUCCESS;
155}
156
157// If the package contains an update binary, extract it and run it.
158static int
159try_update_binary(const char *path, ZipArchive *zip) {
160 const ZipEntry* binary_entry =
161 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
162 if (binary_entry == NULL) {
163 return INSTALL_CORRUPT;
164 }
165
166 char* binary = "/tmp/update_binary";
167 unlink(binary);
168 int fd = creat(binary, 0755);
169 if (fd < 0) {
170 LOGE("Can't make %s\n", binary);
171 return 1;
172 }
173 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
174 close(fd);
175
176 if (!ok) {
177 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
178 return 1;
179 }
180
181 int pipefd[2];
182 pipe(pipefd);
183
184 // When executing the update binary contained in the package, the
185 // arguments passed are:
186 //
187 // - the version number for this interface (currently 1)
188 //
189 // - an fd to which the program can write in order to update the
190 // progress bar. The program can write single-line commands:
191 //
192 // progress <frac> <secs>
193 // fill up <frac> of the progress bar over <secs> seconds.
194 //
195 // firmware <"hboot"|"radio"> <filename>
196 // arrange to install the contents of <filename> in the
197 // given partition on reboot.
198 //
199 // - the name of the package zip file.
200 //
201
202 char** args = malloc(sizeof(char*) * 5);
203 args[0] = binary;
204 args[1] = "1";
205 args[2] = malloc(10);
206 sprintf(args[2], "%d", pipefd[1]);
207 args[3] = (char*)path;
208 args[4] = NULL;
209
210 pid_t pid = fork();
211 if (pid == 0) {
212 close(pipefd[0]);
213 execv(binary, args);
214 fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno));
215 _exit(-1);
216 }
217 close(pipefd[1]);
218
219 char* firmware_type = NULL;
220 char* firmware_filename = NULL;
221
222 char buffer[81];
223 FILE* from_child = fdopen(pipefd[0], "r");
224 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
225 LOGI("read: %s", buffer);
226
227 char* command = strtok(buffer, " \n");
228 if (command == NULL) {
229 continue;
230 } else if (strcmp(command, "progress") == 0) {
231 char* fraction_s = strtok(NULL, " \n");
232 char* seconds_s = strtok(NULL, " \n");
233
234 float fraction = strtof(fraction_s, NULL);
235 int seconds = strtol(seconds_s, NULL, 10);
236
237 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
238 seconds);
239 } else if (strcmp(command, "firmware") == 0) {
240 char* type = strtok(NULL, " \n");
241 char* filename = strtok(NULL, " \n");
242
243 if (type != NULL && filename != NULL) {
244 if (firmware_type != NULL) {
245 LOGE("ignoring attempt to do multiple firmware updates");
246 } else {
247 firmware_type = strdup(type);
248 firmware_filename = strdup(filename);
249 }
250 }
251 } else {
252 LOGE("unknown command [%s]\n", command);
253 }
254 }
255 fclose(from_child);
256
257 int status;
258 waitpid(pid, &status, 0);
259 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700260 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700261 return INSTALL_ERROR;
262 }
263
264 if (firmware_type != NULL) {
265 return handle_firmware_update(firmware_type, firmware_filename);
266 } else {
267 return INSTALL_SUCCESS;
268 }
269}
270
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271static int
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700272handle_update_package(const char *path, ZipArchive *zip,
273 const RSAPublicKey *keys, int numKeys)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800274{
275 // Give verification half the progress bar...
276 ui_print("Verifying update package...\n");
277 ui_show_progress(
278 VERIFICATION_PROGRESS_FRACTION,
279 VERIFICATION_PROGRESS_TIME);
280
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700281 if (!verify_jar_signature(zip, keys, numKeys)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 LOGE("Verification failed\n");
283 return INSTALL_CORRUPT;
284 }
285
286 // Update should take the rest of the progress bar.
287 ui_print("Installing update...\n");
288
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700289 int result = try_update_binary(path, zip);
290 if (result == INSTALL_SUCCESS || result == INSTALL_ERROR) {
291 register_package_root(NULL, NULL); // Unregister package root
292 return result;
293 }
294
295 // if INSTALL_CORRUPT is returned, this package doesn't have an
296 // update binary. Fall back to the older mechanism of looking for
297 // an update script.
298
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299 const ZipEntry *script_entry;
300 script_entry = find_update_script(zip);
301 if (script_entry == NULL) {
302 LOGE("Can't find update script\n");
303 return INSTALL_CORRUPT;
304 }
305
306 if (register_package_root(zip, path) < 0) {
307 LOGE("Can't register package root\n");
308 return INSTALL_ERROR;
309 }
310
311 int ret = handle_update_script(zip, script_entry);
312 register_package_root(NULL, NULL); // Unregister package root
313 return ret;
314}
315
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700316// Reads a file containing one or more public keys as produced by
317// DumpPublicKey: this is an RSAPublicKey struct as it would appear
318// as a C source literal, eg:
319//
320// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
321//
322// (Note that the braces and commas in this example are actual
323// characters the parser expects to find in the file; the ellipses
324// indicate more numbers omitted from this example.)
325//
326// The file may contain multiple keys in this format, separated by
327// commas. The last key must not be followed by a comma.
328//
329// Returns NULL if the file failed to parse, or if it contain zero keys.
330static RSAPublicKey*
331load_keys(const char* filename, int* numKeys) {
332 RSAPublicKey* out = NULL;
333 *numKeys = 0;
334
335 FILE* f = fopen(filename, "r");
336 if (f == NULL) {
337 LOGE("opening %s: %s\n", filename, strerror(errno));
338 goto exit;
339 }
340
341 int i;
342 bool done = false;
343 while (!done) {
344 ++*numKeys;
345 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
346 RSAPublicKey* key = out + (*numKeys - 1);
347 if (fscanf(f, " { %i , %i , { %i",
348 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
349 goto exit;
350 }
351 if (key->len != RSANUMWORDS) {
352 LOGE("key length (%d) does not match expected size\n", key->len);
353 goto exit;
354 }
355 for (i = 1; i < key->len; ++i) {
356 if (fscanf(f, " , %i", &(key->n[i])) != 1) goto exit;
357 }
358 if (fscanf(f, " } , { %i", &(key->rr[0])) != 1) goto exit;
359 for (i = 1; i < key->len; ++i) {
360 if (fscanf(f, " , %i", &(key->rr[i])) != 1) goto exit;
361 }
362 fscanf(f, " } } ");
363
364 // if the line ends in a comma, this file has more keys.
365 switch (fgetc(f)) {
366 case ',':
367 // more keys to come.
368 break;
369
370 case EOF:
371 done = true;
372 break;
373
374 default:
375 LOGE("unexpected character between keys\n");
376 goto exit;
377 }
378 }
379
380 fclose(f);
381 return out;
382
383exit:
384 if (f) fclose(f);
385 free(out);
386 *numKeys = 0;
387 return NULL;
388}
389
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800390int
391install_package(const char *root_path)
392{
393 ui_set_background(BACKGROUND_ICON_INSTALLING);
394 ui_print("Finding update package...\n");
395 ui_show_indeterminate_progress();
396 LOGI("Update location: %s\n", root_path);
397
398 if (ensure_root_path_mounted(root_path) != 0) {
399 LOGE("Can't mount %s\n", root_path);
400 return INSTALL_CORRUPT;
401 }
402
403 char path[PATH_MAX] = "";
404 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
405 LOGE("Bad path %s\n", root_path);
406 return INSTALL_CORRUPT;
407 }
408
409 ui_print("Opening update package...\n");
410 LOGI("Update file path: %s\n", path);
411
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700412 int numKeys;
413 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
414 if (loadedKeys == NULL) {
415 LOGE("Failed to load keys\n");
416 return INSTALL_CORRUPT;
417 }
418 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
419
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800420 /* Try to open the package.
421 */
422 ZipArchive zip;
423 int err = mzOpenZipArchive(path, &zip);
424 if (err != 0) {
425 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
426 return INSTALL_CORRUPT;
427 }
428
429 /* Verify and install the contents of the package.
430 */
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700431 int status = handle_update_package(path, &zip, loadedKeys, numKeys);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800432 mzCloseZipArchive(&zip);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700433 free(loadedKeys);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800434 return status;
435}