blob: ce89e0dc0dcdfc2f9be876d38fec65f452809f1a [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
Tao Bao20c581e2016-12-28 20:55:51 -080017#include "install.h"
18
Doug Zongkerb2ee9202009-06-04 10:24:53 -070019#include <ctype.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080020#include <errno.h>
21#include <fcntl.h>
Alex Deymo4e29ce02016-08-12 13:43:04 -070022#include <inttypes.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <limits.h>
Elliott Hughes26dbad22015-01-28 12:09:05 -080024#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <sys/stat.h>
Doug Zongkerb2ee9202009-06-04 10:24:53 -070026#include <sys/wait.h>
27#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Elliott Hughes8febafa2016-04-13 16:39:56 -070029#include <chrono>
Alex Deymo4344d632016-08-03 21:03:53 -070030#include <limits>
31#include <map>
Tianjie Xudd874b12016-05-13 12:13:15 -070032#include <string>
Tao Bao71e3e092016-02-02 14:02:27 -080033#include <vector>
34
Tianjie Xue16e7992016-09-09 10:55:44 -070035#include <android-base/file.h>
36#include <android-base/logging.h>
Tao Bao20c581e2016-12-28 20:55:51 -080037#include <android-base/parsedouble.h>
Tianjie Xub0ddae52016-06-08 14:30:04 -070038#include <android-base/parseint.h>
Tao Baoefc35592017-01-08 22:45:47 -080039#include <android-base/properties.h>
Tianjie Xu16255832016-04-30 11:49:59 -070040#include <android-base/stringprintf.h>
41#include <android-base/strings.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070042#include <ziparchive/zip_archive.h>
Tianjie Xu16255832016-04-30 11:49:59 -070043
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070045#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046#include "minui/minui.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070047#include "otautil/SysUtil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070049#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020050#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Doug Zongkerb2ee9202009-06-04 10:24:53 -070052#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo4344d632016-08-03 21:03:53 -070053static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
54static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070055#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070056static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
Tianjie Xue16e7992016-09-09 10:55:44 -070057static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080058
Doug Zongker74406302011-10-28 15:13:10 -070059// Default allocation of progress bar segments to operations
Tao Bao20c581e2016-12-28 20:55:51 -080060static constexpr int VERIFICATION_PROGRESS_TIME = 60;
61static constexpr float VERIFICATION_PROGRESS_FRACTION = 0.25;
62static constexpr float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
63static constexpr float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
Doug Zongker74406302011-10-28 15:13:10 -070064
Tianjie Xub0ddae52016-06-08 14:30:04 -070065// This function parses and returns the build.version.incremental
Chih-Hung Hsieh8b238112016-08-26 14:54:29 -070066static int parse_build_number(const std::string& str) {
67 size_t pos = str.find('=');
Tianjie Xub0ddae52016-06-08 14:30:04 -070068 if (pos != std::string::npos) {
69 std::string num_string = android::base::Trim(str.substr(pos+1));
70 int build_number;
71 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
72 return build_number;
73 }
74 }
75
Tianjie Xuc21edd42016-08-05 18:00:04 -070076 LOG(ERROR) << "Failed to parse build number in " << str;
Tianjie Xub0ddae52016-06-08 14:30:04 -070077 return -1;
78}
79
Tianjie Xu8176cf22016-10-18 14:13:07 -070080bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070081 ZipString metadata_path(METADATA_PATH);
82 ZipEntry meta_entry;
Tianjie Xu8176cf22016-10-18 14:13:07 -070083 if (meta_data == nullptr) {
84 LOG(ERROR) << "string* meta_data can't be nullptr";
85 return false;
86 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070087 if (FindEntry(zip, metadata_path, &meta_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070088 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070089 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070090 }
91
Tianjie Xu8176cf22016-10-18 14:13:07 -070092 meta_data->resize(meta_entry.uncompressed_length, '\0');
93 if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]),
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070094 meta_entry.uncompressed_length) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070095 LOG(ERROR) << "Failed to read metadata in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070096 return false;
97 }
98 return true;
99}
100
101// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
Tianjie Xu8176cf22016-10-18 14:13:07 -0700102static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) {
Yabin Cui6faf0262016-06-09 14:09:39 -0700103 std::string meta_data;
104 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -0700105 return;
106 }
Tianjie Xub0ddae52016-06-08 14:30:04 -0700107 // Examples of the pre-build and post-build strings in metadata:
108 // pre-build-incremental=2943039
109 // post-build-incremental=2951741
110 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
111 for (const std::string& line : lines) {
112 std::string str = android::base::Trim(line);
113 if (android::base::StartsWith(str, "pre-build-incremental")){
114 int source_build = parse_build_number(str);
115 if (source_build != -1) {
116 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
117 source_build));
118 }
119 } else if (android::base::StartsWith(str, "post-build-incremental")) {
120 int target_build = parse_build_number(str);
121 if (target_build != -1) {
122 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
123 target_build));
124 }
125 }
126 }
127}
128
Alex Deymo4344d632016-08-03 21:03:53 -0700129// Extract the update binary from the open zip archive |zip| located at |path|
130// and store into |cmd| the command line that should be called. The |status_fd|
131// is the file descriptor the child process should use to report back the
132// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700133static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700134update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700135 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700136
Alex Deymo4344d632016-08-03 21:03:53 -0700137#ifdef AB_OTA_UPDATER
138
139// Parses the metadata of the OTA package in |zip| and checks whether we are
140// allowed to accept this A/B package. Downgrading is not allowed unless
141// explicitly enabled in the package and only for incremental packages.
Tao Baoefc35592017-01-08 22:45:47 -0800142static int check_newer_ab_build(ZipArchiveHandle zip) {
143 std::string metadata_str;
144 if (!read_metadata_from_package(zip, &metadata_str)) {
145 return INSTALL_CORRUPT;
146 }
147 std::map<std::string, std::string> metadata;
148 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
149 size_t eq = line.find('=');
150 if (eq != std::string::npos) {
151 metadata[line.substr(0, eq)] = line.substr(eq + 1);
Alex Deymo4344d632016-08-03 21:03:53 -0700152 }
Tao Baoefc35592017-01-08 22:45:47 -0800153 }
Alex Deymo4344d632016-08-03 21:03:53 -0700154
Tao Baoefc35592017-01-08 22:45:47 -0800155 std::string value = android::base::GetProperty("ro.product.device", "");
156 const std::string& pkg_device = metadata["pre-device"];
157 if (pkg_device != value || pkg_device.empty()) {
158 LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value;
159 return INSTALL_ERROR;
160 }
Alex Deymo4344d632016-08-03 21:03:53 -0700161
Tao Baoefc35592017-01-08 22:45:47 -0800162 // We allow the package to not have any serialno, but if it has a non-empty
163 // value it should match.
164 value = android::base::GetProperty("ro.serialno", "");
165 const std::string& pkg_serial_no = metadata["serialno"];
166 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
167 LOG(ERROR) << "Package is for serial " << pkg_serial_no;
168 return INSTALL_ERROR;
169 }
Alex Deymo4344d632016-08-03 21:03:53 -0700170
Tao Baoefc35592017-01-08 22:45:47 -0800171 if (metadata["ota-type"] != "AB") {
172 LOG(ERROR) << "Package is not A/B";
173 return INSTALL_ERROR;
174 }
Alex Deymo4344d632016-08-03 21:03:53 -0700175
Tao Baoefc35592017-01-08 22:45:47 -0800176 // Incremental updates should match the current build.
177 value = android::base::GetProperty("ro.build.version.incremental", "");
178 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
179 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
180 LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value;
181 return INSTALL_ERROR;
182 }
Alex Deymo4344d632016-08-03 21:03:53 -0700183
Tao Baoefc35592017-01-08 22:45:47 -0800184 value = android::base::GetProperty("ro.build.fingerprint", "");
185 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
186 if (!pkg_pre_build_fingerprint.empty() && pkg_pre_build_fingerprint != value) {
187 LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint << " but expected "
188 << value;
189 return INSTALL_ERROR;
190 }
Alex Deymo4344d632016-08-03 21:03:53 -0700191
Tao Baoefc35592017-01-08 22:45:47 -0800192 // Check for downgrade version.
193 int64_t build_timestamp =
194 android::base::GetIntProperty("ro.build.date.utc", std::numeric_limits<int64_t>::max());
195 int64_t pkg_post_timestamp = 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(), &pkg_post_timestamp) ||
200 pkg_post_timestamp < build_timestamp) {
201 if (metadata["ota-downgrade"] != "yes") {
202 LOG(ERROR) << "Update package is older than the current build, expected a build "
203 "newer than timestamp "
204 << build_timestamp << " but package has timestamp " << pkg_post_timestamp
205 << " and downgrade not allowed.";
206 return INSTALL_ERROR;
207 }
208 if (pkg_pre_build_fingerprint.empty()) {
209 LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed.";
210 return INSTALL_ERROR;
211 }
212 }
213
214 return 0;
Alex Deymo4344d632016-08-03 21:03:53 -0700215}
216
217static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700218update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700219 int status_fd, std::vector<std::string>* cmd)
220{
221 int ret = check_newer_ab_build(zip);
222 if (ret) {
223 return ret;
224 }
225
226 // For A/B updates we extract the payload properties to a buffer and obtain
227 // the RAW payload offset in the zip file.
Tianjie Xu8176cf22016-10-18 14:13:07 -0700228 ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES);
229 ZipEntry properties_entry;
230 if (FindEntry(zip, property_name, &properties_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700231 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700232 return INSTALL_CORRUPT;
233 }
Tianjie Xu8176cf22016-10-18 14:13:07 -0700234 std::vector<uint8_t> payload_properties(
235 properties_entry.uncompressed_length);
236 if (ExtractToMemory(zip, &properties_entry, payload_properties.data(),
237 properties_entry.uncompressed_length) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700238 LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700239 return INSTALL_CORRUPT;
240 }
241
Tianjie Xu8176cf22016-10-18 14:13:07 -0700242 ZipString payload_name(AB_OTA_PAYLOAD);
243 ZipEntry payload_entry;
244 if (FindEntry(zip, payload_name, &payload_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700245 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD;
Alex Deymo4344d632016-08-03 21:03:53 -0700246 return INSTALL_CORRUPT;
247 }
Tianjie Xu8176cf22016-10-18 14:13:07 -0700248 long payload_offset = payload_entry.offset;
Alex Deymo4344d632016-08-03 21:03:53 -0700249 *cmd = {
250 "/sbin/update_engine_sideload",
251 android::base::StringPrintf("--payload=file://%s", path),
252 android::base::StringPrintf("--offset=%ld", payload_offset),
253 "--headers=" + std::string(payload_properties.begin(),
254 payload_properties.end()),
255 android::base::StringPrintf("--status_fd=%d", status_fd),
256 };
257 return 0;
258}
259
260#else // !AB_OTA_UPDATER
261
262static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700263update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700264 int status_fd, std::vector<std::string>* cmd)
265{
266 // On traditional updates we extract the update binary from the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700267 ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME);
268 ZipEntry binary_entry;
269 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700270 return INSTALL_CORRUPT;
271 }
272
Doug Zongker10e418d2011-10-28 10:33:05 -0700273 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700274 unlink(binary);
275 int fd = creat(binary, 0755);
276 if (fd < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700277 PLOG(ERROR) << "Can't make " << binary;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700278 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700279 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700280 int error = ExtractEntryToFile(zip, &binary_entry, fd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700281 close(fd);
282
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700283 if (error != 0) {
284 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME
285 << " : " << ErrorCodeString(error);
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.
Tao Bao20c581e2016-12-28 20:55:51 -0800302static int try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
303 std::vector<std::string>& log_buffer, int retry_count) {
304 read_source_target_build(zip, log_buffer);
Alex Deymo4344d632016-08-03 21:03:53 -0700305
Tao Bao20c581e2016-12-28 20:55:51 -0800306 int pipefd[2];
307 pipe(pipefd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700308
Tao Bao20c581e2016-12-28 20:55:51 -0800309 std::vector<std::string> args;
310 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
311 if (ret) {
312 close(pipefd[0]);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700313 close(pipefd[1]);
Tao Bao20c581e2016-12-28 20:55:51 -0800314 return ret;
315 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700316
Tao Bao20c581e2016-12-28 20:55:51 -0800317 // When executing the update binary contained in the package, the
318 // arguments passed are:
319 //
320 // - the version number for this interface
321 //
322 // - an FD to which the program can write in order to update the
323 // progress bar. The program can write single-line commands:
324 //
325 // progress <frac> <secs>
326 // fill up the next <frac> part of of the progress bar
327 // over <secs> seconds. If <secs> is zero, use
328 // set_progress commands to manually control the
329 // progress of this segment of the bar.
330 //
331 // set_progress <frac>
332 // <frac> should be between 0.0 and 1.0; sets the
333 // progress bar within the segment defined by the most
334 // recent progress command.
335 //
336 // ui_print <string>
337 // display <string> on the screen.
338 //
339 // wipe_cache
340 // a wipe of cache will be performed following a successful
341 // installation.
342 //
343 // clear_display
344 // turn off the text display.
345 //
346 // enable_reboot
347 // packages can explicitly request that they want the user
348 // to be able to reboot during installation (useful for
349 // debugging packages that don't exit).
350 //
351 // retry_update
352 // updater encounters some issue during the update. It requests
353 // a reboot to retry the same package automatically.
354 //
355 // log <string>
356 // updater requests logging the string (e.g. cause of the
357 // failure).
358 //
359 // - the name of the package zip file.
360 //
361 // - an optional argument "retry" if this update is a retry of a failed
362 // update attempt.
363 //
Doug Zongkerd0181b82011-10-19 10:51:12 -0700364
Tao Bao20c581e2016-12-28 20:55:51 -0800365 // 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()] = nullptr;
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
Tao Bao20c581e2016-12-28 20:55:51 -0800372 pid_t pid = fork();
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700373
Tao Bao20c581e2016-12-28 20:55:51 -0800374 if (pid == -1) {
375 close(pipefd[0]);
376 close(pipefd[1]);
377 PLOG(ERROR) << "Failed to fork update binary";
378 return INSTALL_ERROR;
379 }
380
381 if (pid == 0) {
382 umask(022);
383 close(pipefd[0]);
384 execv(chr_args[0], const_cast<char**>(chr_args));
Tianjie Xuab1abae2017-01-30 16:48:52 -0800385 // Bug: 34769056
386 // We shouldn't use LOG/PLOG in the forked process, since they may cause
387 // the child process to hang. This deadlock results from an improperly
388 // copied mutex in the ui functions.
389 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Tao Bao3da88012017-02-03 13:09:23 -0800390 _exit(EXIT_FAILURE);
Tao Bao20c581e2016-12-28 20:55:51 -0800391 }
392 close(pipefd[1]);
393
394 *wipe_cache = false;
395 bool retry_update = false;
396
397 char buffer[1024];
398 FILE* from_child = fdopen(pipefd[0], "r");
399 while (fgets(buffer, sizeof(buffer), from_child) != nullptr) {
400 std::string line(buffer);
401 size_t space = line.find_first_of(" \n");
402 std::string command(line.substr(0, space));
403 if (command.empty()) continue;
404
405 // Get rid of the leading and trailing space and/or newline.
406 std::string args = space == std::string::npos ? "" : android::base::Trim(line.substr(space));
407
408 if (command == "progress") {
409 std::vector<std::string> tokens = android::base::Split(args, " ");
410 double fraction;
411 int seconds;
412 if (tokens.size() == 2 && android::base::ParseDouble(tokens[0].c_str(), &fraction) &&
413 android::base::ParseInt(tokens[1], &seconds)) {
414 ui->ShowProgress(fraction * (1 - VERIFICATION_PROGRESS_FRACTION), seconds);
415 } else {
416 LOG(ERROR) << "invalid \"progress\" parameters: " << line;
417 }
418 } else if (command == "set_progress") {
419 std::vector<std::string> tokens = android::base::Split(args, " ");
420 double fraction;
421 if (tokens.size() == 1 && android::base::ParseDouble(tokens[0].c_str(), &fraction)) {
422 ui->SetProgress(fraction);
423 } else {
424 LOG(ERROR) << "invalid \"set_progress\" parameters: " << line;
425 }
426 } else if (command == "ui_print") {
Tao Baof0136422017-01-21 13:03:25 -0800427 ui->PrintOnScreenOnly("%s\n", args.c_str());
Tao Bao20c581e2016-12-28 20:55:51 -0800428 fflush(stdout);
429 } else if (command == "wipe_cache") {
430 *wipe_cache = true;
431 } else if (command == "clear_display") {
432 ui->SetBackground(RecoveryUI::NONE);
433 } else if (command == "enable_reboot") {
434 // packages can explicitly request that they want the user
435 // to be able to reboot during installation (useful for
436 // debugging packages that don't exit).
437 ui->SetEnableReboot(true);
438 } else if (command == "retry_update") {
439 retry_update = true;
440 } else if (command == "log") {
441 if (!args.empty()) {
442 // Save the logging request from updater and write to last_install later.
443 log_buffer.push_back(args);
444 } else {
445 LOG(ERROR) << "invalid \"log\" parameters: " << line;
446 }
447 } else {
448 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700449 }
Tao Bao20c581e2016-12-28 20:55:51 -0800450 }
451 fclose(from_child);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700452
Tao Bao20c581e2016-12-28 20:55:51 -0800453 int status;
454 waitpid(pid, &status, 0);
455 if (retry_update) {
456 return INSTALL_RETRY;
457 }
458 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
459 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
460 return INSTALL_ERROR;
461 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700462
Tao Bao20c581e2016-12-28 20:55:51 -0800463 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700464}
465
Doug Zongker469243e2011-04-12 09:28:10 -0700466static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700467really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700468 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800469{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700470 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700471 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700472 // Give verification half the progress bar...
473 ui->SetProgressType(RecoveryUI::DETERMINATE);
474 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xuc21edd42016-08-05 18:00:04 -0700475 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800476
Doug Zongker99916f02014-01-13 14:16:58 -0800477 // Map the update package into memory.
478 ui->Print("Opening update package...\n");
479
Doug Zongker075ad802014-06-26 15:35:51 -0700480 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800481 if (path[0] == '@') {
482 ensure_path_mounted(path+1);
483 } else {
484 ensure_path_mounted(path);
485 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800486 }
487
Doug Zongker99916f02014-01-13 14:16:58 -0800488 MemMapping map;
489 if (sysMapFile(path, &map) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700490 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800491 return INSTALL_CORRUPT;
492 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800493
Elliott Hughes8febafa2016-04-13 16:39:56 -0700494 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700495 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700496 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800497 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700498 return INSTALL_CORRUPT;
499 }
500
Elliott Hughes8febafa2016-04-13 16:39:56 -0700501 // Try to open the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700502 ZipArchiveHandle zip;
Tianjie Xu8176cf22016-10-18 14:13:07 -0700503 int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800504 if (err != 0) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700505 LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
Tianjie Xu16255832016-04-30 11:49:59 -0700506 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
507
Doug Zongker99916f02014-01-13 14:16:58 -0800508 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700509 CloseArchive(zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800510 return INSTALL_CORRUPT;
511 }
512
Elliott Hughes8febafa2016-04-13 16:39:56 -0700513 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700514 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700515 if (retry_count > 0) {
516 ui->Print("Retry attempt: %d\n", retry_count);
517 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700518 ui->SetEnableReboot(false);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700519 int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700520 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700521 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800522
523 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700524 CloseArchive(zip);
Doug Zongker99916f02014-01-13 14:16:58 -0800525 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800526}
Doug Zongker469243e2011-04-12 09:28:10 -0700527
528int
Tao Bao145d8612015-03-25 15:51:15 -0700529install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700530 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700531{
Tao Bao682c34b2015-04-07 17:16:35 -0700532 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700533 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700534
Doug Zongker239ac6a2013-08-20 16:03:25 -0700535 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700536 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700537 if (setup_install_mounts() != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700538 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700539 result = INSTALL_ERROR;
540 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700541 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700542 }
Tianjie Xudd874b12016-05-13 12:13:15 -0700543
Tao Baobadaac42016-09-26 11:39:14 -0700544 // Measure the time spent to apply OTA update in seconds.
545 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
546 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700547
Tao Baobadaac42016-09-26 11:39:14 -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 Xu68fc81e2016-09-24 15:31:34 -0700554 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
Tao Baobadaac42016-09-26 11:39:14 -0700555 PLOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xue16e7992016-09-09 10:55:44 -0700556 } else {
Tao Baobadaac42016-09-26 11:39:14 -0700557 log_buffer.push_back(android::base::Trim(uncrypt_status));
Tianjie Xue16e7992016-09-09 10:55:44 -0700558 }
Doug Zongker469243e2011-04-12 09:28:10 -0700559 }
Tao Baobadaac42016-09-26 11:39:14 -0700560
561 // The first two lines need to be the package name and install result.
562 std::vector<std::string> log_header = {
563 path,
564 result == INSTALL_SUCCESS ? "1" : "0",
565 "time_total: " + std::to_string(time_total),
566 "retry: " + std::to_string(retry_count),
567 };
568 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
569 android::base::Join(log_buffer, "\n");
570 if (!android::base::WriteStringToFile(log_content, install_file)) {
571 PLOG(ERROR) << "failed to write " << install_file;
572 }
573
574 // Write a copy into last_log.
575 LOG(INFO) << log_content;
576
Doug Zongker469243e2011-04-12 09:28:10 -0700577 return result;
578}
Yabin Cui6faf0262016-06-09 14:09:39 -0700579
580bool verify_package(const unsigned char* package_data, size_t package_size) {
581 std::vector<Certificate> loadedKeys;
582 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700583 LOG(ERROR) << "Failed to load keys";
Yabin Cui6faf0262016-06-09 14:09:39 -0700584 return false;
585 }
Tianjie Xuc21edd42016-08-05 18:00:04 -0700586 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
Yabin Cui6faf0262016-06-09 14:09:39 -0700587
588 // Verify package.
589 ui->Print("Verifying update package...\n");
590 auto t0 = std::chrono::system_clock::now();
591 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
592 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
593 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
594 if (err != VERIFY_SUCCESS) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700595 LOG(ERROR) << "Signature verification failed";
596 LOG(ERROR) << "error: " << kZipVerificationFailure;
Yabin Cui6faf0262016-06-09 14:09:39 -0700597 return false;
598 }
599 return true;
600}