blob: 711f8490828eb32eba4132002250cc673a7ea8fd [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"
Koushik Dutta67c381a2011-01-02 12:26:35 -080042#define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070043#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Doug Zongkerb2ee9202009-06-04 10:24:53 -070045// The update binary ask us to install a firmware file on reboot. Set
46// that up. Takes ownership of type and filename.
47static int
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070048handle_firmware_update(char* type, char* filename, ZipArchive* zip) {
49 unsigned int data_size;
50 const ZipEntry* entry = NULL;
51
52 if (strncmp(filename, "PACKAGE:", 8) == 0) {
53 entry = mzFindZipEntry(zip, filename+8);
54 if (entry == NULL) {
55 LOGE("Failed to find \"%s\" in package", filename+8);
56 return INSTALL_ERROR;
57 }
58 data_size = entry->uncompLen;
59 } else {
60 struct stat st_data;
61 if (stat(filename, &st_data) < 0) {
62 LOGE("Error stat'ing %s: %s\n", filename, strerror(errno));
63 return INSTALL_ERROR;
64 }
65 data_size = st_data.st_size;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070066 }
67
Doug Zongker8edb00c2009-06-11 17:21:44 -070068 LOGI("type is %s; size is %d; file is %s\n",
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070069 type, data_size, filename);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070070
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070071 char* data = malloc(data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070072 if (data == NULL) {
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070073 LOGI("Can't allocate %d bytes for firmware data\n", data_size);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070074 return INSTALL_ERROR;
75 }
76
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070077 if (entry) {
78 if (mzReadZipEntry(zip, entry, data, data_size) == false) {
79 LOGE("Failed to read \"%s\" from package", filename+8);
80 return INSTALL_ERROR;
81 }
82 } else {
83 FILE* f = fopen(filename, "rb");
84 if (f == NULL) {
85 LOGE("Failed to open %s: %s\n", filename, strerror(errno));
86 return INSTALL_ERROR;
87 }
88 if (fread(data, 1, data_size, f) != data_size) {
89 LOGE("Failed to read firmware data: %s\n", strerror(errno));
90 return INSTALL_ERROR;
91 }
92 fclose(f);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070093 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -070094
Koushik Dutta7f13e152011-09-08 16:55:35 -070095 if (remember_firmware_update(type, data, data_size)) {
96 LOGE("Can't store %s image\n", type);
97 free(data);
98 return INSTALL_ERROR;
99 }
100
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700101 free(filename);
102
103 return INSTALL_SUCCESS;
104}
105
Doug Zongker469243e2011-04-12 09:28:10 -0700106static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
107
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700108// If the package contains an update binary, extract it and run it.
109static int
110try_update_binary(const char *path, ZipArchive *zip) {
111 const ZipEntry* binary_entry =
112 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
113 if (binary_entry == NULL) {
Koushik Dutta67c381a2011-01-02 12:26:35 -0800114 const ZipEntry* update_script_entry =
115 mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME);
116 if (update_script_entry != NULL) {
117 ui_print("Amend scripting (update-script) is no longer supported.\n");
118 ui_print("Amend scripting was deprecated by Google in Android 1.5.\n");
119 ui_print("It was necessary to remove it when upgrading to the ClockworkMod 3.0 Gingerbread based recovery.\n");
120 ui_print("Please switch to Edify scripting (updater-script and update-binary) to create working update zip packages.\n");
121 return INSTALL_UPDATE_BINARY_MISSING;
122 }
123
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700124 mzCloseZipArchive(zip);
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700125 return INSTALL_UPDATE_BINARY_MISSING;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700126 }
127
128 char* binary = "/tmp/update_binary";
129 unlink(binary);
130 int fd = creat(binary, 0755);
131 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700132 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700133 LOGE("Can't make %s\n", binary);
134 return 1;
135 }
136 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
137 close(fd);
138
139 if (!ok) {
140 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Koushik Dutta49553c72011-05-13 13:01:26 -0700141 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700142 return 1;
143 }
144
145 int pipefd[2];
146 pipe(pipefd);
147
148 // When executing the update binary contained in the package, the
149 // arguments passed are:
150 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700151 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700152 //
153 // - an fd to which the program can write in order to update the
154 // progress bar. The program can write single-line commands:
155 //
156 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700157 // fill up the next <frac> part of of the progress bar
158 // over <secs> seconds. If <secs> is zero, use
159 // set_progress commands to manually control the
160 // progress of this segment of the bar
161 //
162 // set_progress <frac>
163 // <frac> should be between 0.0 and 1.0; sets the
164 // progress bar within the segment defined by the most
165 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700166 //
167 // firmware <"hboot"|"radio"> <filename>
168 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800169 // given partition on reboot.
170 //
171 // (API v2: <filename> may start with "PACKAGE:" to
172 // indicate taking a file from the OTA package.)
173 //
174 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700175 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700176 // ui_print <string>
177 // display <string> on the screen.
178 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700179 // - the name of the package zip file.
180 //
181
182 char** args = malloc(sizeof(char*) * 5);
183 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700184 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700185 args[2] = malloc(10);
186 sprintf(args[2], "%d", pipefd[1]);
187 args[3] = (char*)path;
188 args[4] = NULL;
189
190 pid_t pid = fork();
191 if (pid == 0) {
Koushik Dutta20b516a2011-05-15 18:48:17 -0700192 setenv("UPDATE_PACKAGE", path, 1);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700193 close(pipefd[0]);
194 execv(binary, args);
Doug Zongker56c51052010-07-01 09:18:44 -0700195 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700196 _exit(-1);
197 }
198 close(pipefd[1]);
199
Koushik Dutta19447c02010-11-10 10:40:44 -0800200 char* firmware_type = NULL;
201 char* firmware_filename = NULL;
202
Doug Zongker64893cc2009-07-14 16:31:56 -0700203 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700204 FILE* from_child = fdopen(pipefd[0], "r");
205 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700206 char* command = strtok(buffer, " \n");
207 if (command == NULL) {
208 continue;
209 } else if (strcmp(command, "progress") == 0) {
210 char* fraction_s = strtok(NULL, " \n");
211 char* seconds_s = strtok(NULL, " \n");
212
213 float fraction = strtof(fraction_s, NULL);
214 int seconds = strtol(seconds_s, NULL, 10);
215
216 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
217 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700218 } else if (strcmp(command, "set_progress") == 0) {
219 char* fraction_s = strtok(NULL, " \n");
220 float fraction = strtof(fraction_s, NULL);
221 ui_set_progress(fraction);
Koushik Dutta19447c02010-11-10 10:40:44 -0800222 } else if (strcmp(command, "firmware") == 0) {
223 char* type = strtok(NULL, " \n");
224 char* filename = strtok(NULL, " \n");
225
226 if (type != NULL && filename != NULL) {
227 if (firmware_type != NULL) {
228 LOGE("ignoring attempt to do multiple firmware updates");
229 } else {
230 firmware_type = strdup(type);
231 firmware_filename = strdup(filename);
232 }
233 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700234 } else if (strcmp(command, "ui_print") == 0) {
235 char* str = strtok(NULL, "\n");
236 if (str) {
Nick Kralevich21b97ed2010-06-24 16:11:17 -0700237 ui_print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700238 } else {
239 ui_print("\n");
240 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700241 } else {
242 LOGE("unknown command [%s]\n", command);
243 }
244 }
245 fclose(from_child);
246
247 int status;
248 waitpid(pid, &status, 0);
249 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Koushik Dutta49553c72011-05-13 13:01:26 -0700251 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700252 return INSTALL_ERROR;
253 }
254
Koushik Dutta19447c02010-11-10 10:40:44 -0800255 if (firmware_type != NULL) {
Koushik Duttad79b7542011-05-25 10:47:41 -0700256 int ret = handle_firmware_update(firmware_type, firmware_filename, zip);
Koushik Dutta49553c72011-05-13 13:01:26 -0700257 mzCloseZipArchive(zip);
Koushik Duttad79b7542011-05-25 10:47:41 -0700258 return ret;
Koushik Dutta19447c02010-11-10 10:40:44 -0800259 }
Doug Zongkere08991e2010-02-02 13:09:52 -0800260 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700261}
262
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700263// Reads a file containing one or more public keys as produced by
264// DumpPublicKey: this is an RSAPublicKey struct as it would appear
265// as a C source literal, eg:
266//
267// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
268//
269// (Note that the braces and commas in this example are actual
270// characters the parser expects to find in the file; the ellipses
271// indicate more numbers omitted from this example.)
272//
273// The file may contain multiple keys in this format, separated by
274// commas. The last key must not be followed by a comma.
275//
276// Returns NULL if the file failed to parse, or if it contain zero keys.
277static RSAPublicKey*
278load_keys(const char* filename, int* numKeys) {
279 RSAPublicKey* out = NULL;
280 *numKeys = 0;
281
282 FILE* f = fopen(filename, "r");
283 if (f == NULL) {
284 LOGE("opening %s: %s\n", filename, strerror(errno));
285 goto exit;
286 }
287
288 int i;
289 bool done = false;
290 while (!done) {
291 ++*numKeys;
292 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
293 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800294 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700295 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
296 goto exit;
297 }
298 if (key->len != RSANUMWORDS) {
299 LOGE("key length (%d) does not match expected size\n", key->len);
300 goto exit;
301 }
302 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800303 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700304 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800305 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700306 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800307 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700308 }
309 fscanf(f, " } } ");
310
311 // if the line ends in a comma, this file has more keys.
312 switch (fgetc(f)) {
313 case ',':
314 // more keys to come.
315 break;
316
317 case EOF:
318 done = true;
319 break;
320
321 default:
322 LOGE("unexpected character between keys\n");
323 goto exit;
324 }
325 }
326
327 fclose(f);
328 return out;
329
330exit:
331 if (f) fclose(f);
332 free(out);
333 *numKeys = 0;
334 return NULL;
335}
336
Doug Zongker469243e2011-04-12 09:28:10 -0700337static int
338really_install_package(const char *path)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800339{
340 ui_set_background(BACKGROUND_ICON_INSTALLING);
341 ui_print("Finding update package...\n");
342 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700343 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344
Doug Zongkerd4208f92010-09-20 12:16:13 -0700345 if (ensure_path_mounted(path) != 0) {
346 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347 return INSTALL_CORRUPT;
348 }
349
350 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800351
Doug Zongker60151a22009-08-12 18:30:03 -0700352 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800353
354 if (signature_check_enabled) {
355 int numKeys;
356 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
357 if (loadedKeys == NULL) {
358 LOGE("Failed to load keys\n");
359 return INSTALL_CORRUPT;
360 }
361 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
362
363 // Give verification half the progress bar...
364 ui_print("Verifying update package...\n");
365 ui_show_progress(
366 VERIFICATION_PROGRESS_FRACTION,
367 VERIFICATION_PROGRESS_TIME);
368
369 err = verify_file(path, loadedKeys, numKeys);
370 free(loadedKeys);
371 LOGI("verify_file returned %d\n", err);
372 if (err != VERIFY_SUCCESS) {
373 LOGE("signature verification failed\n");
374 return INSTALL_CORRUPT;
375 }
Doug Zongker60151a22009-08-12 18:30:03 -0700376 }
377
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800378 /* Try to open the package.
379 */
380 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700381 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800382 if (err != 0) {
383 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
384 return INSTALL_CORRUPT;
385 }
386
387 /* Verify and install the contents of the package.
388 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700389 ui_print("Installing update...\n");
390 return try_update_binary(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800391}
Doug Zongker469243e2011-04-12 09:28:10 -0700392
393int
394install_package(const char* path)
395{
396 FILE* install_log = fopen_path(LAST_INSTALL_FILE, "w");
397 if (install_log) {
398 fputs(path, install_log);
399 fputc('\n', install_log);
400 } else {
401 LOGE("failed to open last_install: %s\n", strerror(errno));
402 }
403 int result = really_install_package(path);
404 if (install_log) {
405 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
406 fputc('\n', install_log);
407 fclose(install_log);
408 chmod(LAST_INSTALL_FILE, 0644);
409 }
410 return result;
411}