blob: bb9924a24e98cc3a82dbbf3af6d9cabeabc54698 [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"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Doug Zongkerb2ee9202009-06-04 10:24:53 -070037#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070038#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Doug Zongkerb2ee9202009-06-04 10:24:53 -070040// The update binary ask us to install a firmware file on reboot. Set
41// that up. Takes ownership of type and filename.
42static int
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070043handle_firmware_update(char* type, char* filename, ZipArchive* zip) {
44 unsigned int data_size;
45 const ZipEntry* entry = NULL;
46
47 if (strncmp(filename, "PACKAGE:", 8) == 0) {
48 entry = mzFindZipEntry(zip, filename+8);
49 if (entry == NULL) {
50 LOGE("Failed to find \"%s\" in package", filename+8);
51 return INSTALL_ERROR;
52 }
53 data_size = entry->uncompLen;
54 } else {
55 struct stat st_data;
56 if (stat(filename, &st_data) < 0) {
57 LOGE("Error stat'ing %s: %s\n", filename, strerror(errno));
58 return INSTALL_ERROR;
59 }
60 data_size = st_data.st_size;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070061 }
62
Doug Zongker8edb00c2009-06-11 17:21:44 -070063 LOGI("type is %s; size is %d; file is %s\n",
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070064 type, data_size, filename);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070065
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070066 char* data = malloc(data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070067 if (data == NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070068 LOGI("Can't allocate %d bytes for firmware data\n", data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070069 return INSTALL_ERROR;
70 }
71
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070072 if (entry) {
73 if (mzReadZipEntry(zip, entry, data, data_size) == false) {
74 LOGE("Failed to read \"%s\" from package", filename+8);
75 return INSTALL_ERROR;
76 }
77 } else {
78 FILE* f = fopen(filename, "rb");
79 if (f == NULL) {
80 LOGE("Failed to open %s: %s\n", filename, strerror(errno));
81 return INSTALL_ERROR;
82 }
83 if (fread(data, 1, data_size, f) != data_size) {
84 LOGE("Failed to read firmware data: %s\n", strerror(errno));
85 return INSTALL_ERROR;
86 }
87 fclose(f);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070088 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -070089
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070090 if (remember_firmware_update(type, data, data_size)) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070091 LOGE("Can't store %s image\n", type);
92 free(data);
93 return INSTALL_ERROR;
94 }
95 free(filename);
96
97 return INSTALL_SUCCESS;
98}
99
100// If the package contains an update binary, extract it and run it.
101static int
102try_update_binary(const char *path, ZipArchive *zip) {
103 const ZipEntry* binary_entry =
104 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
105 if (binary_entry == NULL) {
106 return INSTALL_CORRUPT;
107 }
108
109 char* binary = "/tmp/update_binary";
110 unlink(binary);
111 int fd = creat(binary, 0755);
112 if (fd < 0) {
113 LOGE("Can't make %s\n", binary);
114 return 1;
115 }
116 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
117 close(fd);
118
119 if (!ok) {
120 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
121 return 1;
122 }
123
124 int pipefd[2];
125 pipe(pipefd);
126
127 // When executing the update binary contained in the package, the
128 // arguments passed are:
129 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700130 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 //
132 // - an fd to which the program can write in order to update the
133 // progress bar. The program can write single-line commands:
134 //
135 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700136 // fill up the next <frac> part of of the progress bar
137 // over <secs> seconds. If <secs> is zero, use
138 // set_progress commands to manually control the
139 // progress of this segment of the bar
140 //
141 // set_progress <frac>
142 // <frac> should be between 0.0 and 1.0; sets the
143 // progress bar within the segment defined by the most
144 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700145 //
146 // firmware <"hboot"|"radio"> <filename>
147 // arrange to install the contents of <filename> in the
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700148 // given partition on reboot. (API v2: <filename> may
149 // start with "PACKAGE:" to indicate taking a file from
150 // the OTA package.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700151 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700152 // ui_print <string>
153 // display <string> on the screen.
154 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700155 // - the name of the package zip file.
156 //
157
158 char** args = malloc(sizeof(char*) * 5);
159 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700160 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700161 args[2] = malloc(10);
162 sprintf(args[2], "%d", pipefd[1]);
163 args[3] = (char*)path;
164 args[4] = NULL;
165
166 pid_t pid = fork();
167 if (pid == 0) {
168 close(pipefd[0]);
169 execv(binary, args);
170 fprintf(stderr, "E:Can't run %s (%s)\n", binary, strerror(errno));
171 _exit(-1);
172 }
173 close(pipefd[1]);
174
175 char* firmware_type = NULL;
176 char* firmware_filename = NULL;
177
Doug Zongker64893cc2009-07-14 16:31:56 -0700178 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700179 FILE* from_child = fdopen(pipefd[0], "r");
180 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700181 char* command = strtok(buffer, " \n");
182 if (command == NULL) {
183 continue;
184 } else if (strcmp(command, "progress") == 0) {
185 char* fraction_s = strtok(NULL, " \n");
186 char* seconds_s = strtok(NULL, " \n");
187
188 float fraction = strtof(fraction_s, NULL);
189 int seconds = strtol(seconds_s, NULL, 10);
190
191 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
192 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700193 } else if (strcmp(command, "set_progress") == 0) {
194 char* fraction_s = strtok(NULL, " \n");
195 float fraction = strtof(fraction_s, NULL);
196 ui_set_progress(fraction);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700197 } else if (strcmp(command, "firmware") == 0) {
198 char* type = strtok(NULL, " \n");
199 char* filename = strtok(NULL, " \n");
200
201 if (type != NULL && filename != NULL) {
202 if (firmware_type != NULL) {
203 LOGE("ignoring attempt to do multiple firmware updates");
204 } else {
205 firmware_type = strdup(type);
206 firmware_filename = strdup(filename);
207 }
208 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700209 } else if (strcmp(command, "ui_print") == 0) {
210 char* str = strtok(NULL, "\n");
211 if (str) {
212 ui_print(str);
213 } else {
214 ui_print("\n");
215 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700216 } else {
217 LOGE("unknown command [%s]\n", command);
218 }
219 }
220 fclose(from_child);
221
222 int status;
223 waitpid(pid, &status, 0);
224 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700225 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700226 return INSTALL_ERROR;
227 }
228
229 if (firmware_type != NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700230 return handle_firmware_update(firmware_type, firmware_filename, zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700231 } else {
232 return INSTALL_SUCCESS;
233 }
234}
235
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236static int
Doug Zongker60151a22009-08-12 18:30:03 -0700237handle_update_package(const char *path, ZipArchive *zip)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800238{
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239 // Update should take the rest of the progress bar.
240 ui_print("Installing update...\n");
241
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700242 int result = try_update_binary(path, zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243 register_package_root(NULL, NULL); // Unregister package root
Doug Zongker64893cc2009-07-14 16:31:56 -0700244 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245}
246
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700247// Reads a file containing one or more public keys as produced by
248// DumpPublicKey: this is an RSAPublicKey struct as it would appear
249// as a C source literal, eg:
250//
251// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
252//
253// (Note that the braces and commas in this example are actual
254// characters the parser expects to find in the file; the ellipses
255// indicate more numbers omitted from this example.)
256//
257// The file may contain multiple keys in this format, separated by
258// commas. The last key must not be followed by a comma.
259//
260// Returns NULL if the file failed to parse, or if it contain zero keys.
261static RSAPublicKey*
262load_keys(const char* filename, int* numKeys) {
263 RSAPublicKey* out = NULL;
264 *numKeys = 0;
265
266 FILE* f = fopen(filename, "r");
267 if (f == NULL) {
268 LOGE("opening %s: %s\n", filename, strerror(errno));
269 goto exit;
270 }
271
272 int i;
273 bool done = false;
274 while (!done) {
275 ++*numKeys;
276 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
277 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800278 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700279 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
280 goto exit;
281 }
282 if (key->len != RSANUMWORDS) {
283 LOGE("key length (%d) does not match expected size\n", key->len);
284 goto exit;
285 }
286 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800287 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700288 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800289 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700290 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800291 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700292 }
293 fscanf(f, " } } ");
294
295 // if the line ends in a comma, this file has more keys.
296 switch (fgetc(f)) {
297 case ',':
298 // more keys to come.
299 break;
300
301 case EOF:
302 done = true;
303 break;
304
305 default:
306 LOGE("unexpected character between keys\n");
307 goto exit;
308 }
309 }
310
311 fclose(f);
312 return out;
313
314exit:
315 if (f) fclose(f);
316 free(out);
317 *numKeys = 0;
318 return NULL;
319}
320
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800321int
322install_package(const char *root_path)
323{
324 ui_set_background(BACKGROUND_ICON_INSTALLING);
325 ui_print("Finding update package...\n");
326 ui_show_indeterminate_progress();
327 LOGI("Update location: %s\n", root_path);
328
329 if (ensure_root_path_mounted(root_path) != 0) {
330 LOGE("Can't mount %s\n", root_path);
331 return INSTALL_CORRUPT;
332 }
333
334 char path[PATH_MAX] = "";
335 if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
336 LOGE("Bad path %s\n", root_path);
337 return INSTALL_CORRUPT;
338 }
339
340 ui_print("Opening update package...\n");
341 LOGI("Update file path: %s\n", path);
342
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700343 int numKeys;
344 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
345 if (loadedKeys == NULL) {
346 LOGE("Failed to load keys\n");
347 return INSTALL_CORRUPT;
348 }
349 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
350
Doug Zongker60151a22009-08-12 18:30:03 -0700351 // Give verification half the progress bar...
352 ui_print("Verifying update package...\n");
353 ui_show_progress(
354 VERIFICATION_PROGRESS_FRACTION,
355 VERIFICATION_PROGRESS_TIME);
356
357 int err;
358 err = verify_file(path, loadedKeys, numKeys);
359 free(loadedKeys);
360 LOGI("verify_file returned %d\n", err);
361 if (err != VERIFY_SUCCESS) {
362 LOGE("signature verification failed\n");
363 return INSTALL_CORRUPT;
364 }
365
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800366 /* Try to open the package.
367 */
368 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700369 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800370 if (err != 0) {
371 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
372 return INSTALL_CORRUPT;
373 }
374
375 /* Verify and install the contents of the package.
376 */
Doug Zongker60151a22009-08-12 18:30:03 -0700377 int status = handle_update_package(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800378 mzCloseZipArchive(&zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800379 return status;
380}