blob: b46bbdf9db869eab7a0cb1aec944e111ffe80ab8 [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>
Elliott Hughes26dbad22015-01-28 12:09:05 -080021#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include <sys/stat.h>
Doug Zongkerb2ee9202009-06-04 10:24:53 -070023#include <sys/wait.h>
24#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include "common.h"
27#include "install.h"
28#include "mincrypt/rsa.h"
29#include "minui/minui.h"
30#include "minzip/SysUtil.h"
31#include "minzip/Zip.h"
32#include "mtdutils/mounts.h"
33#include "mtdutils/mtdutils.h"
34#include "roots.h"
35#include "verifier.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070036#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Doug Zongker74406302011-10-28 15:13:10 -070038extern RecoveryUI* ui;
39
Doug Zongkerb2ee9202009-06-04 10:24:53 -070040#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070041#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Doug Zongker74406302011-10-28 15:13:10 -070043// Default allocation of progress bar segments to operations
44static const int VERIFICATION_PROGRESS_TIME = 60;
45static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
46static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
47static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
48
Doug Zongkerb2ee9202009-06-04 10:24:53 -070049// If the package contains an update binary, extract it and run it.
50static int
Tao Bao145d8612015-03-25 15:51:15 -070051try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070052 const ZipEntry* binary_entry =
53 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
54 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070055 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070056 return INSTALL_CORRUPT;
57 }
58
Doug Zongker10e418d2011-10-28 10:33:05 -070059 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -070060 unlink(binary);
61 int fd = creat(binary, 0755);
62 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070063 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070064 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -070065 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070066 }
67 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
68 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070069 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070070
71 if (!ok) {
72 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -070073 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070074 }
75
76 int pipefd[2];
77 pipe(pipefd);
78
79 // When executing the update binary contained in the package, the
80 // arguments passed are:
81 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070082 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070083 //
84 // - an fd to which the program can write in order to update the
85 // progress bar. The program can write single-line commands:
86 //
87 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070088 // fill up the next <frac> part of of the progress bar
89 // over <secs> seconds. If <secs> is zero, use
90 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -070091 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -070092 //
93 // set_progress <frac>
94 // <frac> should be between 0.0 and 1.0; sets the
95 // progress bar within the segment defined by the most
96 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -070097 //
98 // firmware <"hboot"|"radio"> <filename>
99 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800100 // given partition on reboot.
101 //
102 // (API v2: <filename> may start with "PACKAGE:" to
103 // indicate taking a file from the OTA package.)
104 //
105 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700106 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700107 // ui_print <string>
108 // display <string> on the screen.
109 //
Tao Baob07e1f32015-04-10 16:14:52 -0700110 // wipe_cache
111 // a wipe of cache will be performed following a successful
112 // installation.
113 //
114 // clear_display
115 // turn off the text display.
116 //
117 // enable_reboot
118 // packages can explicitly request that they want the user
119 // to be able to reboot during installation (useful for
120 // debugging packages that don't exit).
121 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700122 // - the name of the package zip file.
123 //
124
Doug Zongker10e418d2011-10-28 10:33:05 -0700125 const char** args = (const char**)malloc(sizeof(char*) * 5);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700126 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700127 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongker10e418d2011-10-28 10:33:05 -0700128 char* temp = (char*)malloc(10);
129 sprintf(temp, "%d", pipefd[1]);
130 args[2] = temp;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 args[3] = (char*)path;
132 args[4] = NULL;
133
134 pid_t pid = fork();
135 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700136 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700137 close(pipefd[0]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700138 execv(binary, (char* const*)args);
Doug Zongker56c51052010-07-01 09:18:44 -0700139 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700140 _exit(-1);
141 }
142 close(pipefd[1]);
143
Tao Bao145d8612015-03-25 15:51:15 -0700144 *wipe_cache = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700145
Doug Zongker64893cc2009-07-14 16:31:56 -0700146 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700147 FILE* from_child = fdopen(pipefd[0], "r");
148 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700149 char* command = strtok(buffer, " \n");
150 if (command == NULL) {
151 continue;
152 } else if (strcmp(command, "progress") == 0) {
153 char* fraction_s = strtok(NULL, " \n");
154 char* seconds_s = strtok(NULL, " \n");
155
156 float fraction = strtof(fraction_s, NULL);
157 int seconds = strtol(seconds_s, NULL, 10);
158
Doug Zongker74406302011-10-28 15:13:10 -0700159 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700160 } else if (strcmp(command, "set_progress") == 0) {
161 char* fraction_s = strtok(NULL, " \n");
162 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700163 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700164 } else if (strcmp(command, "ui_print") == 0) {
165 char* str = strtok(NULL, "\n");
166 if (str) {
Doug Zongker74406302011-10-28 15:13:10 -0700167 ui->Print("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700168 } else {
Doug Zongker74406302011-10-28 15:13:10 -0700169 ui->Print("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700171 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700172 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700173 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700174 } else if (strcmp(command, "clear_display") == 0) {
175 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700176 } else if (strcmp(command, "enable_reboot") == 0) {
177 // packages can explicitly request that they want the user
178 // to be able to reboot during installation (useful for
179 // debugging packages that don't exit).
180 ui->SetEnableReboot(true);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700181 } else {
182 LOGE("unknown command [%s]\n", command);
183 }
184 }
185 fclose(from_child);
186
187 int status;
188 waitpid(pid, &status, 0);
189 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700191 return INSTALL_ERROR;
192 }
193
Doug Zongkere08991e2010-02-02 13:09:52 -0800194 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700195}
196
Doug Zongker469243e2011-04-12 09:28:10 -0700197static int
Tao Bao145d8612015-03-25 15:51:15 -0700198really_install_package(const char *path, bool* wipe_cache, bool needs_mount)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700200 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700201 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700202 // Give verification half the progress bar...
203 ui->SetProgressType(RecoveryUI::DETERMINATE);
204 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700205 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206
Doug Zongker99916f02014-01-13 14:16:58 -0800207 // Map the update package into memory.
208 ui->Print("Opening update package...\n");
209
Doug Zongker075ad802014-06-26 15:35:51 -0700210 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800211 if (path[0] == '@') {
212 ensure_path_mounted(path+1);
213 } else {
214 ensure_path_mounted(path);
215 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 }
217
Doug Zongker99916f02014-01-13 14:16:58 -0800218 MemMapping map;
219 if (sysMapFile(path, &map) != 0) {
220 LOGE("failed to map file\n");
221 return INSTALL_CORRUPT;
222 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700224 int numKeys;
Doug Zongkerbac7fba2013-04-10 11:32:17 -0700225 Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700226 if (loadedKeys == NULL) {
227 LOGE("Failed to load keys\n");
228 return INSTALL_CORRUPT;
229 }
230 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
231
Doug Zongker74406302011-10-28 15:13:10 -0700232 ui->Print("Verifying update package...\n");
Doug Zongker60151a22009-08-12 18:30:03 -0700233
234 int err;
Matt Mower2b18a532015-02-20 16:58:05 -0600235 err = verify_file(map.addr, map.length);
Doug Zongker60151a22009-08-12 18:30:03 -0700236 free(loadedKeys);
237 LOGI("verify_file returned %d\n", err);
238 if (err != VERIFY_SUCCESS) {
239 LOGE("signature verification failed\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800240 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700241 return INSTALL_CORRUPT;
242 }
243
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800244 /* Try to open the package.
245 */
246 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800247 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 if (err != 0) {
249 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Doug Zongker99916f02014-01-13 14:16:58 -0800250 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800251 return INSTALL_CORRUPT;
252 }
253
254 /* Verify and install the contents of the package.
255 */
Doug Zongker74406302011-10-28 15:13:10 -0700256 ui->Print("Installing update...\n");
Doug Zongkerc704e062014-05-23 08:40:35 -0700257 ui->SetEnableReboot(false);
Doug Zongker99916f02014-01-13 14:16:58 -0800258 int result = try_update_binary(path, &zip, wipe_cache);
Doug Zongkerc704e062014-05-23 08:40:35 -0700259 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700260 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800261
262 sysReleaseMap(&map);
263
264 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265}
Doug Zongker469243e2011-04-12 09:28:10 -0700266
267int
Tao Bao145d8612015-03-25 15:51:15 -0700268install_package(const char* path, bool* wipe_cache, const char* install_file,
Doug Zongker075ad802014-06-26 15:35:51 -0700269 bool needs_mount)
Doug Zongker469243e2011-04-12 09:28:10 -0700270{
Tao Bao682c34b2015-04-07 17:16:35 -0700271 modified_flash = true;
272
Doug Zongkerd0181b82011-10-19 10:51:12 -0700273 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700274 if (install_log) {
275 fputs(path, install_log);
276 fputc('\n', install_log);
277 } else {
278 LOGE("failed to open last_install: %s\n", strerror(errno));
279 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700280 int result;
281 if (setup_install_mounts() != 0) {
282 LOGE("failed to set up expected mounts for install; aborting\n");
283 result = INSTALL_ERROR;
284 } else {
Doug Zongker075ad802014-06-26 15:35:51 -0700285 result = really_install_package(path, wipe_cache, needs_mount);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700286 }
Doug Zongker469243e2011-04-12 09:28:10 -0700287 if (install_log) {
288 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
289 fputc('\n', install_log);
290 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700291 }
292 return result;
293}