blob: cae283c98209f0728b73953d66ffb3d636bcff6b [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
106// If the package contains an update binary, extract it and run it.
107static int
108try_update_binary(const char *path, ZipArchive *zip) {
109 const ZipEntry* binary_entry =
110 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
111 if (binary_entry == NULL) {
Koushik Dutta67c381a2011-01-02 12:26:35 -0800112 const ZipEntry* update_script_entry =
113 mzFindZipEntry(zip, ASSUMED_UPDATE_SCRIPT_NAME);
114 if (update_script_entry != NULL) {
115 ui_print("Amend scripting (update-script) is no longer supported.\n");
116 ui_print("Amend scripting was deprecated by Google in Android 1.5.\n");
117 ui_print("It was necessary to remove it when upgrading to the ClockworkMod 3.0 Gingerbread based recovery.\n");
118 ui_print("Please switch to Edify scripting (updater-script and update-binary) to create working update zip packages.\n");
119 return INSTALL_UPDATE_BINARY_MISSING;
120 }
121
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700122 mzCloseZipArchive(zip);
Koushik K. Dutta581bd862010-03-20 01:08:55 -0700123 return INSTALL_UPDATE_BINARY_MISSING;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700124 }
125
126 char* binary = "/tmp/update_binary";
127 unlink(binary);
128 int fd = creat(binary, 0755);
129 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700130 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 LOGE("Can't make %s\n", binary);
132 return 1;
133 }
134 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
135 close(fd);
136
137 if (!ok) {
138 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Koushik Dutta49553c72011-05-13 13:01:26 -0700139 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700140 return 1;
141 }
142
143 int pipefd[2];
144 pipe(pipefd);
145
146 // When executing the update binary contained in the package, the
147 // arguments passed are:
148 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700149 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700150 //
151 // - an fd to which the program can write in order to update the
152 // progress bar. The program can write single-line commands:
153 //
154 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700155 // fill up the next <frac> part of of the progress bar
156 // over <secs> seconds. If <secs> is zero, use
157 // set_progress commands to manually control the
158 // progress of this segment of the bar
159 //
160 // set_progress <frac>
161 // <frac> should be between 0.0 and 1.0; sets the
162 // progress bar within the segment defined by the most
163 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700164 //
165 // firmware <"hboot"|"radio"> <filename>
166 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800167 // given partition on reboot.
168 //
169 // (API v2: <filename> may start with "PACKAGE:" to
170 // indicate taking a file from the OTA package.)
171 //
172 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700173 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700174 // ui_print <string>
175 // display <string> on the screen.
176 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700177 // - the name of the package zip file.
178 //
179
180 char** args = malloc(sizeof(char*) * 5);
181 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700182 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700183 args[2] = malloc(10);
184 sprintf(args[2], "%d", pipefd[1]);
185 args[3] = (char*)path;
186 args[4] = NULL;
187
188 pid_t pid = fork();
189 if (pid == 0) {
Koushik Dutta20b516a2011-05-15 18:48:17 -0700190 setenv("UPDATE_PACKAGE", path, 1);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700191 close(pipefd[0]);
192 execv(binary, args);
Doug Zongker56c51052010-07-01 09:18:44 -0700193 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700194 _exit(-1);
195 }
196 close(pipefd[1]);
197
Koushik Dutta19447c02010-11-10 10:40:44 -0800198 char* firmware_type = NULL;
199 char* firmware_filename = NULL;
200
Doug Zongker64893cc2009-07-14 16:31:56 -0700201 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700202 FILE* from_child = fdopen(pipefd[0], "r");
203 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700204 char* command = strtok(buffer, " \n");
205 if (command == NULL) {
206 continue;
207 } else if (strcmp(command, "progress") == 0) {
208 char* fraction_s = strtok(NULL, " \n");
209 char* seconds_s = strtok(NULL, " \n");
210
211 float fraction = strtof(fraction_s, NULL);
212 int seconds = strtol(seconds_s, NULL, 10);
213
214 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
215 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700216 } else if (strcmp(command, "set_progress") == 0) {
217 char* fraction_s = strtok(NULL, " \n");
218 float fraction = strtof(fraction_s, NULL);
219 ui_set_progress(fraction);
Koushik Dutta19447c02010-11-10 10:40:44 -0800220 } else if (strcmp(command, "firmware") == 0) {
221 char* type = strtok(NULL, " \n");
222 char* filename = strtok(NULL, " \n");
223
224 if (type != NULL && filename != NULL) {
225 if (firmware_type != NULL) {
226 LOGE("ignoring attempt to do multiple firmware updates");
227 } else {
228 firmware_type = strdup(type);
229 firmware_filename = strdup(filename);
230 }
231 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700232 } else if (strcmp(command, "ui_print") == 0) {
233 char* str = strtok(NULL, "\n");
234 if (str) {
Nick Kralevich21b97ed2010-06-24 16:11:17 -0700235 ui_print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700236 } else {
237 ui_print("\n");
238 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700239 } else {
240 LOGE("unknown command [%s]\n", command);
241 }
242 }
243 fclose(from_child);
244
245 int status;
246 waitpid(pid, &status, 0);
247 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Koushik Dutta49553c72011-05-13 13:01:26 -0700249 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700250 return INSTALL_ERROR;
251 }
252
Koushik Dutta19447c02010-11-10 10:40:44 -0800253 if (firmware_type != NULL) {
Koushik Duttad79b7542011-05-25 10:47:41 -0700254 int ret = handle_firmware_update(firmware_type, firmware_filename, zip);
Koushik Dutta49553c72011-05-13 13:01:26 -0700255 mzCloseZipArchive(zip);
Koushik Duttad79b7542011-05-25 10:47:41 -0700256 return ret;
Koushik Dutta19447c02010-11-10 10:40:44 -0800257 }
Doug Zongkere08991e2010-02-02 13:09:52 -0800258 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700259}
260
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700261// Reads a file containing one or more public keys as produced by
262// DumpPublicKey: this is an RSAPublicKey struct as it would appear
263// as a C source literal, eg:
264//
265// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
266//
267// (Note that the braces and commas in this example are actual
268// characters the parser expects to find in the file; the ellipses
269// indicate more numbers omitted from this example.)
270//
271// The file may contain multiple keys in this format, separated by
272// commas. The last key must not be followed by a comma.
273//
274// Returns NULL if the file failed to parse, or if it contain zero keys.
275static RSAPublicKey*
276load_keys(const char* filename, int* numKeys) {
277 RSAPublicKey* out = NULL;
278 *numKeys = 0;
279
280 FILE* f = fopen(filename, "r");
281 if (f == NULL) {
282 LOGE("opening %s: %s\n", filename, strerror(errno));
283 goto exit;
284 }
285
286 int i;
287 bool done = false;
288 while (!done) {
289 ++*numKeys;
290 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
291 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800292 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700293 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
294 goto exit;
295 }
296 if (key->len != RSANUMWORDS) {
297 LOGE("key length (%d) does not match expected size\n", key->len);
298 goto exit;
299 }
300 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800301 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700302 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800303 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700304 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800305 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700306 }
307 fscanf(f, " } } ");
308
309 // if the line ends in a comma, this file has more keys.
310 switch (fgetc(f)) {
311 case ',':
312 // more keys to come.
313 break;
314
315 case EOF:
316 done = true;
317 break;
318
319 default:
320 LOGE("unexpected character between keys\n");
321 goto exit;
322 }
323 }
324
325 fclose(f);
326 return out;
327
328exit:
329 if (f) fclose(f);
330 free(out);
331 *numKeys = 0;
332 return NULL;
333}
334
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700336install_package(const char *path)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800337{
338 ui_set_background(BACKGROUND_ICON_INSTALLING);
339 ui_print("Finding update package...\n");
340 ui_show_indeterminate_progress();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700341 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800342
Doug Zongkerd4208f92010-09-20 12:16:13 -0700343 if (ensure_path_mounted(path) != 0) {
344 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345 return INSTALL_CORRUPT;
346 }
347
348 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800349
Doug Zongker60151a22009-08-12 18:30:03 -0700350 int err;
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800351
352 if (signature_check_enabled) {
353 int numKeys;
354 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
355 if (loadedKeys == NULL) {
356 LOGE("Failed to load keys\n");
357 return INSTALL_CORRUPT;
358 }
359 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
360
361 // Give verification half the progress bar...
362 ui_print("Verifying update package...\n");
363 ui_show_progress(
364 VERIFICATION_PROGRESS_FRACTION,
365 VERIFICATION_PROGRESS_TIME);
366
367 err = verify_file(path, loadedKeys, numKeys);
368 free(loadedKeys);
369 LOGI("verify_file returned %d\n", err);
370 if (err != VERIFY_SUCCESS) {
371 LOGE("signature verification failed\n");
372 return INSTALL_CORRUPT;
373 }
Doug Zongker60151a22009-08-12 18:30:03 -0700374 }
375
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800376 /* Try to open the package.
377 */
378 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700379 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800380 if (err != 0) {
381 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
382 return INSTALL_CORRUPT;
383 }
384
385 /* Verify and install the contents of the package.
386 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700387 ui_print("Installing update...\n");
388 return try_update_binary(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800389}