blob: cde9cbafd9408e5762b3f7e06033e94298f1b760 [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>
Alex Deymo4344d632016-08-03 21:03:53 -070027#include <limits>
28#include <map>
Tianjie Xudd874b12016-05-13 12:13:15 -070029#include <string>
Tao Bao71e3e092016-02-02 14:02:27 -080030#include <vector>
31
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>
Alex Deymo4344d632016-08-03 21:03:53 -070035#include <cutils/properties.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"
41#include "minzip/SysUtil.h"
42#include "minzip/Zip.h"
43#include "mtdutils/mounts.h"
44#include "mtdutils/mtdutils.h"
45#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070046#include "ui.h"
Elliott Hughes8febafa2016-04-13 16:39:56 -070047#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Doug Zongker74406302011-10-28 15:13:10 -070049extern RecoveryUI* ui;
50
Doug Zongkerb2ee9202009-06-04 10:24:53 -070051#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo4344d632016-08-03 21:03:53 -070052static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
53static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070054#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070055static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056
Doug Zongker74406302011-10-28 15:13:10 -070057// Default allocation of progress bar segments to operations
58static const int VERIFICATION_PROGRESS_TIME = 60;
59static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
60static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
61static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
62
Tianjie Xub0ddae52016-06-08 14:30:04 -070063// This function parses and returns the build.version.incremental
64static int parse_build_number(std::string str) {
65 size_t pos = str.find("=");
66 if (pos != std::string::npos) {
67 std::string num_string = android::base::Trim(str.substr(pos+1));
68 int build_number;
69 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
70 return build_number;
71 }
72 }
73
74 LOGE("Failed to parse build number in %s.\n", str.c_str());
75 return -1;
76}
77
Yabin Cui6faf0262016-06-09 14:09:39 -070078bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070079 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
80 if (meta_entry == nullptr) {
81 LOGE("Failed to find %s in update package.\n", METADATA_PATH);
Yabin Cui6faf0262016-06-09 14:09:39 -070082 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070083 }
84
Yabin Cui6faf0262016-06-09 14:09:39 -070085 meta_data->resize(meta_entry->uncompLen, '\0');
86 if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070087 LOGE("Failed to read metadata in update package.\n");
Yabin Cui6faf0262016-06-09 14:09:39 -070088 return false;
89 }
90 return true;
91}
92
93// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
94static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
95 std::string meta_data;
96 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070097 return;
98 }
Tianjie Xub0ddae52016-06-08 14:30:04 -070099 // Examples of the pre-build and post-build strings in metadata:
100 // pre-build-incremental=2943039
101 // post-build-incremental=2951741
102 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
103 for (const std::string& line : lines) {
104 std::string str = android::base::Trim(line);
105 if (android::base::StartsWith(str, "pre-build-incremental")){
106 int source_build = parse_build_number(str);
107 if (source_build != -1) {
108 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
109 source_build));
110 }
111 } else if (android::base::StartsWith(str, "post-build-incremental")) {
112 int target_build = parse_build_number(str);
113 if (target_build != -1) {
114 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
115 target_build));
116 }
117 }
118 }
119}
120
Alex Deymo4344d632016-08-03 21:03:53 -0700121// Extract the update binary from the open zip archive |zip| located at |path|
122// and store into |cmd| the command line that should be called. The |status_fd|
123// is the file descriptor the child process should use to report back the
124// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700125static int
Alex Deymo4344d632016-08-03 21:03:53 -0700126update_binary_command(const char* path, ZipArchive* zip, int retry_count,
127 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700128
Alex Deymo4344d632016-08-03 21:03:53 -0700129#ifdef AB_OTA_UPDATER
130
131// Parses the metadata of the OTA package in |zip| and checks whether we are
132// allowed to accept this A/B package. Downgrading is not allowed unless
133// explicitly enabled in the package and only for incremental packages.
134static int check_newer_ab_build(ZipArchive* zip)
135{
136 std::string metadata_str;
137 if (!read_metadata_from_package(zip, &metadata_str)) {
138 return INSTALL_CORRUPT;
139 }
140 std::map<std::string, std::string> metadata;
141 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
142 size_t eq = line.find('=');
143 if (eq != std::string::npos) {
144 metadata[line.substr(0, eq)] = line.substr(eq + 1);
145 }
146 }
147 char value[PROPERTY_VALUE_MAX];
148
149 property_get("ro.product.device", value, "");
150 const std::string& pkg_device = metadata["pre-device"];
151 if (pkg_device != value || pkg_device.empty()) {
152 LOGE("Package is for product %s but expected %s\n",
153 pkg_device.c_str(), value);
154 return INSTALL_ERROR;
155 }
156
157 // We allow the package to not have any serialno, but if it has a non-empty
158 // value it should match.
159 property_get("ro.serialno", value, "");
160 const std::string& pkg_serial_no = metadata["serialno"];
161 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
162 LOGE("Package is for serial %s\n", pkg_serial_no.c_str());
163 return INSTALL_ERROR;
164 }
165
166 if (metadata["ota-type"] != "AB") {
167 LOGE("Package is not A/B\n");
168 return INSTALL_ERROR;
169 }
170
171 // Incremental updates should match the current build.
172 property_get("ro.build.version.incremental", value, "");
173 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
174 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
175 LOGE("Package is for source build %s but expected %s\n",
176 pkg_pre_build.c_str(), value);
177 return INSTALL_ERROR;
178 }
179 property_get("ro.build.fingerprint", value, "");
180 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
181 if (!pkg_pre_build_fingerprint.empty() &&
182 pkg_pre_build_fingerprint != value) {
183 LOGE("Package is for source build %s but expected %s\n",
184 pkg_pre_build_fingerprint.c_str(), value);
185 return INSTALL_ERROR;
186 }
187
188 // Check for downgrade version.
189 int64_t build_timestampt = property_get_int64(
190 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
191 int64_t pkg_post_timespampt = 0;
192 // We allow to full update to the same version we are running, in case there
193 // is a problem with the current copy of that version.
194 if (metadata["post-timestamp"].empty() ||
195 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
196 &pkg_post_timespampt) ||
197 pkg_post_timespampt < build_timestampt) {
198 if (metadata["ota-downgrade"] != "yes") {
199 LOGE("Update package is older than the current build, expected a "
200 "build newer than timestamp %" PRIu64 " but package has "
201 "timestamp %" PRIu64 " and downgrade not allowed.\n",
202 build_timestampt, pkg_post_timespampt);
203 return INSTALL_ERROR;
204 }
205 if (pkg_pre_build_fingerprint.empty()) {
206 LOGE("Downgrade package must have a pre-build version set, not "
207 "allowed.\n");
208 return INSTALL_ERROR;
209 }
210 }
211
212 return 0;
213}
214
215static int
216update_binary_command(const char* path, ZipArchive* zip, int retry_count,
217 int status_fd, std::vector<std::string>* cmd)
218{
219 int ret = check_newer_ab_build(zip);
220 if (ret) {
221 return ret;
222 }
223
224 // For A/B updates we extract the payload properties to a buffer and obtain
225 // the RAW payload offset in the zip file.
226 const ZipEntry* properties_entry =
227 mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES);
228 if (!properties_entry) {
229 LOGE("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
230 return INSTALL_CORRUPT;
231 }
232 std::vector<unsigned char> payload_properties(
233 mzGetZipEntryUncompLen(properties_entry));
234 if (!mzExtractZipEntryToBuffer(zip, properties_entry,
235 payload_properties.data())) {
236 LOGE("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
237 return INSTALL_CORRUPT;
238 }
239
240 const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD);
241 if (!payload_entry) {
242 LOGE("Can't find %s\n", AB_OTA_PAYLOAD);
243 return INSTALL_CORRUPT;
244 }
245 long payload_offset = mzGetZipEntryOffset(payload_entry);
246 *cmd = {
247 "/sbin/update_engine_sideload",
248 android::base::StringPrintf("--payload=file://%s", path),
249 android::base::StringPrintf("--offset=%ld", payload_offset),
250 "--headers=" + std::string(payload_properties.begin(),
251 payload_properties.end()),
252 android::base::StringPrintf("--status_fd=%d", status_fd),
253 };
254 return 0;
255}
256
257#else // !AB_OTA_UPDATER
258
259static int
260update_binary_command(const char* path, ZipArchive* zip, int retry_count,
261 int status_fd, std::vector<std::string>* cmd)
262{
263 // On traditional updates we extract the update binary from the package.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700264 const ZipEntry* binary_entry =
265 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
266 if (binary_entry == NULL) {
267 return INSTALL_CORRUPT;
268 }
269
Doug Zongker10e418d2011-10-28 10:33:05 -0700270 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700271 unlink(binary);
272 int fd = creat(binary, 0755);
273 if (fd < 0) {
274 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700275 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700276 }
277 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
278 close(fd);
279
280 if (!ok) {
281 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700282 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700283 }
284
Alex Deymo4344d632016-08-03 21:03:53 -0700285 *cmd = {
286 binary,
287 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
288 std::to_string(status_fd),
289 path,
290 };
291 if (retry_count > 0)
292 cmd->push_back("retry");
293 return 0;
294}
295#endif // !AB_OTA_UPDATER
296
297// If the package contains an update binary, extract it and run it.
298static int
299try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
300 std::vector<std::string>& log_buffer, int retry_count)
301{
302 read_source_target_build(zip, log_buffer);
303
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700304 int pipefd[2];
305 pipe(pipefd);
306
Alex Deymo4344d632016-08-03 21:03:53 -0700307 std::vector<std::string> args;
308 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
309 mzCloseZipArchive(zip);
310 if (ret) {
311 close(pipefd[0]);
312 close(pipefd[1]);
313 return ret;
314 }
315
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700316 // When executing the update binary contained in the package, the
317 // arguments passed are:
318 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700319 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700320 //
321 // - an fd to which the program can write in order to update the
322 // progress bar. The program can write single-line commands:
323 //
324 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700325 // fill up the next <frac> part of of the progress bar
326 // over <secs> seconds. If <secs> is zero, use
327 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700328 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700329 //
330 // set_progress <frac>
331 // <frac> should be between 0.0 and 1.0; sets the
332 // progress bar within the segment defined by the most
333 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700334 //
335 // firmware <"hboot"|"radio"> <filename>
336 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800337 // given partition on reboot.
338 //
339 // (API v2: <filename> may start with "PACKAGE:" to
340 // indicate taking a file from the OTA package.)
341 //
342 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700343 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700344 // ui_print <string>
345 // display <string> on the screen.
346 //
Tao Baob07e1f32015-04-10 16:14:52 -0700347 // wipe_cache
348 // a wipe of cache will be performed following a successful
349 // installation.
350 //
351 // clear_display
352 // turn off the text display.
353 //
354 // enable_reboot
355 // packages can explicitly request that they want the user
356 // to be able to reboot during installation (useful for
357 // debugging packages that don't exit).
358 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700359 // - the name of the package zip file.
360 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700361 // - an optional argument "retry" if this update is a retry of a failed
362 // update attempt.
363 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700364
Alex Deymo4344d632016-08-03 21:03:53 -0700365 // Convert the vector to a NULL-terminated char* array suitable for execv.
366 const char* chr_args[args.size() + 1];
367 chr_args[args.size()] = NULL;
368 for (size_t i = 0; i < args.size(); i++) {
369 chr_args[i] = args[i].c_str();
370 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700371
372 pid_t pid = fork();
373 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700374 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700375 close(pipefd[0]);
Alex Deymo4344d632016-08-03 21:03:53 -0700376 execv(chr_args[0], const_cast<char**>(chr_args));
377 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700378 _exit(-1);
379 }
380 close(pipefd[1]);
381
Tao Bao145d8612015-03-25 15:51:15 -0700382 *wipe_cache = false;
Tianjie Xufa12b972016-02-05 18:25:58 -0800383 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700384
Doug Zongker64893cc2009-07-14 16:31:56 -0700385 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700386 FILE* from_child = fdopen(pipefd[0], "r");
387 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700388 char* command = strtok(buffer, " \n");
389 if (command == NULL) {
390 continue;
391 } else if (strcmp(command, "progress") == 0) {
392 char* fraction_s = strtok(NULL, " \n");
393 char* seconds_s = strtok(NULL, " \n");
394
395 float fraction = strtof(fraction_s, NULL);
396 int seconds = strtol(seconds_s, NULL, 10);
397
Doug Zongker74406302011-10-28 15:13:10 -0700398 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700399 } else if (strcmp(command, "set_progress") == 0) {
400 char* fraction_s = strtok(NULL, " \n");
401 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700402 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700403 } else if (strcmp(command, "ui_print") == 0) {
404 char* str = strtok(NULL, "\n");
405 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700406 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700407 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700408 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700409 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700410 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700411 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700412 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700413 } else if (strcmp(command, "clear_display") == 0) {
414 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700415 } else if (strcmp(command, "enable_reboot") == 0) {
416 // packages can explicitly request that they want the user
417 // to be able to reboot during installation (useful for
418 // debugging packages that don't exit).
419 ui->SetEnableReboot(true);
Tianjie Xufa12b972016-02-05 18:25:58 -0800420 } else if (strcmp(command, "retry_update") == 0) {
421 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700422 } else if (strcmp(command, "log") == 0) {
423 // Save the logging request from updater and write to
424 // last_install later.
425 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700426 } else {
427 LOGE("unknown command [%s]\n", command);
428 }
429 }
430 fclose(from_child);
431
432 int status;
433 waitpid(pid, &status, 0);
Tianjie Xufa12b972016-02-05 18:25:58 -0800434 if (retry_update) {
435 return INSTALL_RETRY;
436 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700437 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700438 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700439 return INSTALL_ERROR;
440 }
441
Doug Zongkere08991e2010-02-02 13:09:52 -0800442 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700443}
444
Doug Zongker469243e2011-04-12 09:28:10 -0700445static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700446really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700447 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800448{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700449 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700450 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700451 // Give verification half the progress bar...
452 ui->SetProgressType(RecoveryUI::DETERMINATE);
453 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700454 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800455
Doug Zongker99916f02014-01-13 14:16:58 -0800456 // Map the update package into memory.
457 ui->Print("Opening update package...\n");
458
Doug Zongker075ad802014-06-26 15:35:51 -0700459 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800460 if (path[0] == '@') {
461 ensure_path_mounted(path+1);
462 } else {
463 ensure_path_mounted(path);
464 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800465 }
466
Doug Zongker99916f02014-01-13 14:16:58 -0800467 MemMapping map;
468 if (sysMapFile(path, &map) != 0) {
469 LOGE("failed to map file\n");
470 return INSTALL_CORRUPT;
471 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800472
Elliott Hughes8febafa2016-04-13 16:39:56 -0700473 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700474 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700475 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800476 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700477 return INSTALL_CORRUPT;
478 }
479
Elliott Hughes8febafa2016-04-13 16:39:56 -0700480 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800481 ZipArchive zip;
Yabin Cui6faf0262016-06-09 14:09:39 -0700482 int err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800483 if (err != 0) {
484 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Tianjie Xu16255832016-04-30 11:49:59 -0700485 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
486
Doug Zongker99916f02014-01-13 14:16:58 -0800487 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800488 return INSTALL_CORRUPT;
489 }
490
Elliott Hughes8febafa2016-04-13 16:39:56 -0700491 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700492 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700493 if (retry_count > 0) {
494 ui->Print("Retry attempt: %d\n", retry_count);
495 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700496 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700497 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700498 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700499 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800500
501 sysReleaseMap(&map);
502
503 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800504}
Doug Zongker469243e2011-04-12 09:28:10 -0700505
506int
Tao Bao145d8612015-03-25 15:51:15 -0700507install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700508 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700509{
Tao Bao682c34b2015-04-07 17:16:35 -0700510 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700511 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700512
Doug Zongkerd0181b82011-10-19 10:51:12 -0700513 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700514 if (install_log) {
515 fputs(path, install_log);
516 fputc('\n', install_log);
517 } else {
518 LOGE("failed to open last_install: %s\n", strerror(errno));
519 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700520 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700521 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700522 if (setup_install_mounts() != 0) {
523 LOGE("failed to set up expected mounts for install; aborting\n");
524 result = INSTALL_ERROR;
525 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700526 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700527 }
Tianjie Xu16255832016-04-30 11:49:59 -0700528 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700529 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
530 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700531 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
532 int count = static_cast<int>(duration.count());
533 // Report the time spent to apply OTA update in seconds.
534 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700535 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700536
537 for (const auto& s : log_buffer) {
538 fprintf(install_log, "%s\n", s.c_str());
539 }
540
Doug Zongker469243e2011-04-12 09:28:10 -0700541 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700542 }
543 return result;
544}
Yabin Cui6faf0262016-06-09 14:09:39 -0700545
546bool verify_package(const unsigned char* package_data, size_t package_size) {
547 std::vector<Certificate> loadedKeys;
548 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
549 LOGE("Failed to load keys\n");
550 return false;
551 }
552 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
553
554 // Verify package.
555 ui->Print("Verifying update package...\n");
556 auto t0 = std::chrono::system_clock::now();
557 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
558 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
559 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
560 if (err != VERIFY_SUCCESS) {
561 LOGE("Signature verification failed\n");
562 LOGE("error: %d\n", kZipVerificationFailure);
563 return false;
564 }
565 return true;
566}