blob: 0f9088a7a17b55270b7868f06da72547a9aa1cea [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 Xufe16b5c2016-09-09 10:55:44 -070030#include <android-base/file.h>
31#include <android-base/logging.h>
Tianjie Xub0ddae52016-06-08 14:30:04 -070032#include <android-base/parseint.h>
Tianjie Xu16255832016-04-30 11:49:59 -070033#include <android-base/stringprintf.h>
34#include <android-base/strings.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070035#include <ziparchive/zip_archive.h>
Tianjie Xu16255832016-04-30 11:49:59 -070036
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070038#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "minui/minui.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070041#include "otautil/SysUtil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070043#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020044#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Doug Zongker74406302011-10-28 15:13:10 -070046extern RecoveryUI* ui;
47
Doug Zongkerb2ee9202009-06-04 10:24:53 -070048#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070049#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070050static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
Tianjie Xufe16b5c2016-09-09 10:55:44 -070051static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052
Doug Zongker74406302011-10-28 15:13:10 -070053// Default allocation of progress bar segments to operations
54static const int VERIFICATION_PROGRESS_TIME = 60;
55static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
56static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
57static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
58
Tianjie Xub0ddae52016-06-08 14:30:04 -070059// This function parses and returns the build.version.incremental
Chih-Hung Hsieh8b238112016-08-26 14:54:29 -070060static int parse_build_number(const std::string& str) {
61 size_t pos = str.find('=');
Tianjie Xub0ddae52016-06-08 14:30:04 -070062 if (pos != std::string::npos) {
63 std::string num_string = android::base::Trim(str.substr(pos+1));
64 int build_number;
65 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
66 return build_number;
67 }
68 }
69
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070070 LOG(ERROR) << "Failed to parse build number in " << str;
Tianjie Xub0ddae52016-06-08 14:30:04 -070071 return -1;
72}
73
Yabin Cuifd99a312016-06-09 14:09:39 -070074bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070075 ZipString metadata_path(METADATA_PATH);
76 ZipEntry meta_entry;
Yabin Cuifd99a312016-06-09 14:09:39 -070077 if (meta_data == nullptr) {
78 LOG(ERROR) << "string* meta_data can't be nullptr";
79 return false;
80 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070081 if (FindEntry(zip, metadata_path, &meta_entry) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070082 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Yabin Cuifd99a312016-06-09 14:09:39 -070083 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070084 }
85
Yabin Cuifd99a312016-06-09 14:09:39 -070086 meta_data->resize(meta_entry.uncompressed_length, '\0');
87 if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]),
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070088 meta_entry.uncompressed_length) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070089 LOG(ERROR) << "Failed to read metadata in update package";
Yabin Cuifd99a312016-06-09 14:09:39 -070090 return false;
91 }
92 return true;
93}
94
95// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
96static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) {
97 std::string meta_data;
98 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070099 return;
100 }
Tianjie Xub0ddae52016-06-08 14:30:04 -0700101 // Examples of the pre-build and post-build strings in metadata:
102 // pre-build-incremental=2943039
103 // post-build-incremental=2951741
104 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
105 for (const std::string& line : lines) {
106 std::string str = android::base::Trim(line);
107 if (android::base::StartsWith(str, "pre-build-incremental")){
108 int source_build = parse_build_number(str);
109 if (source_build != -1) {
110 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
111 source_build));
112 }
113 } else if (android::base::StartsWith(str, "post-build-incremental")) {
114 int target_build = parse_build_number(str);
115 if (target_build != -1) {
116 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
117 target_build));
118 }
119 }
120 }
121}
122
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700123// If the package contains an update binary, extract it and run it.
124static int
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700125try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700126 std::vector<std::string>& log_buffer, int retry_count)
Tianjie Xudd874b12016-05-13 12:13:15 -0700127{
Tianjie Xub0ddae52016-06-08 14:30:04 -0700128 read_source_target_build(zip, log_buffer);
129
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700130 ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME);
131 ZipEntry binary_entry;
132 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700133 return INSTALL_CORRUPT;
134 }
135
Doug Zongker10e418d2011-10-28 10:33:05 -0700136 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700137 unlink(binary);
138 int fd = creat(binary, 0755);
139 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700140 PLOG(ERROR) << "Can't make " << binary;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700141 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700142 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700143 int error = ExtractEntryToFile(zip, &binary_entry, fd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700144 close(fd);
145
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700146 if (error != 0) {
147 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME
148 << " : " << ErrorCodeString(error);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700149 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700150 }
151
152 int pipefd[2];
153 pipe(pipefd);
154
155 // When executing the update binary contained in the package, the
156 // arguments passed are:
157 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700158 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700159 //
160 // - an fd to which the program can write in order to update the
161 // progress bar. The program can write single-line commands:
162 //
163 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700164 // fill up the next <frac> part of of the progress bar
165 // over <secs> seconds. If <secs> is zero, use
166 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700167 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700168 //
169 // set_progress <frac>
170 // <frac> should be between 0.0 and 1.0; sets the
171 // progress bar within the segment defined by the most
172 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700173 //
174 // firmware <"hboot"|"radio"> <filename>
175 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800176 // given partition on reboot.
177 //
178 // (API v2: <filename> may start with "PACKAGE:" to
179 // indicate taking a file from the OTA package.)
180 //
181 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700182 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700183 // ui_print <string>
184 // display <string> on the screen.
185 //
Tao Baob07e1f32015-04-10 16:14:52 -0700186 // wipe_cache
187 // a wipe of cache will be performed following a successful
188 // installation.
189 //
190 // clear_display
191 // turn off the text display.
192 //
193 // enable_reboot
194 // packages can explicitly request that they want the user
195 // to be able to reboot during installation (useful for
196 // debugging packages that don't exit).
197 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700198 // - the name of the package zip file.
199 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700200 // - an optional argument "retry" if this update is a retry of a failed
201 // update attempt.
202 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700203
Tianjie Xu64f46fb2016-06-03 15:44:52 -0700204 const char* args[6];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700205 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700206 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Yabin Cui4425c1d2016-02-10 13:47:32 -0800207 char temp[16];
208 snprintf(temp, sizeof(temp), "%d", pipefd[1]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700209 args[2] = temp;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800210 args[3] = path;
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700211 args[4] = retry_count > 0 ? "retry" : NULL;
212 args[5] = NULL;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700213
214 pid_t pid = fork();
215 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700216 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700217 close(pipefd[0]);
Yabin Cui4425c1d2016-02-10 13:47:32 -0800218 execv(binary, const_cast<char**>(args));
Doug Zongker56c51052010-07-01 09:18:44 -0700219 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700220 _exit(-1);
221 }
222 close(pipefd[1]);
223
Tao Bao145d8612015-03-25 15:51:15 -0700224 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800225 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700226
Doug Zongker64893cc2009-07-14 16:31:56 -0700227 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700228 FILE* from_child = fdopen(pipefd[0], "r");
229 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700230 char* command = strtok(buffer, " \n");
231 if (command == NULL) {
232 continue;
233 } else if (strcmp(command, "progress") == 0) {
234 char* fraction_s = strtok(NULL, " \n");
235 char* seconds_s = strtok(NULL, " \n");
236
237 float fraction = strtof(fraction_s, NULL);
238 int seconds = strtol(seconds_s, NULL, 10);
239
Doug Zongker74406302011-10-28 15:13:10 -0700240 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700241 } else if (strcmp(command, "set_progress") == 0) {
242 char* fraction_s = strtok(NULL, " \n");
243 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700244 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700245 } else if (strcmp(command, "ui_print") == 0) {
246 char* str = strtok(NULL, "\n");
247 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700248 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700249 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700250 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700251 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700252 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700253 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700254 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700255 } else if (strcmp(command, "clear_display") == 0) {
256 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700257 } else if (strcmp(command, "enable_reboot") == 0) {
258 // packages can explicitly request that they want the user
259 // to be able to reboot during installation (useful for
260 // debugging packages that don't exit).
261 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800262 } else if (strcmp(command, "retry_update") == 0) {
263 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700264 } else if (strcmp(command, "log") == 0) {
265 // Save the logging request from updater and write to
266 // last_install later.
267 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700268 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700269 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700270 }
271 }
272 fclose(from_child);
273
274 int status;
275 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800276 if (retry_update) {
277 return INSTALL_RETRY;
278 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700279 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700280 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700281 return INSTALL_ERROR;
282 }
283
Doug Zongkere08991e2010-02-02 13:09:52 -0800284 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700285}
286
Doug Zongker469243e2011-04-12 09:28:10 -0700287static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700288really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700289 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700291 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700292 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700293 // Give verification half the progress bar...
294 ui->SetProgressType(RecoveryUI::DETERMINATE);
295 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700296 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800297
Doug Zongker99916f02014-01-13 14:16:58 -0800298 // Map the update package into memory.
299 ui->Print("Opening update package...\n");
300
Doug Zongker075ad802014-06-26 15:35:51 -0700301 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800302 if (path[0] == '@') {
303 ensure_path_mounted(path+1);
304 } else {
305 ensure_path_mounted(path);
306 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307 }
308
Doug Zongker99916f02014-01-13 14:16:58 -0800309 MemMapping map;
310 if (sysMapFile(path, &map) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700311 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800312 return INSTALL_CORRUPT;
313 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800314
Elliott Hughes8febafa2016-04-13 16:39:56 -0700315 // Verify package.
Yabin Cuifd99a312016-06-09 14:09:39 -0700316 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700317 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800318 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700319 return INSTALL_CORRUPT;
320 }
321
Elliott Hughes8febafa2016-04-13 16:39:56 -0700322 // Try to open the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700323 ZipArchiveHandle zip;
Yabin Cuifd99a312016-06-09 14:09:39 -0700324 int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800325 if (err != 0) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700326 LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
Tianjie Xu16255832016-04-30 11:49:59 -0700327 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
328
Doug Zongker99916f02014-01-13 14:16:58 -0800329 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700330 CloseArchive(zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331 return INSTALL_CORRUPT;
332 }
333
Elliott Hughes8febafa2016-04-13 16:39:56 -0700334 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700335 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700336 if (retry_count > 0) {
337 ui->Print("Retry attempt: %d\n", retry_count);
338 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700339 ui->SetEnableReboot(false);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700340 int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700341 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700342 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800343
344 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700345 CloseArchive(zip);
Doug Zongker99916f02014-01-13 14:16:58 -0800346 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347}
Doug Zongker469243e2011-04-12 09:28:10 -0700348
349int
Tao Bao145d8612015-03-25 15:51:15 -0700350install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700351 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700352{
Tao Bao682c34b2015-04-07 17:16:35 -0700353 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700354 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700355
Doug Zongker239ac6a2013-08-20 16:03:25 -0700356 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700357 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700358 if (setup_install_mounts() != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700359 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700360 result = INSTALL_ERROR;
361 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700362 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700363 }
Tianjie Xudd874b12016-05-13 12:13:15 -0700364
Tao Baof4885ad2016-09-26 11:39:14 -0700365 // Measure the time spent to apply OTA update in seconds.
366 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
367 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700368
Tao Baof4885ad2016-09-26 11:39:14 -0700369 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
370 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
371 } else {
372 std::string uncrypt_status;
373 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
374 PLOG(WARNING) << "failed to read uncrypt status";
Tao Baoee9b9552016-10-13 16:04:07 -0700375 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
376 LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700377 } else {
Tao Baof4885ad2016-09-26 11:39:14 -0700378 log_buffer.push_back(android::base::Trim(uncrypt_status));
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700379 }
Doug Zongker469243e2011-04-12 09:28:10 -0700380 }
Tao Baof4885ad2016-09-26 11:39:14 -0700381
382 // The first two lines need to be the package name and install result.
383 std::vector<std::string> log_header = {
384 path,
385 result == INSTALL_SUCCESS ? "1" : "0",
386 "time_total: " + std::to_string(time_total),
387 "retry: " + std::to_string(retry_count),
388 };
389 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
390 android::base::Join(log_buffer, "\n");
391 if (!android::base::WriteStringToFile(log_content, install_file)) {
392 PLOG(ERROR) << "failed to write " << install_file;
393 }
394
395 // Write a copy into last_log.
396 LOG(INFO) << log_content;
397
Doug Zongker469243e2011-04-12 09:28:10 -0700398 return result;
399}
Yabin Cuifd99a312016-06-09 14:09:39 -0700400
401bool verify_package(const unsigned char* package_data, size_t package_size) {
402 std::vector<Certificate> loadedKeys;
403 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
404 LOG(ERROR) << "Failed to load keys";
405 return false;
406 }
407 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
408
409 // Verify package.
410 ui->Print("Verifying update package...\n");
411 auto t0 = std::chrono::system_clock::now();
412 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
413 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
414 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
415 if (err != VERIFY_SUCCESS) {
416 LOG(ERROR) << "Signature verification failed";
417 LOG(ERROR) << "error: " << kZipVerificationFailure;
418 return false;
419 }
420 return true;
421}