blob: 0860d85bfd253ba09213a2a649c656a948799c1d [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>
Amit Blaycc8897b2015-08-23 20:26:31 +030025#include <sys/mount.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026
Elliott Hughes8febafa2016-04-13 16:39:56 -070027#include <chrono>
Tianjie Xudd874b12016-05-13 12:13:15 -070028#include <string>
Tao Bao71e3e092016-02-02 14:02:27 -080029#include <vector>
30
Tianjie Xub0ddae52016-06-08 14:30:04 -070031#include <android-base/parseint.h>
Tianjie Xu16255832016-04-30 11:49:59 -070032#include <android-base/stringprintf.h>
33#include <android-base/strings.h>
34
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"
41#include "mtdutils/mounts.h"
42#include "mtdutils/mtdutils.h"
43#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070044#include "ui.h"
Elliott Hughes8febafa2016-04-13 16:39:56 -070045#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046
Doug Zongker74406302011-10-28 15:13:10 -070047extern RecoveryUI* ui;
48
Doug Zongkerb2ee9202009-06-04 10:24:53 -070049#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Doug Zongkerd1b19b92009-04-01 15:48:46 -070050#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070051static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
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
60static int parse_build_number(std::string str) {
61 size_t pos = str.find("=");
62 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
70 LOGE("Failed to parse build number in %s.\n", str.c_str());
71 return -1;
72}
73
74// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
75static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
76 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
77 if (meta_entry == nullptr) {
78 LOGE("Failed to find %s in update package.\n", METADATA_PATH);
79 return;
80 }
81
82 std::string meta_data(meta_entry->uncompLen, '\0');
83 if (!mzReadZipEntry(zip, meta_entry, &meta_data[0], meta_entry->uncompLen)) {
84 LOGE("Failed to read metadata in update package.\n");
85 return;
86 }
87
88 // Examples of the pre-build and post-build strings in metadata:
89 // pre-build-incremental=2943039
90 // post-build-incremental=2951741
91 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
92 for (const std::string& line : lines) {
93 std::string str = android::base::Trim(line);
94 if (android::base::StartsWith(str, "pre-build-incremental")){
95 int source_build = parse_build_number(str);
96 if (source_build != -1) {
97 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
98 source_build));
99 }
100 } else if (android::base::StartsWith(str, "post-build-incremental")) {
101 int target_build = parse_build_number(str);
102 if (target_build != -1) {
103 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
104 target_build));
105 }
106 }
107 }
108}
109
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700110// If the package contains an update binary, extract it and run it.
111static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700112try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700113 std::vector<std::string>& log_buffer, int retry_count)
Tianjie Xudd874b12016-05-13 12:13:15 -0700114{
Tianjie Xub0ddae52016-06-08 14:30:04 -0700115 read_source_target_build(zip, log_buffer);
116
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700117 const ZipEntry* binary_entry =
118 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
119 if (binary_entry == NULL) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700120 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700121 return INSTALL_CORRUPT;
122 }
123
Doug Zongker10e418d2011-10-28 10:33:05 -0700124 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700125 unlink(binary);
126 int fd = creat(binary, 0755);
127 if (fd < 0) {
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700128 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700129 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700130 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131 }
132 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
133 close(fd);
Doug Zongker8e5e4da2010-09-14 18:06:55 -0700134 mzCloseZipArchive(zip);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700135
136 if (!ok) {
137 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
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 Xu7ce287d2016-05-31 09:29:49 -0700193 const char** args = (const char**)malloc(sizeof(char*) * 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
Doug Zongker10e418d2011-10-28 10:33:05 -0700196 char* temp = (char*)malloc(10);
197 sprintf(temp, "%d", pipefd[1]);
198 args[2] = temp;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700199 args[3] = (char*)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]);
Doug Zongker10e418d2011-10-28 10:33:05 -0700207 execv(binary, (char* const*)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 Xufa12b972016-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 Xufa12b972016-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 {
258 LOGE("unknown command [%s]\n", command);
259 }
260 }
261 fclose(from_child);
262
263 int status;
264 waitpid(pid, &status, 0);
Tianjie Xufa12b972016-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) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700269 LOGE("Error in %s\n(Status %d)\n", path, 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
Amit Blaycc8897b2015-08-23 20:26:31 +0300276#ifdef USE_MDTP
277static int
278mdtp_update()
279{
280 const char** args = (const char**)malloc(sizeof(char*) * 2);
281
282 if (args == NULL) {
283 LOGE("Failed to allocate memory for MDTP FOTA app arguments\n");
284 return 0;
285 }
286
287 args[0] = "/sbin/mdtp_fota";
288 args[1] = NULL;
289 int status = 0;
290
291 ui->Print("Running MDTP integrity verification and update...\n");
292
293 /* Make sure system partition is mounted, so MDTP can process its content. */
294 mkdir("/system", 0755);
295 status = mount("/dev/block/bootdevice/by-name/system", "/system", "ext4",
296 MS_NOATIME | MS_NODEV | MS_NODIRATIME |
297 MS_RDONLY, "");
298
299 if (status) {
300 LOGE("Failed to mount the system partition, error=%s.\n", strerror(errno));
301 free(args);
302 return 0;
303 }
304
305 status = 0;
306
307 pid_t pid = fork();
308 if (pid == 0) {
309 execv(args[0], (char* const*)args);
310 LOGE("Can't run %s (%s)\n", args[0], strerror(errno));
311 _exit(-1);
312 }
313 if (pid > 0) {
314 LOGE("Waiting for MDTP FOTA to complete...\n");
315 pid = waitpid(pid, &status, 0);
316 LOGE("MDTP FOTA completed, status: %d\n", status);
317 }
318
319 /* Leave the system partition unmounted before we finish. */
320 umount("/system");
321
322 free(args);
323
324 return (status > 0) ? 1 : 0;
325}
326#endif /* USE_MDTP */
327
Doug Zongker469243e2011-04-12 09:28:10 -0700328static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700329really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700330 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700332 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700333 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700334 // Give verification half the progress bar...
335 ui->SetProgressType(RecoveryUI::DETERMINATE);
336 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700337 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338
Doug Zongker99916f02014-01-13 14:16:58 -0800339 // Map the update package into memory.
340 ui->Print("Opening update package...\n");
341
Doug Zongker075ad802014-06-26 15:35:51 -0700342 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800343 if (path[0] == '@') {
344 ensure_path_mounted(path+1);
345 } else {
346 ensure_path_mounted(path);
347 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800348 }
349
Doug Zongker99916f02014-01-13 14:16:58 -0800350 MemMapping map;
351 if (sysMapFile(path, &map) != 0) {
352 LOGE("failed to map file\n");
353 return INSTALL_CORRUPT;
354 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800355
Elliott Hughes8febafa2016-04-13 16:39:56 -0700356 // Load keys.
Tao Bao71e3e092016-02-02 14:02:27 -0800357 std::vector<Certificate> loadedKeys;
358 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700359 LOGE("Failed to load keys\n");
360 return INSTALL_CORRUPT;
361 }
Tao Bao71e3e092016-02-02 14:02:27 -0800362 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
Doug Zongkerd1b19b92009-04-01 15:48:46 -0700363
Elliott Hughes8febafa2016-04-13 16:39:56 -0700364 // Verify package.
Doug Zongker74406302011-10-28 15:13:10 -0700365 ui->Print("Verifying update package...\n");
Elliott Hughes8febafa2016-04-13 16:39:56 -0700366 auto t0 = std::chrono::system_clock::now();
Tao Bao71e3e092016-02-02 14:02:27 -0800367 int err = verify_file(map.addr, map.length, loadedKeys);
Elliott Hughes8febafa2016-04-13 16:39:56 -0700368 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
369 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
Doug Zongker60151a22009-08-12 18:30:03 -0700370 if (err != VERIFY_SUCCESS) {
371 LOGE("signature verification failed\n");
Tianjie Xu16255832016-04-30 11:49:59 -0700372 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
373
Doug Zongker99916f02014-01-13 14:16:58 -0800374 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700375 return INSTALL_CORRUPT;
376 }
377
Elliott Hughes8febafa2016-04-13 16:39:56 -0700378 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800379 ZipArchive zip;
Doug Zongker99916f02014-01-13 14:16:58 -0800380 err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800381 if (err != 0) {
382 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Tianjie Xu16255832016-04-30 11:49:59 -0700383 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
384
Doug Zongker99916f02014-01-13 14:16:58 -0800385 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800386 return INSTALL_CORRUPT;
387 }
388
Elliott Hughes8febafa2016-04-13 16:39:56 -0700389 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700390 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700391 if (retry_count > 0) {
392 ui->Print("Retry attempt: %d\n", retry_count);
393 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700394 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700395 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700396 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700397 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800398
399 sysReleaseMap(&map);
400
Amit Blaycc8897b2015-08-23 20:26:31 +0300401#ifdef USE_MDTP
402 /* If MDTP update failed, return an error such that recovery will not finish. */
403 if (result == INSTALL_SUCCESS) {
404 if (!mdtp_update()) {
405 ui->Print("Unable to verify integrity of /system for MDTP, update aborted.\n");
406 return INSTALL_ERROR;
407 }
408 ui->Print("Successfully verified integrity of /system for MDTP.\n");
409 }
410#endif /* USE_MDTP */
411
Doug Zongker99916f02014-01-13 14:16:58 -0800412 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800413}
Doug Zongker469243e2011-04-12 09:28:10 -0700414
415int
Tao Bao145d8612015-03-25 15:51:15 -0700416install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700417 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700418{
Tao Bao682c34b2015-04-07 17:16:35 -0700419 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700420 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700421
Doug Zongkerd0181b82011-10-19 10:51:12 -0700422 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700423 if (install_log) {
424 fputs(path, install_log);
425 fputc('\n', install_log);
426 } else {
427 LOGE("failed to open last_install: %s\n", strerror(errno));
428 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700429 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700430 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700431 if (setup_install_mounts() != 0) {
432 LOGE("failed to set up expected mounts for install; aborting\n");
433 result = INSTALL_ERROR;
434 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700435 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700436 }
Tianjie Xu16255832016-04-30 11:49:59 -0700437 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700438 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
439 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700440 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
441 int count = static_cast<int>(duration.count());
442 // Report the time spent to apply OTA update in seconds.
443 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700444 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700445
446 for (const auto& s : log_buffer) {
447 fprintf(install_log, "%s\n", s.c_str());
448 }
449
Doug Zongker469243e2011-04-12 09:28:10 -0700450 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700451 }
452 return result;
453}