blob: 1ed046546a1969c2baa124212cb17ff3ac25fa77 [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
Doug Zongkerb2ee9202009-06-04 10:24:53 -070095 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) {
Koushik Dutta67c381a2011-01-02 12:26:35 -0800106 const ZipEntry* update_script_entry =
107 mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME);
108 if (update_script_entry != NULL) {
109 ui_print("Amend scripting (update-script) is no longer supported.\n");
110 ui_print("Amend scripting was deprecated by Google in Android 1.5.\n");
111 ui_print("It was necessary to remove it when upgrading to the ClockworkMod 3.0 Gingerbread based recovery.\n");
112 ui_print("Please switch to Edify scripting (updater-script and update-binary) to create working update zip packages.\n");
113 return INSTALL_UPDATE_BINARY_MISSING;
114 }
115
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700116 mzCloseZipArchive(zip);
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700117 return INSTALL_UPDATE_BINARY_MISSING;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700118 }
119
120 char* binary = "/tmp/update_binary";
121 unlink(binary);
122 int fd = creat(binary, 0755);
123 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700124 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700125 LOGE("Can't make %s\n", binary);
126 return 1;
127 }
128 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
129 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700130 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131
132 if (!ok) {
133 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
134 return 1;
135 }
136
137 int pipefd[2];
138 pipe(pipefd);
139
140 // When executing the update binary contained in the package, the
141 // arguments passed are:
142 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700143 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700144 //
145 // - an fd to which the program can write in order to update the
146 // progress bar. The program can write single-line commands:
147 //
148 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700149 // fill up the next <frac> part of of the progress bar
150 // over <secs> seconds. If <secs> is zero, use
151 // set_progress commands to manually control the
152 // progress of this segment of the bar
153 //
154 // set_progress <frac>
155 // <frac> should be between 0.0 and 1.0; sets the
156 // progress bar within the segment defined by the most
157 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700158 //
159 // firmware <"hboot"|"radio"> <filename>
160 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800161 // given partition on reboot.
162 //
163 // (API v2: <filename> may start with "PACKAGE:" to
164 // indicate taking a file from the OTA package.)
165 //
166 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700167 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700168 // ui_print <string>
169 // display <string> on the screen.
170 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700171 // - the name of the package zip file.
172 //
173
174 char** args = malloc(sizeof(char*) * 5);
175 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700176 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700177 args[2] = malloc(10);
178 sprintf(args[2], "%d", pipefd[1]);
179 args[3] = (char*)path;
180 args[4] = NULL;
181
182 pid_t pid = fork();
183 if (pid == 0) {
Koushik Dutta20b516a2011-05-15 18:48:17 -0700184 setenv("UPDATE_PACKAGE", path, 1);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700185 close(pipefd[0]);
186 execv(binary, args);
Doug Zongker56c51052010-07-01 09:18:44 -0700187 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700188 _exit(-1);
189 }
190 close(pipefd[1]);
191
Koushik Dutta19447c02010-11-10 10:40:44 -0800192 char* firmware_type = NULL;
193 char* firmware_filename = NULL;
194
Doug Zongker64893cc2009-07-14 16:31:56 -0700195 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700196 FILE* from_child = fdopen(pipefd[0], "r");
197 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700198 char* command = strtok(buffer, " \n");
199 if (command == NULL) {
200 continue;
201 } else if (strcmp(command, "progress") == 0) {
202 char* fraction_s = strtok(NULL, " \n");
203 char* seconds_s = strtok(NULL, " \n");
204
205 float fraction = strtof(fraction_s, NULL);
206 int seconds = strtol(seconds_s, NULL, 10);
207
208 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
209 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700210 } else if (strcmp(command, "set_progress") == 0) {
211 char* fraction_s = strtok(NULL, " \n");
212 float fraction = strtof(fraction_s, NULL);
213 ui_set_progress(fraction);
Koushik Dutta19447c02010-11-10 10:40:44 -0800214 } else if (strcmp(command, "firmware") == 0) {
215 char* type = strtok(NULL, " \n");
216 char* filename = strtok(NULL, " \n");
217
218 if (type != NULL && filename != NULL) {
219 if (firmware_type != NULL) {
220 LOGE("ignoring attempt to do multiple firmware updates");
221 } else {
222 firmware_type = strdup(type);
223 firmware_filename = strdup(filename);
224 }
225 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700226 } else if (strcmp(command, "ui_print") == 0) {
227 char* str = strtok(NULL, "\n");
228 if (str) {
Nick Kralevich21b97ed2010-06-24 16:11:17 -0700229 ui_print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700230 } else {
231 ui_print("\n");
232 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700233 } else {
234 LOGE("unknown command [%s]\n", command);
235 }
236 }
237 fclose(from_child);
238
239 int status;
240 waitpid(pid, &status, 0);
241 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700243 return INSTALL_ERROR;
244 }
245
Koushik Dutta19447c02010-11-10 10:40:44 -0800246 if (firmware_type != NULL) {
247 return handle_firmware_update(firmware_type, firmware_filename, zip);
248 } else {
249 return INSTALL_SUCCESS;
250 }
Doug Zongkere08991e2010-02-02 13:09:52 -0800251 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700252}
253
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700254// Reads a file containing one or more public keys as produced by
255// DumpPublicKey: this is an RSAPublicKey struct as it would appear
256// as a C source literal, eg:
257//
258// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
259//
260// (Note that the braces and commas in this example are actual
261// characters the parser expects to find in the file; the ellipses
262// indicate more numbers omitted from this example.)
263//
264// The file may contain multiple keys in this format, separated by
265// commas. The last key must not be followed by a comma.
266//
267// Returns NULL if the file failed to parse, or if it contain zero keys.
268static RSAPublicKey*
269load_keys(const char* filename, int* numKeys) {
270 RSAPublicKey* out = NULL;
271 *numKeys = 0;
272
273 FILE* f = fopen(filename, "r");
274 if (f == NULL) {
275 LOGE("opening %s: %s\n", filename, strerror(errno));
276 goto exit;
277 }
278
279 int i;
280 bool done = false;
281 while (!done) {
282 ++*numKeys;
283 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
284 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800285 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700286 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
287 goto exit;
288 }
289 if (key->len != RSANUMWORDS) {
290 LOGE("key length (%d) does not match expected size\n", key->len);
291 goto exit;
292 }
293 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800294 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700295 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800296 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700297 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800298 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700299 }
300 fscanf(f, " } } ");
301
302 // if the line ends in a comma, this file has more keys.
303 switch (fgetc(f)) {
304 case ',':
305 // more keys to come.
306 break;
307
308 case EOF:
309 done = true;
310 break;
311
312 default:
313 LOGE("unexpected character between keys\n");
314 goto exit;
315 }
316 }
317
318 fclose(f);
319 return out;
320
321exit:
322 if (f) fclose(f);
323 free(out);
324 *numKeys = 0;
325 return NULL;
326}
327
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800328int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700329install_package(const char *path)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800330{
331 ui_set_background(BACKGROUND_ICON_INSTALLING);
332 ui_print("Finding update package...\n");
333 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700334 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335
Doug Zongkerd4208f92010-09-20 12:16:13 -0700336 if (ensure_path_mounted(path) != 0) {
337 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338 return INSTALL_CORRUPT;
339 }
340
341 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800342
Doug Zongker60151a22009-08-12 18:30:03 -0700343 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800344
345 if (signature_check_enabled) {
346 int numKeys;
347 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
348 if (loadedKeys == NULL) {
349 LOGE("Failed to load keys\n");
350 return INSTALL_CORRUPT;
351 }
352 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
353
354 // Give verification half the progress bar...
355 ui_print("Verifying update package...\n");
356 ui_show_progress(
357 VERIFICATION_PROGRESS_FRACTION,
358 VERIFICATION_PROGRESS_TIME);
359
360 err = verify_file(path, loadedKeys, numKeys);
361 free(loadedKeys);
362 LOGI("verify_file returned %d\n", err);
363 if (err != VERIFY_SUCCESS) {
364 LOGE("signature verification failed\n");
365 return INSTALL_CORRUPT;
366 }
Doug Zongker60151a22009-08-12 18:30:03 -0700367 }
368
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800369 /* Try to open the package.
370 */
371 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700372 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800373 if (err != 0) {
374 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
375 return INSTALL_CORRUPT;
376 }
377
378 /* Verify and install the contents of the package.
379 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700380 ui_print("Installing update...\n");
381 return try_update_binary(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800382}