blob: c98715231cf6501f253b6f72e66089c284cc94b7 [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
74// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070075static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) {
76 ZipString metadata_path(METADATA_PATH);
77 ZipEntry meta_entry;
78 if (FindEntry(zip, metadata_path, &meta_entry) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070079 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Tianjie Xub0ddae52016-06-08 14:30:04 -070080 return;
81 }
82
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070083 std::string meta_data(meta_entry.uncompressed_length, '\0');
84 if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&meta_data[0]),
85 meta_entry.uncompressed_length) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070086 LOG(ERROR) << "Failed to read metadata in update package";
Tianjie Xub0ddae52016-06-08 14:30:04 -070087 return;
88 }
89
90 // Examples of the pre-build and post-build strings in metadata:
91 // pre-build-incremental=2943039
92 // post-build-incremental=2951741
93 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
94 for (const std::string& line : lines) {
95 std::string str = android::base::Trim(line);
96 if (android::base::StartsWith(str, "pre-build-incremental")){
97 int source_build = parse_build_number(str);
98 if (source_build != -1) {
99 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
100 source_build));
101 }
102 } else if (android::base::StartsWith(str, "post-build-incremental")) {
103 int target_build = parse_build_number(str);
104 if (target_build != -1) {
105 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
106 target_build));
107 }
108 }
109 }
110}
111
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700112// If the package contains an update binary, extract it and run it.
113static int
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700114try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700115 std::vector<std::string>& log_buffer, int retry_count)
Tianjie Xudd874b12016-05-13 12:13:15 -0700116{
Tianjie Xub0ddae52016-06-08 14:30:04 -0700117 read_source_target_build(zip, log_buffer);
118
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700119 ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME);
120 ZipEntry binary_entry;
121 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700122 return INSTALL_CORRUPT;
123 }
124
Doug Zongker10e418d2011-10-28 10:33:05 -0700125 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700126 unlink(binary);
127 int fd = creat(binary, 0755);
128 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700129 PLOG(ERROR) << "Can't make " << binary;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700130 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700132 int error = ExtractEntryToFile(zip, &binary_entry, fd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700133 close(fd);
134
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700135 if (error != 0) {
136 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME
137 << " : " << ErrorCodeString(error);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700138 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700139 }
140
141 int pipefd[2];
142 pipe(pipefd);
143
144 // When executing the update binary contained in the package, the
145 // arguments passed are:
146 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700147 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700148 //
149 // - an fd to which the program can write in order to update the
150 // progress bar. The program can write single-line commands:
151 //
152 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700153 // fill up the next <frac> part of of the progress bar
154 // over <secs> seconds. If <secs> is zero, use
155 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700156 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700157 //
158 // set_progress <frac>
159 // <frac> should be between 0.0 and 1.0; sets the
160 // progress bar within the segment defined by the most
161 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700162 //
163 // firmware <"hboot"|"radio"> <filename>
164 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800165 // given partition on reboot.
166 //
167 // (API v2: <filename> may start with "PACKAGE:" to
168 // indicate taking a file from the OTA package.)
169 //
170 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700171 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172 // ui_print <string>
173 // display <string> on the screen.
174 //
Tao Baob07e1f32015-04-10 16:14:52 -0700175 // wipe_cache
176 // a wipe of cache will be performed following a successful
177 // installation.
178 //
179 // clear_display
180 // turn off the text display.
181 //
182 // enable_reboot
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 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700187 // - the name of the package zip file.
188 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700189 // - an optional argument "retry" if this update is a retry of a failed
190 // update attempt.
191 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700192
Tianjie Xu64f46fb2016-06-03 15:44:52 -0700193 const char* args[6];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700194 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700195 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Yabin Cui4425c1d2016-02-10 13:47:32 -0800196 char temp[16];
197 snprintf(temp, sizeof(temp), "%d", pipefd[1]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700198 args[2] = temp;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800199 args[3] = path;
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700200 args[4] = retry_count > 0 ? "retry" : NULL;
201 args[5] = NULL;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700202
203 pid_t pid = fork();
204 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700205 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700206 close(pipefd[0]);
Yabin Cui4425c1d2016-02-10 13:47:32 -0800207 execv(binary, const_cast<char**>(args));
Doug Zongker56c51052010-07-01 09:18:44 -0700208 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700209 _exit(-1);
210 }
211 close(pipefd[1]);
212
Tao Bao145d8612015-03-25 15:51:15 -0700213 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800214 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700215
Doug Zongker64893cc2009-07-14 16:31:56 -0700216 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700217 FILE* from_child = fdopen(pipefd[0], "r");
218 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700219 char* command = strtok(buffer, " \n");
220 if (command == NULL) {
221 continue;
222 } else if (strcmp(command, "progress") == 0) {
223 char* fraction_s = strtok(NULL, " \n");
224 char* seconds_s = strtok(NULL, " \n");
225
226 float fraction = strtof(fraction_s, NULL);
227 int seconds = strtol(seconds_s, NULL, 10);
228
Doug Zongker74406302011-10-28 15:13:10 -0700229 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700230 } else if (strcmp(command, "set_progress") == 0) {
231 char* fraction_s = strtok(NULL, " \n");
232 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700233 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700234 } else if (strcmp(command, "ui_print") == 0) {
235 char* str = strtok(NULL, "\n");
236 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700237 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700238 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700239 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700240 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700241 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700242 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700243 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700244 } else if (strcmp(command, "clear_display") == 0) {
245 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700246 } else if (strcmp(command, "enable_reboot") == 0) {
247 // packages can explicitly request that they want the user
248 // to be able to reboot during installation (useful for
249 // debugging packages that don't exit).
250 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800251 } else if (strcmp(command, "retry_update") == 0) {
252 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700253 } else if (strcmp(command, "log") == 0) {
254 // Save the logging request from updater and write to
255 // last_install later.
256 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700257 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700258 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700259 }
260 }
261 fclose(from_child);
262
263 int status;
264 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800265 if (retry_update) {
266 return INSTALL_RETRY;
267 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700268 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700269 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700270 return INSTALL_ERROR;
271 }
272
Doug Zongkere08991e2010-02-02 13:09:52 -0800273 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700274}
275
Doug Zongker469243e2011-04-12 09:28:10 -0700276static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700277really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700278 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700280 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700281 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700282 // Give verification half the progress bar...
283 ui->SetProgressType(RecoveryUI::DETERMINATE);
284 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700285 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800286
Doug Zongker99916f02014-01-13 14:16:58 -0800287 // Map the update package into memory.
288 ui->Print("Opening update package...\n");
289
Doug Zongker075ad802014-06-26 15:35:51 -0700290 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800291 if (path[0] == '@') {
292 ensure_path_mounted(path+1);
293 } else {
294 ensure_path_mounted(path);
295 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800296 }
297
Doug Zongker99916f02014-01-13 14:16:58 -0800298 MemMapping map;
299 if (sysMapFile(path, &map) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700300 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800301 return INSTALL_CORRUPT;
302 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303
Elliott Hughes8febafa2016-04-13 16:39:56 -0700304 // Load keys.
Tao Bao71e3e092016-02-02 14:02:27 -0800305 std::vector<Certificate> loadedKeys;
306 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700307 LOG(ERROR) << "Failed to load keys";
WiZarDedafac62016-08-08 10:30:16 +0530308 sysReleaseMap(&map);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700309 return INSTALL_CORRUPT;
310 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700311 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700312
Elliott Hughes8febafa2016-04-13 16:39:56 -0700313 // Verify package.
Doug Zongker74406302011-10-28 15:13:10 -0700314 ui->Print("Verifying update package...\n");
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700315
Elliott Hughes8febafa2016-04-13 16:39:56 -0700316 auto t0 = std::chrono::system_clock::now();
Tao Bao71e3e092016-02-02 14:02:27 -0800317 int err = verify_file(map.addr, map.length, loadedKeys);
Elliott Hughes8febafa2016-04-13 16:39:56 -0700318 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
319 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
Doug Zongker60151a22009-08-12 18:30:03 -0700320 if (err != VERIFY_SUCCESS) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700321 LOG(ERROR) << "signature verification failed";
Tianjie Xu16255832016-04-30 11:49:59 -0700322 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
323
Doug Zongker99916f02014-01-13 14:16:58 -0800324 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700325 return INSTALL_CORRUPT;
326 }
327
Elliott Hughes8febafa2016-04-13 16:39:56 -0700328 // Try to open the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700329 ZipArchiveHandle zip;
330 err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331 if (err != 0) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700332 LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
Tianjie Xu16255832016-04-30 11:49:59 -0700333 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
334
Doug Zongker99916f02014-01-13 14:16:58 -0800335 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700336 CloseArchive(zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800337 return INSTALL_CORRUPT;
338 }
339
Elliott Hughes8febafa2016-04-13 16:39:56 -0700340 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700341 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700342 if (retry_count > 0) {
343 ui->Print("Retry attempt: %d\n", retry_count);
344 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700345 ui->SetEnableReboot(false);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700346 int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700347 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700348 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800349
350 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700351 CloseArchive(zip);
Doug Zongker99916f02014-01-13 14:16:58 -0800352 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353}
Doug Zongker469243e2011-04-12 09:28:10 -0700354
355int
Tao Bao145d8612015-03-25 15:51:15 -0700356install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700357 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700358{
Tao Bao682c34b2015-04-07 17:16:35 -0700359 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700360 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700361
Doug Zongker239ac6a2013-08-20 16:03:25 -0700362 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700363 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700364 if (setup_install_mounts() != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700365 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700366 result = INSTALL_ERROR;
367 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700368 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700369 }
Tianjie Xudd874b12016-05-13 12:13:15 -0700370
Tao Baof4885ad2016-09-26 11:39:14 -0700371 // Measure the time spent to apply OTA update in seconds.
372 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
373 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700374
Tao Baof4885ad2016-09-26 11:39:14 -0700375 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
376 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
377 } else {
378 std::string uncrypt_status;
379 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
380 PLOG(WARNING) << "failed to read uncrypt status";
Tao Baoee9b9552016-10-13 16:04:07 -0700381 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
382 LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700383 } else {
Tao Baof4885ad2016-09-26 11:39:14 -0700384 log_buffer.push_back(android::base::Trim(uncrypt_status));
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700385 }
Doug Zongker469243e2011-04-12 09:28:10 -0700386 }
Tao Baof4885ad2016-09-26 11:39:14 -0700387
388 // The first two lines need to be the package name and install result.
389 std::vector<std::string> log_header = {
390 path,
391 result == INSTALL_SUCCESS ? "1" : "0",
392 "time_total: " + std::to_string(time_total),
393 "retry: " + std::to_string(retry_count),
394 };
395 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
396 android::base::Join(log_buffer, "\n");
397 if (!android::base::WriteStringToFile(log_content, install_file)) {
398 PLOG(ERROR) << "failed to write " << install_file;
399 }
400
401 // Write a copy into last_log.
402 LOG(INFO) << log_content;
403
Doug Zongker469243e2011-04-12 09:28:10 -0700404 return result;
405}