blob: 7113fa286f1016d93d1949de9a9a4e8329e1afd8 [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
Elliott Hughes8febafa2016-04-13 16:39:56 -070026#include <chrono>
Tao Bao71e3e092016-02-02 14:02:27 -080027#include <vector>
28
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include "common.h"
30#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031#include "minui/minui.h"
32#include "minzip/SysUtil.h"
33#include "minzip/Zip.h"
34#include "mtdutils/mounts.h"
35#include "mtdutils/mtdutils.h"
36#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070037#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020038#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Doug Zongker74406302011-10-28 15:13:10 -070040extern RecoveryUI* ui;
41
Doug Zongkerb2ee9202009-06-04 10:24:53 -070042#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070043#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Doug Zongker74406302011-10-28 15:13:10 -070045// Default allocation of progress bar segments to operations
46static const int VERIFICATION_PROGRESS_TIME = 60;
47static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
48static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
49static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
50
Doug Zongkerb2ee9202009-06-04 10:24:53 -070051// If the package contains an update binary, extract it and run it.
52static int
Tao Bao145d8612015-03-25 15:51:15 -070053try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -070054 const ZipEntry* binary_entry =
55 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
56 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070057 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070058 return INSTALL_CORRUPT;
59 }
60
Doug Zongker10e418d2011-10-28 10:33:05 -070061 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -070062 unlink(binary);
63 int fd = creat(binary, 0755);
64 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070065 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070066 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -070067 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070068 }
69 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
70 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070071 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070072
73 if (!ok) {
74 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -070075 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070076 }
77
78 int pipefd[2];
79 pipe(pipefd);
80
81 // When executing the update binary contained in the package, the
82 // arguments passed are:
83 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070084 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070085 //
86 // - an fd to which the program can write in order to update the
87 // progress bar. The program can write single-line commands:
88 //
89 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070090 // fill up the next <frac> part of of the progress bar
91 // over <secs> seconds. If <secs> is zero, use
92 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -070093 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -070094 //
95 // set_progress <frac>
96 // <frac> should be between 0.0 and 1.0; sets the
97 // progress bar within the segment defined by the most
98 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -070099 //
100 // firmware <"hboot"|"radio"> <filename>
101 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800102 // given partition on reboot.
103 //
104 // (API v2: <filename> may start with "PACKAGE:" to
105 // indicate taking a file from the OTA package.)
106 //
107 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700108 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700109 // ui_print <string>
110 // display <string> on the screen.
111 //
Tao Baob07e1f32015-04-10 16:14:52 -0700112 // wipe_cache
113 // a wipe of cache will be performed following a successful
114 // installation.
115 //
116 // clear_display
117 // turn off the text display.
118 //
119 // enable_reboot
120 // packages can explicitly request that they want the user
121 // to be able to reboot during installation (useful for
122 // debugging packages that don't exit).
123 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700124 // - the name of the package zip file.
125 //
126
Yabin Cui4425c1d2016-02-10 13:47:32 -0800127 const char* args[5];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700128 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700129 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Yabin Cui4425c1d2016-02-10 13:47:32 -0800130 char temp[16];
131 snprintf(temp, sizeof(temp), "%d", pipefd[1]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700132 args[2] = temp;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800133 args[3] = path;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700134 args[4] = NULL;
135
136 pid_t pid = fork();
137 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700138 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700139 close(pipefd[0]);
Yabin Cui4425c1d2016-02-10 13:47:32 -0800140 execv(binary, const_cast<char**>(args));
Doug Zongker56c51052010-07-01 09:18:44 -0700141 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700142 _exit(-1);
143 }
144 close(pipefd[1]);
145
Tao Bao145d8612015-03-25 15:51:15 -0700146 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800147 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700148
Doug Zongker64893cc2009-07-14 16:31:56 -0700149 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700150 FILE* from_child = fdopen(pipefd[0], "r");
151 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700152 char* command = strtok(buffer, " \n");
153 if (command == NULL) {
154 continue;
155 } else if (strcmp(command, "progress") == 0) {
156 char* fraction_s = strtok(NULL, " \n");
157 char* seconds_s = strtok(NULL, " \n");
158
159 float fraction = strtof(fraction_s, NULL);
160 int seconds = strtol(seconds_s, NULL, 10);
161
Doug Zongker74406302011-10-28 15:13:10 -0700162 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700163 } else if (strcmp(command, "set_progress") == 0) {
164 char* fraction_s = strtok(NULL, " \n");
165 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700166 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700167 } else if (strcmp(command, "ui_print") == 0) {
168 char* str = strtok(NULL, "\n");
169 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700170 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700171 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700172 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700173 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700174 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700175 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700176 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700177 } else if (strcmp(command, "clear_display") == 0) {
178 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700179 } else if (strcmp(command, "enable_reboot") == 0) {
180 // packages can explicitly request that they want the user
181 // to be able to reboot during installation (useful for
182 // debugging packages that don't exit).
183 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800184 } else if (strcmp(command, "retry_update") == 0) {
185 retry_update = true;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700186 } else {
187 LOGE("unknown command [%s]\n", command);
188 }
189 }
190 fclose(from_child);
191
192 int status;
193 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800194 if (retry_update) {
195 return INSTALL_RETRY;
196 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700197 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700198 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700199 return INSTALL_ERROR;
200 }
201
Doug Zongkere08991e2010-02-02 13:09:52 -0800202 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700203}
204
Doug Zongker469243e2011-04-12 09:28:10 -0700205static int
Tao Bao145d8612015-03-25 15:51:15 -0700206really_install_package(const char *path, bool* wipe_cache, bool needs_mount)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700208 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700209 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700210 // Give verification half the progress bar...
211 ui->SetProgressType(RecoveryUI::DETERMINATE);
212 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700213 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800214
Doug Zongker99916f02014-01-13 14:16:58 -0800215 // Map the update package into memory.
216 ui->Print("Opening update package...\n");
217
Doug Zongker075ad802014-06-26 15:35:51 -0700218 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800219 if (path[0] == '@') {
220 ensure_path_mounted(path+1);
221 } else {
222 ensure_path_mounted(path);
223 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224 }
225
Doug Zongker99916f02014-01-13 14:16:58 -0800226 MemMapping map;
227 if (sysMapFile(path, &map) != 0) {
228 LOGE("failed to map file\n");
229 return INSTALL_CORRUPT;
230 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800231
Elliott Hughes8febafa2016-04-13 16:39:56 -0700232 // Load keys.
Tao Bao71e3e092016-02-02 14:02:27 -0800233 std::vector<Certificate> loadedKeys;
234 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700235 LOGE("Failed to load keys\n");
236 return INSTALL_CORRUPT;
237 }
Tao Bao71e3e092016-02-02 14:02:27 -0800238 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700239
Elliott Hughes8febafa2016-04-13 16:39:56 -0700240 // Verify package.
Doug Zongker74406302011-10-28 15:13:10 -0700241 ui->Print("Verifying update package...\n");
Elliott Hughes8febafa2016-04-13 16:39:56 -0700242 auto t0 = std::chrono::system_clock::now();
Tao Bao71e3e092016-02-02 14:02:27 -0800243 int err = verify_file(map.addr, map.length, loadedKeys);
Elliott Hughes8febafa2016-04-13 16:39:56 -0700244 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
245 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
Doug Zongker60151a22009-08-12 18:30:03 -0700246 if (err != VERIFY_SUCCESS) {
247 LOGE("signature verification failed\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800248 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700249 return INSTALL_CORRUPT;
250 }
251
Elliott Hughes8febafa2016-04-13 16:39:56 -0700252 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800253 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800254 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800255 if (err != 0) {
256 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Doug Zongker99916f02014-01-13 14:16:58 -0800257 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800258 return INSTALL_CORRUPT;
259 }
260
Elliott Hughes8febafa2016-04-13 16:39:56 -0700261 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700262 ui->Print("Installing update...\n");
Doug Zongkerc704e062014-05-23 08:40:35 -0700263 ui->SetEnableReboot(false);
Doug Zongker99916f02014-01-13 14:16:58 -0800264 int result = try_update_binary(path, &zip, wipe_cache);
Doug Zongkerc704e062014-05-23 08:40:35 -0700265 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700266 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800267
268 sysReleaseMap(&map);
269
270 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271}
Doug Zongker469243e2011-04-12 09:28:10 -0700272
273int
Tao Bao145d8612015-03-25 15:51:15 -0700274install_package(const char* path, bool* wipe_cache, const char* install_file,
Doug Zongker075ad802014-06-26 15:35:51 -0700275 bool needs_mount)
Doug Zongker469243e2011-04-12 09:28:10 -0700276{
Tao Bao682c34b2015-04-07 17:16:35 -0700277 modified_flash = true;
278
Doug Zongkerd0181b82011-10-19 10:51:12 -0700279 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700280 if (install_log) {
281 fputs(path, install_log);
282 fputc('\n', install_log);
283 } else {
284 LOGE("failed to open last_install: %s\n", strerror(errno));
285 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700286 int result;
287 if (setup_install_mounts() != 0) {
288 LOGE("failed to set up expected mounts for install; aborting\n");
289 result = INSTALL_ERROR;
290 } else {
Doug Zongker075ad802014-06-26 15:35:51 -0700291 result = really_install_package(path, wipe_cache, needs_mount);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700292 }
Doug Zongker469243e2011-04-12 09:28:10 -0700293 if (install_log) {
294 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
295 fputc('\n', install_log);
296 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700297 }
298 return result;
299}