blob: afc3c92318ffa12311d85883c7b25c79c2e980b3 [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>
Alex Deymo4e29ce02016-08-12 13:43:04 -070020#include <inttypes.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <limits.h>
Elliott Hughes26dbad22015-01-28 12:09:05 -080022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <sys/stat.h>
Doug Zongkerb2ee9202009-06-04 10:24:53 -070024#include <sys/wait.h>
25#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026
Elliott Hughes8febafa2016-04-13 16:39:56 -070027#include <chrono>
Alex Deymo4344d632016-08-03 21:03:53 -070028#include <limits>
29#include <map>
Tianjie Xudd874b12016-05-13 12:13:15 -070030#include <string>
Tao Bao71e3e092016-02-02 14:02:27 -080031#include <vector>
32
Tianjie Xub0ddae52016-06-08 14:30:04 -070033#include <android-base/parseint.h>
Tianjie Xu16255832016-04-30 11:49:59 -070034#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
Alex Deymo4344d632016-08-03 21:03:53 -070036#include <cutils/properties.h>
Tianjie Xu16255832016-04-30 11:49:59 -070037
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070039#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041#include "minui/minui.h"
42#include "minzip/SysUtil.h"
43#include "minzip/Zip.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070045#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020046#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Doug Zongker74406302011-10-28 15:13:10 -070048extern RecoveryUI* ui;
49
Doug Zongkerb2ee9202009-06-04 10:24:53 -070050#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo4344d632016-08-03 21:03:53 -070051static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
52static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070053#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070054static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080055
Doug Zongker74406302011-10-28 15:13:10 -070056// Default allocation of progress bar segments to operations
57static const int VERIFICATION_PROGRESS_TIME = 60;
58static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
59static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
60static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
61
Tianjie Xub0ddae52016-06-08 14:30:04 -070062// This function parses and returns the build.version.incremental
Chih-Hung Hsieh8b238112016-08-26 14:54:29 -070063static int parse_build_number(const std::string& str) {
64 size_t pos = str.find('=');
Tianjie Xub0ddae52016-06-08 14:30:04 -070065 if (pos != std::string::npos) {
66 std::string num_string = android::base::Trim(str.substr(pos+1));
67 int build_number;
68 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
69 return build_number;
70 }
71 }
72
73 LOGE("Failed to parse build number in %s.\n", str.c_str());
74 return -1;
75}
76
Yabin Cui6faf0262016-06-09 14:09:39 -070077bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070078 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
79 if (meta_entry == nullptr) {
80 LOGE("Failed to find %s in update package.\n", METADATA_PATH);
Yabin Cui6faf0262016-06-09 14:09:39 -070081 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070082 }
83
Yabin Cui6faf0262016-06-09 14:09:39 -070084 meta_data->resize(meta_entry->uncompLen, '\0');
85 if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070086 LOGE("Failed to read metadata in update package.\n");
Yabin Cui6faf0262016-06-09 14:09:39 -070087 return false;
88 }
89 return true;
90}
91
92// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
93static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
94 std::string meta_data;
95 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070096 return;
97 }
Tianjie Xub0ddae52016-06-08 14:30:04 -070098 // Examples of the pre-build and post-build strings in metadata:
99 // pre-build-incremental=2943039
100 // post-build-incremental=2951741
101 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
102 for (const std::string& line : lines) {
103 std::string str = android::base::Trim(line);
104 if (android::base::StartsWith(str, "pre-build-incremental")){
105 int source_build = parse_build_number(str);
106 if (source_build != -1) {
107 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
108 source_build));
109 }
110 } else if (android::base::StartsWith(str, "post-build-incremental")) {
111 int target_build = parse_build_number(str);
112 if (target_build != -1) {
113 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
114 target_build));
115 }
116 }
117 }
118}
119
Alex Deymo4344d632016-08-03 21:03:53 -0700120// Extract the update binary from the open zip archive |zip| located at |path|
121// and store into |cmd| the command line that should be called. The |status_fd|
122// is the file descriptor the child process should use to report back the
123// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700124static int
Alex Deymo4344d632016-08-03 21:03:53 -0700125update_binary_command(const char* path, ZipArchive* zip, int retry_count,
126 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700127
Alex Deymo4344d632016-08-03 21:03:53 -0700128#ifdef AB_OTA_UPDATER
129
130// Parses the metadata of the OTA package in |zip| and checks whether we are
131// allowed to accept this A/B package. Downgrading is not allowed unless
132// explicitly enabled in the package and only for incremental packages.
133static int check_newer_ab_build(ZipArchive* zip)
134{
135 std::string metadata_str;
136 if (!read_metadata_from_package(zip, &metadata_str)) {
137 return INSTALL_CORRUPT;
138 }
139 std::map<std::string, std::string> metadata;
140 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
141 size_t eq = line.find('=');
142 if (eq != std::string::npos) {
143 metadata[line.substr(0, eq)] = line.substr(eq + 1);
144 }
145 }
146 char value[PROPERTY_VALUE_MAX];
147
148 property_get("ro.product.device", value, "");
149 const std::string& pkg_device = metadata["pre-device"];
150 if (pkg_device != value || pkg_device.empty()) {
151 LOGE("Package is for product %s but expected %s\n",
152 pkg_device.c_str(), value);
153 return INSTALL_ERROR;
154 }
155
156 // We allow the package to not have any serialno, but if it has a non-empty
157 // value it should match.
158 property_get("ro.serialno", value, "");
159 const std::string& pkg_serial_no = metadata["serialno"];
160 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
161 LOGE("Package is for serial %s\n", pkg_serial_no.c_str());
162 return INSTALL_ERROR;
163 }
164
165 if (metadata["ota-type"] != "AB") {
166 LOGE("Package is not A/B\n");
167 return INSTALL_ERROR;
168 }
169
170 // Incremental updates should match the current build.
171 property_get("ro.build.version.incremental", value, "");
172 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
173 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
174 LOGE("Package is for source build %s but expected %s\n",
175 pkg_pre_build.c_str(), value);
176 return INSTALL_ERROR;
177 }
178 property_get("ro.build.fingerprint", value, "");
179 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
180 if (!pkg_pre_build_fingerprint.empty() &&
181 pkg_pre_build_fingerprint != value) {
182 LOGE("Package is for source build %s but expected %s\n",
183 pkg_pre_build_fingerprint.c_str(), value);
184 return INSTALL_ERROR;
185 }
186
187 // Check for downgrade version.
188 int64_t build_timestampt = property_get_int64(
189 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
190 int64_t pkg_post_timespampt = 0;
191 // We allow to full update to the same version we are running, in case there
192 // is a problem with the current copy of that version.
193 if (metadata["post-timestamp"].empty() ||
194 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
195 &pkg_post_timespampt) ||
196 pkg_post_timespampt < build_timestampt) {
197 if (metadata["ota-downgrade"] != "yes") {
198 LOGE("Update package is older than the current build, expected a "
199 "build newer than timestamp %" PRIu64 " but package has "
200 "timestamp %" PRIu64 " and downgrade not allowed.\n",
201 build_timestampt, pkg_post_timespampt);
202 return INSTALL_ERROR;
203 }
204 if (pkg_pre_build_fingerprint.empty()) {
205 LOGE("Downgrade package must have a pre-build version set, not "
206 "allowed.\n");
207 return INSTALL_ERROR;
208 }
209 }
210
211 return 0;
212}
213
214static int
215update_binary_command(const char* path, ZipArchive* zip, int retry_count,
216 int status_fd, std::vector<std::string>* cmd)
217{
218 int ret = check_newer_ab_build(zip);
219 if (ret) {
220 return ret;
221 }
222
223 // For A/B updates we extract the payload properties to a buffer and obtain
224 // the RAW payload offset in the zip file.
225 const ZipEntry* properties_entry =
226 mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES);
227 if (!properties_entry) {
228 LOGE("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
229 return INSTALL_CORRUPT;
230 }
231 std::vector<unsigned char> payload_properties(
232 mzGetZipEntryUncompLen(properties_entry));
233 if (!mzExtractZipEntryToBuffer(zip, properties_entry,
234 payload_properties.data())) {
235 LOGE("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
236 return INSTALL_CORRUPT;
237 }
238
239 const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD);
240 if (!payload_entry) {
241 LOGE("Can't find %s\n", AB_OTA_PAYLOAD);
242 return INSTALL_CORRUPT;
243 }
244 long payload_offset = mzGetZipEntryOffset(payload_entry);
245 *cmd = {
246 "/sbin/update_engine_sideload",
247 android::base::StringPrintf("--payload=file://%s", path),
248 android::base::StringPrintf("--offset=%ld", payload_offset),
249 "--headers=" + std::string(payload_properties.begin(),
250 payload_properties.end()),
251 android::base::StringPrintf("--status_fd=%d", status_fd),
252 };
253 return 0;
254}
255
256#else // !AB_OTA_UPDATER
257
258static int
259update_binary_command(const char* path, ZipArchive* zip, int retry_count,
260 int status_fd, std::vector<std::string>* cmd)
261{
262 // On traditional updates we extract the update binary from the package.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700263 const ZipEntry* binary_entry =
264 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
265 if (binary_entry == NULL) {
266 return INSTALL_CORRUPT;
267 }
268
Doug Zongker10e418d2011-10-28 10:33:05 -0700269 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700270 unlink(binary);
271 int fd = creat(binary, 0755);
272 if (fd < 0) {
273 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700274 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700275 }
276 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
277 close(fd);
278
279 if (!ok) {
280 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700281 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700282 }
283
Alex Deymo4344d632016-08-03 21:03:53 -0700284 *cmd = {
285 binary,
286 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
287 std::to_string(status_fd),
288 path,
289 };
290 if (retry_count > 0)
291 cmd->push_back("retry");
292 return 0;
293}
294#endif // !AB_OTA_UPDATER
295
296// If the package contains an update binary, extract it and run it.
297static int
298try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
299 std::vector<std::string>& log_buffer, int retry_count)
300{
301 read_source_target_build(zip, log_buffer);
302
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700303 int pipefd[2];
304 pipe(pipefd);
305
Alex Deymo4344d632016-08-03 21:03:53 -0700306 std::vector<std::string> args;
307 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
308 mzCloseZipArchive(zip);
309 if (ret) {
310 close(pipefd[0]);
311 close(pipefd[1]);
312 return ret;
313 }
314
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700315 // When executing the update binary contained in the package, the
316 // arguments passed are:
317 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700318 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700319 //
320 // - an fd to which the program can write in order to update the
321 // progress bar. The program can write single-line commands:
322 //
323 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700324 // fill up the next <frac> part of of the progress bar
325 // over <secs> seconds. If <secs> is zero, use
326 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700327 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700328 //
329 // set_progress <frac>
330 // <frac> should be between 0.0 and 1.0; sets the
331 // progress bar within the segment defined by the most
332 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700333 //
334 // firmware <"hboot"|"radio"> <filename>
335 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800336 // given partition on reboot.
337 //
338 // (API v2: <filename> may start with "PACKAGE:" to
339 // indicate taking a file from the OTA package.)
340 //
341 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700342 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700343 // ui_print <string>
344 // display <string> on the screen.
345 //
Tao Baob07e1f32015-04-10 16:14:52 -0700346 // wipe_cache
347 // a wipe of cache will be performed following a successful
348 // installation.
349 //
350 // clear_display
351 // turn off the text display.
352 //
353 // enable_reboot
354 // packages can explicitly request that they want the user
355 // to be able to reboot during installation (useful for
356 // debugging packages that don't exit).
357 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700358 // - the name of the package zip file.
359 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700360 // - an optional argument "retry" if this update is a retry of a failed
361 // update attempt.
362 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700363
Alex Deymo4344d632016-08-03 21:03:53 -0700364 // Convert the vector to a NULL-terminated char* array suitable for execv.
365 const char* chr_args[args.size() + 1];
366 chr_args[args.size()] = NULL;
367 for (size_t i = 0; i < args.size(); i++) {
368 chr_args[i] = args[i].c_str();
369 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700370
371 pid_t pid = fork();
372 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700373 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700374 close(pipefd[0]);
Alex Deymo4344d632016-08-03 21:03:53 -0700375 execv(chr_args[0], const_cast<char**>(chr_args));
376 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700377 _exit(-1);
378 }
379 close(pipefd[1]);
380
Tao Bao145d8612015-03-25 15:51:15 -0700381 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800382 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700383
Doug Zongker64893cc2009-07-14 16:31:56 -0700384 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700385 FILE* from_child = fdopen(pipefd[0], "r");
386 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700387 char* command = strtok(buffer, " \n");
388 if (command == NULL) {
389 continue;
390 } else if (strcmp(command, "progress") == 0) {
391 char* fraction_s = strtok(NULL, " \n");
392 char* seconds_s = strtok(NULL, " \n");
393
394 float fraction = strtof(fraction_s, NULL);
395 int seconds = strtol(seconds_s, NULL, 10);
396
Doug Zongker74406302011-10-28 15:13:10 -0700397 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700398 } else if (strcmp(command, "set_progress") == 0) {
399 char* fraction_s = strtok(NULL, " \n");
400 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700401 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700402 } else if (strcmp(command, "ui_print") == 0) {
403 char* str = strtok(NULL, "\n");
404 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700405 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700406 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700407 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700408 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700409 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700410 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700411 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700412 } else if (strcmp(command, "clear_display") == 0) {
413 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700414 } else if (strcmp(command, "enable_reboot") == 0) {
415 // packages can explicitly request that they want the user
416 // to be able to reboot during installation (useful for
417 // debugging packages that don't exit).
418 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800419 } else if (strcmp(command, "retry_update") == 0) {
420 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700421 } else if (strcmp(command, "log") == 0) {
422 // Save the logging request from updater and write to
423 // last_install later.
424 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700425 } else {
426 LOGE("unknown command [%s]\n", command);
427 }
428 }
429 fclose(from_child);
430
431 int status;
432 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800433 if (retry_update) {
434 return INSTALL_RETRY;
435 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700436 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700437 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700438 return INSTALL_ERROR;
439 }
440
Doug Zongkere08991e2010-02-02 13:09:52 -0800441 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700442}
443
Doug Zongker469243e2011-04-12 09:28:10 -0700444static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700445really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700446 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800447{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700448 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700449 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700450 // Give verification half the progress bar...
451 ui->SetProgressType(RecoveryUI::DETERMINATE);
452 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700453 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800454
Doug Zongker99916f02014-01-13 14:16:58 -0800455 // Map the update package into memory.
456 ui->Print("Opening update package...\n");
457
Doug Zongker075ad802014-06-26 15:35:51 -0700458 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800459 if (path[0] == '@') {
460 ensure_path_mounted(path+1);
461 } else {
462 ensure_path_mounted(path);
463 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464 }
465
Doug Zongker99916f02014-01-13 14:16:58 -0800466 MemMapping map;
467 if (sysMapFile(path, &map) != 0) {
468 LOGE("failed to map file\n");
469 return INSTALL_CORRUPT;
470 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800471
Elliott Hughes8febafa2016-04-13 16:39:56 -0700472 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700473 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700474 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800475 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700476 return INSTALL_CORRUPT;
477 }
478
Elliott Hughes8febafa2016-04-13 16:39:56 -0700479 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800480 ZipArchive zip;
Yabin Cui6faf0262016-06-09 14:09:39 -0700481 int err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800482 if (err != 0) {
483 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Tianjie Xu16255832016-04-30 11:49:59 -0700484 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
485
Doug Zongker99916f02014-01-13 14:16:58 -0800486 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800487 return INSTALL_CORRUPT;
488 }
489
Elliott Hughes8febafa2016-04-13 16:39:56 -0700490 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700491 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700492 if (retry_count > 0) {
493 ui->Print("Retry attempt: %d\n", retry_count);
494 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700495 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700496 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700497 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700498 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800499
500 sysReleaseMap(&map);
501
502 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800503}
Doug Zongker469243e2011-04-12 09:28:10 -0700504
505int
Tao Bao145d8612015-03-25 15:51:15 -0700506install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700507 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700508{
Tao Bao682c34b2015-04-07 17:16:35 -0700509 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700510 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700511
Doug Zongkerd0181b82011-10-19 10:51:12 -0700512 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700513 if (install_log) {
514 fputs(path, install_log);
515 fputc('\n', install_log);
516 } else {
517 LOGE("failed to open last_install: %s\n", strerror(errno));
518 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700519 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700520 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700521 if (setup_install_mounts() != 0) {
522 LOGE("failed to set up expected mounts for install; aborting\n");
523 result = INSTALL_ERROR;
524 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700525 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700526 }
Tianjie Xu16255832016-04-30 11:49:59 -0700527 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700528 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
529 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700530 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
531 int count = static_cast<int>(duration.count());
532 // Report the time spent to apply OTA update in seconds.
533 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700534 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700535
536 for (const auto& s : log_buffer) {
537 fprintf(install_log, "%s\n", s.c_str());
538 }
539
Doug Zongker469243e2011-04-12 09:28:10 -0700540 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700541 }
542 return result;
543}
Yabin Cui6faf0262016-06-09 14:09:39 -0700544
545bool verify_package(const unsigned char* package_data, size_t package_size) {
546 std::vector<Certificate> loadedKeys;
547 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
548 LOGE("Failed to load keys\n");
549 return false;
550 }
551 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
552
553 // Verify package.
554 ui->Print("Verifying update package...\n");
555 auto t0 = std::chrono::system_clock::now();
556 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
557 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
558 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
559 if (err != VERIFY_SUCCESS) {
560 LOGE("Signature verification failed\n");
561 LOGE("error: %d\n", kZipVerificationFailure);
562 return false;
563 }
564 return true;
565}