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