blob: d600ea33061f43ece1ce1b37b26b7311b5bb4c8b [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 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 Xuc21edd42016-08-05 18:00:04 -070039#include <android-base/logging.h>
Tianjie Xu16255832016-04-30 11:49:59 -070040
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070042#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044#include "minui/minui.h"
45#include "minzip/SysUtil.h"
46#include "minzip/Zip.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070048#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020049#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
Doug Zongker74406302011-10-28 15:13:10 -070051extern RecoveryUI* ui;
52
Doug Zongkerb2ee9202009-06-04 10:24:53 -070053#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo4344d632016-08-03 21:03:53 -070054static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
55static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070056#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070057static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
Tianjie Xue16e7992016-09-09 10:55:44 -070058static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059
Doug Zongker74406302011-10-28 15:13:10 -070060// Default allocation of progress bar segments to operations
61static const int VERIFICATION_PROGRESS_TIME = 60;
62static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
63static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
64static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
65
Tianjie Xub0ddae52016-06-08 14:30:04 -070066// This function parses and returns the build.version.incremental
Chih-Hung Hsieh8b238112016-08-26 14:54:29 -070067static int parse_build_number(const std::string& str) {
68 size_t pos = str.find('=');
Tianjie Xub0ddae52016-06-08 14:30:04 -070069 if (pos != std::string::npos) {
70 std::string num_string = android::base::Trim(str.substr(pos+1));
71 int build_number;
72 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
73 return build_number;
74 }
75 }
76
Tianjie Xuc21edd42016-08-05 18:00:04 -070077 LOG(ERROR) << "Failed to parse build number in " << str;
Tianjie Xub0ddae52016-06-08 14:30:04 -070078 return -1;
79}
80
Yabin Cui6faf0262016-06-09 14:09:39 -070081bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070082 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
83 if (meta_entry == nullptr) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070084 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070085 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070086 }
87
Yabin Cui6faf0262016-06-09 14:09:39 -070088 meta_data->resize(meta_entry->uncompLen, '\0');
89 if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070090 LOG(ERROR) << "Failed to read metadata in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070091 return false;
92 }
93 return true;
94}
95
96// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
97static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
98 std::string meta_data;
99 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -0700100 return;
101 }
Tianjie Xub0ddae52016-06-08 14:30:04 -0700102 // Examples of the pre-build and post-build strings in metadata:
103 // pre-build-incremental=2943039
104 // post-build-incremental=2951741
105 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
106 for (const std::string& line : lines) {
107 std::string str = android::base::Trim(line);
108 if (android::base::StartsWith(str, "pre-build-incremental")){
109 int source_build = parse_build_number(str);
110 if (source_build != -1) {
111 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
112 source_build));
113 }
114 } else if (android::base::StartsWith(str, "post-build-incremental")) {
115 int target_build = parse_build_number(str);
116 if (target_build != -1) {
117 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
118 target_build));
119 }
120 }
121 }
122}
123
Alex Deymo4344d632016-08-03 21:03:53 -0700124// Extract the update binary from the open zip archive |zip| located at |path|
125// and store into |cmd| the command line that should be called. The |status_fd|
126// is the file descriptor the child process should use to report back the
127// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700128static int
Alex Deymo4344d632016-08-03 21:03:53 -0700129update_binary_command(const char* path, ZipArchive* zip, int retry_count,
130 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700131
Alex Deymo4344d632016-08-03 21:03:53 -0700132#ifdef AB_OTA_UPDATER
133
134// Parses the metadata of the OTA package in |zip| and checks whether we are
135// allowed to accept this A/B package. Downgrading is not allowed unless
136// explicitly enabled in the package and only for incremental packages.
137static int check_newer_ab_build(ZipArchive* zip)
138{
139 std::string metadata_str;
140 if (!read_metadata_from_package(zip, &metadata_str)) {
141 return INSTALL_CORRUPT;
142 }
143 std::map<std::string, std::string> metadata;
144 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
145 size_t eq = line.find('=');
146 if (eq != std::string::npos) {
147 metadata[line.substr(0, eq)] = line.substr(eq + 1);
148 }
149 }
150 char value[PROPERTY_VALUE_MAX];
151
152 property_get("ro.product.device", value, "");
153 const std::string& pkg_device = metadata["pre-device"];
154 if (pkg_device != value || pkg_device.empty()) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700155 LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700156 return INSTALL_ERROR;
157 }
158
159 // We allow the package to not have any serialno, but if it has a non-empty
160 // value it should match.
161 property_get("ro.serialno", value, "");
162 const std::string& pkg_serial_no = metadata["serialno"];
163 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700164 LOG(ERROR) << "Package is for serial " << pkg_serial_no;
Alex Deymo4344d632016-08-03 21:03:53 -0700165 return INSTALL_ERROR;
166 }
167
168 if (metadata["ota-type"] != "AB") {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700169 LOG(ERROR) << "Package is not A/B";
Alex Deymo4344d632016-08-03 21:03:53 -0700170 return INSTALL_ERROR;
171 }
172
173 // Incremental updates should match the current build.
174 property_get("ro.build.version.incremental", value, "");
175 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
176 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700177 LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700178 return INSTALL_ERROR;
179 }
180 property_get("ro.build.fingerprint", value, "");
181 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
182 if (!pkg_pre_build_fingerprint.empty() &&
183 pkg_pre_build_fingerprint != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700184 LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint
185 << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700186 return INSTALL_ERROR;
187 }
188
189 // Check for downgrade version.
Tianjie Xu073451a2016-09-01 11:51:17 -0700190 int64_t build_timestamp = property_get_int64(
Alex Deymo4344d632016-08-03 21:03:53 -0700191 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
Tianjie Xu073451a2016-09-01 11:51:17 -0700192 int64_t pkg_post_timestamp = 0;
Alex Deymo4344d632016-08-03 21:03:53 -0700193 // We allow to full update to the same version we are running, in case there
194 // is a problem with the current copy of that version.
195 if (metadata["post-timestamp"].empty() ||
196 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
Tianjie Xu073451a2016-09-01 11:51:17 -0700197 &pkg_post_timestamp) ||
198 pkg_post_timestamp < build_timestamp) {
Alex Deymo4344d632016-08-03 21:03:53 -0700199 if (metadata["ota-downgrade"] != "yes") {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700200 LOG(ERROR) << "Update package is older than the current build, expected a build "
Tianjie Xu073451a2016-09-01 11:51:17 -0700201 "newer than timestamp " << build_timestamp << " but package has "
202 "timestamp " << pkg_post_timestamp << " and downgrade not allowed.";
Alex Deymo4344d632016-08-03 21:03:53 -0700203 return INSTALL_ERROR;
204 }
205 if (pkg_pre_build_fingerprint.empty()) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700206 LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed.";
Alex Deymo4344d632016-08-03 21:03:53 -0700207 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) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700228 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700229 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())) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700235 LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700236 return INSTALL_CORRUPT;
237 }
238
239 const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD);
240 if (!payload_entry) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700241 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD;
Alex Deymo4344d632016-08-03 21:03:53 -0700242 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) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700273 PLOG(ERROR) << "Can't make " << 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) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700280 LOG(ERROR) << "Can't copy " << 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();
Matthew Bouyackc8db4812016-09-20 19:08:42 -0700372
373 if (pid == -1) {
374 close(pipefd[0]);
375 close(pipefd[1]);
376 PLOG(ERROR) << "Failed to fork update binary";
377 return INSTALL_ERROR;
378 }
379
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700380 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700381 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700382 close(pipefd[0]);
Alex Deymo4344d632016-08-03 21:03:53 -0700383 execv(chr_args[0], const_cast<char**>(chr_args));
384 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700385 _exit(-1);
386 }
387 close(pipefd[1]);
388
Tao Bao145d8612015-03-25 15:51:15 -0700389 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800390 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700391
Doug Zongker64893cc2009-07-14 16:31:56 -0700392 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700393 FILE* from_child = fdopen(pipefd[0], "r");
394 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700395 char* command = strtok(buffer, " \n");
396 if (command == NULL) {
397 continue;
398 } else if (strcmp(command, "progress") == 0) {
399 char* fraction_s = strtok(NULL, " \n");
400 char* seconds_s = strtok(NULL, " \n");
401
402 float fraction = strtof(fraction_s, NULL);
403 int seconds = strtol(seconds_s, NULL, 10);
404
Doug Zongker74406302011-10-28 15:13:10 -0700405 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700406 } else if (strcmp(command, "set_progress") == 0) {
407 char* fraction_s = strtok(NULL, " \n");
408 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700409 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700410 } else if (strcmp(command, "ui_print") == 0) {
411 char* str = strtok(NULL, "\n");
412 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700413 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700414 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700415 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700416 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700417 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700418 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700419 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700420 } else if (strcmp(command, "clear_display") == 0) {
421 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700422 } else if (strcmp(command, "enable_reboot") == 0) {
423 // packages can explicitly request that they want the user
424 // to be able to reboot during installation (useful for
425 // debugging packages that don't exit).
426 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800427 } else if (strcmp(command, "retry_update") == 0) {
428 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700429 } else if (strcmp(command, "log") == 0) {
430 // Save the logging request from updater and write to
431 // last_install later.
432 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700433 } else {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700434 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700435 }
436 }
437 fclose(from_child);
438
439 int status;
440 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800441 if (retry_update) {
442 return INSTALL_RETRY;
443 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700444 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700445 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700446 return INSTALL_ERROR;
447 }
448
Doug Zongkere08991e2010-02-02 13:09:52 -0800449 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700450}
451
Doug Zongker469243e2011-04-12 09:28:10 -0700452static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700453really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700454 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800455{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700456 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700457 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700458 // Give verification half the progress bar...
459 ui->SetProgressType(RecoveryUI::DETERMINATE);
460 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xuc21edd42016-08-05 18:00:04 -0700461 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800462
Doug Zongker99916f02014-01-13 14:16:58 -0800463 // Map the update package into memory.
464 ui->Print("Opening update package...\n");
465
Doug Zongker075ad802014-06-26 15:35:51 -0700466 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800467 if (path[0] == '@') {
468 ensure_path_mounted(path+1);
469 } else {
470 ensure_path_mounted(path);
471 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800472 }
473
Doug Zongker99916f02014-01-13 14:16:58 -0800474 MemMapping map;
475 if (sysMapFile(path, &map) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700476 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800477 return INSTALL_CORRUPT;
478 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800479
Elliott Hughes8febafa2016-04-13 16:39:56 -0700480 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700481 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700482 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800483 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700484 return INSTALL_CORRUPT;
485 }
486
Elliott Hughes8febafa2016-04-13 16:39:56 -0700487 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800488 ZipArchive zip;
Yabin Cui6faf0262016-06-09 14:09:39 -0700489 int err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800490 if (err != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700491 LOG(ERROR) << "Can't open " << path;
Tianjie Xu16255832016-04-30 11:49:59 -0700492 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
493
Doug Zongker99916f02014-01-13 14:16:58 -0800494 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495 return INSTALL_CORRUPT;
496 }
497
Elliott Hughes8febafa2016-04-13 16:39:56 -0700498 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700499 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700500 if (retry_count > 0) {
501 ui->Print("Retry attempt: %d\n", retry_count);
502 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700503 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700504 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700505 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700506 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800507
508 sysReleaseMap(&map);
509
510 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800511}
Doug Zongker469243e2011-04-12 09:28:10 -0700512
513int
Tao Bao145d8612015-03-25 15:51:15 -0700514install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700515 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700516{
Tao Bao682c34b2015-04-07 17:16:35 -0700517 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700518 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700519
Doug Zongkerd0181b82011-10-19 10:51:12 -0700520 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700521 if (install_log) {
522 fputs(path, install_log);
523 fputc('\n', install_log);
524 } else {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700525 PLOG(ERROR) << "failed to open last_install";
Doug Zongker469243e2011-04-12 09:28:10 -0700526 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700527 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700528 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700529 if (setup_install_mounts() != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700530 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700531 result = INSTALL_ERROR;
532 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700533 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700534 }
Tianjie Xu16255832016-04-30 11:49:59 -0700535 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700536 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
537 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700538 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
539 int count = static_cast<int>(duration.count());
540 // Report the time spent to apply OTA update in seconds.
541 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700542 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700543
544 for (const auto& s : log_buffer) {
545 fprintf(install_log, "%s\n", s.c_str());
546 }
547
Tianjie Xue16e7992016-09-09 10:55:44 -0700548 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
549 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
550 } else {
551 std::string uncrypt_status;
552 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
553 PLOG(WARNING) << "failed to read uncrypt status";
Tianjie Xu1c1864f2016-09-12 16:43:37 -0700554 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_time:")) {
555 PLOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xue16e7992016-09-09 10:55:44 -0700556 } else {
557 fprintf(install_log, "%s\n", android::base::Trim(uncrypt_status).c_str());
558 }
559 }
Doug Zongker469243e2011-04-12 09:28:10 -0700560 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700561 }
562 return result;
563}
Yabin Cui6faf0262016-06-09 14:09:39 -0700564
565bool verify_package(const unsigned char* package_data, size_t package_size) {
566 std::vector<Certificate> loadedKeys;
567 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700568 LOG(ERROR) << "Failed to load keys";
Yabin Cui6faf0262016-06-09 14:09:39 -0700569 return false;
570 }
Tianjie Xuc21edd42016-08-05 18:00:04 -0700571 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
Yabin Cui6faf0262016-06-09 14:09:39 -0700572
573 // Verify package.
574 ui->Print("Verifying update package...\n");
575 auto t0 = std::chrono::system_clock::now();
576 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
577 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
578 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
579 if (err != VERIFY_SUCCESS) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700580 LOG(ERROR) << "Signature verification failed";
581 LOG(ERROR) << "error: " << kZipVerificationFailure;
Yabin Cui6faf0262016-06-09 14:09:39 -0700582 return false;
583 }
584 return true;
585}