blob: 238c5953df896e6b3a406d3602196e2478cfeea9 [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 Deymo53c107f2016-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>
Amit Blaycc8897b2015-08-23 20:26:31 +030026#include <sys/mount.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027
Elliott Hughes8febafa2016-04-13 16:39:56 -070028#include <chrono>
Alex Deymo4344d632016-08-03 21:03:53 -070029#include <limits>
30#include <map>
Tianjie Xudd874b12016-05-13 12:13:15 -070031#include <string>
Tao Bao71e3e092016-02-02 14:02:27 -080032#include <vector>
33
Tianjie Xue16e7992016-09-09 10:55:44 -070034#include <android-base/file.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 Xu16255832016-04-30 11:49:59 -070039
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "common.h"
Tianjie Xu16255832016-04-30 11:49:59 -070041#include "error_code.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042#include "install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043#include "minui/minui.h"
44#include "minzip/SysUtil.h"
45#include "minzip/Zip.h"
46#include "mtdutils/mounts.h"
47#include "mtdutils/mtdutils.h"
48#include "roots.h"
Doug Zongker10e418d2011-10-28 10:33:05 -070049#include "ui.h"
Elliott Hughes8febafa2016-04-13 16:39:56 -070050#include "verifier.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Doug Zongker74406302011-10-28 15:13:10 -070052extern RecoveryUI* ui;
53
Doug Zongkerb2ee9202009-06-04 10:24:53 -070054#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
Alex Deymo4344d632016-08-03 21:03:53 -070055static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
56static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
Doug Zongkerd1b19b92009-04-01 15:48:46 -070057#define PUBLIC_KEYS_FILE "/res/keys"
Tianjie Xub0ddae52016-06-08 14:30:04 -070058static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";
Tianjie Xue16e7992016-09-09 10:55:44 -070059static constexpr const char* UNCRYPT_STATUS = "/cache/recovery/uncrypt_status";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060
Doug Zongker74406302011-10-28 15:13:10 -070061// Default allocation of progress bar segments to operations
62static const int VERIFICATION_PROGRESS_TIME = 60;
63static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
64static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
65static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
66
Tianjie Xub0ddae52016-06-08 14:30:04 -070067// This function parses and returns the build.version.incremental
68static int parse_build_number(std::string str) {
69 size_t pos = str.find("=");
70 if (pos != std::string::npos) {
71 std::string num_string = android::base::Trim(str.substr(pos+1));
72 int build_number;
73 if (android::base::ParseInt(num_string.c_str(), &build_number, 0)) {
74 return build_number;
75 }
76 }
77
78 LOGE("Failed to parse build number in %s.\n", str.c_str());
79 return -1;
80}
81
Yabin Cui6faf0262016-06-09 14:09:39 -070082bool read_metadata_from_package(ZipArchive* zip, std::string* meta_data) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070083 const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH);
84 if (meta_entry == nullptr) {
85 LOGE("Failed to find %s in update package.\n", METADATA_PATH);
Yabin Cui6faf0262016-06-09 14:09:39 -070086 return false;
Tianjie Xub0ddae52016-06-08 14:30:04 -070087 }
88
Yabin Cui6faf0262016-06-09 14:09:39 -070089 meta_data->resize(meta_entry->uncompLen, '\0');
90 if (!mzReadZipEntry(zip, meta_entry, &(*meta_data)[0], meta_entry->uncompLen)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -070091 LOGE("Failed to read metadata in update package.\n");
Yabin Cui6faf0262016-06-09 14:09:39 -070092 return false;
93 }
94 return true;
95}
96
97// Read the build.version.incremental of src/tgt from the metadata and log it to last_install.
98static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) {
99 std::string meta_data;
100 if (!read_metadata_from_package(zip, &meta_data)) {
Tianjie Xub0ddae52016-06-08 14:30:04 -0700101 return;
102 }
Tianjie Xub0ddae52016-06-08 14:30:04 -0700103 // Examples of the pre-build and post-build strings in metadata:
104 // pre-build-incremental=2943039
105 // post-build-incremental=2951741
106 std::vector<std::string> lines = android::base::Split(meta_data, "\n");
107 for (const std::string& line : lines) {
108 std::string str = android::base::Trim(line);
109 if (android::base::StartsWith(str, "pre-build-incremental")){
110 int source_build = parse_build_number(str);
111 if (source_build != -1) {
112 log_buffer.push_back(android::base::StringPrintf("source_build: %d",
113 source_build));
114 }
115 } else if (android::base::StartsWith(str, "post-build-incremental")) {
116 int target_build = parse_build_number(str);
117 if (target_build != -1) {
118 log_buffer.push_back(android::base::StringPrintf("target_build: %d",
119 target_build));
120 }
121 }
122 }
123}
124
Alex Deymo4344d632016-08-03 21:03:53 -0700125// Extract the update binary from the open zip archive |zip| located at |path|
126// and store into |cmd| the command line that should be called. The |status_fd|
127// is the file descriptor the child process should use to report back the
128// progress of the update.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700129static int
Alex Deymo4344d632016-08-03 21:03:53 -0700130update_binary_command(const char* path, ZipArchive* zip, int retry_count,
131 int status_fd, std::vector<std::string>* cmd);
Tianjie Xub0ddae52016-06-08 14:30:04 -0700132
Alex Deymo4344d632016-08-03 21:03:53 -0700133#ifdef AB_OTA_UPDATER
134
135// Parses the metadata of the OTA package in |zip| and checks whether we are
136// allowed to accept this A/B package. Downgrading is not allowed unless
137// explicitly enabled in the package and only for incremental packages.
138static int check_newer_ab_build(ZipArchive* zip)
139{
140 std::string metadata_str;
141 if (!read_metadata_from_package(zip, &metadata_str)) {
142 return INSTALL_CORRUPT;
143 }
144 std::map<std::string, std::string> metadata;
145 for (const std::string& line : android::base::Split(metadata_str, "\n")) {
146 size_t eq = line.find('=');
147 if (eq != std::string::npos) {
148 metadata[line.substr(0, eq)] = line.substr(eq + 1);
149 }
150 }
151 char value[PROPERTY_VALUE_MAX];
152
153 property_get("ro.product.device", value, "");
154 const std::string& pkg_device = metadata["pre-device"];
155 if (pkg_device != value || pkg_device.empty()) {
156 LOGE("Package is for product %s but expected %s\n",
157 pkg_device.c_str(), value);
158 return INSTALL_ERROR;
159 }
160
161 // We allow the package to not have any serialno, but if it has a non-empty
162 // value it should match.
163 property_get("ro.serialno", value, "");
164 const std::string& pkg_serial_no = metadata["serialno"];
165 if (!pkg_serial_no.empty() && pkg_serial_no != value) {
166 LOGE("Package is for serial %s\n", pkg_serial_no.c_str());
167 return INSTALL_ERROR;
168 }
169
170 if (metadata["ota-type"] != "AB") {
171 LOGE("Package is not A/B\n");
172 return INSTALL_ERROR;
173 }
174
175 // Incremental updates should match the current build.
176 property_get("ro.build.version.incremental", value, "");
177 const std::string& pkg_pre_build = metadata["pre-build-incremental"];
178 if (!pkg_pre_build.empty() && pkg_pre_build != value) {
179 LOGE("Package is for source build %s but expected %s\n",
180 pkg_pre_build.c_str(), 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 LOGE("Package is for source build %s but expected %s\n",
188 pkg_pre_build_fingerprint.c_str(), value);
189 return INSTALL_ERROR;
190 }
191
192 // Check for downgrade version.
193 int64_t build_timestampt = property_get_int64(
194 "ro.build.date.utc", std::numeric_limits<int64_t>::max());
195 int64_t pkg_post_timespampt = 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_timespampt) ||
201 pkg_post_timespampt < build_timestampt) {
202 if (metadata["ota-downgrade"] != "yes") {
203 LOGE("Update package is older than the current build, expected a "
204 "build newer than timestamp %" PRIu64 " but package has "
205 "timestamp %" PRIu64 " and downgrade not allowed.\n",
206 build_timestampt, pkg_post_timespampt);
207 return INSTALL_ERROR;
208 }
209 if (pkg_pre_build_fingerprint.empty()) {
210 LOGE("Downgrade package must have a pre-build version set, not "
211 "allowed.\n");
212 return INSTALL_ERROR;
213 }
214 }
215
216 return 0;
217}
218
219static int
220update_binary_command(const char* path, ZipArchive* zip, int retry_count,
221 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.
230 const ZipEntry* properties_entry =
231 mzFindZipEntry(zip, AB_OTA_PAYLOAD_PROPERTIES);
232 if (!properties_entry) {
233 LOGE("Can't find %s\n", AB_OTA_PAYLOAD_PROPERTIES);
234 return INSTALL_CORRUPT;
235 }
236 std::vector<unsigned char> payload_properties(
237 mzGetZipEntryUncompLen(properties_entry));
238 if (!mzExtractZipEntryToBuffer(zip, properties_entry,
239 payload_properties.data())) {
240 LOGE("Can't extract %s\n", AB_OTA_PAYLOAD_PROPERTIES);
241 return INSTALL_CORRUPT;
242 }
243
244 const ZipEntry* payload_entry = mzFindZipEntry(zip, AB_OTA_PAYLOAD);
245 if (!payload_entry) {
246 LOGE("Can't find %s\n", AB_OTA_PAYLOAD);
247 return INSTALL_CORRUPT;
248 }
249 long payload_offset = mzGetZipEntryOffset(payload_entry);
250 *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
264update_binary_command(const char* path, ZipArchive* zip, int retry_count,
265 int status_fd, std::vector<std::string>* cmd)
266{
267 // On traditional updates we extract the update binary from the package.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700268 const ZipEntry* binary_entry =
269 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
270 if (binary_entry == NULL) {
271 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) {
278 LOGE("Can't make %s\n", binary);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700279 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700280 }
281 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
282 close(fd);
283
284 if (!ok) {
285 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700286 return INSTALL_ERROR;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700287 }
288
Alex Deymo4344d632016-08-03 21:03:53 -0700289 *cmd = {
290 binary,
291 EXPAND(RECOVERY_API_VERSION), // defined in Android.mk
292 std::to_string(status_fd),
293 path,
294 };
295 if (retry_count > 0)
296 cmd->push_back("retry");
297 return 0;
298}
299#endif // !AB_OTA_UPDATER
300
301// If the package contains an update binary, extract it and run it.
302static int
303try_update_binary(const char* path, ZipArchive* 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 Deymo4344d632016-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 mzCloseZipArchive(zip);
314 if (ret) {
315 close(pipefd[0]);
316 close(pipefd[1]);
317 return ret;
318 }
319
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700320 // When executing the update binary contained in the package, the
321 // arguments passed are:
322 //
Doug Zongkerfb2e3af2009-06-17 17:29:40 -0700323 // - the version number for this interface
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700324 //
325 // - an fd to which the program can write in order to update the
326 // progress bar. The program can write single-line commands:
327 //
328 // progress <frac> <secs>
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700329 // fill up the next <frac> part of of the progress bar
330 // over <secs> seconds. If <secs> is zero, use
331 // set_progress commands to manually control the
Tao Baob07e1f32015-04-10 16:14:52 -0700332 // progress of this segment of the bar.
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700333 //
334 // set_progress <frac>
335 // <frac> should be between 0.0 and 1.0; sets the
336 // progress bar within the segment defined by the most
337 // recent progress command.
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700338 //
339 // firmware <"hboot"|"radio"> <filename>
340 // arrange to install the contents of <filename> in the
Doug Zongkere08991e2010-02-02 13:09:52 -0800341 // given partition on reboot.
342 //
343 // (API v2: <filename> may start with "PACKAGE:" to
344 // indicate taking a file from the OTA package.)
345 //
346 // (API v3: this command no longer exists.)
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700347 //
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700348 // ui_print <string>
349 // display <string> on the screen.
350 //
Tao Baob07e1f32015-04-10 16:14:52 -0700351 // wipe_cache
352 // a wipe of cache will be performed following a successful
353 // installation.
354 //
355 // clear_display
356 // turn off the text display.
357 //
358 // enable_reboot
359 // packages can explicitly request that they want the user
360 // to be able to reboot during installation (useful for
361 // debugging packages that don't exit).
362 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700363 // - the name of the package zip file.
364 //
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700365 // - an optional argument "retry" if this update is a retry of a failed
366 // update attempt.
367 //
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700368
Alex Deymo4344d632016-08-03 21:03:53 -0700369 // Convert the vector to a NULL-terminated char* array suitable for execv.
370 const char* chr_args[args.size() + 1];
371 chr_args[args.size()] = NULL;
372 for (size_t i = 0; i < args.size(); i++) {
373 chr_args[i] = args[i].c_str();
374 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700375
376 pid_t pid = fork();
377 if (pid == 0) {
Alistair Strachan027429a2013-07-17 10:41:49 -0700378 umask(022);
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700379 close(pipefd[0]);
Alex Deymo4344d632016-08-03 21:03:53 -0700380 execv(chr_args[0], const_cast<char**>(chr_args));
381 fprintf(stdout, "E:Can't run %s (%s)\n", chr_args[0], strerror(errno));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700382 _exit(-1);
383 }
384 close(pipefd[1]);
385
Tao Bao145d8612015-03-25 15:51:15 -0700386 *wipe_cache = false;
Tianjie Xufa12b972016-02-05 18:25:58 -0800387 bool retry_update = false;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700388
Doug Zongker64893cc2009-07-14 16:31:56 -0700389 char buffer[1024];
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700390 FILE* from_child = fdopen(pipefd[0], "r");
391 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700392 char* command = strtok(buffer, " \n");
393 if (command == NULL) {
394 continue;
395 } else if (strcmp(command, "progress") == 0) {
396 char* fraction_s = strtok(NULL, " \n");
397 char* seconds_s = strtok(NULL, " \n");
398
399 float fraction = strtof(fraction_s, NULL);
400 int seconds = strtol(seconds_s, NULL, 10);
401
Doug Zongker74406302011-10-28 15:13:10 -0700402 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700403 } else if (strcmp(command, "set_progress") == 0) {
404 char* fraction_s = strtok(NULL, " \n");
405 float fraction = strtof(fraction_s, NULL);
Doug Zongker74406302011-10-28 15:13:10 -0700406 ui->SetProgress(fraction);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700407 } else if (strcmp(command, "ui_print") == 0) {
408 char* str = strtok(NULL, "\n");
409 if (str) {
Tao Baob6918c72015-05-19 17:02:16 -0700410 ui->PrintOnScreenOnly("%s", str);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700411 } else {
Tao Baob6918c72015-05-19 17:02:16 -0700412 ui->PrintOnScreenOnly("\n");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700413 }
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700414 fflush(stdout);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700415 } else if (strcmp(command, "wipe_cache") == 0) {
Tao Bao145d8612015-03-25 15:51:15 -0700416 *wipe_cache = true;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700417 } else if (strcmp(command, "clear_display") == 0) {
418 ui->SetBackground(RecoveryUI::NONE);
Doug Zongkerc704e062014-05-23 08:40:35 -0700419 } else if (strcmp(command, "enable_reboot") == 0) {
420 // packages can explicitly request that they want the user
421 // to be able to reboot during installation (useful for
422 // debugging packages that don't exit).
423 ui->SetEnableReboot(true);
Tianjie Xufa12b972016-02-05 18:25:58 -0800424 } else if (strcmp(command, "retry_update") == 0) {
425 retry_update = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700426 } else if (strcmp(command, "log") == 0) {
427 // Save the logging request from updater and write to
428 // last_install later.
429 log_buffer.push_back(std::string(strtok(NULL, "\n")));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700430 } else {
431 LOGE("unknown command [%s]\n", command);
432 }
433 }
434 fclose(from_child);
435
436 int status;
437 waitpid(pid, &status, 0);
Tianjie Xufa12b972016-02-05 18:25:58 -0800438 if (retry_update) {
439 return INSTALL_RETRY;
440 }
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700441 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700442 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700443 return INSTALL_ERROR;
444 }
445
Doug Zongkere08991e2010-02-02 13:09:52 -0800446 return INSTALL_SUCCESS;
Doug Zongkerb2ee9202009-06-04 10:24:53 -0700447}
448
Amit Blaycc8897b2015-08-23 20:26:31 +0300449#ifdef USE_MDTP
450static int
451mdtp_update()
452{
453 const char** args = (const char**)malloc(sizeof(char*) * 2);
454
455 if (args == NULL) {
456 LOGE("Failed to allocate memory for MDTP FOTA app arguments\n");
457 return 0;
458 }
459
460 args[0] = "/sbin/mdtp_fota";
461 args[1] = NULL;
462 int status = 0;
463
464 ui->Print("Running MDTP integrity verification and update...\n");
465
466 /* Make sure system partition is mounted, so MDTP can process its content. */
Amit Blaycc8897b2015-08-23 20:26:31 +0300467 status = mount("/dev/block/bootdevice/by-name/system", "/system", "ext4",
468 MS_NOATIME | MS_NODEV | MS_NODIRATIME |
469 MS_RDONLY, "");
470
471 if (status) {
472 LOGE("Failed to mount the system partition, error=%s.\n", strerror(errno));
473 free(args);
474 return 0;
475 }
476
Amit Blay8aca5122016-08-22 12:27:14 +0300477 status = mount("/dev/block/bootdevice/by-name/modem", "/firmware", "vfat",
478 MS_NOATIME | MS_NODEV | MS_NODIRATIME |
479 MS_RDONLY, "");
480
481 if (status) {
482 LOGE("Failed to mount the modem (firmware) partition, error=%s.\n", strerror(errno));
483 free(args);
484 return 0;
485 }
486
Amit Blaycc8897b2015-08-23 20:26:31 +0300487 status = 0;
488
489 pid_t pid = fork();
490 if (pid == 0) {
491 execv(args[0], (char* const*)args);
492 LOGE("Can't run %s (%s)\n", args[0], strerror(errno));
493 _exit(-1);
494 }
495 if (pid > 0) {
496 LOGE("Waiting for MDTP FOTA to complete...\n");
497 pid = waitpid(pid, &status, 0);
498 LOGE("MDTP FOTA completed, status: %d\n", status);
499 }
500
501 /* Leave the system partition unmounted before we finish. */
502 umount("/system");
Amit Blay8aca5122016-08-22 12:27:14 +0300503 umount("/firmware");
Amit Blaycc8897b2015-08-23 20:26:31 +0300504
505 free(args);
506
507 return (status > 0) ? 1 : 0;
508}
509#endif /* USE_MDTP */
510
Doug Zongker469243e2011-04-12 09:28:10 -0700511static int
Tianjie Xudd874b12016-05-13 12:13:15 -0700512really_install_package(const char *path, bool* wipe_cache, bool needs_mount,
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700513 std::vector<std::string>& log_buffer, int retry_count)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800514{
Doug Zongker02ec6b82012-08-22 17:26:40 -0700515 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Doug Zongker74406302011-10-28 15:13:10 -0700516 ui->Print("Finding update package...\n");
Doug Zongker239ac6a2013-08-20 16:03:25 -0700517 // Give verification half the progress bar...
518 ui->SetProgressType(RecoveryUI::DETERMINATE);
519 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700520 LOGI("Update location: %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800521
Doug Zongker99916f02014-01-13 14:16:58 -0800522 // Map the update package into memory.
523 ui->Print("Opening update package...\n");
524
Doug Zongker075ad802014-06-26 15:35:51 -0700525 if (path && needs_mount) {
Doug Zongker99916f02014-01-13 14:16:58 -0800526 if (path[0] == '@') {
527 ensure_path_mounted(path+1);
528 } else {
529 ensure_path_mounted(path);
530 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800531 }
532
Doug Zongker99916f02014-01-13 14:16:58 -0800533 MemMapping map;
534 if (sysMapFile(path, &map) != 0) {
535 LOGE("failed to map file\n");
536 return INSTALL_CORRUPT;
537 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800538
Elliott Hughes8febafa2016-04-13 16:39:56 -0700539 // Verify package.
Yabin Cui6faf0262016-06-09 14:09:39 -0700540 if (!verify_package(map.addr, map.length)) {
Tianjie Xu16255832016-04-30 11:49:59 -0700541 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipVerificationFailure));
Doug Zongker99916f02014-01-13 14:16:58 -0800542 sysReleaseMap(&map);
Doug Zongker60151a22009-08-12 18:30:03 -0700543 return INSTALL_CORRUPT;
544 }
545
Elliott Hughes8febafa2016-04-13 16:39:56 -0700546 // Try to open the package.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800547 ZipArchive zip;
Yabin Cui6faf0262016-06-09 14:09:39 -0700548 int err = mzOpenZipArchive(map.addr, map.length, &zip);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800549 if (err != 0) {
550 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
Tianjie Xu16255832016-04-30 11:49:59 -0700551 log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure));
552
Doug Zongker99916f02014-01-13 14:16:58 -0800553 sysReleaseMap(&map);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800554 return INSTALL_CORRUPT;
555 }
556
Elliott Hughes8febafa2016-04-13 16:39:56 -0700557 // Verify and install the contents of the package.
Doug Zongker74406302011-10-28 15:13:10 -0700558 ui->Print("Installing update...\n");
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700559 if (retry_count > 0) {
560 ui->Print("Retry attempt: %d\n", retry_count);
561 }
Doug Zongkerc704e062014-05-23 08:40:35 -0700562 ui->SetEnableReboot(false);
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700563 int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count);
Doug Zongkerc704e062014-05-23 08:40:35 -0700564 ui->SetEnableReboot(true);
Doug Zongker075ad802014-06-26 15:35:51 -0700565 ui->Print("\n");
Doug Zongker99916f02014-01-13 14:16:58 -0800566
567 sysReleaseMap(&map);
568
Amit Blaycc8897b2015-08-23 20:26:31 +0300569#ifdef USE_MDTP
570 /* If MDTP update failed, return an error such that recovery will not finish. */
571 if (result == INSTALL_SUCCESS) {
572 if (!mdtp_update()) {
573 ui->Print("Unable to verify integrity of /system for MDTP, update aborted.\n");
574 return INSTALL_ERROR;
575 }
576 ui->Print("Successfully verified integrity of /system for MDTP.\n");
577 }
578#endif /* USE_MDTP */
579
Doug Zongker99916f02014-01-13 14:16:58 -0800580 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800581}
Doug Zongker469243e2011-04-12 09:28:10 -0700582
583int
Tao Bao145d8612015-03-25 15:51:15 -0700584install_package(const char* path, bool* wipe_cache, const char* install_file,
Tianjie Xu16255832016-04-30 11:49:59 -0700585 bool needs_mount, int retry_count)
Doug Zongker469243e2011-04-12 09:28:10 -0700586{
Tao Bao682c34b2015-04-07 17:16:35 -0700587 modified_flash = true;
Tianjie Xudd874b12016-05-13 12:13:15 -0700588 auto start = std::chrono::system_clock::now();
Tao Bao682c34b2015-04-07 17:16:35 -0700589
Ameya Thakur74e750e2016-08-15 13:16:03 -0700590 int result = 0;
Tianjie Xudd874b12016-05-13 12:13:15 -0700591 std::vector<std::string> log_buffer;
Ameya Thakur74e750e2016-08-15 13:16:03 -0700592 if (needs_mount == true)
593 result = setup_install_mounts();
594 if (result != 0) {
Doug Zongker239ac6a2013-08-20 16:03:25 -0700595 LOGE("failed to set up expected mounts for install; aborting\n");
596 result = INSTALL_ERROR;
597 } else {
Tianjie Xu7ce287d2016-05-31 09:29:49 -0700598 result = really_install_package(path, wipe_cache, needs_mount, log_buffer, retry_count);
Doug Zongker239ac6a2013-08-20 16:03:25 -0700599 }
Tianjie Xudd874b12016-05-13 12:13:15 -0700600
Tao Baoa8c0d0b2016-09-26 11:39:14 -0700601 // Measure the time spent to apply OTA update in seconds.
602 std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
603 int time_total = static_cast<int>(duration.count());
Tianjie Xudd874b12016-05-13 12:13:15 -0700604
Tao Baoa8c0d0b2016-09-26 11:39:14 -0700605 if (ensure_path_mounted(UNCRYPT_STATUS) != 0) {
606 LOGW("Can't mount %s\n", UNCRYPT_STATUS);
607 } else {
608 std::string uncrypt_status;
609 if (!android::base::ReadFileToString(UNCRYPT_STATUS, &uncrypt_status)) {
610 LOGW("failed to read uncrypt status: %s\n", strerror(errno));
Tianjie Xu37d7d672016-09-24 15:31:34 -0700611 } else if (!android::base::StartsWith(uncrypt_status, "uncrypt_")) {
Tao Baoa8c0d0b2016-09-26 11:39:14 -0700612 LOGW("corrupted uncrypt_status: %s: %s\n", uncrypt_status.c_str(), strerror(errno));
Tianjie Xue16e7992016-09-09 10:55:44 -0700613 } else {
Tao Baoa8c0d0b2016-09-26 11:39:14 -0700614 log_buffer.push_back(android::base::Trim(uncrypt_status));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800615 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800616 }
Tao Baoa8c0d0b2016-09-26 11:39:14 -0700617
618 // The first two lines need to be the package name and install result.
619 std::vector<std::string> log_header = {
620 path,
621 result == INSTALL_SUCCESS ? "1" : "0",
622 "time_total: " + std::to_string(time_total),
623 "retry: " + std::to_string(retry_count),
624 };
625 std::string log_content = android::base::Join(log_header, "\n") + "\n" +
626 android::base::Join(log_buffer, "\n");
627 if (!android::base::WriteStringToFile(log_content, install_file)) {
628 LOGE("failed to write %s: %s\n", install_file, strerror(errno));
629 }
630
631 // Write a copy into last_log.
632 LOGI("%s\n", log_content.c_str());
633
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800634 return result;
635}
Yabin Cui6faf0262016-06-09 14:09:39 -0700636
637bool verify_package(const unsigned char* package_data, size_t package_size) {
638 std::vector<Certificate> loadedKeys;
639 if (!load_keys(PUBLIC_KEYS_FILE, loadedKeys)) {
640 LOGE("Failed to load keys\n");
641 return false;
642 }
643 LOGI("%zu key(s) loaded from %s\n", loadedKeys.size(), PUBLIC_KEYS_FILE);
644
645 // Verify package.
646 ui->Print("Verifying update package...\n");
647 auto t0 = std::chrono::system_clock::now();
648 int err = verify_file(const_cast<unsigned char*>(package_data), package_size, loadedKeys);
649 std::chrono::duration<double> duration = std::chrono::system_clock::now() - t0;
650 ui->Print("Update package verification took %.1f s (result %d).\n", duration.count(), err);
651 if (err != VERIFY_SUCCESS) {
652 LOGE("Signature verification failed\n");
653 LOGE("error: %d\n", kZipVerificationFailure);
654 return false;
655 }
656 return true;
657}