blob: a256d9b130e4b6ef5759d1ff0c6e92fdc65f6d60 [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>
Tao Bao5e535012017-03-16 17:37:38 -070030#include <functional>
Alex Deymo4344d632016-08-03 21:03:53 -070031#include <limits>
32#include <map>
Tianjie Xudd874b12016-05-13 12:13:15 -070033#include <string>
Tao Bao71e3e092016-02-02 14:02:27 -080034#include <vector>
35
Tianjie Xue16e7992016-09-09 10:55:44 -070036#include <android-base/file.h>
37#include <android-base/logging.h>
Tao Bao20c581e2016-12-28 20:55:51 -080038#include <android-base/parsedouble.h>
Tianjie Xub0ddae52016-06-08 14:30:04 -070039#include <android-base/parseint.h>
Tao Baoefc35592017-01-08 22:45:47 -080040#include <android-base/properties.h>
Tianjie Xu16255832016-04-30 11:49:59 -070041#include <android-base/stringprintf.h>
42#include <android-base/strings.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070043#include <ziparchive/zip_archive.h>
Tianjie Xu16255832016-04-30 11:49:59 -070044
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070046#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047#include "minui/minui.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070048#include "otautil/SysUtil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070050#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020051#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080052
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
Tao Bao20c581e2016-12-28 20:55:51 -080061static constexpr int VERIFICATION_PROGRESS_TIME = 60;
62static constexpr float VERIFICATION_PROGRESS_FRACTION = 0.25;
63static constexpr float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
64static constexpr float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
Doug Zongker74406302011-10-28 15:13:10 -070065
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
Tianjie Xu8176cf22016-10-18 14:13:07 -070081bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070082 ZipString metadata_path(METADATA_PATH);
83 ZipEntry meta_entry;
Tianjie Xu8176cf22016-10-18 14:13:07 -070084 if (meta_data == nullptr) {
85 LOG(ERROR) << "string* meta_data can't be nullptr";
86 return false;
87 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070088 if (FindEntry(zip, metadata_path, &meta_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070089 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070090 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070091 }
92
Tianjie Xu8176cf22016-10-18 14:13:07 -070093 meta_data->resize(meta_entry.uncompressed_length, '\0');
94 if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]),
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070095 meta_entry.uncompressed_length) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070096 LOG(ERROR) << "Failed to read metadata in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070097 return false;
98 }
99 return true;
100}
101
102// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
Tianjie Xu8176cf22016-10-18 14:13:07 -0700103static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) {
Yabin Cui6faf0262016-06-09 14:09:39 -0700104 std::string meta_data;
105 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -0700106 return;
107 }
Tianjie Xub0ddae52016-06-08 14:30:04 -0700108 // Examples of the pre-build and post-build strings in metadata:
109 // pre-build-incremental=2943039
110 // post-build-incremental=2951741
111 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
112 for (const std::string& line : lines) {
113 std::string str = android::base::Trim(line);
114 if (android::base::StartsWith(str, "pre-build-incremental")){
115 int source_build = parse_build_number(str);
116 if (source_build != -1) {
117 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
118 source_build));
119 }
120 } else if (android::base::StartsWith(str, "post-build-incremental")) {
121 int target_build = parse_build_number(str);
122 if (target_build != -1) {
123 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
124 target_build));
125 }
126 }
127 }
128}
129
Alex Deymo4344d632016-08-03 21:03:53 -0700130// Extract the update binary from the open zip archive |zip| located at |path|
131// and store into |cmd| the command line that should be called. The |status_fd|
132// is the file descriptor the child process should use to report back the
133// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700134static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700135update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700136 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700137
Alex Deymo4344d632016-08-03 21:03:53 -0700138#ifdef AB_OTA_UPDATER
139
140// Parses the metadata of the OTA package in |zip| and checks whether we are
141// allowed to accept this A/B package. Downgrading is not allowed unless
142// explicitly enabled in the package and only for incremental packages.
Tao Baoefc35592017-01-08 22:45:47 -0800143static int check_newer_ab_build(ZipArchiveHandle zip) {
144 std::string metadata_str;
145 if (!read_metadata_from_package(zip, &metadata_str)) {
146 return INSTALL_CORRUPT;
147 }
148 std::map<std::string, std::string> metadata;
149 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
150 size_t eq = line.find('=');
151 if (eq != std::string::npos) {
152 metadata[line.substr(0, eq)] = line.substr(eq + 1);
Alex Deymo4344d632016-08-03 21:03:53 -0700153 }
Tao Baoefc35592017-01-08 22:45:47 -0800154 }
Alex Deymo4344d632016-08-03 21:03:53 -0700155
Tao Baoefc35592017-01-08 22:45:47 -0800156 std::string value = android::base::GetProperty("ro.product.device", "");
157 const std::string& pkg_device = metadata["pre-device"];
158 if (pkg_device != value || pkg_device.empty()) {
159 LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value;
160 return INSTALL_ERROR;
161 }
Alex Deymo4344d632016-08-03 21:03:53 -0700162
Tao Baoefc35592017-01-08 22:45:47 -0800163 // We allow the package to not have any serialno, but if it has a non-empty
164 // value it should match.
165 value = android::base::GetProperty("ro.serialno", "");
166 const std::string& pkg_serial_no = metadata["serialno"];
167 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
168 LOG(ERROR) << "Package is for serial " << pkg_serial_no;
169 return INSTALL_ERROR;
170 }
Alex Deymo4344d632016-08-03 21:03:53 -0700171
Tao Baoefc35592017-01-08 22:45:47 -0800172 if (metadata["ota-type"] != "AB") {
173 LOG(ERROR) << "Package is not A/B";
174 return INSTALL_ERROR;
175 }
Alex Deymo4344d632016-08-03 21:03:53 -0700176
Tao Baoefc35592017-01-08 22:45:47 -0800177 // Incremental updates should match the current build.
178 value = android::base::GetProperty("ro.build.version.incremental", "");
179 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
180 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
181 LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value;
182 return INSTALL_ERROR;
183 }
Alex Deymo4344d632016-08-03 21:03:53 -0700184
Tao Baoefc35592017-01-08 22:45:47 -0800185 value = android::base::GetProperty("ro.build.fingerprint", "");
186 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
187 if (!pkg_pre_build_fingerprint.empty() && pkg_pre_build_fingerprint != value) {
188 LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint << " but expected "
189 << value;
190 return INSTALL_ERROR;
191 }
Alex Deymo4344d632016-08-03 21:03:53 -0700192
Tao Baoefc35592017-01-08 22:45:47 -0800193 // Check for downgrade version.
194 int64_t build_timestamp =
195 android::base::GetIntProperty("ro.build.date.utc", std::numeric_limits<int64_t>::max());
196 int64_t pkg_post_timestamp = 0;
197 // We allow to full update to the same version we are running, in case there
198 // is a problem with the current copy of that version.
199 if (metadata["post-timestamp"].empty() ||
200 !android::base::ParseInt(metadata["post-timestamp"].c_str(), &pkg_post_timestamp) ||
201 pkg_post_timestamp < build_timestamp) {
202 if (metadata["ota-downgrade"] != "yes") {
203 LOG(ERROR) << "Update package is older than the current build, expected a build "
204 "newer than timestamp "
205 << build_timestamp << " but package has timestamp " << pkg_post_timestamp
206 << " and downgrade not allowed.";
207 return INSTALL_ERROR;
208 }
209 if (pkg_pre_build_fingerprint.empty()) {
210 LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed.";
211 return INSTALL_ERROR;
212 }
213 }
214
215 return 0;
Alex Deymo4344d632016-08-03 21:03:53 -0700216}
217
218static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700219update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700220 int status_fd, std::vector<std::string>* cmd)
221{
222 int ret = check_newer_ab_build(zip);
223 if (ret) {
224 return ret;
225 }
226
227 // For A/B updates we extract the payload properties to a buffer and obtain
228 // the RAW payload offset in the zip file.
Tianjie Xu8176cf22016-10-18 14:13:07 -0700229 ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES);
230 ZipEntry properties_entry;
231 if (FindEntry(zip, property_name, &properties_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700232 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700233 return INSTALL_CORRUPT;
234 }
Tianjie Xu8176cf22016-10-18 14:13:07 -0700235 std::vector<uint8_t> payload_properties(
236 properties_entry.uncompressed_length);
237 if (ExtractToMemory(zip, &properties_entry, payload_properties.data(),
238 properties_entry.uncompressed_length) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700239 LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700240 return INSTALL_CORRUPT;
241 }
242
Tianjie Xu8176cf22016-10-18 14:13:07 -0700243 ZipString payload_name(AB_OTA_PAYLOAD);
244 ZipEntry payload_entry;
245 if (FindEntry(zip, payload_name, &payload_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700246 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD;
Alex Deymo4344d632016-08-03 21:03:53 -0700247 return INSTALL_CORRUPT;
248 }
Tianjie Xu8176cf22016-10-18 14:13:07 -0700249 long payload_offset = payload_entry.offset;
Alex Deymo4344d632016-08-03 21:03:53 -0700250 *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
Tianjie Xu8176cf22016-10-18 14:13:07 -0700264update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700265 int status_fd, std::vector<std::string>* cmd)
266{
267 // On traditional updates we extract the update binary from the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700268 ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME);
269 ZipEntry binary_entry;
270 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700271 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) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700278 PLOG(ERROR) << "Can't make " << binary;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700279 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700280 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700281 int error = ExtractEntryToFile(zip, &binary_entry, fd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700282 close(fd);
283
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700284 if (error != 0) {
285 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME
286 << " : " << ErrorCodeString(error);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700287 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700288 }
289
Alex Deymo4344d632016-08-03 21:03:53 -0700290 *cmd = {
291 binary,
292 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
293 std::to_string(status_fd),
294 path,
295 };
296 if (retry_count > 0)
297 cmd->push_back("retry");
298 return 0;
299}
300#endif // !AB_OTA_UPDATER
301
302// If the package contains an update binary, extract it and run it.
Tao Bao20c581e2016-12-28 20:55:51 -0800303static int try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
304 std::vector<std::string>& log_buffer, int retry_count) {
305 read_source_target_build(zip, log_buffer);
Alex Deymo4344d632016-08-03 21:03:53 -0700306
Tao Bao20c581e2016-12-28 20:55:51 -0800307 int pipefd[2];
308 pipe(pipefd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700309
Tao Bao20c581e2016-12-28 20:55:51 -0800310 std::vector<std::string> args;
311 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
312 if (ret) {
313 close(pipefd[0]);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700314 close(pipefd[1]);
Tao Bao20c581e2016-12-28 20:55:51 -0800315 return ret;
316 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700317
Tao Bao20c581e2016-12-28 20:55:51 -0800318 // When executing the update binary contained in the package, the
319 // arguments passed are:
320 //
321 // - the version number for this interface
322 //
323 // - an FD to which the program can write in order to update the
324 // progress bar. The program can write single-line commands:
325 //
326 // progress <frac> <secs>
327 // fill up the next <frac> part of of the progress bar
328 // over <secs> seconds. If <secs> is zero, use
329 // set_progress commands to manually control the
330 // progress of this segment of the bar.
331 //
332 // set_progress <frac>
333 // <frac> should be between 0.0 and 1.0; sets the
334 // progress bar within the segment defined by the most
335 // recent progress command.
336 //
337 // ui_print <string>
338 // display <string> on the screen.
339 //
340 // wipe_cache
341 // a wipe of cache will be performed following a successful
342 // installation.
343 //
344 // clear_display
345 // turn off the text display.
346 //
347 // enable_reboot
348 // packages can explicitly request that they want the user
349 // to be able to reboot during installation (useful for
350 // debugging packages that don't exit).
351 //
352 // retry_update
353 // updater encounters some issue during the update. It requests
354 // a reboot to retry the same package automatically.
355 //
356 // log <string>
357 // updater requests logging the string (e.g. cause of the
358 // failure).
359 //
360 // - the name of the package zip file.
361 //
362 // - an optional argument "retry" if this update is a retry of a failed
363 // update attempt.
364 //
Doug Zongkerd0181b82011-10-19 10:51:12 -0700365
Tao Bao20c581e2016-12-28 20:55:51 -0800366 // Convert the vector to a NULL-terminated char* array suitable for execv.
367 const char* chr_args[args.size() + 1];
368 chr_args[args.size()] = nullptr;
369 for (size_t i = 0; i < args.size(); i++) {
370 chr_args[i] = args[i].c_str();
371 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700372
Tao Bao20c581e2016-12-28 20:55:51 -0800373 pid_t pid = fork();
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700374
Tao Bao20c581e2016-12-28 20:55:51 -0800375 if (pid == -1) {
376 close(pipefd[0]);
377 close(pipefd[1]);
378 PLOG(ERROR) << "Failed to fork update binary";
379 return INSTALL_ERROR;
380 }
381
382 if (pid == 0) {
383 umask(022);
384 close(pipefd[0]);
385 execv(chr_args[0], const_cast<char**>(chr_args));
Tianjie Xuab1abae2017-01-30 16:48:52 -0800386 // Bug: 34769056
387 // We shouldn't use LOG/PLOG in the forked process, since they may cause
388 // the child process to hang. This deadlock results from an improperly
389 // copied mutex in the ui functions.
390 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Tao Bao3da88012017-02-03 13:09:23 -0800391 _exit(EXIT_FAILURE);
Tao Bao20c581e2016-12-28 20:55:51 -0800392 }
393 close(pipefd[1]);
394
395 *wipe_cache = false;
396 bool retry_update = false;
397
398 char buffer[1024];
399 FILE* from_child = fdopen(pipefd[0], "r");
400 while (fgets(buffer, sizeof(buffer), from_child) != nullptr) {
401 std::string line(buffer);
402 size_t space = line.find_first_of(" \n");
403 std::string command(line.substr(0, space));
404 if (command.empty()) continue;
405
406 // Get rid of the leading and trailing space and/or newline.
407 std::string args = space == std::string::npos ? "" : android::base::Trim(line.substr(space));
408
409 if (command == "progress") {
410 std::vector<std::string> tokens = android::base::Split(args, " ");
411 double fraction;
412 int seconds;
413 if (tokens.size() == 2 && android::base::ParseDouble(tokens[0].c_str(), &fraction) &&
414 android::base::ParseInt(tokens[1], &seconds)) {
415 ui->ShowProgress(fraction * (1 - VERIFICATION_PROGRESS_FRACTION), seconds);
416 } else {
417 LOG(ERROR) << "invalid \"progress\" parameters: " << line;
418 }
419 } else if (command == "set_progress") {
420 std::vector<std::string> tokens = android::base::Split(args, " ");
421 double fraction;
422 if (tokens.size() == 1 && android::base::ParseDouble(tokens[0].c_str(), &fraction)) {
423 ui->SetProgress(fraction);
424 } else {
425 LOG(ERROR) << "invalid \"set_progress\" parameters: " << line;
426 }
427 } else if (command == "ui_print") {
Tao Baof0136422017-01-21 13:03:25 -0800428 ui->PrintOnScreenOnly("%s\n", args.c_str());
Tao Bao20c581e2016-12-28 20:55:51 -0800429 fflush(stdout);
430 } else if (command == "wipe_cache") {
431 *wipe_cache = true;
432 } else if (command == "clear_display") {
433 ui->SetBackground(RecoveryUI::NONE);
434 } else if (command == "enable_reboot") {
435 // packages can explicitly request that they want the user
436 // to be able to reboot during installation (useful for
437 // debugging packages that don't exit).
438 ui->SetEnableReboot(true);
439 } else if (command == "retry_update") {
440 retry_update = true;
441 } else if (command == "log") {
442 if (!args.empty()) {
443 // Save the logging request from updater and write to last_install later.
444 log_buffer.push_back(args);
445 } else {
446 LOG(ERROR) << "invalid \"log\" parameters: " << line;
447 }
448 } else {
449 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700450 }
Tao Bao20c581e2016-12-28 20:55:51 -0800451 }
452 fclose(from_child);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700453
Tao Bao20c581e2016-12-28 20:55:51 -0800454 int status;
455 waitpid(pid, &status, 0);
456 if (retry_update) {
457 return INSTALL_RETRY;
458 }
459 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
460 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
461 return INSTALL_ERROR;
462 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700463
Tao Bao20c581e2016-12-28 20:55:51 -0800464 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700465}
466
Doug Zongker469243e2011-04-12 09:28:10 -0700467static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700468really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700469 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800470{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700471 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700472 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700473 // Give verification half the progress bar...
474 ui->SetProgressType(RecoveryUI::DETERMINATE);
475 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xuc21edd42016-08-05 18:00:04 -0700476 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800477
Doug Zongker99916f02014-01-13 14:16:58 -0800478 // Map the update package into memory.
479 ui->Print("Opening update package...\n");
480
Doug Zongker075ad802014-06-26 15:35:51 -0700481 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800482 if (path[0] == '@') {
483 ensure_path_mounted(path+1);
484 } else {
485 ensure_path_mounted(path);
486 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800487 }
488
Doug Zongker99916f02014-01-13 14:16:58 -0800489 MemMapping map;
490 if (sysMapFile(path, &map) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700491 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800492 return INSTALL_CORRUPT;
493 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494
Elliott Hughes8febafa2016-04-13 16:39:56 -0700495 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700496 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700497 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800498 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700499 return INSTALL_CORRUPT;
500 }
501
Elliott Hughes8febafa2016-04-13 16:39:56 -0700502 // Try to open the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700503 ZipArchiveHandle zip;
Tianjie Xu8176cf22016-10-18 14:13:07 -0700504 int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800505 if (err != 0) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700506 LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
Tianjie Xu16255832016-04-30 11:49:59 -0700507 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
508
Doug Zongker99916f02014-01-13 14:16:58 -0800509 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700510 CloseArchive(zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800511 return INSTALL_CORRUPT;
512 }
513
Elliott Hughes8febafa2016-04-13 16:39:56 -0700514 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700515 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700516 if (retry_count > 0) {
517 ui->Print("Retry attempt: %d\n", retry_count);
518 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700519 ui->SetEnableReboot(false);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700520 int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700521 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700522 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800523
524 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700525 CloseArchive(zip);
Doug Zongker99916f02014-01-13 14:16:58 -0800526 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800527}
Doug Zongker469243e2011-04-12 09:28:10 -0700528
529int
Tao Bao145d8612015-03-25 15:51:15 -0700530install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700531 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700532{
Tao Bao682c34b2015-04-07 17:16:35 -0700533 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700534 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700535
Doug Zongker239ac6a2013-08-20 16:03:25 -0700536 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700537 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700538 if (setup_install_mounts() != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700539 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700540 result = INSTALL_ERROR;
541 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700542 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700543 }
Tianjie Xudd874b12016-05-13 12:13:15 -0700544
Tao Baobadaac42016-09-26 11:39:14 -0700545 // Measure the time spent to apply OTA update in seconds.
546 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
547 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700548
Tao Baobadaac42016-09-26 11:39:14 -0700549 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
550 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
551 } else {
552 std::string uncrypt_status;
553 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
554 PLOG(WARNING) << "failed to read uncrypt status";
Tianjie Xu68fc81e2016-09-24 15:31:34 -0700555 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
Tao Bao1709d222016-10-13 16:04:07 -0700556 LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xue16e7992016-09-09 10:55:44 -0700557 } else {
Tao Baobadaac42016-09-26 11:39:14 -0700558 log_buffer.push_back(android::base::Trim(uncrypt_status));
Tianjie Xue16e7992016-09-09 10:55:44 -0700559 }
Doug Zongker469243e2011-04-12 09:28:10 -0700560 }
Tao Baobadaac42016-09-26 11:39:14 -0700561
562 // The first two lines need to be the package name and install result.
563 std::vector<std::string> log_header = {
564 path,
565 result == INSTALL_SUCCESS ? "1" : "0",
566 "time_total: " + std::to_string(time_total),
567 "retry: " + std::to_string(retry_count),
568 };
569 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
570 android::base::Join(log_buffer, "\n");
571 if (!android::base::WriteStringToFile(log_content, install_file)) {
572 PLOG(ERROR) << "failed to write " << install_file;
573 }
574
575 // Write a copy into last_log.
576 LOG(INFO) << log_content;
577
Doug Zongker469243e2011-04-12 09:28:10 -0700578 return result;
579}
Yabin Cui6faf0262016-06-09 14:09:39 -0700580
581bool verify_package(const unsigned char* package_data, size_t package_size) {
Tao Bao5e535012017-03-16 17:37:38 -0700582 std::vector<Certificate> loadedKeys;
583 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
584 LOG(ERROR) << "Failed to load keys";
585 return false;
586 }
587 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
Yabin Cui6faf0262016-06-09 14:09:39 -0700588
Tao Bao5e535012017-03-16 17:37:38 -0700589 // Verify package.
590 ui->Print("Verifying update package...\n");
591 auto t0 = std::chrono::system_clock::now();
Tao Bao76fdb242017-03-20 17:09:13 -0700592 int err = verify_file(package_data, package_size, loadedKeys,
Tao Bao5e535012017-03-16 17:37:38 -0700593 std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
594 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
595 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
596 if (err != VERIFY_SUCCESS) {
597 LOG(ERROR) << "Signature verification failed";
598 LOG(ERROR) << "error: " << kZipVerificationFailure;
599 return false;
600 }
601 return true;
Yabin Cui6faf0262016-06-09 14:09:39 -0700602}