blob: 5bb3a78faaad77619713deb737924a0e2da903d6 [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"
35
Doug Zongkerb2ee9202009-06-04 10:24:53 -070036#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070037#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Doug Zongkerb2ee9202009-06-04 10:24:53 -070039// If the package contains an update binary, extract it and run it.
40static int
41try_update_binary(const char *path, ZipArchive *zip) {
42 const ZipEntry* binary_entry =
43 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
44 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070045 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070046 return INSTALL_CORRUPT;
47 }
48
49 char* binary = "/tmp/update_binary";
50 unlink(binary);
51 int fd = creat(binary, 0755);
52 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070053 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070054 LOGE("Can't make %s\n", binary);
55 return 1;
56 }
57 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
58 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070059 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070060
61 if (!ok) {
62 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
63 return 1;
64 }
65
66 int pipefd[2];
67 pipe(pipefd);
68
69 // When executing the update binary contained in the package, the
70 // arguments passed are:
71 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070072 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070073 //
74 // - an fd to which the program can write in order to update the
75 // progress bar. The program can write single-line commands:
76 //
77 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070078 // fill up the next <frac> part of of the progress bar
79 // over <secs> seconds. If <secs> is zero, use
80 // set_progress commands to manually control the
81 // progress of this segment of the bar
82 //
83 // set_progress <frac>
84 // <frac> should be between 0.0 and 1.0; sets the
85 // progress bar within the segment defined by the most
86 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -070087 //
88 // firmware <"hboot"|"radio"> <filename>
89 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -080090 // given partition on reboot.
91 //
92 // (API v2: <filename> may start with "PACKAGE:" to
93 // indicate taking a file from the OTA package.)
94 //
95 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -070096 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -070097 // ui_print <string>
98 // display <string> on the screen.
99 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700100 // - the name of the package zip file.
101 //
102
103 char** args = malloc(sizeof(char*) * 5);
104 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700105 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700106 args[2] = malloc(10);
107 sprintf(args[2], "%d", pipefd[1]);
108 args[3] = (char*)path;
109 args[4] = NULL;
110
111 pid_t pid = fork();
112 if (pid == 0) {
113 close(pipefd[0]);
114 execv(binary, args);
Doug Zongker56c51052010-07-01 09:18:44 -0700115 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700116 _exit(-1);
117 }
118 close(pipefd[1]);
119
Doug Zongker64893cc2009-07-14 16:31:56 -0700120 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700121 FILE* from_child = fdopen(pipefd[0], "r");
122 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700123 char* command = strtok(buffer, " \n");
124 if (command == NULL) {
125 continue;
126 } else if (strcmp(command, "progress") == 0) {
127 char* fraction_s = strtok(NULL, " \n");
128 char* seconds_s = strtok(NULL, " \n");
129
130 float fraction = strtof(fraction_s, NULL);
131 int seconds = strtol(seconds_s, NULL, 10);
132
133 ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
134 seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700135 } else if (strcmp(command, "set_progress") == 0) {
136 char* fraction_s = strtok(NULL, " \n");
137 float fraction = strtof(fraction_s, NULL);
138 ui_set_progress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700139 } else if (strcmp(command, "ui_print") == 0) {
140 char* str = strtok(NULL, "\n");
141 if (str) {
Nick Kralevich21b97ed2010-06-24 16:11:17 -0700142 ui_print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700143 } else {
144 ui_print("\n");
145 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700146 } else {
147 LOGE("unknown command [%s]\n", command);
148 }
149 }
150 fclose(from_child);
151
152 int status;
153 waitpid(pid, &status, 0);
154 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700155 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700156 return INSTALL_ERROR;
157 }
158
Doug Zongkere08991e2010-02-02 13:09:52 -0800159 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700160}
161
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700162// Reads a file containing one or more public keys as produced by
163// DumpPublicKey: this is an RSAPublicKey struct as it would appear
164// as a C source literal, eg:
165//
166// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
167//
168// (Note that the braces and commas in this example are actual
169// characters the parser expects to find in the file; the ellipses
170// indicate more numbers omitted from this example.)
171//
172// The file may contain multiple keys in this format, separated by
173// commas. The last key must not be followed by a comma.
174//
175// Returns NULL if the file failed to parse, or if it contain zero keys.
176static RSAPublicKey*
177load_keys(const char* filename, int* numKeys) {
178 RSAPublicKey* out = NULL;
179 *numKeys = 0;
180
181 FILE* f = fopen(filename, "r");
182 if (f == NULL) {
183 LOGE("opening %s: %s\n", filename, strerror(errno));
184 goto exit;
185 }
186
187 int i;
188 bool done = false;
189 while (!done) {
190 ++*numKeys;
191 out = realloc(out, *numKeys * sizeof(RSAPublicKey));
192 RSAPublicKey* key = out + (*numKeys - 1);
Doug Zongkeraa062532010-01-28 16:47:20 -0800193 if (fscanf(f, " { %i , 0x%x , { %u",
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700194 &(key->len), &(key->n0inv), &(key->n[0])) != 3) {
195 goto exit;
196 }
197 if (key->len != RSANUMWORDS) {
198 LOGE("key length (%d) does not match expected size\n", key->len);
199 goto exit;
200 }
201 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800202 if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700203 }
Doug Zongkeraa062532010-01-28 16:47:20 -0800204 if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700205 for (i = 1; i < key->len; ++i) {
Doug Zongkeraa062532010-01-28 16:47:20 -0800206 if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700207 }
208 fscanf(f, " } } ");
209
210 // if the line ends in a comma, this file has more keys.
211 switch (fgetc(f)) {
212 case ',':
213 // more keys to come.
214 break;
215
216 case EOF:
217 done = true;
218 break;
219
220 default:
221 LOGE("unexpected character between keys\n");
222 goto exit;
223 }
224 }
225
226 fclose(f);
227 return out;
228
229exit:
230 if (f) fclose(f);
231 free(out);
232 *numKeys = 0;
233 return NULL;
234}
235
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800236int
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700237install_package(const char *path)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800238{
239 ui_set_background(BACKGROUND_ICON_INSTALLING);
240 ui_print("Finding update package...\n");
241 ui_show_indeterminate_progress();
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700242 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800243
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700244 if (ensure_path_mounted(path) != 0) {
245 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800246 return INSTALL_CORRUPT;
247 }
248
249 ui_print("Opening update package...\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700251 int numKeys;
252 RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
253 if (loadedKeys == NULL) {
254 LOGE("Failed to load keys\n");
255 return INSTALL_CORRUPT;
256 }
257 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
258
Doug Zongker60151a22009-08-12 18:30:03 -0700259 // Give verification half the progress bar...
260 ui_print("Verifying update package...\n");
261 ui_show_progress(
262 VERIFICATION_PROGRESS_FRACTION,
263 VERIFICATION_PROGRESS_TIME);
264
265 int err;
266 err = verify_file(path, loadedKeys, numKeys);
267 free(loadedKeys);
268 LOGI("verify_file returned %d\n", err);
269 if (err != VERIFY_SUCCESS) {
270 LOGE("signature verification failed\n");
271 return INSTALL_CORRUPT;
272 }
273
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800274 /* Try to open the package.
275 */
276 ZipArchive zip;
Doug Zongker60151a22009-08-12 18:30:03 -0700277 err = mzOpenZipArchive(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 if (err != 0) {
279 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
280 return INSTALL_CORRUPT;
281 }
282
283 /* Verify and install the contents of the package.
284 */
Doug Zongkerd7d42082010-09-17 13:02:48 -0700285 ui_print("Installing update...\n");
286 return try_update_binary(path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287}