blob: 919f241a2531a73ac7a84e88ba3c6c7d2e380588 [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 Xu8cf5c8f2016-09-08 20:10:11 -070039#include <ziparchive/zip_archive.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"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070045#include "otautil/SysUtil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070047#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020048#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
Dan Albert8584fcf2016-10-27 03:08:08 +000050extern RecoveryUI* ui;
51
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
60static const int VERIFICATION_PROGRESS_TIME = 60;
61static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
62static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
63static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
64
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.
Tianjie Xu8176cf22016-10-18 14:13:07 -0700142static int check_newer_ab_build(ZipArchiveHandle zip)
Alex Deymo4344d632016-08-03 21:03:53 -0700143{
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);
153 }
154 }
155 char value[PROPERTY_VALUE_MAX];
156
157 property_get("ro.product.device", value, "");
158 const std::string& pkg_device = metadata["pre-device"];
159 if (pkg_device != value || pkg_device.empty()) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700160 LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700161 return INSTALL_ERROR;
162 }
163
164 // We allow the package to not have any serialno, but if it has a non-empty
165 // value it should match.
166 property_get("ro.serialno", value, "");
167 const std::string& pkg_serial_no = metadata["serialno"];
168 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700169 LOG(ERROR) << "Package is for serial " << pkg_serial_no;
Alex Deymo4344d632016-08-03 21:03:53 -0700170 return INSTALL_ERROR;
171 }
172
173 if (metadata["ota-type"] != "AB") {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700174 LOG(ERROR) << "Package is not A/B";
Alex Deymo4344d632016-08-03 21:03:53 -0700175 return INSTALL_ERROR;
176 }
177
178 // Incremental updates should match the current build.
179 property_get("ro.build.version.incremental", value, "");
180 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
181 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700182 LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700183 return INSTALL_ERROR;
184 }
185 property_get("ro.build.fingerprint", value, "");
186 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
187 if (!pkg_pre_build_fingerprint.empty() &&
188 pkg_pre_build_fingerprint != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700189 LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint
190 << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700191 return INSTALL_ERROR;
192 }
193
194 // Check for downgrade version.
Tianjie Xu073451a2016-09-01 11:51:17 -0700195 int64_t build_timestamp = property_get_int64(
Alex Deymo4344d632016-08-03 21:03:53 -0700196 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
Tianjie Xu073451a2016-09-01 11:51:17 -0700197 int64_t pkg_post_timestamp = 0;
Alex Deymo4344d632016-08-03 21:03:53 -0700198 // We allow to full update to the same version we are running, in case there
199 // is a problem with the current copy of that version.
200 if (metadata["post-timestamp"].empty() ||
201 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
Tianjie Xu073451a2016-09-01 11:51:17 -0700202 &pkg_post_timestamp) ||
203 pkg_post_timestamp < build_timestamp) {
Alex Deymo4344d632016-08-03 21:03:53 -0700204 if (metadata["ota-downgrade"] != "yes") {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700205 LOG(ERROR) << "Update package is older than the current build, expected a build "
Tianjie Xu073451a2016-09-01 11:51:17 -0700206 "newer than timestamp " << build_timestamp << " but package has "
207 "timestamp " << pkg_post_timestamp << " and downgrade not allowed.";
Alex Deymo4344d632016-08-03 21:03:53 -0700208 return INSTALL_ERROR;
209 }
210 if (pkg_pre_build_fingerprint.empty()) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700211 LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed.";
Alex Deymo4344d632016-08-03 21:03:53 -0700212 return INSTALL_ERROR;
213 }
214 }
215
216 return 0;
217}
218
219static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700220update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700221 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.
Tianjie Xu8176cf22016-10-18 14:13:07 -0700230 ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES);
231 ZipEntry properties_entry;
232 if (FindEntry(zip, property_name, &properties_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700233 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700234 return INSTALL_CORRUPT;
235 }
Tianjie Xu8176cf22016-10-18 14:13:07 -0700236 std::vector<uint8_t> payload_properties(
237 properties_entry.uncompressed_length);
238 if (ExtractToMemory(zip, &properties_entry, payload_properties.data(),
239 properties_entry.uncompressed_length) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700240 LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700241 return INSTALL_CORRUPT;
242 }
243
Tianjie Xu8176cf22016-10-18 14:13:07 -0700244 ZipString payload_name(AB_OTA_PAYLOAD);
245 ZipEntry payload_entry;
246 if (FindEntry(zip, payload_name, &payload_entry) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700247 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD;
Alex Deymo4344d632016-08-03 21:03:53 -0700248 return INSTALL_CORRUPT;
249 }
Tianjie Xu8176cf22016-10-18 14:13:07 -0700250 long payload_offset = payload_entry.offset;
Alex Deymo4344d632016-08-03 21:03:53 -0700251 *cmd = {
252 "/sbin/update_engine_sideload",
253 android::base::StringPrintf("--payload=file://%s", path),
254 android::base::StringPrintf("--offset=%ld", payload_offset),
255 "--headers=" + std::string(payload_properties.begin(),
256 payload_properties.end()),
257 android::base::StringPrintf("--status_fd=%d", status_fd),
258 };
259 return 0;
260}
261
262#else // !AB_OTA_UPDATER
263
264static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700265update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
Alex Deymo4344d632016-08-03 21:03:53 -0700266 int status_fd, std::vector<std::string>* cmd)
267{
268 // On traditional updates we extract the update binary from the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700269 ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME);
270 ZipEntry binary_entry;
271 if (FindEntry(zip, binary_name, &binary_entry) != 0) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700272 return INSTALL_CORRUPT;
273 }
274
Doug Zongker10e418d2011-10-28 10:33:05 -0700275 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700276 unlink(binary);
277 int fd = creat(binary, 0755);
278 if (fd < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700279 PLOG(ERROR) << "Can't make " << binary;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700280 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700281 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700282 int error = ExtractEntryToFile(zip, &binary_entry, fd);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700283 close(fd);
284
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700285 if (error != 0) {
286 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME
287 << " : " << ErrorCodeString(error);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700288 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700289 }
290
Alex Deymo4344d632016-08-03 21:03:53 -0700291 *cmd = {
292 binary,
293 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
294 std::to_string(status_fd),
295 path,
296 };
297 if (retry_count > 0)
298 cmd->push_back("retry");
299 return 0;
300}
301#endif // !AB_OTA_UPDATER
302
303// If the package contains an update binary, extract it and run it.
304static int
Tianjie Xu8176cf22016-10-18 14:13:07 -0700305try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
Alex Deymo4344d632016-08-03 21:03:53 -0700306 std::vector<std::string>& log_buffer, int retry_count)
307{
308 read_source_target_build(zip, log_buffer);
309
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700310 int pipefd[2];
311 pipe(pipefd);
312
Alex Deymo4344d632016-08-03 21:03:53 -0700313 std::vector<std::string> args;
314 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
Alex Deymo4344d632016-08-03 21:03:53 -0700315 if (ret) {
316 close(pipefd[0]);
317 close(pipefd[1]);
318 return ret;
319 }
320
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700321 // When executing the update binary contained in the package, the
322 // arguments passed are:
323 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700324 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700325 //
326 // - an fd to which the program can write in order to update the
327 // progress bar. The program can write single-line commands:
328 //
329 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700330 // fill up the next <frac> part of of the progress bar
331 // over <secs> seconds. If <secs> is zero, use
332 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700333 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700334 //
335 // set_progress <frac>
336 // <frac> should be between 0.0 and 1.0; sets the
337 // progress bar within the segment defined by the most
338 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700339 //
340 // firmware <"hboot"|"radio"> <filename>
341 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800342 // given partition on reboot.
343 //
344 // (API v2: <filename> may start with "PACKAGE:" to
345 // indicate taking a file from the OTA package.)
346 //
347 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700348 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700349 // ui_print <string>
350 // display <string> on the screen.
351 //
Tao Baob07e1f32015-04-10 16:14:52 -0700352 // wipe_cache
353 // a wipe of cache will be performed following a successful
354 // installation.
355 //
356 // clear_display
357 // turn off the text display.
358 //
359 // enable_reboot
360 // packages can explicitly request that they want the user
361 // to be able to reboot during installation (useful for
362 // debugging packages that don't exit).
363 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700364 // - the name of the package zip file.
365 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700366 // - an optional argument "retry" if this update is a retry of a failed
367 // update attempt.
368 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700369
Alex Deymo4344d632016-08-03 21:03:53 -0700370 // Convert the vector to a NULL-terminated char* array suitable for execv.
371 const char* chr_args[args.size() + 1];
372 chr_args[args.size()] = NULL;
373 for (size_t i = 0; i < args.size(); i++) {
374 chr_args[i] = args[i].c_str();
375 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700376
377 pid_t pid = fork();
Matthew Bouyackc8db4812016-09-20 19:08:42 -0700378
379 if (pid == -1) {
380 close(pipefd[0]);
381 close(pipefd[1]);
382 PLOG(ERROR) << "Failed to fork update binary";
383 return INSTALL_ERROR;
384 }
385
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700386 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700387 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700388 close(pipefd[0]);
Alex Deymo4344d632016-08-03 21:03:53 -0700389 execv(chr_args[0], const_cast<char**>(chr_args));
390 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700391 _exit(-1);
392 }
393 close(pipefd[1]);
394
Tao Bao145d8612015-03-25 15:51:15 -0700395 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800396 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700397
Doug Zongker64893cc2009-07-14 16:31:56 -0700398 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700399 FILE* from_child = fdopen(pipefd[0], "r");
400 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700401 char* command = strtok(buffer, " \n");
402 if (command == NULL) {
403 continue;
404 } else if (strcmp(command, "progress") == 0) {
405 char* fraction_s = strtok(NULL, " \n");
406 char* seconds_s = strtok(NULL, " \n");
407
408 float fraction = strtof(fraction_s, NULL);
409 int seconds = strtol(seconds_s, NULL, 10);
410
Doug Zongker74406302011-10-28 15:13:10 -0700411 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700412 } else if (strcmp(command, "set_progress") == 0) {
413 char* fraction_s = strtok(NULL, " \n");
414 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700415 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700416 } else if (strcmp(command, "ui_print") == 0) {
417 char* str = strtok(NULL, "\n");
418 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700419 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700420 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700421 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700422 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700423 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700424 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700425 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700426 } else if (strcmp(command, "clear_display") == 0) {
427 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700428 } else if (strcmp(command, "enable_reboot") == 0) {
429 // packages can explicitly request that they want the user
430 // to be able to reboot during installation (useful for
431 // debugging packages that don't exit).
432 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800433 } else if (strcmp(command, "retry_update") == 0) {
434 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700435 } else if (strcmp(command, "log") == 0) {
436 // Save the logging request from updater and write to
437 // last_install later.
438 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700439 } else {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700440 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700441 }
442 }
443 fclose(from_child);
444
445 int status;
446 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800447 if (retry_update) {
448 return INSTALL_RETRY;
449 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700450 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700451 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700452 return INSTALL_ERROR;
453 }
454
Doug Zongkere08991e2010-02-02 13:09:52 -0800455 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700456}
457
Doug Zongker469243e2011-04-12 09:28:10 -0700458static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700459really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700460 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800461{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700462 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700463 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700464 // Give verification half the progress bar...
465 ui->SetProgressType(RecoveryUI::DETERMINATE);
466 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xuc21edd42016-08-05 18:00:04 -0700467 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468
Doug Zongker99916f02014-01-13 14:16:58 -0800469 // Map the update package into memory.
470 ui->Print("Opening update package...\n");
471
Doug Zongker075ad802014-06-26 15:35:51 -0700472 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800473 if (path[0] == '@') {
474 ensure_path_mounted(path+1);
475 } else {
476 ensure_path_mounted(path);
477 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800478 }
479
Doug Zongker99916f02014-01-13 14:16:58 -0800480 MemMapping map;
481 if (sysMapFile(path, &map) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700482 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800483 return INSTALL_CORRUPT;
484 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800485
Elliott Hughes8febafa2016-04-13 16:39:56 -0700486 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700487 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700488 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800489 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700490 return INSTALL_CORRUPT;
491 }
492
Elliott Hughes8febafa2016-04-13 16:39:56 -0700493 // Try to open the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700494 ZipArchiveHandle zip;
Tianjie Xu8176cf22016-10-18 14:13:07 -0700495 int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800496 if (err != 0) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700497 LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
Tianjie Xu16255832016-04-30 11:49:59 -0700498 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
499
Doug Zongker99916f02014-01-13 14:16:58 -0800500 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700501 CloseArchive(zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800502 return INSTALL_CORRUPT;
503 }
504
Elliott Hughes8febafa2016-04-13 16:39:56 -0700505 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700506 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700507 if (retry_count > 0) {
508 ui->Print("Retry attempt: %d\n", retry_count);
509 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700510 ui->SetEnableReboot(false);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700511 int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700512 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700513 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800514
515 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700516 CloseArchive(zip);
Doug Zongker99916f02014-01-13 14:16:58 -0800517 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800518}
Doug Zongker469243e2011-04-12 09:28:10 -0700519
520int
Tao Bao145d8612015-03-25 15:51:15 -0700521install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700522 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700523{
Tao Bao682c34b2015-04-07 17:16:35 -0700524 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700525 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -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 Xudd874b12016-05-13 12:13:15 -0700535
Tao Baobadaac42016-09-26 11:39:14 -0700536 // Measure the time spent to apply OTA update in seconds.
537 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
538 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700539
Tao Baobadaac42016-09-26 11:39:14 -0700540 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
541 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
542 } else {
543 std::string uncrypt_status;
544 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
545 PLOG(WARNING) << "failed to read uncrypt status";
Tianjie Xu68fc81e2016-09-24 15:31:34 -0700546 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
Tao Bao1709d222016-10-13 16:04:07 -0700547 LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xue16e7992016-09-09 10:55:44 -0700548 } else {
Tao Baobadaac42016-09-26 11:39:14 -0700549 log_buffer.push_back(android::base::Trim(uncrypt_status));
Tianjie Xue16e7992016-09-09 10:55:44 -0700550 }
Doug Zongker469243e2011-04-12 09:28:10 -0700551 }
Tao Baobadaac42016-09-26 11:39:14 -0700552
553 // The first two lines need to be the package name and install result.
554 std::vector<std::string> log_header = {
555 path,
556 result == INSTALL_SUCCESS ? "1" : "0",
557 "time_total: " + std::to_string(time_total),
558 "retry: " + std::to_string(retry_count),
559 };
560 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
561 android::base::Join(log_buffer, "\n");
562 if (!android::base::WriteStringToFile(log_content, install_file)) {
563 PLOG(ERROR) << "failed to write " << install_file;
564 }
565
566 // Write a copy into last_log.
567 LOG(INFO) << log_content;
568
Doug Zongker469243e2011-04-12 09:28:10 -0700569 return result;
570}
Yabin Cui6faf0262016-06-09 14:09:39 -0700571
572bool verify_package(const unsigned char* package_data, size_t package_size) {
573 std::vector<Certificate> loadedKeys;
574 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700575 LOG(ERROR) << "Failed to load keys";
Yabin Cui6faf0262016-06-09 14:09:39 -0700576 return false;
577 }
Tianjie Xuc21edd42016-08-05 18:00:04 -0700578 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
Yabin Cui6faf0262016-06-09 14:09:39 -0700579
580 // Verify package.
581 ui->Print("Verifying update package...\n");
582 auto t0 = std::chrono::system_clock::now();
583 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
584 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
585 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
586 if (err != VERIFY_SUCCESS) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700587 LOG(ERROR) << "Signature verification failed";
588 LOG(ERROR) << "error: " << kZipVerificationFailure;
Yabin Cui6faf0262016-06-09 14:09:39 -0700589 return false;
590 }
591 return true;
592}