blob: 92a62db5fe15bc16ea04b05a70d903c477ed1ff6 [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 Xub0ddae52016-06-08 14:30:04 -070030#include <android-base/parseint.h>
Tianjie Xu16255832016-04-30 11:49:59 -070031#include <android-base/stringprintf.h>
32#include <android-base/strings.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070033#include <android-base/logging.h>
Tianjie Xu16255832016-04-30 11:49:59 -070034
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070036#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#include "minui/minui.h"
39#include "minzip/SysUtil.h"
40#include "minzip/Zip.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041#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"
Tianjie Xub0ddae52016-06-08 14:30:04 -070049static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
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
72// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
73static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
74 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
75 if (meta_entry == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070076 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Tianjie Xub0ddae52016-06-08 14:30:04 -070077 return;
78 }
79
80 std::string meta_data(meta_entry->uncompLen, '\0');
81 if (!mzReadZipEntry(zip, meta_entry, &meta_data[0], meta_entry->uncompLen)) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070082 LOG(ERROR) << "Failed to read metadata in update package";
Tianjie Xub0ddae52016-06-08 14:30:04 -070083 return;
84 }
85
86 // Examples of the pre-build and post-build strings in metadata:
87 // pre-build-incremental=2943039
88 // post-build-incremental=2951741
89 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
90 for (const std::string& line : lines) {
91 std::string str = android::base::Trim(line);
92 if (android::base::StartsWith(str, "pre-build-incremental")){
93 int source_build = parse_build_number(str);
94 if (source_build != -1) {
95 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
96 source_build));
97 }
98 } else if (android::base::StartsWith(str, "post-build-incremental")) {
99 int target_build = parse_build_number(str);
100 if (target_build != -1) {
101 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
102 target_build));
103 }
104 }
105 }
106}
107
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700108// If the package contains an update binary, extract it and run it.
109static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700110try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700111 std::vector<std::string>& log_buffer, int retry_count)
Tianjie Xudd874b12016-05-13 12:13:15 -0700112{
Tianjie Xub0ddae52016-06-08 14:30:04 -0700113 read_source_target_build(zip, log_buffer);
114
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700115 const ZipEntry* binary_entry =
116 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
117 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700118 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700119 return INSTALL_CORRUPT;
120 }
121
Doug Zongker10e418d2011-10-28 10:33:05 -0700122 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700123 unlink(binary);
124 int fd = creat(binary, 0755);
125 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700126 PLOG(ERROR) << "Can't make " << binary;
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700127 mzCloseZipArchive(zip);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700128 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700129 }
130 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
131 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700132 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700133
134 if (!ok) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700135 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700136 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700137 }
138
139 int pipefd[2];
140 pipe(pipefd);
141
142 // When executing the update binary contained in the package, the
143 // arguments passed are:
144 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700145 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700146 //
147 // - an fd to which the program can write in order to update the
148 // progress bar. The program can write single-line commands:
149 //
150 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700151 // fill up the next <frac> part of of the progress bar
152 // over <secs> seconds. If <secs> is zero, use
153 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700154 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700155 //
156 // set_progress <frac>
157 // <frac> should be between 0.0 and 1.0; sets the
158 // progress bar within the segment defined by the most
159 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700160 //
161 // firmware <"hboot"|"radio"> <filename>
162 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800163 // given partition on reboot.
164 //
165 // (API v2: <filename> may start with "PACKAGE:" to
166 // indicate taking a file from the OTA package.)
167 //
168 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700169 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 // ui_print <string>
171 // display <string> on the screen.
172 //
Tao Baob07e1f32015-04-10 16:14:52 -0700173 // wipe_cache
174 // a wipe of cache will be performed following a successful
175 // installation.
176 //
177 // clear_display
178 // turn off the text display.
179 //
180 // enable_reboot
181 // packages can explicitly request that they want the user
182 // to be able to reboot during installation (useful for
183 // debugging packages that don't exit).
184 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700185 // - the name of the package zip file.
186 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700187 // - an optional argument "retry" if this update is a retry of a failed
188 // update attempt.
189 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700190
Tianjie Xu64f46fb2016-06-03 15:44:52 -0700191 const char* args[6];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700192 args[0] = binary;
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700193 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
Yabin Cui4425c1d2016-02-10 13:47:32 -0800194 char temp[16];
195 snprintf(temp, sizeof(temp), "%d", pipefd[1]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700196 args[2] = temp;
Yabin Cui4425c1d2016-02-10 13:47:32 -0800197 args[3] = path;
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700198 args[4] = retry_count > 0 ? "retry" : NULL;
199 args[5] = NULL;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700200
201 pid_t pid = fork();
202 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700203 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700204 close(pipefd[0]);
Yabin Cui4425c1d2016-02-10 13:47:32 -0800205 execv(binary, const_cast<char**>(args));
Doug Zongker56c51052010-07-01 09:18:44 -0700206 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700207 _exit(-1);
208 }
209 close(pipefd[1]);
210
Tao Bao145d8612015-03-25 15:51:15 -0700211 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800212 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700213
Doug Zongker64893cc2009-07-14 16:31:56 -0700214 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700215 FILE* from_child = fdopen(pipefd[0], "r");
216 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700217 char* command = strtok(buffer, " \n");
218 if (command == NULL) {
219 continue;
220 } else if (strcmp(command, "progress") == 0) {
221 char* fraction_s = strtok(NULL, " \n");
222 char* seconds_s = strtok(NULL, " \n");
223
224 float fraction = strtof(fraction_s, NULL);
225 int seconds = strtol(seconds_s, NULL, 10);
226
Doug Zongker74406302011-10-28 15:13:10 -0700227 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700228 } else if (strcmp(command, "set_progress") == 0) {
229 char* fraction_s = strtok(NULL, " \n");
230 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700231 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700232 } else if (strcmp(command, "ui_print") == 0) {
233 char* str = strtok(NULL, "\n");
234 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700235 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700236 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700237 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700238 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700239 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700240 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700241 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700242 } else if (strcmp(command, "clear_display") == 0) {
243 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700244 } else if (strcmp(command, "enable_reboot") == 0) {
245 // packages can explicitly request that they want the user
246 // to be able to reboot during installation (useful for
247 // debugging packages that don't exit).
248 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800249 } else if (strcmp(command, "retry_update") == 0) {
250 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700251 } else if (strcmp(command, "log") == 0) {
252 // Save the logging request from updater and write to
253 // last_install later.
254 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700255 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700256 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700257 }
258 }
259 fclose(from_child);
260
261 int status;
262 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800263 if (retry_update) {
264 return INSTALL_RETRY;
265 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700266 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700267 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700268 return INSTALL_ERROR;
269 }
270
Doug Zongkere08991e2010-02-02 13:09:52 -0800271 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700272}
273
Doug Zongker469243e2011-04-12 09:28:10 -0700274static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700275really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700276 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800277{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700278 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700279 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700280 // Give verification half the progress bar...
281 ui->SetProgressType(RecoveryUI::DETERMINATE);
282 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700283 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800284
Doug Zongker99916f02014-01-13 14:16:58 -0800285 // Map the update package into memory.
286 ui->Print("Opening update package...\n");
287
Doug Zongker075ad802014-06-26 15:35:51 -0700288 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800289 if (path[0] == '@') {
290 ensure_path_mounted(path+1);
291 } else {
292 ensure_path_mounted(path);
293 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800294 }
295
Doug Zongker99916f02014-01-13 14:16:58 -0800296 MemMapping map;
297 if (sysMapFile(path, &map) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700298 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800299 return INSTALL_CORRUPT;
300 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301
Elliott Hughes8febafa2016-04-13 16:39:56 -0700302 // Load keys.
Tao Bao71e3e092016-02-02 14:02:27 -0800303 std::vector<Certificate> loadedKeys;
304 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700305 LOG(ERROR) << "Failed to load keys";
WiZarDedafac62016-08-08 10:30:16 +0530306 sysReleaseMap(&map);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700307 return INSTALL_CORRUPT;
308 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700309 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700310
Elliott Hughes8febafa2016-04-13 16:39:56 -0700311 // Verify package.
Doug Zongker74406302011-10-28 15:13:10 -0700312 ui->Print("Verifying update package...\n");
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700313
Elliott Hughes8febafa2016-04-13 16:39:56 -0700314 auto t0 = std::chrono::system_clock::now();
Tao Bao71e3e092016-02-02 14:02:27 -0800315 int err = verify_file(map.addr, map.length, loadedKeys);
Elliott Hughes8febafa2016-04-13 16:39:56 -0700316 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
317 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
Doug Zongker60151a22009-08-12 18:30:03 -0700318 if (err != VERIFY_SUCCESS) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700319 LOG(ERROR) << "signature verification failed";
Tianjie Xu16255832016-04-30 11:49:59 -0700320 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
321
Doug Zongker99916f02014-01-13 14:16:58 -0800322 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700323 return INSTALL_CORRUPT;
324 }
325
Elliott Hughes8febafa2016-04-13 16:39:56 -0700326 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800327 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800328 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800329 if (err != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700330 LOG(ERROR) << "Can't open " << path;
Tianjie Xu16255832016-04-30 11:49:59 -0700331 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
332
Doug Zongker99916f02014-01-13 14:16:58 -0800333 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800334 return INSTALL_CORRUPT;
335 }
336
Elliott Hughes8febafa2016-04-13 16:39:56 -0700337 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700338 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700339 if (retry_count > 0) {
340 ui->Print("Retry attempt: %d\n", retry_count);
341 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700342 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700343 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700344 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700345 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800346
347 sysReleaseMap(&map);
348
349 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800350}
Doug Zongker469243e2011-04-12 09:28:10 -0700351
352int
Tao Bao145d8612015-03-25 15:51:15 -0700353install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700354 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700355{
Tao Bao682c34b2015-04-07 17:16:35 -0700356 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700357 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700358
Doug Zongkerd0181b82011-10-19 10:51:12 -0700359 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700360 if (install_log) {
361 fputs(path, install_log);
362 fputc('\n', install_log);
363 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700364 PLOG(ERROR) << "failed to open last_install";
Doug Zongker469243e2011-04-12 09:28:10 -0700365 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700366 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700367 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700368 if (setup_install_mounts() != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700369 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700370 result = INSTALL_ERROR;
371 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700372 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700373 }
Tianjie Xu16255832016-04-30 11:49:59 -0700374 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700375 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
376 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700377 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
378 int count = static_cast<int>(duration.count());
379 // Report the time spent to apply OTA update in seconds.
380 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700381 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700382
383 for (const auto& s : log_buffer) {
384 fprintf(install_log, "%s\n", s.c_str());
385 }
386
Doug Zongker469243e2011-04-12 09:28:10 -0700387 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700388 }
389 return result;
390}