blob: 0b35ee72300fa43c389e6d622e1fed1da41e40aa [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);
130
131 if (!ok) {
132 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Koushik Dutta49553c72011-05-13 13:01:26 -0700133 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700134 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));
Koushik Dutta49553c72011-05-13 13:01:26 -0700243 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700244 return INSTALL_ERROR;
245 }
246
Koushik Dutta19447c02010-11-10 10:40:44 -0800247 if (firmware_type != NULL) {
Koushik Duttad79b7542011-05-25 10:47:41 -0700248 int ret = handle_firmware_update(firmware_type, firmware_filename, zip);
Koushik Dutta49553c72011-05-13 13:01:26 -0700249 mzCloseZipArchive(zip);
Koushik Duttad79b7542011-05-25 10:47:41 -0700250 return ret;
Koushik Dutta19447c02010-11-10 10:40:44 -0800251 }
Doug Zongkere08991e2010-02-02 13:09:52 -0800252 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700253}
254
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700255// Reads a file containing one or more public keys as produced by
256// DumpPublicKey: this is an RSAPublicKey struct as it would appear
257// as a C source literal, eg:
258//
259// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
260//
261// (Note that the braces and commas in this example are actual
262// characters the parser expects to find in the file; the ellipses
263// indicate more numbers omitted from this example.)
264//
265// The file may contain multiple keys in this format, separated by
266// commas. The last key must not be followed by a comma.
267//
268// Returns NULL if the file failed to parse, or if it contain zero keys.
269static RSAPublicKey*
270load_keys(const char* filename, int* numKeys) {
271 RSAPublicKey* out = NULL;
272 *numKeys = 0;
273
274 FILE* f = fopen(filename, "r");
275 if (f == NULL) {
276 LOGE("opening %s: %s\n", filename, strerror(errno));
277 goto exit;
278 }
279
280 int i;
281 bool done = false;
282 while (!done) {
283 ++*numKeys;
284 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
285 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800286 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700287 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
288 goto exit;
289 }
290 if (key->len != RSANUMWORDS) {
291 LOGE("key length (%d) does not match expected size\n", key->len);
292 goto exit;
293 }
294 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800295 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700296 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800297 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700298 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800299 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700300 }
301 fscanf(f, " } } ");
302
303 // if the line ends in a comma, this file has more keys.
304 switch (fgetc(f)) {
305 case ',':
306 // more keys to come.
307 break;
308
309 case EOF:
310 done = true;
311 break;
312
313 default:
314 LOGE("unexpected character between keys\n");
315 goto exit;
316 }
317 }
318
319 fclose(f);
320 return out;
321
322exit:
323 if (f) fclose(f);
324 free(out);
325 *numKeys = 0;
326 return NULL;
327}
328
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800329int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700330install_package(const char *path)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331{
332 ui_set_background(BACKGROUND_ICON_INSTALLING);
333 ui_print("Finding update package...\n");
334 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700335 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800336
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 if (ensure_path_mounted(path) != 0) {
338 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800339 return INSTALL_CORRUPT;
340 }
341
342 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800343
Doug Zongker60151a22009-08-12 18:30:03 -0700344 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800345
346 if (signature_check_enabled) {
347 int numKeys;
348 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
349 if (loadedKeys == NULL) {
350 LOGE("Failed to load keys\n");
351 return INSTALL_CORRUPT;
352 }
353 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
354
355 // Give verification half the progress bar...
356 ui_print("Verifying update package...\n");
357 ui_show_progress(
358 VERIFICATION_PROGRESS_FRACTION,
359 VERIFICATION_PROGRESS_TIME);
360
361 err = verify_file(path, loadedKeys, numKeys);
362 free(loadedKeys);
363 LOGI("verify_file returned %d\n", err);
364 if (err != VERIFY_SUCCESS) {
365 LOGE("signature verification failed\n");
366 return INSTALL_CORRUPT;
367 }
Doug Zongker60151a22009-08-12 18:30:03 -0700368 }
369
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800370 /* Try to open the package.
371 */
372 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700373 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800374 if (err != 0) {
375 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
376 return INSTALL_CORRUPT;
377 }
378
379 /* Verify and install the contents of the package.
380 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700381 ui_print("Installing update...\n");
382 return try_update_binary(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800383}