blob: 190d8525b7cd2d6ce18db8f16efaf79c4e670746 [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>
Tianjie Xudd874b12016-05-13 12:13:15 -070027#include <string>
Tao Bao71e3e092016-02-02 14:02:27 -080028#include <vector>
29
Tianjie Xu16255832016-04-30 11:49:59 -070030#include <android-base/stringprintf.h>
31#include <android-base/strings.h>
32
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070034#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#include "minui/minui.h"
37#include "minzip/SysUtil.h"
38#include "minzip/Zip.h"
39#include "mtdutils/mounts.h"
40#include "mtdutils/mtdutils.h"
41#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070042#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020043#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Doug Zongker74406302011-10-28 15:13:10 -070045extern RecoveryUI* ui;
46
Doug Zongkerb2ee9202009-06-04 10:24:53 -070047#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070048#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
Doug Zongker74406302011-10-28 15:13:10 -070050// Default allocation of progress bar segments to operations
51static const int VERIFICATION_PROGRESS_TIME = 60;
52static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
53static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
54static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
55
Doug Zongkerb2ee9202009-06-04 10:24:53 -070056// If the package contains an update binary, extract it and run it.
57static int
Tianjie Xudd874b12016-05-13 12:13:15 -070058try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
Tianjie Xu7ce287d2016-05-31 09:29:49 -070059 std::vector<std::string>& log_buffer, int retry_count)
Tianjie Xudd874b12016-05-13 12:13:15 -070060{
Doug Zongkerb2ee9202009-06-04 10:24:53 -070061 const ZipEntry* binary_entry =
62 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
63 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070064 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070065 return INSTALL_CORRUPT;
66 }
67
Doug Zongker10e418d2011-10-28 10:33:05 -070068 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -070069 unlink(binary);
70 int fd = creat(binary, 0755);
71 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070072 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070073 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -070074 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070075 }
76 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
77 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070078 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070079
80 if (!ok) {
81 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -070082 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070083 }
84
85 int pipefd[2];
86 pipe(pipefd);
87
88 // When executing the update binary contained in the package, the
89 // arguments passed are:
90 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070091 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070092 //
93 // - an fd to which the program can write in order to update the
94 // progress bar. The program can write single-line commands:
95 //
96 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070097 // fill up the next <frac> part of of the progress bar
98 // over <secs> seconds. If <secs> is zero, use
99 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700100 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700101 //
102 // set_progress <frac>
103 // <frac> should be between 0.0 and 1.0; sets the
104 // progress bar within the segment defined by the most
105 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700106 //
107 // firmware <"hboot"|"radio"> <filename>
108 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800109 // given partition on reboot.
110 //
111 // (API v2: <filename> may start with "PACKAGE:" to
112 // indicate taking a file from the OTA package.)
113 //
114 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700115 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700116 // ui_print <string>
117 // display <string> on the screen.
118 //
Tao Baob07e1f32015-04-10 16:14:52 -0700119 // wipe_cache
120 // a wipe of cache will be performed following a successful
121 // installation.
122 //
123 // clear_display
124 // turn off the text display.
125 //
126 // enable_reboot
127 // packages can explicitly request that they want the user
128 // to be able to reboot during installation (useful for
129 // debugging packages that don't exit).
130 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 // - the name of the package zip file.
132 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700133 // - an optional argument "retry" if this update is a retry of a failed
134 // update attempt.
135 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700136
Tianjie Xu64f46fb2016-06-03 15:44:52 -0700137 const char* args[6];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700138 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700139 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Yabin Cui4425c1d2016-02-10 13:47:32 -0800140 char temp[16];
141 snprintf(temp, sizeof(temp), "%d", pipefd[1]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700142 args[2] = temp;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800143 args[3] = path;
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700144 args[4] = retry_count > 0 ? "retry" : NULL;
145 args[5] = NULL;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700146
147 pid_t pid = fork();
148 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700149 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700150 close(pipefd[0]);
Yabin Cui4425c1d2016-02-10 13:47:32 -0800151 execv(binary, const_cast<char**>(args));
Doug Zongker56c51052010-07-01 09:18:44 -0700152 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700153 _exit(-1);
154 }
155 close(pipefd[1]);
156
Tao Bao145d8612015-03-25 15:51:15 -0700157 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800158 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700159
Doug Zongker64893cc2009-07-14 16:31:56 -0700160 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700161 FILE* from_child = fdopen(pipefd[0], "r");
162 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700163 char* command = strtok(buffer, " \n");
164 if (command == NULL) {
165 continue;
166 } else if (strcmp(command, "progress") == 0) {
167 char* fraction_s = strtok(NULL, " \n");
168 char* seconds_s = strtok(NULL, " \n");
169
170 float fraction = strtof(fraction_s, NULL);
171 int seconds = strtol(seconds_s, NULL, 10);
172
Doug Zongker74406302011-10-28 15:13:10 -0700173 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700174 } else if (strcmp(command, "set_progress") == 0) {
175 char* fraction_s = strtok(NULL, " \n");
176 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700177 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700178 } else if (strcmp(command, "ui_print") == 0) {
179 char* str = strtok(NULL, "\n");
180 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700181 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700182 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700183 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700184 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700185 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700186 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700187 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700188 } else if (strcmp(command, "clear_display") == 0) {
189 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700190 } else if (strcmp(command, "enable_reboot") == 0) {
191 // packages can explicitly request that they want the user
192 // to be able to reboot during installation (useful for
193 // debugging packages that don't exit).
194 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800195 } else if (strcmp(command, "retry_update") == 0) {
196 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700197 } else if (strcmp(command, "log") == 0) {
198 // Save the logging request from updater and write to
199 // last_install later.
200 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700201 } else {
202 LOGE("unknown command [%s]\n", command);
203 }
204 }
205 fclose(from_child);
206
207 int status;
208 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800209 if (retry_update) {
210 return INSTALL_RETRY;
211 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700212 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700213 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700214 return INSTALL_ERROR;
215 }
216
Doug Zongkere08991e2010-02-02 13:09:52 -0800217 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700218}
219
Doug Zongker469243e2011-04-12 09:28:10 -0700220static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700221really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700222 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700224 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700225 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700226 // Give verification half the progress bar...
227 ui->SetProgressType(RecoveryUI::DETERMINATE);
228 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700229 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800230
Doug Zongker99916f02014-01-13 14:16:58 -0800231 // Map the update package into memory.
232 ui->Print("Opening update package...\n");
233
Doug Zongker075ad802014-06-26 15:35:51 -0700234 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800235 if (path[0] == '@') {
236 ensure_path_mounted(path+1);
237 } else {
238 ensure_path_mounted(path);
239 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800240 }
241
Doug Zongker99916f02014-01-13 14:16:58 -0800242 MemMapping map;
243 if (sysMapFile(path, &map) != 0) {
244 LOGE("failed to map file\n");
245 return INSTALL_CORRUPT;
246 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800247
Elliott Hughes8febafa2016-04-13 16:39:56 -0700248 // Load keys.
Tao Bao71e3e092016-02-02 14:02:27 -0800249 std::vector<Certificate> loadedKeys;
250 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700251 LOGE("Failed to load keys\n");
252 return INSTALL_CORRUPT;
253 }
Tao Bao71e3e092016-02-02 14:02:27 -0800254 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700255
Elliott Hughes8febafa2016-04-13 16:39:56 -0700256 // Verify package.
Doug Zongker74406302011-10-28 15:13:10 -0700257 ui->Print("Verifying update package...\n");
Elliott Hughes8febafa2016-04-13 16:39:56 -0700258 auto t0 = std::chrono::system_clock::now();
Tao Bao71e3e092016-02-02 14:02:27 -0800259 int err = verify_file(map.addr, map.length, loadedKeys);
Elliott Hughes8febafa2016-04-13 16:39:56 -0700260 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
261 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
Doug Zongker60151a22009-08-12 18:30:03 -0700262 if (err != VERIFY_SUCCESS) {
263 LOGE("signature verification failed\n");
Tianjie Xu16255832016-04-30 11:49:59 -0700264 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
265
Doug Zongker99916f02014-01-13 14:16:58 -0800266 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700267 return INSTALL_CORRUPT;
268 }
269
Elliott Hughes8febafa2016-04-13 16:39:56 -0700270 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800272 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800273 if (err != 0) {
274 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Tianjie Xu16255832016-04-30 11:49:59 -0700275 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
276
Doug Zongker99916f02014-01-13 14:16:58 -0800277 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800278 return INSTALL_CORRUPT;
279 }
280
Elliott Hughes8febafa2016-04-13 16:39:56 -0700281 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700282 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700283 if (retry_count > 0) {
284 ui->Print("Retry attempt: %d\n", retry_count);
285 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700286 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700287 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700288 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700289 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800290
291 sysReleaseMap(&map);
292
293 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294}
Doug Zongker469243e2011-04-12 09:28:10 -0700295
296int
Tao Bao145d8612015-03-25 15:51:15 -0700297install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700298 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700299{
Tao Bao682c34b2015-04-07 17:16:35 -0700300 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700301 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700302
Doug Zongkerd0181b82011-10-19 10:51:12 -0700303 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700304 if (install_log) {
305 fputs(path, install_log);
306 fputc('\n', install_log);
307 } else {
308 LOGE("failed to open last_install: %s\n", strerror(errno));
309 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700310 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700311 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700312 if (setup_install_mounts() != 0) {
313 LOGE("failed to set up expected mounts for install; aborting\n");
314 result = INSTALL_ERROR;
315 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700316 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700317 }
Tianjie Xu16255832016-04-30 11:49:59 -0700318 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700319 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
320 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700321 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
322 int count = static_cast<int>(duration.count());
323 // Report the time spent to apply OTA update in seconds.
324 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700325 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700326
327 for (const auto& s : log_buffer) {
328 fprintf(install_log, "%s\n", s.c_str());
329 }
330
Doug Zongker469243e2011-04-12 09:28:10 -0700331 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700332 }
333 return result;
334}