blob: 4a856cd7102b41a1e2cf1796ac3dcb50aed6e2c2 [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 Xub0ddae52016-06-08 14:30:04 -070033#include <android-base/parseint.h>
Tianjie Xu16255832016-04-30 11:49:59 -070034#include <android-base/stringprintf.h>
35#include <android-base/strings.h>
Alex Deymo4344d632016-08-03 21:03:53 -070036#include <cutils/properties.h>
Tianjie Xuc21edd42016-08-05 18:00:04 -070037#include <android-base/logging.h>
Tianjie Xu16255832016-04-30 11:49:59 -070038
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070040#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042#include "minui/minui.h"
43#include "minzip/SysUtil.h"
44#include "minzip/Zip.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070046#include "ui.h"
Mattias Nissler452df6d2016-04-04 16:17:01 +020047#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Doug Zongker74406302011-10-28 15:13:10 -070049extern RecoveryUI* ui;
50
Doug Zongkerb2ee9202009-06-04 10:24:53 -070051#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo4344d632016-08-03 21:03:53 -070052static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
53static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070054#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070055static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056
Doug Zongker74406302011-10-28 15:13:10 -070057// Default allocation of progress bar segments to operations
58static const int VERIFICATION_PROGRESS_TIME = 60;
59static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
60static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
61static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
62
Tianjie Xub0ddae52016-06-08 14:30:04 -070063// This function parses and returns the build.version.incremental
Chih-Hung Hsieh8b238112016-08-26 14:54:29 -070064static int parse_build_number(const std::string& str) {
65 size_t pos = str.find('=');
Tianjie Xub0ddae52016-06-08 14:30:04 -070066 if (pos != std::string::npos) {
67 std::string num_string = android::base::Trim(str.substr(pos+1));
68 int build_number;
69 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
70 return build_number;
71 }
72 }
73
Tianjie Xuc21edd42016-08-05 18:00:04 -070074 LOG(ERROR) << "Failed to parse build number in " << str;
Tianjie Xub0ddae52016-06-08 14:30:04 -070075 return -1;
76}
77
Yabin Cui6faf0262016-06-09 14:09:39 -070078bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070079 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
80 if (meta_entry == nullptr) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070081 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070082 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070083 }
84
Yabin Cui6faf0262016-06-09 14:09:39 -070085 meta_data->resize(meta_entry->uncompLen, '\0');
86 if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -070087 LOG(ERROR) << "Failed to read metadata in update package";
Yabin Cui6faf0262016-06-09 14:09:39 -070088 return false;
89 }
90 return true;
91}
92
93// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
94static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
95 std::string meta_data;
96 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070097 return;
98 }
Tianjie Xub0ddae52016-06-08 14:30:04 -070099 // Examples of the pre-build and post-build strings in metadata:
100 // pre-build-incremental=2943039
101 // post-build-incremental=2951741
102 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
103 for (const std::string& line : lines) {
104 std::string str = android::base::Trim(line);
105 if (android::base::StartsWith(str, "pre-build-incremental")){
106 int source_build = parse_build_number(str);
107 if (source_build != -1) {
108 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
109 source_build));
110 }
111 } else if (android::base::StartsWith(str, "post-build-incremental")) {
112 int target_build = parse_build_number(str);
113 if (target_build != -1) {
114 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
115 target_build));
116 }
117 }
118 }
119}
120
Alex Deymo4344d632016-08-03 21:03:53 -0700121// Extract the update binary from the open zip archive |zip| located at |path|
122// and store into |cmd| the command line that should be called. The |status_fd|
123// is the file descriptor the child process should use to report back the
124// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700125static int
Alex Deymo4344d632016-08-03 21:03:53 -0700126update_binary_command(const char* path, ZipArchive* zip, int retry_count,
127 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700128
Alex Deymo4344d632016-08-03 21:03:53 -0700129#ifdef AB_OTA_UPDATER
130
131// Parses the metadata of the OTA package in |zip| and checks whether we are
132// allowed to accept this A/B package. Downgrading is not allowed unless
133// explicitly enabled in the package and only for incremental packages.
134static int check_newer_ab_build(ZipArchive* zip)
135{
136 std::string metadata_str;
137 if (!read_metadata_from_package(zip, &metadata_str)) {
138 return INSTALL_CORRUPT;
139 }
140 std::map<std::string, std::string> metadata;
141 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
142 size_t eq = line.find('=');
143 if (eq != std::string::npos) {
144 metadata[line.substr(0, eq)] = line.substr(eq + 1);
145 }
146 }
147 char value[PROPERTY_VALUE_MAX];
148
149 property_get("ro.product.device", value, "");
150 const std::string& pkg_device = metadata["pre-device"];
151 if (pkg_device != value || pkg_device.empty()) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700152 LOG(ERROR) << "Package is for product " << pkg_device << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700153 return INSTALL_ERROR;
154 }
155
156 // We allow the package to not have any serialno, but if it has a non-empty
157 // value it should match.
158 property_get("ro.serialno", value, "");
159 const std::string& pkg_serial_no = metadata["serialno"];
160 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700161 LOG(ERROR) << "Package is for serial " << pkg_serial_no;
Alex Deymo4344d632016-08-03 21:03:53 -0700162 return INSTALL_ERROR;
163 }
164
165 if (metadata["ota-type"] != "AB") {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700166 LOG(ERROR) << "Package is not A/B";
Alex Deymo4344d632016-08-03 21:03:53 -0700167 return INSTALL_ERROR;
168 }
169
170 // Incremental updates should match the current build.
171 property_get("ro.build.version.incremental", value, "");
172 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
173 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700174 LOG(ERROR) << "Package is for source build " << pkg_pre_build << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700175 return INSTALL_ERROR;
176 }
177 property_get("ro.build.fingerprint", value, "");
178 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
179 if (!pkg_pre_build_fingerprint.empty() &&
180 pkg_pre_build_fingerprint != value) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700181 LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint
182 << " but expected " << value;
Alex Deymo4344d632016-08-03 21:03:53 -0700183 return INSTALL_ERROR;
184 }
185
186 // Check for downgrade version.
Tianjie Xu073451a2016-09-01 11:51:17 -0700187 int64_t build_timestamp = property_get_int64(
Alex Deymo4344d632016-08-03 21:03:53 -0700188 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
Tianjie Xu073451a2016-09-01 11:51:17 -0700189 int64_t pkg_post_timestamp = 0;
Alex Deymo4344d632016-08-03 21:03:53 -0700190 // We allow to full update to the same version we are running, in case there
191 // is a problem with the current copy of that version.
192 if (metadata["post-timestamp"].empty() ||
193 !android::base::ParseInt(metadata["post-timestamp"].c_str(),
Tianjie Xu073451a2016-09-01 11:51:17 -0700194 &pkg_post_timestamp) ||
195 pkg_post_timestamp < build_timestamp) {
Alex Deymo4344d632016-08-03 21:03:53 -0700196 if (metadata["ota-downgrade"] != "yes") {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700197 LOG(ERROR) << "Update package is older than the current build, expected a build "
Tianjie Xu073451a2016-09-01 11:51:17 -0700198 "newer than timestamp " << build_timestamp << " but package has "
199 "timestamp " << pkg_post_timestamp << " and downgrade not allowed.";
Alex Deymo4344d632016-08-03 21:03:53 -0700200 return INSTALL_ERROR;
201 }
202 if (pkg_pre_build_fingerprint.empty()) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700203 LOG(ERROR) << "Downgrade package must have a pre-build version set, not allowed.";
Alex Deymo4344d632016-08-03 21:03:53 -0700204 return INSTALL_ERROR;
205 }
206 }
207
208 return 0;
209}
210
211static int
212update_binary_command(const char* path, ZipArchive* zip, int retry_count,
213 int status_fd, std::vector<std::string>* cmd)
214{
215 int ret = check_newer_ab_build(zip);
216 if (ret) {
217 return ret;
218 }
219
220 // For A/B updates we extract the payload properties to a buffer and obtain
221 // the RAW payload offset in the zip file.
222 const ZipEntry* properties_entry =
223 mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES);
224 if (!properties_entry) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700225 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700226 return INSTALL_CORRUPT;
227 }
228 std::vector<unsigned char> payload_properties(
229 mzGetZipEntryUncompLen(properties_entry));
230 if (!mzExtractZipEntryToBuffer(zip, properties_entry,
231 payload_properties.data())) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700232 LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES;
Alex Deymo4344d632016-08-03 21:03:53 -0700233 return INSTALL_CORRUPT;
234 }
235
236 const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD);
237 if (!payload_entry) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700238 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD;
Alex Deymo4344d632016-08-03 21:03:53 -0700239 return INSTALL_CORRUPT;
240 }
241 long payload_offset = mzGetZipEntryOffset(payload_entry);
242 *cmd = {
243 "/sbin/update_engine_sideload",
244 android::base::StringPrintf("--payload=file://%s", path),
245 android::base::StringPrintf("--offset=%ld", payload_offset),
246 "--headers=" + std::string(payload_properties.begin(),
247 payload_properties.end()),
248 android::base::StringPrintf("--status_fd=%d", status_fd),
249 };
250 return 0;
251}
252
253#else // !AB_OTA_UPDATER
254
255static int
256update_binary_command(const char* path, ZipArchive* zip, int retry_count,
257 int status_fd, std::vector<std::string>* cmd)
258{
259 // On traditional updates we extract the update binary from the package.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700260 const ZipEntry* binary_entry =
261 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
262 if (binary_entry == NULL) {
263 return INSTALL_CORRUPT;
264 }
265
Doug Zongker10e418d2011-10-28 10:33:05 -0700266 const char* binary = "/tmp/update_binary";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700267 unlink(binary);
268 int fd = creat(binary, 0755);
269 if (fd < 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700270 PLOG(ERROR) << "Can't make " << binary;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700271 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700272 }
273 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
274 close(fd);
275
276 if (!ok) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700277 LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700278 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700279 }
280
Alex Deymo4344d632016-08-03 21:03:53 -0700281 *cmd = {
282 binary,
283 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
284 std::to_string(status_fd),
285 path,
286 };
287 if (retry_count > 0)
288 cmd->push_back("retry");
289 return 0;
290}
291#endif // !AB_OTA_UPDATER
292
293// If the package contains an update binary, extract it and run it.
294static int
295try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache,
296 std::vector<std::string>& log_buffer, int retry_count)
297{
298 read_source_target_build(zip, log_buffer);
299
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700300 int pipefd[2];
301 pipe(pipefd);
302
Alex Deymo4344d632016-08-03 21:03:53 -0700303 std::vector<std::string> args;
304 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
305 mzCloseZipArchive(zip);
306 if (ret) {
307 close(pipefd[0]);
308 close(pipefd[1]);
309 return ret;
310 }
311
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700312 // When executing the update binary contained in the package, the
313 // arguments passed are:
314 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700315 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700316 //
317 // - an fd to which the program can write in order to update the
318 // progress bar. The program can write single-line commands:
319 //
320 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700321 // fill up the next <frac> part of of the progress bar
322 // over <secs> seconds. If <secs> is zero, use
323 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700324 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700325 //
326 // set_progress <frac>
327 // <frac> should be between 0.0 and 1.0; sets the
328 // progress bar within the segment defined by the most
329 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700330 //
331 // firmware <"hboot"|"radio"> <filename>
332 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800333 // given partition on reboot.
334 //
335 // (API v2: <filename> may start with "PACKAGE:" to
336 // indicate taking a file from the OTA package.)
337 //
338 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700339 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700340 // ui_print <string>
341 // display <string> on the screen.
342 //
Tao Baob07e1f32015-04-10 16:14:52 -0700343 // wipe_cache
344 // a wipe of cache will be performed following a successful
345 // installation.
346 //
347 // clear_display
348 // turn off the text display.
349 //
350 // enable_reboot
351 // packages can explicitly request that they want the user
352 // to be able to reboot during installation (useful for
353 // debugging packages that don't exit).
354 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700355 // - the name of the package zip file.
356 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700357 // - an optional argument "retry" if this update is a retry of a failed
358 // update attempt.
359 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700360
Alex Deymo4344d632016-08-03 21:03:53 -0700361 // Convert the vector to a NULL-terminated char* array suitable for execv.
362 const char* chr_args[args.size() + 1];
363 chr_args[args.size()] = NULL;
364 for (size_t i = 0; i < args.size(); i++) {
365 chr_args[i] = args[i].c_str();
366 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700367
368 pid_t pid = fork();
369 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700370 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700371 close(pipefd[0]);
Alex Deymo4344d632016-08-03 21:03:53 -0700372 execv(chr_args[0], const_cast<char**>(chr_args));
373 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700374 _exit(-1);
375 }
376 close(pipefd[1]);
377
Tao Bao145d8612015-03-25 15:51:15 -0700378 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800379 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700380
Doug Zongker64893cc2009-07-14 16:31:56 -0700381 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700382 FILE* from_child = fdopen(pipefd[0], "r");
383 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700384 char* command = strtok(buffer, " \n");
385 if (command == NULL) {
386 continue;
387 } else if (strcmp(command, "progress") == 0) {
388 char* fraction_s = strtok(NULL, " \n");
389 char* seconds_s = strtok(NULL, " \n");
390
391 float fraction = strtof(fraction_s, NULL);
392 int seconds = strtol(seconds_s, NULL, 10);
393
Doug Zongker74406302011-10-28 15:13:10 -0700394 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700395 } else if (strcmp(command, "set_progress") == 0) {
396 char* fraction_s = strtok(NULL, " \n");
397 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700398 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700399 } else if (strcmp(command, "ui_print") == 0) {
400 char* str = strtok(NULL, "\n");
401 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700402 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700403 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700404 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700405 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700406 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700407 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700408 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700409 } else if (strcmp(command, "clear_display") == 0) {
410 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700411 } else if (strcmp(command, "enable_reboot") == 0) {
412 // packages can explicitly request that they want the user
413 // to be able to reboot during installation (useful for
414 // debugging packages that don't exit).
415 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800416 } else if (strcmp(command, "retry_update") == 0) {
417 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700418 } else if (strcmp(command, "log") == 0) {
419 // Save the logging request from updater and write to
420 // last_install later.
421 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700422 } else {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700423 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700424 }
425 }
426 fclose(from_child);
427
428 int status;
429 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800430 if (retry_update) {
431 return INSTALL_RETRY;
432 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700433 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700434 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700435 return INSTALL_ERROR;
436 }
437
Doug Zongkere08991e2010-02-02 13:09:52 -0800438 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700439}
440
Doug Zongker469243e2011-04-12 09:28:10 -0700441static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700442really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700443 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800444{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700445 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700446 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700447 // Give verification half the progress bar...
448 ui->SetProgressType(RecoveryUI::DETERMINATE);
449 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xuc21edd42016-08-05 18:00:04 -0700450 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800451
Doug Zongker99916f02014-01-13 14:16:58 -0800452 // Map the update package into memory.
453 ui->Print("Opening update package...\n");
454
Doug Zongker075ad802014-06-26 15:35:51 -0700455 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800456 if (path[0] == '@') {
457 ensure_path_mounted(path+1);
458 } else {
459 ensure_path_mounted(path);
460 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800461 }
462
Doug Zongker99916f02014-01-13 14:16:58 -0800463 MemMapping map;
464 if (sysMapFile(path, &map) != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700465 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800466 return INSTALL_CORRUPT;
467 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468
Elliott Hughes8febafa2016-04-13 16:39:56 -0700469 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700470 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700471 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800472 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700473 return INSTALL_CORRUPT;
474 }
475
Elliott Hughes8febafa2016-04-13 16:39:56 -0700476 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800477 ZipArchive zip;
Yabin Cui6faf0262016-06-09 14:09:39 -0700478 int err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800479 if (err != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700480 LOG(ERROR) << "Can't open " << path;
Tianjie Xu16255832016-04-30 11:49:59 -0700481 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
482
Doug Zongker99916f02014-01-13 14:16:58 -0800483 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800484 return INSTALL_CORRUPT;
485 }
486
Elliott Hughes8febafa2016-04-13 16:39:56 -0700487 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700488 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700489 if (retry_count > 0) {
490 ui->Print("Retry attempt: %d\n", retry_count);
491 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700492 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700493 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700494 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700495 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800496
497 sysReleaseMap(&map);
498
499 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800500}
Doug Zongker469243e2011-04-12 09:28:10 -0700501
502int
Tao Bao145d8612015-03-25 15:51:15 -0700503install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700504 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700505{
Tao Bao682c34b2015-04-07 17:16:35 -0700506 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700507 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700508
Doug Zongkerd0181b82011-10-19 10:51:12 -0700509 FILE* install_log = fopen_path(install_file, "w");
Doug Zongker469243e2011-04-12 09:28:10 -0700510 if (install_log) {
511 fputs(path, install_log);
512 fputc('\n', install_log);
513 } else {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700514 PLOG(ERROR) << "failed to open last_install";
Doug Zongker469243e2011-04-12 09:28:10 -0700515 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700516 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700517 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700518 if (setup_install_mounts() != 0) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700519 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700520 result = INSTALL_ERROR;
521 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700522 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700523 }
Tianjie Xu16255832016-04-30 11:49:59 -0700524 if (install_log != nullptr) {
Doug Zongker469243e2011-04-12 09:28:10 -0700525 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
526 fputc('\n', install_log);
Tianjie Xudd874b12016-05-13 12:13:15 -0700527 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
528 int count = static_cast<int>(duration.count());
529 // Report the time spent to apply OTA update in seconds.
530 fprintf(install_log, "time_total: %d\n", count);
Tianjie Xu16255832016-04-30 11:49:59 -0700531 fprintf(install_log, "retry: %d\n", retry_count);
Tianjie Xudd874b12016-05-13 12:13:15 -0700532
533 for (const auto& s : log_buffer) {
534 fprintf(install_log, "%s\n", s.c_str());
535 }
536
Doug Zongker469243e2011-04-12 09:28:10 -0700537 fclose(install_log);
Doug Zongker469243e2011-04-12 09:28:10 -0700538 }
539 return result;
540}
Yabin Cui6faf0262016-06-09 14:09:39 -0700541
542bool verify_package(const unsigned char* package_data, size_t package_size) {
543 std::vector<Certificate> loadedKeys;
544 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700545 LOG(ERROR) << "Failed to load keys";
Yabin Cui6faf0262016-06-09 14:09:39 -0700546 return false;
547 }
Tianjie Xuc21edd42016-08-05 18:00:04 -0700548 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
Yabin Cui6faf0262016-06-09 14:09:39 -0700549
550 // Verify package.
551 ui->Print("Verifying update package...\n");
552 auto t0 = std::chrono::system_clock::now();
553 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
554 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
555 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
556 if (err != VERIFY_SUCCESS) {
Tianjie Xuc21edd42016-08-05 18:00:04 -0700557 LOG(ERROR) << "Signature verification failed";
558 LOG(ERROR) << "error: " << kZipVerificationFailure;
Yabin Cui6faf0262016-06-09 14:09:39 -0700559 return false;
560 }
561 return true;
562}