blob: a7b59c3e75c94a4a8b2b4bc050d8173027c46497 [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
Tao Bao71e3e092016-02-02 14:02:27 -080026#include <vector>
27
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include "common.h"
29#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include "minui/minui.h"
31#include "minzip/SysUtil.h"
32#include "minzip/Zip.h"
33#include "mtdutils/mounts.h"
34#include "mtdutils/mtdutils.h"
35#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070036#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020037#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Doug Zongker74406302011-10-28 15:13:10 -070039extern RecoveryUI* ui;
40
Doug Zongkerb2ee9202009-06-04 10:24:53 -070041#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070042#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Doug Zongker74406302011-10-28 15:13:10 -070044// Default allocation of progress bar segments to operations
45static const int VERIFICATION_PROGRESS_TIME = 60;
46static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
47static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
48static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
49
Doug Zongkerb2ee9202009-06-04 10:24:53 -070050// If the package contains an update binary, extract it and run it.
51static int
Tao Bao145d8612015-03-25 15:51:15 -070052try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070053 const ZipEntry* binary_entry =
54 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
55 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070056 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070057 return INSTALL_CORRUPT;
58 }
59
Doug Zongker10e418d2011-10-28 10:33:05 -070060 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -070061 unlink(binary);
62 int fd = creat(binary, 0755);
63 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070064 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070065 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -070066 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070067 }
68 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
69 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070070 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070071
72 if (!ok) {
73 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -070074 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070075 }
76
77 int pipefd[2];
78 pipe(pipefd);
79
80 // When executing the update binary contained in the package, the
81 // arguments passed are:
82 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070083 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070084 //
85 // - an fd to which the program can write in order to update the
86 // progress bar. The program can write single-line commands:
87 //
88 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070089 // fill up the next <frac> part of of the progress bar
90 // over <secs> seconds. If <secs> is zero, use
91 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -070092 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -070093 //
94 // set_progress <frac>
95 // <frac> should be between 0.0 and 1.0; sets the
96 // progress bar within the segment defined by the most
97 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -070098 //
99 // firmware <"hboot"|"radio"> <filename>
100 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800101 // given partition on reboot.
102 //
103 // (API v2: <filename> may start with "PACKAGE:" to
104 // indicate taking a file from the OTA package.)
105 //
106 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700107 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700108 // ui_print <string>
109 // display <string> on the screen.
110 //
Tao Baob07e1f32015-04-10 16:14:52 -0700111 // wipe_cache
112 // a wipe of cache will be performed following a successful
113 // installation.
114 //
115 // clear_display
116 // turn off the text display.
117 //
118 // enable_reboot
119 // packages can explicitly request that they want the user
120 // to be able to reboot during installation (useful for
121 // debugging packages that don't exit).
122 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700123 // - the name of the package zip file.
124 //
125
Yabin Cui4425c1d2016-02-10 13:47:32 -0800126 const char* args[5];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700127 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700128 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Yabin Cui4425c1d2016-02-10 13:47:32 -0800129 char temp[16];
130 snprintf(temp, sizeof(temp), "%d", pipefd[1]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700131 args[2] = temp;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800132 args[3] = path;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700133 args[4] = NULL;
134
135 pid_t pid = fork();
136 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700137 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700138 close(pipefd[0]);
Yabin Cui4425c1d2016-02-10 13:47:32 -0800139 execv(binary, const_cast<char**>(args));
Doug Zongker56c51052010-07-01 09:18:44 -0700140 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700141 _exit(-1);
142 }
143 close(pipefd[1]);
144
Tao Bao145d8612015-03-25 15:51:15 -0700145 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800146 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700147
Doug Zongker64893cc2009-07-14 16:31:56 -0700148 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700149 FILE* from_child = fdopen(pipefd[0], "r");
150 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700151 char* command = strtok(buffer, " \n");
152 if (command == NULL) {
153 continue;
154 } else if (strcmp(command, "progress") == 0) {
155 char* fraction_s = strtok(NULL, " \n");
156 char* seconds_s = strtok(NULL, " \n");
157
158 float fraction = strtof(fraction_s, NULL);
159 int seconds = strtol(seconds_s, NULL, 10);
160
Doug Zongker74406302011-10-28 15:13:10 -0700161 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700162 } else if (strcmp(command, "set_progress") == 0) {
163 char* fraction_s = strtok(NULL, " \n");
164 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700165 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700166 } else if (strcmp(command, "ui_print") == 0) {
167 char* str = strtok(NULL, "\n");
168 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700169 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700171 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700173 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700174 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700175 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700176 } else if (strcmp(command, "clear_display") == 0) {
177 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700178 } else if (strcmp(command, "enable_reboot") == 0) {
179 // packages can explicitly request that they want the user
180 // to be able to reboot during installation (useful for
181 // debugging packages that don't exit).
182 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800183 } else if (strcmp(command, "retry_update") == 0) {
184 retry_update = true;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700185 } else {
186 LOGE("unknown command [%s]\n", command);
187 }
188 }
189 fclose(from_child);
190
191 int status;
192 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800193 if (retry_update) {
194 return INSTALL_RETRY;
195 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700196 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700197 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700198 return INSTALL_ERROR;
199 }
200
Doug Zongkere08991e2010-02-02 13:09:52 -0800201 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700202}
203
Doug Zongker469243e2011-04-12 09:28:10 -0700204static int
Tao Bao145d8612015-03-25 15:51:15 -0700205really_install_package(const char *path, bool* wipe_cache, bool needs_mount)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700207 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700208 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700209 // Give verification half the progress bar...
210 ui->SetProgressType(RecoveryUI::DETERMINATE);
211 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700212 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800213
Doug Zongker99916f02014-01-13 14:16:58 -0800214 // Map the update package into memory.
215 ui->Print("Opening update package...\n");
216
Doug Zongker075ad802014-06-26 15:35:51 -0700217 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800218 if (path[0] == '@') {
219 ensure_path_mounted(path+1);
220 } else {
221 ensure_path_mounted(path);
222 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223 }
224
Doug Zongker99916f02014-01-13 14:16:58 -0800225 MemMapping map;
226 if (sysMapFile(path, &map) != 0) {
227 LOGE("failed to map file\n");
228 return INSTALL_CORRUPT;
229 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800230
Tao Bao71e3e092016-02-02 14:02:27 -0800231 std::vector<Certificate> loadedKeys;
232 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700233 LOGE("Failed to load keys\n");
234 return INSTALL_CORRUPT;
235 }
Tao Bao71e3e092016-02-02 14:02:27 -0800236 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700237
Doug Zongker74406302011-10-28 15:13:10 -0700238 ui->Print("Verifying update package...\n");
Doug Zongker60151a22009-08-12 18:30:03 -0700239
Tao Bao71e3e092016-02-02 14:02:27 -0800240 int err = verify_file(map.addr, map.length, loadedKeys);
Doug Zongker60151a22009-08-12 18:30:03 -0700241 LOGI("verify_file returned %d\n", err);
242 if (err != VERIFY_SUCCESS) {
243 LOGE("signature verification failed\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800244 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700245 return INSTALL_CORRUPT;
246 }
247
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 /* Try to open the package.
249 */
250 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800251 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800252 if (err != 0) {
253 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Doug Zongker99916f02014-01-13 14:16:58 -0800254 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 return INSTALL_CORRUPT;
256 }
257
258 /* Verify and install the contents of the package.
259 */
Doug Zongker74406302011-10-28 15:13:10 -0700260 ui->Print("Installing update...\n");
Doug Zongkerc704e062014-05-23 08:40:35 -0700261 ui->SetEnableReboot(false);
Doug Zongker99916f02014-01-13 14:16:58 -0800262 int result = try_update_binary(path, &zip, wipe_cache);
Doug Zongkerc704e062014-05-23 08:40:35 -0700263 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700264 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800265
266 sysReleaseMap(&map);
267
268 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269}
Doug Zongker469243e2011-04-12 09:28:10 -0700270
271int
Tao Bao145d8612015-03-25 15:51:15 -0700272install_package(const char* path, bool* wipe_cache, const char* install_file,
Doug Zongker075ad802014-06-26 15:35:51 -0700273 bool needs_mount)
Doug Zongker469243e2011-04-12 09:28:10 -0700274{
Tao Bao682c34b2015-04-07 17:16:35 -0700275 modified_flash = true;
276
Doug Zongkerd0181b82011-10-19 10:51:12 -0700277 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700278 if (install_log) {
279 fputs(path, install_log);
280 fputc('\n', install_log);
281 } else {
282 LOGE("failed to open last_install: %s\n", strerror(errno));
283 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700284 int result;
285 if (setup_install_mounts() != 0) {
286 LOGE("failed to set up expected mounts for install; aborting\n");
287 result = INSTALL_ERROR;
288 } else {
Doug Zongker075ad802014-06-26 15:35:51 -0700289 result = really_install_package(path, wipe_cache, needs_mount);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700290 }
Doug Zongker469243e2011-04-12 09:28:10 -0700291 if (install_log) {
292 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
293 fputc('\n', install_log);
294 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700295 }
296 return result;
297}