blob: d725c7b8876a16ca6a7648a9f58765b156a5819d [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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include "common.h"
31#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032#include "minui/minui.h"
33#include "minzip/SysUtil.h"
34#include "minzip/Zip.h"
35#include "mtdutils/mounts.h"
36#include "mtdutils/mtdutils.h"
37#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070038#include "ui.h"
Elliott Hughes8febafa2016-04-13 16:39:56 -070039#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040
Doug Zongker74406302011-10-28 15:13:10 -070041extern RecoveryUI* ui;
42
Doug Zongkerb2ee9202009-06-04 10:24:53 -070043#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070044#define PUBLIC_KEYS_FILE "/res/keys"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Doug Zongker74406302011-10-28 15:13:10 -070046// Default allocation of progress bar segments to operations
47static const int VERIFICATION_PROGRESS_TIME = 60;
48static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
49static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
50static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
51
Doug Zongkerb2ee9202009-06-04 10:24:53 -070052// If the package contains an update binary, extract it and run it.
53static int
Tianjie Xudd874b12016-05-13 12:13:15 -070054try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
55 std::vector<std::string>& log_buffer)
56{
Doug Zongkerb2ee9202009-06-04 10:24:53 -070057 const ZipEntry* binary_entry =
58 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
59 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070060 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070061 return INSTALL_CORRUPT;
62 }
63
Doug Zongker10e418d2011-10-28 10:33:05 -070064 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -070065 unlink(binary);
66 int fd = creat(binary, 0755);
67 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -070068 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070069 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -070070 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070071 }
72 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
73 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -070074 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -070075
76 if (!ok) {
77 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -070078 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -070079 }
80
81 int pipefd[2];
82 pipe(pipefd);
83
84 // When executing the update binary contained in the package, the
85 // arguments passed are:
86 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -070087 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -070088 //
89 // - an fd to which the program can write in order to update the
90 // progress bar. The program can write single-line commands:
91 //
92 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070093 // fill up the next <frac> part of of the progress bar
94 // over <secs> seconds. If <secs> is zero, use
95 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -070096 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -070097 //
98 // set_progress <frac>
99 // <frac> should be between 0.0 and 1.0; sets the
100 // progress bar within the segment defined by the most
101 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700102 //
103 // firmware <"hboot"|"radio"> <filename>
104 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800105 // given partition on reboot.
106 //
107 // (API v2: <filename> may start with "PACKAGE:" to
108 // indicate taking a file from the OTA package.)
109 //
110 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700111 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700112 // ui_print <string>
113 // display <string> on the screen.
114 //
Tao Baob07e1f32015-04-10 16:14:52 -0700115 // wipe_cache
116 // a wipe of cache will be performed following a successful
117 // installation.
118 //
119 // clear_display
120 // turn off the text display.
121 //
122 // enable_reboot
123 // packages can explicitly request that they want the user
124 // to be able to reboot during installation (useful for
125 // debugging packages that don't exit).
126 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700127 // - the name of the package zip file.
128 //
129
Doug Zongker10e418d2011-10-28 10:33:05 -0700130 const char** args = (const char**)malloc(sizeof(char*) * 5);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700132 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Doug Zongker10e418d2011-10-28 10:33:05 -0700133 char* temp = (char*)malloc(10);
134 sprintf(temp, "%d", pipefd[1]);
135 args[2] = temp;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700136 args[3] = (char*)path;
137 args[4] = NULL;
138
139 pid_t pid = fork();
140 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700141 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700142 close(pipefd[0]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700143 execv(binary, (char* const*)args);
Doug Zongker56c51052010-07-01 09:18:44 -0700144 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700145 _exit(-1);
146 }
147 close(pipefd[1]);
148
Tao Bao145d8612015-03-25 15:51:15 -0700149 *wipe_cache = false;
Tianjie Xufa12b972016-02-05 18:25:58 -0800150 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700151
Doug Zongker64893cc2009-07-14 16:31:56 -0700152 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700153 FILE* from_child = fdopen(pipefd[0], "r");
154 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700155 char* command = strtok(buffer, " \n");
156 if (command == NULL) {
157 continue;
158 } else if (strcmp(command, "progress") == 0) {
159 char* fraction_s = strtok(NULL, " \n");
160 char* seconds_s = strtok(NULL, " \n");
161
162 float fraction = strtof(fraction_s, NULL);
163 int seconds = strtol(seconds_s, NULL, 10);
164
Doug Zongker74406302011-10-28 15:13:10 -0700165 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700166 } else if (strcmp(command, "set_progress") == 0) {
167 char* fraction_s = strtok(NULL, " \n");
168 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700169 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 } else if (strcmp(command, "ui_print") == 0) {
171 char* str = strtok(NULL, "\n");
172 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700173 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700174 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700175 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700176 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700177 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700178 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700179 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700180 } else if (strcmp(command, "clear_display") == 0) {
181 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700182 } else if (strcmp(command, "enable_reboot") == 0) {
183 // packages can explicitly request that they want the user
184 // to be able to reboot during installation (useful for
185 // debugging packages that don't exit).
186 ui->SetEnableReboot(true);
Tianjie Xufa12b972016-02-05 18:25:58 -0800187 } else if (strcmp(command, "retry_update") == 0) {
188 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700189 } else if (strcmp(command, "log") == 0) {
190 // Save the logging request from updater and write to
191 // last_install later.
192 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700193 } else {
194 LOGE("unknown command [%s]\n", command);
195 }
196 }
197 fclose(from_child);
198
199 int status;
200 waitpid(pid, &status, 0);
Tianjie Xufa12b972016-02-05 18:25:58 -0800201 if (retry_update) {
202 return INSTALL_RETRY;
203 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700204 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700206 return INSTALL_ERROR;
207 }
208
Doug Zongkere08991e2010-02-02 13:09:52 -0800209 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700210}
211
Doug Zongker469243e2011-04-12 09:28:10 -0700212static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700213really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
214 std::vector<std::string>& log_buffer)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700216 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700217 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700218 // Give verification half the progress bar...
219 ui->SetProgressType(RecoveryUI::DETERMINATE);
220 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700221 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222
Doug Zongker99916f02014-01-13 14:16:58 -0800223 // Map the update package into memory.
224 ui->Print("Opening update package...\n");
225
Doug Zongker075ad802014-06-26 15:35:51 -0700226 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800227 if (path[0] == '@') {
228 ensure_path_mounted(path+1);
229 } else {
230 ensure_path_mounted(path);
231 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800232 }
233
Doug Zongker99916f02014-01-13 14:16:58 -0800234 MemMapping map;
235 if (sysMapFile(path, &map) != 0) {
236 LOGE("failed to map file\n");
237 return INSTALL_CORRUPT;
238 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800239
Elliott Hughes8febafa2016-04-13 16:39:56 -0700240 // Load keys.
Tao Bao71e3e092016-02-02 14:02:27 -0800241 std::vector<Certificate> loadedKeys;
242 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700243 LOGE("Failed to load keys\n");
244 return INSTALL_CORRUPT;
245 }
Tao Bao71e3e092016-02-02 14:02:27 -0800246 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700247
Elliott Hughes8febafa2016-04-13 16:39:56 -0700248 // Verify package.
Doug Zongker74406302011-10-28 15:13:10 -0700249 ui->Print("Verifying update package...\n");
Elliott Hughes8febafa2016-04-13 16:39:56 -0700250 auto t0 = std::chrono::system_clock::now();
Tao Bao71e3e092016-02-02 14:02:27 -0800251 int err = verify_file(map.addr, map.length, loadedKeys);
Elliott Hughes8febafa2016-04-13 16:39:56 -0700252 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
253 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
Doug Zongker60151a22009-08-12 18:30:03 -0700254 if (err != VERIFY_SUCCESS) {
255 LOGE("signature verification failed\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800256 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700257 return INSTALL_CORRUPT;
258 }
259
Elliott Hughes8febafa2016-04-13 16:39:56 -0700260 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800262 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 if (err != 0) {
264 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Doug Zongker99916f02014-01-13 14:16:58 -0800265 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800266 return INSTALL_CORRUPT;
267 }
268
Elliott Hughes8febafa2016-04-13 16:39:56 -0700269 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700270 ui->Print("Installing update...\n");
Doug Zongkerc704e062014-05-23 08:40:35 -0700271 ui->SetEnableReboot(false);
Tianjie Xudd874b12016-05-13 12:13:15 -0700272 int result = try_update_binary(path, &zip, wipe_cache, log_buffer);
Doug Zongkerc704e062014-05-23 08:40:35 -0700273 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700274 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800275
276 sysReleaseMap(&map);
277
278 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279}
Doug Zongker469243e2011-04-12 09:28:10 -0700280
281int
Tao Bao145d8612015-03-25 15:51:15 -0700282install_package(const char* path, bool* wipe_cache, const char* install_file,
Doug Zongker075ad802014-06-26 15:35:51 -0700283 bool needs_mount)
Doug Zongker469243e2011-04-12 09:28:10 -0700284{
Tao Bao682c34b2015-04-07 17:16:35 -0700285 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700286 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700287
Doug Zongkerd0181b82011-10-19 10:51:12 -0700288 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700289 if (install_log) {
290 fputs(path, install_log);
291 fputc('\n', install_log);
292 } else {
293 LOGE("failed to open last_install: %s\n", strerror(errno));
294 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700295 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700296 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700297 if (setup_install_mounts() != 0) {
298 LOGE("failed to set up expected mounts for install; aborting\n");
299 result = INSTALL_ERROR;
300 } else {
Tianjie Xudd874b12016-05-13 12:13:15 -0700301 result = really_install_package(path, wipe_cache, needs_mount, log_buffer);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700302 }
Doug Zongker469243e2011-04-12 09:28:10 -0700303 if (install_log) {
304 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
305 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700306 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
307 int count = static_cast<int>(duration.count());
308 // Report the time spent to apply OTA update in seconds.
309 fprintf(install_log, "time_total: %d\n", count);
310
311 for (const auto& s : log_buffer) {
312 fprintf(install_log, "%s\n", s.c_str());
313 }
314
Doug Zongker469243e2011-04-12 09:28:10 -0700315 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700316 }
317 return result;
318}