blob: 78dc6a0d55e038fe13ecb29df6e2134d14374bbd [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 Deymo53c107f2016-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 Xue16e7992016-09-09 10:55:44 -070033#include <android-base/file.h>
34#include <android-base/logging.h>
Tianjie Xub0ddae52016-06-08 14:30:04 -070035#include <android-base/parseint.h>
Tianjie Xu16255832016-04-30 11:49:59 -070036#include <android-base/stringprintf.h>
37#include <android-base/strings.h>
Alex Deymo4344d632016-08-03 21:03:53 -070038#include <cutils/properties.h>
Tianjie Xu16255832016-04-30 11:49:59 -070039
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070041#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043#include "minui/minui.h"
44#include "minzip/SysUtil.h"
45#include "minzip/Zip.h"
46#include "mtdutils/mounts.h"
47#include "mtdutils/mtdutils.h"
48#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070049#include "ui.h"
Elliott Hughes8febafa2016-04-13 16:39:56 -070050#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Doug Zongker74406302011-10-28 15:13:10 -070052extern RecoveryUI* ui;
53
Doug Zongkerb2ee9202009-06-04 10:24:53 -070054#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo4344d632016-08-03 21:03:53 -070055static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
56static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070057#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070058static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
Tianjie Xue16e7992016-09-09 10:55:44 -070059static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060
Doug Zongker74406302011-10-28 15:13:10 -070061// Default allocation of progress bar segments to operations
62static const int VERIFICATION_PROGRESS_TIME = 60;
63static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
64static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
65static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
66
Tianjie Xub0ddae52016-06-08 14:30:04 -070067// This function parses and returns the build.version.incremental
68static int parse_build_number(std::string str) {
69 size_t pos = str.find("=");
70 if (pos != std::string::npos) {
71 std::string num_string = android::base::Trim(str.substr(pos+1));
72 int build_number;
73 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
74 return build_number;
75 }
76 }
77
78 LOGE("Failed to parse build number in %s.\n", str.c_str());
79 return -1;
80}
81
Yabin Cui6faf0262016-06-09 14:09:39 -070082bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070083 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
84 if (meta_entry == nullptr) {
85 LOGE("Failed to find %s in update package.\n", METADATA_PATH);
Yabin Cui6faf0262016-06-09 14:09:39 -070086 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070087 }
88
Yabin Cui6faf0262016-06-09 14:09:39 -070089 meta_data->resize(meta_entry->uncompLen, '\0');
90 if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070091 LOGE("Failed to read metadata in update package.\n");
Yabin Cui6faf0262016-06-09 14:09:39 -070092 return false;
93 }
94 return true;
95}
96
97// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
98static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
99 std::string meta_data;
100 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -0700101 return;
102 }
Tianjie Xub0ddae52016-06-08 14:30:04 -0700103 // Examples of the pre-build and post-build strings in metadata:
104 // pre-build-incremental=2943039
105 // post-build-incremental=2951741
106 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
107 for (const std::string& line : lines) {
108 std::string str = android::base::Trim(line);
109 if (android::base::StartsWith(str, "pre-build-incremental")){
110 int source_build = parse_build_number(str);
111 if (source_build != -1) {
112 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
113 source_build));
114 }
115 } else if (android::base::StartsWith(str, "post-build-incremental")) {
116 int target_build = parse_build_number(str);
117 if (target_build != -1) {
118 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
119 target_build));
120 }
121 }
122 }
123}
124
Alex Deymo4344d632016-08-03 21:03:53 -0700125// Extract the update binary from the open zip archive |zip| located at |path|
126// and store into |cmd| the command line that should be called. The |status_fd|
127// is the file descriptor the child process should use to report back the
128// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700129static int
Alex Deymo4344d632016-08-03 21:03:53 -0700130update_binary_command(const char* path, ZipArchive* zip, int retry_count,
131 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700132
Alex Deymo4344d632016-08-03 21:03:53 -0700133#ifdef AB_OTA_UPDATER
134
135// Parses the metadata of the OTA package in |zip| and checks whether we are
136// allowed to accept this A/B package. Downgrading is not allowed unless
137// explicitly enabled in the package and only for incremental packages.
138static int check_newer_ab_build(ZipArchive* zip)
139{
140 std::string metadata_str;
141 if (!read_metadata_from_package(zip, &metadata_str)) {
142 return INSTALL_CORRUPT;
143 }
144 std::map<std::string, std::string> metadata;
145 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
146 size_t eq = line.find('=');
147 if (eq != std::string::npos) {
148 metadata[line.substr(0, eq)] = line.substr(eq + 1);
149 }
150 }
151 char value[PROPERTY_VALUE_MAX];
152
153 property_get("ro.product.device", value, "");
154 const std::string& pkg_device = metadata["pre-device"];
155 if (pkg_device != value || pkg_device.empty()) {
156 LOGE("Package is for product %s but expected %s\n",
157 pkg_device.c_str(), value);
158 return INSTALL_ERROR;
159 }
160
161 // We allow the package to not have any serialno, but if it has a non-empty
162 // value it should match.
163 property_get("ro.serialno", value, "");
164 const std::string& pkg_serial_no = metadata["serialno"];
165 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
166 LOGE("Package is for serial %s\n", pkg_serial_no.c_str());
167 return INSTALL_ERROR;
168 }
169
170 if (metadata["ota-type"] != "AB") {
171 LOGE("Package is not A/B\n");
172 return INSTALL_ERROR;
173 }
174
175 // Incremental updates should match the current build.
176 property_get("ro.build.version.incremental", value, "");
177 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
178 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
179 LOGE("Package is for source build %s but expected %s\n",
180 pkg_pre_build.c_str(), value);
181 return INSTALL_ERROR;
182 }
183 property_get("ro.build.fingerprint", value, "");
184 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
185 if (!pkg_pre_build_fingerprint.empty() &&
186 pkg_pre_build_fingerprint != value) {
187 LOGE("Package is for source build %s but expected %s\n",
188 pkg_pre_build_fingerprint.c_str(), value);
189 return INSTALL_ERROR;
190 }
191
192 // Check for downgrade version.
193 int64_t build_timestampt = property_get_int64(
194 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
195 int64_t pkg_post_timespampt = 0;
196 // We allow to full update to the same version we are running, in case there
197 // is a problem with the current copy of that version.
198 if (metadata["post-timestamp"].empty() ||
199 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
200 &pkg_post_timespampt) ||
201 pkg_post_timespampt < build_timestampt) {
202 if (metadata["ota-downgrade"] != "yes") {
203 LOGE("Update package is older than the current build, expected a "
204 "build newer than timestamp %" PRIu64 " but package has "
205 "timestamp %" PRIu64 " and downgrade not allowed.\n",
206 build_timestampt, pkg_post_timespampt);
207 return INSTALL_ERROR;
208 }
209 if (pkg_pre_build_fingerprint.empty()) {
210 LOGE("Downgrade package must have a pre-build version set, not "
211 "allowed.\n");
212 return INSTALL_ERROR;
213 }
214 }
215
216 return 0;
217}
218
219static int
220update_binary_command(const char* path, ZipArchive* zip, int retry_count,
221 int status_fd, std::vector<std::string>* cmd)
222{
223 int ret = check_newer_ab_build(zip);
224 if (ret) {
225 return ret;
226 }
227
228 // For A/B updates we extract the payload properties to a buffer and obtain
229 // the RAW payload offset in the zip file.
230 const ZipEntry* properties_entry =
231 mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES);
232 if (!properties_entry) {
233 LOGE("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
234 return INSTALL_CORRUPT;
235 }
236 std::vector<unsigned char> payload_properties(
237 mzGetZipEntryUncompLen(properties_entry));
238 if (!mzExtractZipEntryToBuffer(zip, properties_entry,
239 payload_properties.data())) {
240 LOGE("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
241 return INSTALL_CORRUPT;
242 }
243
244 const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD);
245 if (!payload_entry) {
246 LOGE("Can't find %s\n", AB_OTA_PAYLOAD);
247 return INSTALL_CORRUPT;
248 }
249 long payload_offset = mzGetZipEntryOffset(payload_entry);
250 *cmd = {
251 "/sbin/update_engine_sideload",
252 android::base::StringPrintf("--payload=file://%s", path),
253 android::base::StringPrintf("--offset=%ld", payload_offset),
254 "--headers=" + std::string(payload_properties.begin(),
255 payload_properties.end()),
256 android::base::StringPrintf("--status_fd=%d", status_fd),
257 };
258 return 0;
259}
260
261#else // !AB_OTA_UPDATER
262
263static int
264update_binary_command(const char* path, ZipArchive* zip, int retry_count,
265 int status_fd, std::vector<std::string>* cmd)
266{
267 // On traditional updates we extract the update binary from the package.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700268 const ZipEntry* binary_entry =
269 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
270 if (binary_entry == NULL) {
271 return INSTALL_CORRUPT;
272 }
273
Doug Zongker10e418d2011-10-28 10:33:05 -0700274 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700275 unlink(binary);
276 int fd = creat(binary, 0755);
277 if (fd < 0) {
278 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700279 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700280 }
281 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
282 close(fd);
283
284 if (!ok) {
285 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700286 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700287 }
288
Alex Deymo4344d632016-08-03 21:03:53 -0700289 *cmd = {
290 binary,
291 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
292 std::to_string(status_fd),
293 path,
294 };
295 if (retry_count > 0)
296 cmd->push_back("retry");
297 return 0;
298}
299#endif // !AB_OTA_UPDATER
300
301// If the package contains an update binary, extract it and run it.
302static int
303try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
304 std::vector<std::string>& log_buffer, int retry_count)
305{
306 read_source_target_build(zip, log_buffer);
307
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700308 int pipefd[2];
309 pipe(pipefd);
310
Alex Deymo4344d632016-08-03 21:03:53 -0700311 std::vector<std::string> args;
312 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
313 mzCloseZipArchive(zip);
314 if (ret) {
315 close(pipefd[0]);
316 close(pipefd[1]);
317 return ret;
318 }
319
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700320 // When executing the update binary contained in the package, the
321 // arguments passed are:
322 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700323 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700324 //
325 // - an fd to which the program can write in order to update the
326 // progress bar. The program can write single-line commands:
327 //
328 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700329 // fill up the next <frac> part of of the progress bar
330 // over <secs> seconds. If <secs> is zero, use
331 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700332 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700333 //
334 // set_progress <frac>
335 // <frac> should be between 0.0 and 1.0; sets the
336 // progress bar within the segment defined by the most
337 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700338 //
339 // firmware <"hboot"|"radio"> <filename>
340 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800341 // given partition on reboot.
342 //
343 // (API v2: <filename> may start with "PACKAGE:" to
344 // indicate taking a file from the OTA package.)
345 //
346 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700347 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700348 // ui_print <string>
349 // display <string> on the screen.
350 //
Tao Baob07e1f32015-04-10 16:14:52 -0700351 // wipe_cache
352 // a wipe of cache will be performed following a successful
353 // installation.
354 //
355 // clear_display
356 // turn off the text display.
357 //
358 // enable_reboot
359 // packages can explicitly request that they want the user
360 // to be able to reboot during installation (useful for
361 // debugging packages that don't exit).
362 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700363 // - the name of the package zip file.
364 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700365 // - an optional argument "retry" if this update is a retry of a failed
366 // update attempt.
367 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700368
Alex Deymo4344d632016-08-03 21:03:53 -0700369 // Convert the vector to a NULL-terminated char* array suitable for execv.
370 const char* chr_args[args.size() + 1];
371 chr_args[args.size()] = NULL;
372 for (size_t i = 0; i < args.size(); i++) {
373 chr_args[i] = args[i].c_str();
374 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700375
376 pid_t pid = fork();
Matthew Bouyackde1b53d2016-09-16 16:38:11 -0700377
378 if (pid == -1) {
379 close(pipefd[0]);
380 close(pipefd[1]);
381 LOGE("Failed to fork update binary: %s\n", strerror(errno));
382 return INSTALL_ERROR;
383 }
384
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700385 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700386 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700387 close(pipefd[0]);
Alex Deymo4344d632016-08-03 21:03:53 -0700388 execv(chr_args[0], const_cast<char**>(chr_args));
389 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700390 _exit(-1);
391 }
392 close(pipefd[1]);
393
Tao Bao145d8612015-03-25 15:51:15 -0700394 *wipe_cache = false;
Tianjie Xufa12b972016-02-05 18:25:58 -0800395 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700396
Doug Zongker64893cc2009-07-14 16:31:56 -0700397 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700398 FILE* from_child = fdopen(pipefd[0], "r");
399 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700400 char* command = strtok(buffer, " \n");
401 if (command == NULL) {
402 continue;
403 } else if (strcmp(command, "progress") == 0) {
404 char* fraction_s = strtok(NULL, " \n");
405 char* seconds_s = strtok(NULL, " \n");
406
407 float fraction = strtof(fraction_s, NULL);
408 int seconds = strtol(seconds_s, NULL, 10);
409
Doug Zongker74406302011-10-28 15:13:10 -0700410 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700411 } else if (strcmp(command, "set_progress") == 0) {
412 char* fraction_s = strtok(NULL, " \n");
413 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700414 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700415 } else if (strcmp(command, "ui_print") == 0) {
416 char* str = strtok(NULL, "\n");
417 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700418 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700419 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700420 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700421 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700422 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700423 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700424 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700425 } else if (strcmp(command, "clear_display") == 0) {
426 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700427 } else if (strcmp(command, "enable_reboot") == 0) {
428 // packages can explicitly request that they want the user
429 // to be able to reboot during installation (useful for
430 // debugging packages that don't exit).
431 ui->SetEnableReboot(true);
Tianjie Xufa12b972016-02-05 18:25:58 -0800432 } else if (strcmp(command, "retry_update") == 0) {
433 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700434 } else if (strcmp(command, "log") == 0) {
435 // Save the logging request from updater and write to
436 // last_install later.
437 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700438 } else {
439 LOGE("unknown command [%s]\n", command);
440 }
441 }
442 fclose(from_child);
443
444 int status;
445 waitpid(pid, &status, 0);
Tianjie Xufa12b972016-02-05 18:25:58 -0800446 if (retry_update) {
447 return INSTALL_RETRY;
448 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700449 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700451 return INSTALL_ERROR;
452 }
453
Doug Zongkere08991e2010-02-02 13:09:52 -0800454 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700455}
456
Doug Zongker469243e2011-04-12 09:28:10 -0700457static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700458really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700459 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800460{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700461 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700462 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700463 // Give verification half the progress bar...
464 ui->SetProgressType(RecoveryUI::DETERMINATE);
465 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700466 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800467
Doug Zongker99916f02014-01-13 14:16:58 -0800468 // Map the update package into memory.
469 ui->Print("Opening update package...\n");
470
Doug Zongker075ad802014-06-26 15:35:51 -0700471 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800472 if (path[0] == '@') {
473 ensure_path_mounted(path+1);
474 } else {
475 ensure_path_mounted(path);
476 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800477 }
478
Doug Zongker99916f02014-01-13 14:16:58 -0800479 MemMapping map;
480 if (sysMapFile(path, &map) != 0) {
481 LOGE("failed to map file\n");
482 return INSTALL_CORRUPT;
483 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800484
Elliott Hughes8febafa2016-04-13 16:39:56 -0700485 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700486 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700487 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800488 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700489 return INSTALL_CORRUPT;
490 }
491
Elliott Hughes8febafa2016-04-13 16:39:56 -0700492 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800493 ZipArchive zip;
Yabin Cui6faf0262016-06-09 14:09:39 -0700494 int err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495 if (err != 0) {
496 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Tianjie Xu16255832016-04-30 11:49:59 -0700497 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
498
Doug Zongker99916f02014-01-13 14:16:58 -0800499 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800500 return INSTALL_CORRUPT;
501 }
502
Elliott Hughes8febafa2016-04-13 16:39:56 -0700503 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700504 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700505 if (retry_count > 0) {
506 ui->Print("Retry attempt: %d\n", retry_count);
507 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700508 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700509 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700510 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700511 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800512
513 sysReleaseMap(&map);
514
515 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800516}
Doug Zongker469243e2011-04-12 09:28:10 -0700517
518int
Tao Bao145d8612015-03-25 15:51:15 -0700519install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700520 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700521{
Tao Bao682c34b2015-04-07 17:16:35 -0700522 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700523 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700524
Doug Zongkerd0181b82011-10-19 10:51:12 -0700525 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700526 if (install_log) {
527 fputs(path, install_log);
528 fputc('\n', install_log);
529 } else {
530 LOGE("failed to open last_install: %s\n", strerror(errno));
531 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700532 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700533 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700534 if (setup_install_mounts() != 0) {
535 LOGE("failed to set up expected mounts for install; aborting\n");
536 result = INSTALL_ERROR;
537 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700538 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700539 }
Tianjie Xu16255832016-04-30 11:49:59 -0700540 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700541 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
542 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700543 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
544 int count = static_cast<int>(duration.count());
545 // Report the time spent to apply OTA update in seconds.
546 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700547 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700548
549 for (const auto& s : log_buffer) {
550 fprintf(install_log, "%s\n", s.c_str());
551 }
552
Tianjie Xue16e7992016-09-09 10:55:44 -0700553 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
554 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
555 } else {
556 std::string uncrypt_status;
557 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
558 PLOG(WARNING) << "failed to read uncrypt status";
Tianjie Xu41a3fd42016-09-12 16:43:37 -0700559 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_time:")) {
560 PLOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xue16e7992016-09-09 10:55:44 -0700561 } else {
562 fprintf(install_log, "%s\n", android::base::Trim(uncrypt_status).c_str());
563 }
564 }
Doug Zongker469243e2011-04-12 09:28:10 -0700565 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700566 }
567 return result;
568}
Yabin Cui6faf0262016-06-09 14:09:39 -0700569
570bool verify_package(const unsigned char* package_data, size_t package_size) {
571 std::vector<Certificate> loadedKeys;
572 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
573 LOGE("Failed to load keys\n");
574 return false;
575 }
576 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
577
578 // Verify package.
579 ui->Print("Verifying update package...\n");
580 auto t0 = std::chrono::system_clock::now();
581 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
582 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
583 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
584 if (err != VERIFY_SUCCESS) {
585 LOGE("Signature verification failed\n");
586 LOGE("error: %d\n", kZipVerificationFailure);
587 return false;
588 }
589 return true;
590}