blob: 0bd7945c4c5abff171444408b940f4070d5b6e31 [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 Zongker10e418d2011-10-28 10:33:05 -070035#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Doug Zongker74406302011-10-28 15:13:10 -070037extern RecoveryUI* ui;
38
Doug Zongkerb2ee9202009-06-04 10:24:53 -070039#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070040#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
Doug Zongker74406302011-10-28 15:13:10 -070042// Default allocation of progress bar segments to operations
43static const int VERIFICATION_PROGRESS_TIME = 60;
44static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
45static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
46static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
47
Doug Zongkerb2ee9202009-06-04 10:24:53 -070048// If the package contains an update binary, extract it and run it.
49static int
Doug Zongkerd0181b82011-10-19 10:51:12 -070050try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070051 const ZipEntry* binary_entry =
52 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
53 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070054 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070055 return INSTALL_CORRUPT;
56 }
57
Doug Zongker10e418d2011-10-28 10:33:05 -070058 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -070059 unlink(binary);
60 int fd = creat(binary, 0755);
61 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070062 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070063 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -070064 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070065 }
66 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
67 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070068 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070069
70 if (!ok) {
71 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -070072 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070073 }
74
75 int pipefd[2];
76 pipe(pipefd);
77
78 // When executing the update binary contained in the package, the
79 // arguments passed are:
80 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070081 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070082 //
83 // - an fd to which the program can write in order to update the
84 // progress bar. The program can write single-line commands:
85 //
86 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070087 // fill up the next <frac> part of of the progress bar
88 // over <secs> seconds. If <secs> is zero, use
89 // set_progress commands to manually control the
90 // progress of this segment of the bar
91 //
92 // set_progress <frac>
93 // <frac> should be between 0.0 and 1.0; sets the
94 // progress bar within the segment defined by the most
95 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -070096 //
97 // firmware <"hboot"|"radio"> <filename>
98 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -080099 // given partition on reboot.
100 //
101 // (API v2: <filename> may start with "PACKAGE:" to
102 // indicate taking a file from the OTA package.)
103 //
104 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700105 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700106 // ui_print <string>
107 // display <string> on the screen.
108 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700109 // - the name of the package zip file.
110 //
111
Doug Zongker10e418d2011-10-28 10:33:05 -0700112 const char** args = (const char**)malloc(sizeof(char*) * 5);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700113 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700114 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongker10e418d2011-10-28 10:33:05 -0700115 char* temp = (char*)malloc(10);
116 sprintf(temp, "%d", pipefd[1]);
117 args[2] = temp;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700118 args[3] = (char*)path;
119 args[4] = NULL;
120
121 pid_t pid = fork();
122 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700123 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700124 close(pipefd[0]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700125 execv(binary, (char* const*)args);
Doug Zongker56c51052010-07-01 09:18:44 -0700126 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700127 _exit(-1);
128 }
129 close(pipefd[1]);
130
Doug Zongkerd0181b82011-10-19 10:51:12 -0700131 *wipe_cache = 0;
132
Doug Zongker64893cc2009-07-14 16:31:56 -0700133 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700134 FILE* from_child = fdopen(pipefd[0], "r");
135 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700136 char* command = strtok(buffer, " \n");
137 if (command == NULL) {
138 continue;
139 } else if (strcmp(command, "progress") == 0) {
140 char* fraction_s = strtok(NULL, " \n");
141 char* seconds_s = strtok(NULL, " \n");
142
143 float fraction = strtof(fraction_s, NULL);
144 int seconds = strtol(seconds_s, NULL, 10);
145
Doug Zongker74406302011-10-28 15:13:10 -0700146 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700147 } else if (strcmp(command, "set_progress") == 0) {
148 char* fraction_s = strtok(NULL, " \n");
149 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700150 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700151 } else if (strcmp(command, "ui_print") == 0) {
152 char* str = strtok(NULL, "\n");
153 if (str) {
Doug Zongker74406302011-10-28 15:13:10 -0700154 ui->Print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700155 } else {
Doug Zongker74406302011-10-28 15:13:10 -0700156 ui->Print("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700157 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700158 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700159 } else if (strcmp(command, "wipe_cache") == 0) {
160 *wipe_cache = 1;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700161 } else if (strcmp(command, "clear_display") == 0) {
162 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700163 } else {
164 LOGE("unknown command [%s]\n", command);
165 }
166 }
167 fclose(from_child);
168
169 int status;
170 waitpid(pid, &status, 0);
171 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700173 return INSTALL_ERROR;
174 }
175
Doug Zongkere08991e2010-02-02 13:09:52 -0800176 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700177}
178
Doug Zongker469243e2011-04-12 09:28:10 -0700179static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700180really_install_package(const char *path, int* wipe_cache)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700182 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700183 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700184 // Give verification half the progress bar...
185 ui->SetProgressType(RecoveryUI::DETERMINATE);
186 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700187 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188
Doug Zongker99916f02014-01-13 14:16:58 -0800189 // Map the update package into memory.
190 ui->Print("Opening update package...\n");
191
192 if (path) {
193 if (path[0] == '@') {
194 ensure_path_mounted(path+1);
195 } else {
196 ensure_path_mounted(path);
197 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198 }
199
Doug Zongker99916f02014-01-13 14:16:58 -0800200 MemMapping map;
201 if (sysMapFile(path, &map) != 0) {
202 LOGE("failed to map file\n");
203 return INSTALL_CORRUPT;
204 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800205
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700206 int numKeys;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700207 Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700208 if (loadedKeys == NULL) {
209 LOGE("Failed to load keys\n");
210 return INSTALL_CORRUPT;
211 }
212 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
213
Doug Zongker74406302011-10-28 15:13:10 -0700214 ui->Print("Verifying update package...\n");
Doug Zongker60151a22009-08-12 18:30:03 -0700215
216 int err;
Doug Zongker99916f02014-01-13 14:16:58 -0800217 err = verify_file(map.addr, map.length, loadedKeys, numKeys);
Doug Zongker60151a22009-08-12 18:30:03 -0700218 free(loadedKeys);
219 LOGI("verify_file returned %d\n", err);
220 if (err != VERIFY_SUCCESS) {
221 LOGE("signature verification failed\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800222 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700223 return INSTALL_CORRUPT;
224 }
225
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800226 /* Try to open the package.
227 */
228 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800229 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800230 if (err != 0) {
231 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Doug Zongker99916f02014-01-13 14:16:58 -0800232 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800233 return INSTALL_CORRUPT;
234 }
235
236 /* Verify and install the contents of the package.
237 */
Doug Zongker74406302011-10-28 15:13:10 -0700238 ui->Print("Installing update...\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800239 int result = try_update_binary(path, &zip, wipe_cache);
240
241 sysReleaseMap(&map);
242
243 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244}
Doug Zongker469243e2011-04-12 09:28:10 -0700245
246int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700247install_package(const char* path, int* wipe_cache, const char* install_file)
Doug Zongker469243e2011-04-12 09:28:10 -0700248{
Doug Zongkerd0181b82011-10-19 10:51:12 -0700249 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700250 if (install_log) {
251 fputs(path, install_log);
252 fputc('\n', install_log);
253 } else {
254 LOGE("failed to open last_install: %s\n", strerror(errno));
255 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700256 int result;
257 if (setup_install_mounts() != 0) {
258 LOGE("failed to set up expected mounts for install; aborting\n");
259 result = INSTALL_ERROR;
260 } else {
261 result = really_install_package(path, wipe_cache);
262 }
Doug Zongker469243e2011-04-12 09:28:10 -0700263 if (install_log) {
264 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
265 fputc('\n', install_log);
266 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700267 }
268 return result;
269}