blob: 482e0d755590a1d164820ebe8595dbfd8178bd18 [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"
31#include "mtdutils/mounts.h"
32#include "mtdutils/mtdutils.h"
33#include "roots.h"
34#include "verifier.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070035#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Doug Zongkerb2ee9202009-06-04 10:24:53 -070037#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070038#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Doug Zongkerb2ee9202009-06-04 10:24:53 -070040// If the package contains an update binary, extract it and run it.
41static int
Doug Zongkerd0181b82011-10-19 10:51:12 -070042try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070043 const ZipEntry* binary_entry =
44 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
45 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070046 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070047 return INSTALL_CORRUPT;
48 }
49
Doug Zongker28ce47c2011-10-28 10:33:05 -070050 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -070051 unlink(binary);
52 int fd = creat(binary, 0755);
53 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070054 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070055 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -070056 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070057 }
58 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
59 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070060 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070061
62 if (!ok) {
63 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -070064 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070065 }
66
67 int pipefd[2];
68 pipe(pipefd);
69
70 // When executing the update binary contained in the package, the
71 // arguments passed are:
72 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070073 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070074 //
75 // - an fd to which the program can write in order to update the
76 // progress bar. The program can write single-line commands:
77 //
78 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070079 // fill up the next <frac> part of of the progress bar
80 // over <secs> seconds. If <secs> is zero, use
81 // set_progress commands to manually control the
82 // progress of this segment of the bar
83 //
84 // set_progress <frac>
85 // <frac> should be between 0.0 and 1.0; sets the
86 // progress bar within the segment defined by the most
87 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -070088 //
89 // firmware <"hboot"|"radio"> <filename>
90 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -080091 // given partition on reboot.
92 //
93 // (API v2: <filename> may start with "PACKAGE:" to
94 // indicate taking a file from the OTA package.)
95 //
96 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -070097 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -070098 // ui_print <string>
99 // display <string> on the screen.
100 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700101 // - the name of the package zip file.
102 //
103
Doug Zongker28ce47c2011-10-28 10:33:05 -0700104 const char** args = (const char**)malloc(sizeof(char*) * 5);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700105 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700106 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongker28ce47c2011-10-28 10:33:05 -0700107 char* temp = (char*)malloc(10);
108 sprintf(temp, "%d", pipefd[1]);
109 args[2] = temp;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700110 args[3] = (char*)path;
111 args[4] = NULL;
112
113 pid_t pid = fork();
114 if (pid == 0) {
115 close(pipefd[0]);
Doug Zongker28ce47c2011-10-28 10:33:05 -0700116 execv(binary, (char* const*)args);
Doug Zongker56c51052010-07-01 09:18:44 -0700117 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700118 _exit(-1);
119 }
120 close(pipefd[1]);
121
Doug Zongkerd0181b82011-10-19 10:51:12 -0700122 *wipe_cache = 0;
123
Doug Zongker64893cc2009-07-14 16:31:56 -0700124 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700125 FILE* from_child = fdopen(pipefd[0], "r");
126 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700127 char* command = strtok(buffer, " \n");
128 if (command == NULL) {
129 continue;
130 } else if (strcmp(command, "progress") == 0) {
131 char* fraction_s = strtok(NULL, " \n");
132 char* seconds_s = strtok(NULL, " \n");
133
134 float fraction = strtof(fraction_s, NULL);
135 int seconds = strtol(seconds_s, NULL, 10);
136
137 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
138 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700139 } else if (strcmp(command, "set_progress") == 0) {
140 char* fraction_s = strtok(NULL, " \n");
141 float fraction = strtof(fraction_s, NULL);
142 ui_set_progress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700143 } else if (strcmp(command, "ui_print") == 0) {
144 char* str = strtok(NULL, "\n");
145 if (str) {
Nick Kralevich21b97ed2010-06-24 16:11:17 -0700146 ui_print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700147 } else {
148 ui_print("\n");
149 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700150 } else if (strcmp(command, "wipe_cache") == 0) {
151 *wipe_cache = 1;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700152 } else {
153 LOGE("unknown command [%s]\n", command);
154 }
155 }
156 fclose(from_child);
157
158 int status;
159 waitpid(pid, &status, 0);
160 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700161 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700162 return INSTALL_ERROR;
163 }
164
Doug Zongkere08991e2010-02-02 13:09:52 -0800165 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700166}
167
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700168// Reads a file containing one or more public keys as produced by
169// DumpPublicKey: this is an RSAPublicKey struct as it would appear
170// as a C source literal, eg:
171//
172// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
173//
174// (Note that the braces and commas in this example are actual
175// characters the parser expects to find in the file; the ellipses
176// indicate more numbers omitted from this example.)
177//
178// The file may contain multiple keys in this format, separated by
179// commas. The last key must not be followed by a comma.
180//
181// Returns NULL if the file failed to parse, or if it contain zero keys.
182static RSAPublicKey*
183load_keys(const char* filename, int* numKeys) {
184 RSAPublicKey* out = NULL;
185 *numKeys = 0;
186
187 FILE* f = fopen(filename, "r");
188 if (f == NULL) {
189 LOGE("opening %s: %s\n", filename, strerror(errno));
190 goto exit;
191 }
192
Doug Zongker28ce47c2011-10-28 10:33:05 -0700193 {
194 int i;
195 bool done = false;
196 while (!done) {
197 ++*numKeys;
198 out = (RSAPublicKey*)realloc(out, *numKeys * sizeof(RSAPublicKey));
199 RSAPublicKey* key = out + (*numKeys - 1);
200 if (fscanf(f, " { %i , 0x%x , { %u",
201 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
202 goto exit;
203 }
204 if (key->len != RSANUMWORDS) {
205 LOGE("key length (%d) does not match expected size\n", key->len);
206 goto exit;
207 }
208 for (i = 1; i < key->len; ++i) {
209 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
210 }
211 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
212 for (i = 1; i < key->len; ++i) {
213 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
214 }
215 fscanf(f, " } } ");
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700216
Doug Zongker28ce47c2011-10-28 10:33:05 -0700217 // if the line ends in a comma, this file has more keys.
218 switch (fgetc(f)) {
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700219 case ',':
220 // more keys to come.
221 break;
222
223 case EOF:
224 done = true;
225 break;
226
227 default:
228 LOGE("unexpected character between keys\n");
229 goto exit;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700230 }
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700231 }
232 }
233
234 fclose(f);
235 return out;
236
237exit:
238 if (f) fclose(f);
239 free(out);
240 *numKeys = 0;
241 return NULL;
242}
243
Doug Zongker469243e2011-04-12 09:28:10 -0700244static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700245really_install_package(const char *path, int* wipe_cache)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800246{
247 ui_set_background(BACKGROUND_ICON_INSTALLING);
248 ui_print("Finding update package...\n");
249 ui_show_indeterminate_progress();
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700250 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700252 if (ensure_path_mounted(path) != 0) {
253 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800254 return INSTALL_CORRUPT;
255 }
256
257 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800258
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700259 int numKeys;
260 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
261 if (loadedKeys == NULL) {
262 LOGE("Failed to load keys\n");
263 return INSTALL_CORRUPT;
264 }
265 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
266
Doug Zongker60151a22009-08-12 18:30:03 -0700267 // Give verification half the progress bar...
268 ui_print("Verifying update package...\n");
269 ui_show_progress(
270 VERIFICATION_PROGRESS_FRACTION,
271 VERIFICATION_PROGRESS_TIME);
272
273 int err;
274 err = verify_file(path, loadedKeys, numKeys);
275 free(loadedKeys);
276 LOGI("verify_file returned %d\n", err);
277 if (err != VERIFY_SUCCESS) {
278 LOGE("signature verification failed\n");
279 return INSTALL_CORRUPT;
280 }
281
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800282 /* Try to open the package.
283 */
284 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700285 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800286 if (err != 0) {
287 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
288 return INSTALL_CORRUPT;
289 }
290
291 /* Verify and install the contents of the package.
292 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700293 ui_print("Installing update...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700294 return try_update_binary(path, &zip, wipe_cache);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295}
Doug Zongker469243e2011-04-12 09:28:10 -0700296
297int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700298install_package(const char* path, int* wipe_cache, const char* install_file)
Doug Zongker469243e2011-04-12 09:28:10 -0700299{
Doug Zongkerd0181b82011-10-19 10:51:12 -0700300 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700301 if (install_log) {
302 fputs(path, install_log);
303 fputc('\n', install_log);
304 } else {
305 LOGE("failed to open last_install: %s\n", strerror(errno));
306 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700307 int result = really_install_package(path, wipe_cache);
Doug Zongker469243e2011-04-12 09:28:10 -0700308 if (install_log) {
309 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
310 fputc('\n', install_log);
311 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700312 }
313 return result;
314}