blob: 98f1e3f36890b70a9934d99418be4872194f334b [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 Deymo9b10e602016-08-03 21:03:53 -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 Deymo9b10e602016-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 Xufe16b5c2016-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 Deymo9b10e602016-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
Doug Zongkerb2ee9202009-06-04 10:24:53 -070050#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo9b10e602016-08-03 21:03:53 -070051static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
52static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070053#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070054static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
Tianjie Xufe16b5c2016-09-09 10:55:44 -070055static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
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 Xu7b0ad9c2016-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 Cuifd99a312016-06-09 14:09:39 -070078bool read_metadata_from_package(ZipArchiveHandle zip, std::string* meta_data) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070079 ZipString metadata_path(METADATA_PATH);
80 ZipEntry meta_entry;
Yabin Cuifd99a312016-06-09 14:09:39 -070081 if (meta_data == nullptr) {
82 LOG(ERROR) << "string* meta_data can't be nullptr";
83 return false;
84 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070085 if (FindEntry(zip, metadata_path, &meta_entry) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070086 LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package";
Yabin Cuifd99a312016-06-09 14:09:39 -070087 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070088 }
89
Yabin Cuifd99a312016-06-09 14:09:39 -070090 meta_data->resize(meta_entry.uncompressed_length, '\0');
91 if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&(*meta_data)[0]),
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070092 meta_entry.uncompressed_length) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070093 LOG(ERROR) << "Failed to read metadata in update package";
Yabin Cuifd99a312016-06-09 14:09:39 -070094 return false;
95 }
96 return true;
97}
98
99// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
100static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) {
101 std::string meta_data;
102 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -0700103 return;
104 }
Tianjie Xub0ddae52016-06-08 14:30:04 -0700105 // Examples of the pre-build and post-build strings in metadata:
106 // pre-build-incremental=2943039
107 // post-build-incremental=2951741
108 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
109 for (const std::string& line : lines) {
110 std::string str = android::base::Trim(line);
111 if (android::base::StartsWith(str, "pre-build-incremental")){
112 int source_build = parse_build_number(str);
113 if (source_build != -1) {
114 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
115 source_build));
116 }
117 } else if (android::base::StartsWith(str, "post-build-incremental")) {
118 int target_build = parse_build_number(str);
119 if (target_build != -1) {
120 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
121 target_build));
122 }
123 }
124 }
125}
126
Alex Deymo9b10e602016-08-03 21:03:53 -0700127// Extract the update binary from the open zip archive |zip| located at |path|
128// and store into |cmd| the command line that should be called. The |status_fd|
129// is the file descriptor the child process should use to report back the
130// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700131static int
Alex Deymo9b10e602016-08-03 21:03:53 -0700132update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
133 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700134
Alex Deymo9b10e602016-08-03 21:03:53 -0700135#ifdef AB_OTA_UPDATER
136
137// Parses the metadata of the OTA package in |zip| and checks whether we are
138// allowed to accept this A/B package. Downgrading is not allowed unless
139// explicitly enabled in the package and only for incremental packages.
140static int check_newer_ab_build(ZipArchiveHandle zip)
141{
142 std::string metadata_str;
143 if (!read_metadata_from_package(zip, &metadata_str)) {
144 return INSTALL_CORRUPT;
145 }
146 std::map<std::string, std::string> metadata;
147 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
148 size_t eq = line.find('=');
149 if (eq != std::string::npos) {
150 metadata[line.substr(0, eq)] = line.substr(eq + 1);
151 }
152 }
153 char value[PROPERTY_VALUE_MAX];
154
155 property_get("ro.product.device", value, "");
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 }
161
162 // We allow the package to not have any serialno, but if it has a non-empty
163 // value it should match.
164 property_get("ro.serialno", value, "");
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 }
170
171 if (metadata["ota-type"] != "AB") {
172 LOG(ERROR) << "Package is not A/B";
173 return INSTALL_ERROR;
174 }
175
176 // Incremental updates should match the current build.
177 property_get("ro.build.version.incremental", value, "");
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 }
183 property_get("ro.build.fingerprint", value, "");
184 const std::string& pkg_pre_build_fingerprint = metadata["pre-build"];
185 if (!pkg_pre_build_fingerprint.empty() &&
186 pkg_pre_build_fingerprint != value) {
187 LOG(ERROR) << "Package is for source build " << pkg_pre_build_fingerprint
188 << " but expected " << value;
189 return INSTALL_ERROR;
190 }
191
192 // Check for downgrade version.
193 int64_t build_timestamp = property_get_int64(
194 "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(),
200 &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 " << build_timestamp << " but package has "
205 "timestamp " << pkg_post_timestamp << " 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;
215}
216
217static int
218update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
219 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.
228 ZipString property_name(AB_OTA_PAYLOAD_PROPERTIES);
229 ZipEntry properties_entry;
230 if (FindEntry(zip, property_name, &properties_entry) != 0) {
231 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD_PROPERTIES;
232 return INSTALL_CORRUPT;
233 }
234 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) {
238 LOG(ERROR) << "Can't extract " << AB_OTA_PAYLOAD_PROPERTIES;
239 return INSTALL_CORRUPT;
240 }
241
242 ZipString payload_name(AB_OTA_PAYLOAD);
243 ZipEntry payload_entry;
244 if (FindEntry(zip, payload_name, &payload_entry) != 0) {
245 LOG(ERROR) << "Can't find " << AB_OTA_PAYLOAD;
246 return INSTALL_CORRUPT;
247 }
248 long payload_offset = payload_entry.offset;
249 *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
263update_binary_command(const char* path, ZipArchiveHandle zip, int retry_count,
264 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 Xu7b0ad9c2016-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 Deymo9b10e602016-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.
302static int
303try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache,
304 std::vector<std::string>& log_buffer, int retry_count)
305{
306 read_source_target_build(zip, log_buffer);
307
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700308 int pipefd[2];
309 pipe(pipefd);
310
Alex Deymo9b10e602016-08-03 21:03:53 -0700311 std::vector<std::string> args;
312 int ret = update_binary_command(path, zip, retry_count, pipefd[1], &args);
313 if (ret) {
314 close(pipefd[0]);
315 close(pipefd[1]);
316 return ret;
317 }
318
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700319 // When executing the update binary contained in the package, the
320 // arguments passed are:
321 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700322 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700323 //
324 // - an fd to which the program can write in order to update the
325 // progress bar. The program can write single-line commands:
326 //
327 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700328 // fill up the next <frac> part of of the progress bar
329 // over <secs> seconds. If <secs> is zero, use
330 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700331 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700332 //
333 // set_progress <frac>
334 // <frac> should be between 0.0 and 1.0; sets the
335 // progress bar within the segment defined by the most
336 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700337 //
338 // firmware <"hboot"|"radio"> <filename>
339 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800340 // given partition on reboot.
341 //
342 // (API v2: <filename> may start with "PACKAGE:" to
343 // indicate taking a file from the OTA package.)
344 //
345 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700346 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700347 // ui_print <string>
348 // display <string> on the screen.
349 //
Tao Baob07e1f32015-04-10 16:14:52 -0700350 // wipe_cache
351 // a wipe of cache will be performed following a successful
352 // installation.
353 //
354 // clear_display
355 // turn off the text display.
356 //
357 // enable_reboot
358 // packages can explicitly request that they want the user
359 // to be able to reboot during installation (useful for
360 // debugging packages that don't exit).
361 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700362 // - the name of the package zip file.
363 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700364 // - an optional argument "retry" if this update is a retry of a failed
365 // update attempt.
366 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700367
Alex Deymo9b10e602016-08-03 21:03:53 -0700368 // Convert the vector to a NULL-terminated char* array suitable for execv.
369 const char* chr_args[args.size() + 1];
370 chr_args[args.size()] = NULL;
371 for (size_t i = 0; i < args.size(); i++) {
372 chr_args[i] = args[i].c_str();
373 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700374
375 pid_t pid = fork();
376 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700377 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700378 close(pipefd[0]);
Alex Deymo9b10e602016-08-03 21:03:53 -0700379 execv(chr_args[0], const_cast<char**>(chr_args));
380 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700381 _exit(-1);
382 }
383 close(pipefd[1]);
384
Tao Bao145d8612015-03-25 15:51:15 -0700385 *wipe_cache = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800386 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700387
Doug Zongker64893cc2009-07-14 16:31:56 -0700388 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700389 FILE* from_child = fdopen(pipefd[0], "r");
390 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700391 char* command = strtok(buffer, " \n");
392 if (command == NULL) {
393 continue;
394 } else if (strcmp(command, "progress") == 0) {
395 char* fraction_s = strtok(NULL, " \n");
396 char* seconds_s = strtok(NULL, " \n");
397
398 float fraction = strtof(fraction_s, NULL);
399 int seconds = strtol(seconds_s, NULL, 10);
400
Doug Zongker74406302011-10-28 15:13:10 -0700401 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700402 } else if (strcmp(command, "set_progress") == 0) {
403 char* fraction_s = strtok(NULL, " \n");
404 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700405 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700406 } else if (strcmp(command, "ui_print") == 0) {
407 char* str = strtok(NULL, "\n");
408 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700409 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700410 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700411 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700412 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700413 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700414 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700415 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700416 } else if (strcmp(command, "clear_display") == 0) {
417 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700418 } else if (strcmp(command, "enable_reboot") == 0) {
419 // packages can explicitly request that they want the user
420 // to be able to reboot during installation (useful for
421 // debugging packages that don't exit).
422 ui->SetEnableReboot(true);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800423 } else if (strcmp(command, "retry_update") == 0) {
424 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700425 } else if (strcmp(command, "log") == 0) {
426 // Save the logging request from updater and write to
427 // last_install later.
428 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700429 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700430 LOG(ERROR) << "unknown command [" << command << "]";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700431 }
432 }
433 fclose(from_child);
434
435 int status;
436 waitpid(pid, &status, 0);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800437 if (retry_update) {
438 return INSTALL_RETRY;
439 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700440 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700441 LOG(ERROR) << "Error in " << path << " (Status " << WEXITSTATUS(status) << ")";
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700442 return INSTALL_ERROR;
443 }
444
Doug Zongkere08991e2010-02-02 13:09:52 -0800445 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700446}
447
Doug Zongker469243e2011-04-12 09:28:10 -0700448static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700449really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700450 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800451{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700452 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700453 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700454 // Give verification half the progress bar...
455 ui->SetProgressType(RecoveryUI::DETERMINATE);
456 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700457 LOG(INFO) << "Update location: " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800458
Doug Zongker99916f02014-01-13 14:16:58 -0800459 // Map the update package into memory.
460 ui->Print("Opening update package...\n");
461
Doug Zongker075ad802014-06-26 15:35:51 -0700462 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800463 if (path[0] == '@') {
464 ensure_path_mounted(path+1);
465 } else {
466 ensure_path_mounted(path);
467 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800468 }
469
Doug Zongker99916f02014-01-13 14:16:58 -0800470 MemMapping map;
471 if (sysMapFile(path, &map) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700472 LOG(ERROR) << "failed to map file";
Doug Zongker99916f02014-01-13 14:16:58 -0800473 return INSTALL_CORRUPT;
474 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800475
Elliott Hughes8febafa2016-04-13 16:39:56 -0700476 // Verify package.
Yabin Cuifd99a312016-06-09 14:09:39 -0700477 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700478 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800479 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700480 return INSTALL_CORRUPT;
481 }
482
Elliott Hughes8febafa2016-04-13 16:39:56 -0700483 // Try to open the package.
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700484 ZipArchiveHandle zip;
Yabin Cuifd99a312016-06-09 14:09:39 -0700485 int err = OpenArchiveFromMemory(map.addr, map.length, path, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800486 if (err != 0) {
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700487 LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err);
Tianjie Xu16255832016-04-30 11:49:59 -0700488 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
489
Doug Zongker99916f02014-01-13 14:16:58 -0800490 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700491 CloseArchive(zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800492 return INSTALL_CORRUPT;
493 }
494
Elliott Hughes8febafa2016-04-13 16:39:56 -0700495 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700496 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700497 if (retry_count > 0) {
498 ui->Print("Retry attempt: %d\n", retry_count);
499 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700500 ui->SetEnableReboot(false);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700501 int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700502 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700503 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800504
505 sysReleaseMap(&map);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700506 CloseArchive(zip);
Doug Zongker99916f02014-01-13 14:16:58 -0800507 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800508}
Doug Zongker469243e2011-04-12 09:28:10 -0700509
510int
Tao Bao145d8612015-03-25 15:51:15 -0700511install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700512 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700513{
Tao Bao682c34b2015-04-07 17:16:35 -0700514 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700515 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700516
Doug Zongker239ac6a2013-08-20 16:03:25 -0700517 int result;
Tianjie Xudd874b12016-05-13 12:13:15 -0700518 std::vector<std::string> log_buffer;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700519 if (setup_install_mounts() != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700520 LOG(ERROR) << "failed to set up expected mounts for install; aborting";
Doug Zongker239ac6a2013-08-20 16:03:25 -0700521 result = INSTALL_ERROR;
522 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700523 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700524 }
Tianjie Xudd874b12016-05-13 12:13:15 -0700525
Tao Baof4885ad2016-09-26 11:39:14 -0700526 // Measure the time spent to apply OTA update in seconds.
527 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
528 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700529
Tao Baof4885ad2016-09-26 11:39:14 -0700530 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
531 LOG(WARNING) << "Can't mount " << UNCRYPT_STATUS;
532 } else {
533 std::string uncrypt_status;
534 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
535 PLOG(WARNING) << "failed to read uncrypt status";
Tao Baoee9b9552016-10-13 16:04:07 -0700536 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
537 LOG(WARNING) << "corrupted uncrypt_status: " << uncrypt_status;
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700538 } else {
Tao Baof4885ad2016-09-26 11:39:14 -0700539 log_buffer.push_back(android::base::Trim(uncrypt_status));
Tianjie Xufe16b5c2016-09-09 10:55:44 -0700540 }
Doug Zongker469243e2011-04-12 09:28:10 -0700541 }
Tao Baof4885ad2016-09-26 11:39:14 -0700542
543 // The first two lines need to be the package name and install result.
544 std::vector<std::string> log_header = {
545 path,
546 result == INSTALL_SUCCESS ? "1" : "0",
547 "time_total: " + std::to_string(time_total),
548 "retry: " + std::to_string(retry_count),
549 };
550 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
551 android::base::Join(log_buffer, "\n");
552 if (!android::base::WriteStringToFile(log_content, install_file)) {
553 PLOG(ERROR) << "failed to write " << install_file;
554 }
555
556 // Write a copy into last_log.
557 LOG(INFO) << log_content;
558
Doug Zongker469243e2011-04-12 09:28:10 -0700559 return result;
560}
Yabin Cuifd99a312016-06-09 14:09:39 -0700561
562bool verify_package(const unsigned char* package_data, size_t package_size) {
563 std::vector<Certificate> loadedKeys;
564 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
565 LOG(ERROR) << "Failed to load keys";
566 return false;
567 }
568 LOG(INFO) << loadedKeys.size() << " key(s) loaded from " << PUBLIC_KEYS_FILE;
569
570 // Verify package.
571 ui->Print("Verifying update package...\n");
572 auto t0 = std::chrono::system_clock::now();
573 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
574 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
575 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
576 if (err != VERIFY_SUCCESS) {
577 LOG(ERROR) << "Signature verification failed";
578 LOG(ERROR) << "error: " << kZipVerificationFailure;
579 return false;
580 }
581 return true;
582}