blob: ad2c21d51b612a1846e66508d3524102e5c92898 [file] [log] [blame]
preludedrew38058dc2011-01-29 23:30:44 -07001/*
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
17#include <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <limits.h>
21#include <sys/stat.h>
22#include <sys/wait.h>
23#include <unistd.h>
24
25#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 "mounts.h"
32#include "mtdutils/mtdutils.h"
33#include "roots.h"
34#include "verifier.h"
35
36#include "firmware.h"
37
38#include "extendedcommands.h"
39
40
41#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
42#define ASSUMED_UPDATE_SCRIPT_NAME "META-INF/com/google/android/update-script"
43#define PUBLIC_KEYS_FILE "/res/keys"
44
45// The update binary ask us to install a firmware file on reboot. Set
46// that up. Takes ownership of type and filename.
47static int
48handle_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;
66 }
67
68 LOGI("type is %s; size is %d; file is %s\n",
69 type, data_size, filename);
70
71 char* data = malloc(data_size);
72 if (data == NULL) {
73 LOGI("Can't allocate %d bytes for firmware data\n", data_size);
74 return INSTALL_ERROR;
75 }
76
77 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);
93 }
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 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
116 mzCloseZipArchive(zip);
117 return INSTALL_UPDATE_BINARY_MISSING;
118 }
119
120 char* binary = "/tmp/update_binary";
121 unlink(binary);
122 int fd = creat(binary, 0755);
123 if (fd < 0) {
124 mzCloseZipArchive(zip);
125 LOGE("Can't make %s\n", binary);
126 return 1;
127 }
128 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
129 close(fd);
130 mzCloseZipArchive(zip);
131
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 //
143 // - the version number for this interface
144 //
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>
149 // 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.
158 //
159 // firmware <"hboot"|"radio"> <filename>
160 // arrange to install the contents of <filename> in the
161 // 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.)
167 //
168 // ui_print <string>
169 // display <string> on the screen.
170 //
171 // - the name of the package zip file.
172 //
173
174 char** args = malloc(sizeof(char*) * 5);
175 args[0] = binary;
176 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
177 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) {
184 close(pipefd[0]);
185 execv(binary, args);
186 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
187 _exit(-1);
188 }
189 close(pipefd[1]);
190
191 char* firmware_type = NULL;
192 char* firmware_filename = NULL;
193
194 char buffer[1024];
195 FILE* from_child = fdopen(pipefd[0], "r");
196 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
197 char* command = strtok(buffer, " \n");
198 if (command == NULL) {
199 continue;
200 } else if (strcmp(command, "progress") == 0) {
201 char* fraction_s = strtok(NULL, " \n");
202 char* seconds_s = strtok(NULL, " \n");
203
204 float fraction = strtof(fraction_s, NULL);
205 int seconds = strtol(seconds_s, NULL, 10);
206
207 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
208 seconds);
209 } else if (strcmp(command, "set_progress") == 0) {
210 char* fraction_s = strtok(NULL, " \n");
211 float fraction = strtof(fraction_s, NULL);
212 ui_set_progress(fraction);
213 } else if (strcmp(command, "firmware") == 0) {
214 char* type = strtok(NULL, " \n");
215 char* filename = strtok(NULL, " \n");
216
217 if (type != NULL && filename != NULL) {
218 if (firmware_type != NULL) {
219 LOGE("ignoring attempt to do multiple firmware updates");
220 } else {
221 firmware_type = strdup(type);
222 firmware_filename = strdup(filename);
223 }
224 }
225 } else if (strcmp(command, "ui_print") == 0) {
226 char* str = strtok(NULL, "\n");
227 if (str) {
228 ui_print("%s", str);
229 } else {
230 ui_print("\n");
231 }
232 } else {
233 LOGE("unknown command [%s]\n", command);
234 }
235 }
236 fclose(from_child);
237
238 int status;
239 waitpid(pid, &status, 0);
240 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
241 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
242 return INSTALL_ERROR;
243 }
244
245 if (firmware_type != NULL) {
246 return handle_firmware_update(firmware_type, firmware_filename, zip);
247 } else {
248 return INSTALL_SUCCESS;
249 }
250 return INSTALL_SUCCESS;
251}
252
253// Reads a file containing one or more public keys as produced by
254// DumpPublicKey: this is an RSAPublicKey struct as it would appear
255// as a C source literal, eg:
256//
257// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
258//
259// (Note that the braces and commas in this example are actual
260// characters the parser expects to find in the file; the ellipses
261// indicate more numbers omitted from this example.)
262//
263// The file may contain multiple keys in this format, separated by
264// commas. The last key must not be followed by a comma.
265//
266// Returns NULL if the file failed to parse, or if it contain zero keys.
267static RSAPublicKey*
268load_keys(const char* filename, int* numKeys) {
269 RSAPublicKey* out = NULL;
270 *numKeys = 0;
271
272 FILE* f = fopen(filename, "r");
273 if (f == NULL) {
274 LOGE("opening %s: %s\n", filename, strerror(errno));
275 goto exit;
276 }
277
278 int i;
279 bool done = false;
280 while (!done) {
281 ++*numKeys;
282 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
283 RSAPublicKey* key = out + (*numKeys - 1);
284 if (fscanf(f, " { %i , 0x%x , { %u",
285 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
286 goto exit;
287 }
288 if (key->len != RSANUMWORDS) {
289 LOGE("key length (%d) does not match expected size\n", key->len);
290 goto exit;
291 }
292 for (i = 1; i < key->len; ++i) {
293 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
294 }
295 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
296 for (i = 1; i < key->len; ++i) {
297 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
298 }
299 fscanf(f, " } } ");
300
301 // if the line ends in a comma, this file has more keys.
302 switch (fgetc(f)) {
303 case ',':
304 // more keys to come.
305 break;
306
307 case EOF:
308 done = true;
309 break;
310
311 default:
312 LOGE("unexpected character between keys\n");
313 goto exit;
314 }
315 }
316
317 fclose(f);
318 return out;
319
320exit:
321 if (f) fclose(f);
322 free(out);
323 *numKeys = 0;
324 return NULL;
325}
326
327int
328install_package(const char *path)
329{
330 ui_set_background(BACKGROUND_ICON_INSTALLING);
331 ui_print("Finding update package...\n");
332 ui_show_indeterminate_progress();
333 LOGI("Update location: %s\n", path);
334
335 if (ensure_path_mounted(path) != 0) {
336 LOGE("Can't mount %s\n", path);
337 return INSTALL_CORRUPT;
338 }
339
340 ui_print("Opening update package...\n");
341
342 int err;
343
344 if (signature_check_enabled) {
345 int numKeys;
346 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
347 if (loadedKeys == NULL) {
348 LOGE("Failed to load keys\n");
349 return INSTALL_CORRUPT;
350 }
351 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
352
353 // Give verification half the progress bar...
354 ui_print("Verifying update package...\n");
355 ui_show_progress(
356 VERIFICATION_PROGRESS_FRACTION,
357 VERIFICATION_PROGRESS_TIME);
358
359 err = verify_file(path, loadedKeys, numKeys);
360 free(loadedKeys);
361 LOGI("verify_file returned %d\n", err);
362 if (err != VERIFY_SUCCESS) {
363 LOGE("signature verification failed\n");
364 return INSTALL_CORRUPT;
365 }
366 }
367
368 /* Try to open the package.
369 */
370 ZipArchive zip;
371 err = mzOpenZipArchive(path, &zip);
372 if (err != 0) {
373 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
374 return INSTALL_CORRUPT;
375 }
376
377 /* Verify and install the contents of the package.
378 */
379 ui_print("Installing update...\n");
380 return try_update_binary(path, &zip);
381}