blob: 3871271f74c6b2b721f64abd585f22bbc6fa5e80 [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 Zongkerb2ee9202009-06-04 10:24:53 -070046#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070047#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070048static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
Tianjie Xufe16b5c2016-09-09 10:55:44 -070049static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
Doug Zongker74406302011-10-28 15:13:10 -070051// Default allocation of progress bar segments to operations
52static const int VERIFICATION_PROGRESS_TIME = 60;
53static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
54static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
55static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
56
Tianjie Xub0ddae52016-06-08 14:30:04 -070057// This function parses and returns the build.version.incremental
Chih-Hung Hsieh8b238112016-08-26 14:54:29 -070058static int parse_build_number(const std::string& str) {
59 size_t pos = str.find('=');
Tianjie Xub0ddae52016-06-08 14:30:04 -070060 if (pos != std::string::npos) {
61 std::string num_string = android::base::Trim(str.substr(pos+1));
62 int build_number;
63 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
64 return build_number;
65 }
66 }
67
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070068 LOG(ERROR) << "Failed to parse build number in " << str;
Tianjie Xub0ddae52016-06-08 14:30:04 -070069 return -1;
70}
71
Yabin Cuifd99a312016-06-09 14:09:39 -070072bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070073 ZipString metadata_path(METADATA_PATH);
74 ZipEntry meta_entry;
Yabin Cuifd99a312016-06-09 14:09:39 -070075 if (meta_data == nullptr) {
76 LOG(ERROR) << "string* meta_data can't be nullptr";
77 return false;
78 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070079 if (FindEntry(zip, metadata_path, &meta_entry) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070080 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Yabin Cuifd99a312016-06-09 14:09:39 -070081 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070082 }
83
Yabin Cuifd99a312016-06-09 14:09:39 -070084 meta_data->resize(meta_entry.uncompressed_length, '\0');
85 if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]),
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070086 meta_entry.uncompressed_length) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070087 LOG(ERROR) << "Failed to read metadata in update package";
Yabin Cuifd99a312016-06-09 14:09:39 -070088 return false;
89 }
90 return true;
91}
92
93// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
94static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) {
95 std::string meta_data;
96 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070097 return;
98 }
Tianjie Xub0ddae52016-06-08 14:30:04 -070099 // Examples of the pre-build and post-build strings in metadata:
100 // pre-build-incremental=2943039
101 // post-build-incremental=2951741
102 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
103 for (const std::string& line : lines) {
104 std::string str = android::base::Trim(line);
105 if (android::base::StartsWith(str, "pre-build-incremental")){
106 int source_build = parse_build_number(str);
107 if (source_build != -1) {
108 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
109 source_build));
110 }
111 } else if (android::base::StartsWith(str, "post-build-incremental")) {
112 int target_build = parse_build_number(str);
113 if (target_build != -1) {
114 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
115 target_build));
116 }
117 }
118 }
119}
120
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700121// If the package contains an update binary, extract it and run it.
122static int
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700123try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700124 std::vector<std::string>& log_buffer, int retry_count)
Tianjie Xudd874b12016-05-13 12:13:15 -0700125{
Tianjie Xub0ddae52016-06-08 14:30:04 -0700126 read_source_target_build(zip, log_buffer);
127
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700128 ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME);
129 ZipEntry binary_entry;
130 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 return INSTALL_CORRUPT;
132 }
133
Doug Zongker10e418d2011-10-28 10:33:05 -0700134 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700135 unlink(binary);
136 int fd = creat(binary, 0755);
137 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700138 PLOG(ERROR) << "Can't make " << binary;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700139 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700140 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700141 int error = ExtractEntryToFile(zip, &binary_entry, fd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700142 close(fd);
143
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700144 if (error != 0) {
145 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME
146 << " : " << ErrorCodeString(error);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700147 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700148 }
149
150 int pipefd[2];
151 pipe(pipefd);
152
153 // When executing the update binary contained in the package, the
154 // arguments passed are:
155 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700156 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700157 //
158 // - an fd to which the program can write in order to update the
159 // progress bar. The program can write single-line commands:
160 //
161 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700162 // fill up the next <frac> part of of the progress bar
163 // over <secs> seconds. If <secs> is zero, use
164 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700165 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700166 //
167 // set_progress <frac>
168 // <frac> should be between 0.0 and 1.0; sets the
169 // progress bar within the segment defined by the most
170 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700171 //
172 // firmware <"hboot"|"radio"> <filename>
173 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800174 // given partition on reboot.
175 //
176 // (API v2: <filename> may start with "PACKAGE:" to
177 // indicate taking a file from the OTA package.)
178 //
179 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700180 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700181 // ui_print <string>
182 // display <string> on the screen.
183 //
Tao Baob07e1f32015-04-10 16:14:52 -0700184 // wipe_cache
185 // a wipe of cache will be performed following a successful
186 // installation.
187 //
188 // clear_display
189 // turn off the text display.
190 //
191 // enable_reboot
192 // packages can explicitly request that they want the user
193 // to be able to reboot during installation (useful for
194 // debugging packages that don't exit).
195 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700196 // - the name of the package zip file.
197 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700198 // - an optional argument "retry" if this update is a retry of a failed
199 // update attempt.
200 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700201
Tianjie Xu64f46fb2016-06-03 15:44:52 -0700202 const char* args[6];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700203 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700204 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Yabin Cui4425c1d2016-02-10 13:47:32 -0800205 char temp[16];
206 snprintf(temp, sizeof(temp), "%d", pipefd[1]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700207 args[2] = temp;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800208 args[3] = path;
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700209 args[4] = retry_count > 0 ? "retry" : NULL;
210 args[5] = NULL;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700211
212 pid_t pid = fork();
213 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700214 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700215 close(pipefd[0]);
Yabin Cui4425c1d2016-02-10 13:47:32 -0800216 execv(binary, const_cast<char**>(args));
Doug Zongker56c51052010-07-01 09:18:44 -0700217 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700218 _exit(-1);
219 }
220 close(pipefd[1]);
221
Tao Bao145d8612015-03-25 15:51:15 -0700222 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800223 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700224
Doug Zongker64893cc2009-07-14 16:31:56 -0700225 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700226 FILE* from_child = fdopen(pipefd[0], "r");
227 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700228 char* command = strtok(buffer, " \n");
229 if (command == NULL) {
230 continue;
231 } else if (strcmp(command, "progress") == 0) {
232 char* fraction_s = strtok(NULL, " \n");
233 char* seconds_s = strtok(NULL, " \n");
234
235 float fraction = strtof(fraction_s, NULL);
236 int seconds = strtol(seconds_s, NULL, 10);
237
Doug Zongker74406302011-10-28 15:13:10 -0700238 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700239 } else if (strcmp(command, "set_progress") == 0) {
240 char* fraction_s = strtok(NULL, " \n");
241 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700242 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700243 } else if (strcmp(command, "ui_print") == 0) {
244 char* str = strtok(NULL, "\n");
245 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700246 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700247 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700248 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700249 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700250 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700251 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700252 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700253 } else if (strcmp(command, "clear_display") == 0) {
254 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700255 } else if (strcmp(command, "enable_reboot") == 0) {
256 // packages can explicitly request that they want the user
257 // to be able to reboot during installation (useful for
258 // debugging packages that don't exit).
259 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800260 } else if (strcmp(command, "retry_update") == 0) {
261 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700262 } else if (strcmp(command, "log") == 0) {
263 // Save the logging request from updater and write to
264 // last_install later.
265 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700266 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700267 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700268 }
269 }
270 fclose(from_child);
271
272 int status;
273 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800274 if (retry_update) {
275 return INSTALL_RETRY;
276 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700277 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700278 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700279 return INSTALL_ERROR;
280 }
281
Doug Zongkere08991e2010-02-02 13:09:52 -0800282 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700283}
284
Doug Zongker469243e2011-04-12 09:28:10 -0700285static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700286really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700287 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700289 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700290 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700291 // Give verification half the progress bar...
292 ui->SetProgressType(RecoveryUI::DETERMINATE);
293 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700294 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295
Doug Zongker99916f02014-01-13 14:16:58 -0800296 // Map the update package into memory.
297 ui->Print("Opening update package...\n");
298
Doug Zongker075ad802014-06-26 15:35:51 -0700299 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800300 if (path[0] == '@') {
301 ensure_path_mounted(path+1);
302 } else {
303 ensure_path_mounted(path);
304 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305 }
306
Doug Zongker99916f02014-01-13 14:16:58 -0800307 MemMapping map;
308 if (sysMapFile(path, &map) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700309 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800310 return INSTALL_CORRUPT;
311 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312
Elliott Hughes8febafa2016-04-13 16:39:56 -0700313 // Verify package.
Yabin Cuifd99a312016-06-09 14:09:39 -0700314 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700315 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800316 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700317 return INSTALL_CORRUPT;
318 }
319
Elliott Hughes8febafa2016-04-13 16:39:56 -0700320 // Try to open the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700321 ZipArchiveHandle zip;
Yabin Cuifd99a312016-06-09 14:09:39 -0700322 int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800323 if (err != 0) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700324 LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
Tianjie Xu16255832016-04-30 11:49:59 -0700325 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
326
Doug Zongker99916f02014-01-13 14:16:58 -0800327 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700328 CloseArchive(zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800329 return INSTALL_CORRUPT;
330 }
331
Elliott Hughes8febafa2016-04-13 16:39:56 -0700332 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700333 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700334 if (retry_count > 0) {
335 ui->Print("Retry attempt: %d\n", retry_count);
336 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700337 ui->SetEnableReboot(false);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700338 int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700339 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700340 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800341
342 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700343 CloseArchive(zip);
Doug Zongker99916f02014-01-13 14:16:58 -0800344 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345}
Doug Zongker469243e2011-04-12 09:28:10 -0700346
347int
Tao Bao145d8612015-03-25 15:51:15 -0700348install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700349 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700350{
Tao Bao682c34b2015-04-07 17:16:35 -0700351 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700352 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700353
Doug Zongker239ac6a2013-08-20 16:03:25 -0700354 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700355 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700356 if (setup_install_mounts() != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700357 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700358 result = INSTALL_ERROR;
359 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700360 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700361 }
Tianjie Xudd874b12016-05-13 12:13:15 -0700362
Tao Baof4885ad2016-09-26 11:39:14 -0700363 // Measure the time spent to apply OTA update in seconds.
364 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
365 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700366
Tao Baof4885ad2016-09-26 11:39:14 -0700367 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
368 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
369 } else {
370 std::string uncrypt_status;
371 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
372 PLOG(WARNING) << "failed to read uncrypt status";
Tao Baoee9b9552016-10-13 16:04:07 -0700373 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
374 LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700375 } else {
Tao Baof4885ad2016-09-26 11:39:14 -0700376 log_buffer.push_back(android::base::Trim(uncrypt_status));
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700377 }
Doug Zongker469243e2011-04-12 09:28:10 -0700378 }
Tao Baof4885ad2016-09-26 11:39:14 -0700379
380 // The first two lines need to be the package name and install result.
381 std::vector<std::string> log_header = {
382 path,
383 result == INSTALL_SUCCESS ? "1" : "0",
384 "time_total: " + std::to_string(time_total),
385 "retry: " + std::to_string(retry_count),
386 };
387 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
388 android::base::Join(log_buffer, "\n");
389 if (!android::base::WriteStringToFile(log_content, install_file)) {
390 PLOG(ERROR) << "failed to write " << install_file;
391 }
392
393 // Write a copy into last_log.
394 LOG(INFO) << log_content;
395
Doug Zongker469243e2011-04-12 09:28:10 -0700396 return result;
397}
Yabin Cuifd99a312016-06-09 14:09:39 -0700398
399bool verify_package(const unsigned char* package_data, size_t package_size) {
400 std::vector<Certificate> loadedKeys;
401 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
402 LOG(ERROR) << "Failed to load keys";
403 return false;
404 }
405 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
406
407 // Verify package.
408 ui->Print("Verifying update package...\n");
409 auto t0 = std::chrono::system_clock::now();
410 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
411 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
412 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
413 if (err != VERIFY_SUCCESS) {
414 LOG(ERROR) << "Signature verification failed";
415 LOG(ERROR) << "error: " << kZipVerificationFailure;
416 return false;
417 }
418 return true;
419}