blob: 0da3946bbcbea80b377d4dcbe3dbc4ca845dad1f [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
17#include <ctype.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070018#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
Tao Bao862a4c12016-06-02 11:16:50 -070022#include <inttypes.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <limits.h>
Tao Bao862a4c12016-06-02 11:16:50 -070024#include <linux/fs.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <linux/input.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070026#include <stdarg.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Patrick Tjincd055ee2014-12-09 11:26:40 -080030#include <sys/klog.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070031#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032#include <sys/types.h>
Tao Baocdcf28f2016-01-13 15:05:20 -080033#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include <time.h>
35#include <unistd.h>
36
Tao Bao04ca4262015-09-10 15:32:24 -070037#include <chrono>
Tao Bao862a4c12016-06-02 11:16:50 -070038#include <string>
39#include <vector>
Tao Bao04ca4262015-09-10 15:32:24 -070040
Tao Bao75238632015-05-27 14:46:17 -070041#include <adb.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070043#include <android-base/logging.h>
Tianjie Xu3c62b672016-02-05 18:25:58 -080044#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070045#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080046#include <android-base/stringprintf.h>
Tao Bao862a4c12016-06-02 11:16:50 -070047#include <android-base/strings.h>
48#include <android-base/unique_fd.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070049#include <bootloader_message/bootloader_message.h>
Tao Bao75238632015-05-27 14:46:17 -070050#include <cutils/android_reboot.h>
Elliott Hughescb220402016-09-23 15:30:55 -070051#include <cutils/properties.h> /* for property_list */
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070052#include <healthd/BatteryMonitor.h>
Mark Salyzyna4f701a2016-03-09 14:58:16 -080053#include <private/android_logger.h> /* private pmsg functions */
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070054#include <selinux/label.h>
55#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070056#include <ziparchive/zip_archive.h>
Yabin Cui99281df2016-02-17 12:21:52 -080057
Tao Bao75238632015-05-27 14:46:17 -070058#include "adb_install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059#include "common.h"
Tao Bao75238632015-05-27 14:46:17 -070060#include "device.h"
Tianjie Xu16255832016-04-30 11:49:59 -070061#include "error_code.h"
Tao Bao75238632015-05-27 14:46:17 -070062#include "fuse_sdcard_provider.h"
63#include "fuse_sideload.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064#include "install.h"
Josh Gaoacb2a2f2016-08-26 18:24:34 -070065#include "minadbd/minadbd.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066#include "minui/minui.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070067#include "otautil/DirUtil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068#include "roots.h"
Tianjie Xue113e4d2016-10-21 17:46:13 -070069#include "rotate_logs.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070070#include "screen_ui.h"
Sen Jiangd5304492016-12-09 16:20:49 -080071#include "stub_ui.h"
Tianjie Xue113e4d2016-10-21 17:46:13 -070072#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073
74static const struct option OPTIONS[] = {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 { "update_package", required_argument, NULL, 'u' },
Tianjie Xu3c62b672016-02-05 18:25:58 -080076 { "retry_count", required_argument, NULL, 'n' },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 { "wipe_data", no_argument, NULL, 'w' },
78 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070079 { "show_text", no_argument, NULL, 't' },
Tao Baoc679f932015-03-30 09:43:49 -070080 { "sideload", no_argument, NULL, 's' },
81 { "sideload_auto_reboot", no_argument, NULL, 'a' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070082 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070083 { "locale", required_argument, NULL, 'l' },
Doug Zongkerc87bab12013-11-25 13:53:25 -080084 { "stages", required_argument, NULL, 'g' },
Doug Zongkerb1d12632014-03-18 10:32:12 -070085 { "shutdown_after", no_argument, NULL, 'p' },
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070086 { "reason", required_argument, NULL, 'r' },
Tianjie Xu35926c42016-04-28 18:06:26 -070087 { "security", no_argument, NULL, 'e'},
Yabin Cui8b309f62016-06-24 18:22:02 -070088 { "wipe_ab", no_argument, NULL, 0 },
Yabin Cuifd99a312016-06-09 14:09:39 -070089 { "wipe_package_size", required_argument, NULL, 0 },
Doug Zongker988500b2009-10-06 14:41:38 -070090 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091};
92
Tianjie Xu06e57ac2016-07-11 14:04:08 -070093// More bootreasons can be found in "system/core/bootstat/bootstat.cpp".
94static const std::vector<std::string> bootreason_blacklist {
95 "kernel_panic",
96 "Panic",
97};
98
Doug Zongker6d0d7ac2013-07-09 13:34:55 -070099static const char *CACHE_LOG_DIR = "/cache/recovery";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700100static const char *COMMAND_FILE = "/cache/recovery/command";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -0700102static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -0700103static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Paul Lawrence661f8a62016-02-25 12:42:19 -0800104static const char *CONVERT_FBE_DIR = "/tmp/convert_fbe";
105static const char *CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
Michael Ward9d2629c2011-06-23 22:14:24 -0700106static const char *CACHE_ROOT = "/cache";
Paul Lawrenced0db3372015-11-05 13:38:40 -0800107static const char *DATA_ROOT = "/data";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700108static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800109static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -0700110static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Tao Baof0124322015-04-11 02:04:11 +0000111static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
Tao Baobef39712015-05-04 18:50:27 -0700112static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Tianjie Xuc37c5c32016-06-23 16:52:17 -0700113// We will try to apply the update package 5 times at most in case of an I/O error.
114static const int EIO_RETRY_COUNT = 4;
Yabin Cui99281df2016-02-17 12:21:52 -0800115static const int BATTERY_READ_TIMEOUT_IN_SEC = 10;
116// GmsCore enters recovery mode to install package when having enough battery
117// percentage. Normally, the threshold is 40% without charger and 20% with charger.
118// So we should check battery with a slightly lower limitation.
119static const int BATTERY_OK_PERCENTAGE = 20;
120static const int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
Tao Baoac9d94d2016-11-03 11:37:15 -0700121static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe";
122static constexpr const char* DEFAULT_LOCALE = "en_US";
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700123
Tao Baoac9d94d2016-11-03 11:37:15 -0700124static std::string locale;
Dan Albert8584fcf2016-10-27 03:08:08 +0000125static bool has_cache = false;
Tao Baoc0319b62016-10-13 15:17:04 -0700126
Tao Baoac9d94d2016-11-03 11:37:15 -0700127RecoveryUI* ui = nullptr;
128bool modified_flash = false;
129const char* stage = nullptr;
130const char* reason = nullptr;
131struct selabel_handle* sehandle;
132
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133/*
134 * The recovery tool communicates with the main system through /cache files.
135 * /cache/recovery/command - INPUT - command line for tool, one arg per line
136 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 *
138 * The arguments which may be supplied in the recovery.command file:
Doug Zongkerd4208f92010-09-20 12:16:13 -0700139 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 * --wipe_data - erase user data (and cache), then reboot
141 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -0800142 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700143 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144 *
145 * After completing, we remove /cache/recovery/command and reboot.
146 * Arguments may also be supplied in the bootloader control block (BCB).
147 * These important scenarios must be safely restartable at any point:
148 *
149 * FACTORY RESET
150 * 1. user selects "factory reset"
151 * 2. main system writes "--wipe_data" to /cache/recovery/command
152 * 3. main system reboots into recovery
153 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
154 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700155 * 5. erase_volume() reformats /data
156 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157 * 7. finish_recovery() erases BCB
158 * -- after this, rebooting will restart the main system --
159 * 8. main() calls reboot() to boot main system
160 *
161 * OTA INSTALL
162 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700163 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164 * 3. main system reboots into recovery
165 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
166 * -- after this, rebooting will attempt to reinstall the update --
167 * 5. install_package() attempts to install the update
168 * NOTE: the package install must itself be restartable from any point
169 * 6. finish_recovery() erases BCB
170 * -- after this, rebooting will (try to) restart the main system --
171 * 7. ** if install failed **
172 * 7a. prompt_and_wait() shows an error icon and waits for the user
Tao Baoc0336392016-12-13 22:29:49 -0800173 * 7b. the user reboots (pulling the battery, etc) into the main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174 */
175
Doug Zongkerd4208f92010-09-20 12:16:13 -0700176// open a given path, mounting partitions as necessary
Tao Bao04ca4262015-09-10 15:32:24 -0700177FILE* fopen_path(const char *path, const char *mode) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700178 if (ensure_path_mounted(path) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700179 LOG(ERROR) << "Can't mount " << path;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800180 return NULL;
181 }
182
183 // When writing, try to create the containing directory, if necessary.
184 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500185 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186
187 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800188 return fp;
189}
190
Tao Bao04ca4262015-09-10 15:32:24 -0700191// close a file, log an error if the error indicator is set
192static void check_and_fclose(FILE *fp, const char *name) {
193 fflush(fp);
Tao Baoac9d94d2016-11-03 11:37:15 -0700194 if (fsync(fileno(fp)) == -1) {
195 PLOG(ERROR) << "Failed to fsync " << name;
196 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700197 if (ferror(fp)) {
198 PLOG(ERROR) << "Error in " << name;
199 }
Tao Bao04ca4262015-09-10 15:32:24 -0700200 fclose(fp);
201}
202
Elliott Hughesf14af802015-02-10 14:46:14 -0800203bool is_ro_debuggable() {
Elliott Hughescb220402016-09-23 15:30:55 -0700204 return android::base::GetBoolProperty("ro.debuggable", false);
Elliott Hughesf14af802015-02-10 14:46:14 -0800205}
206
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700207static void redirect_stdio(const char* filename) {
Tao Bao04ca4262015-09-10 15:32:24 -0700208 int pipefd[2];
209 if (pipe(pipefd) == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700210 PLOG(ERROR) << "pipe failed";
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700211
Tao Bao04ca4262015-09-10 15:32:24 -0700212 // Fall back to traditional logging mode without timestamps.
213 // If these fail, there's not really anywhere to complain...
214 freopen(filename, "a", stdout); setbuf(stdout, NULL);
215 freopen(filename, "a", stderr); setbuf(stderr, NULL);
216
217 return;
218 }
219
220 pid_t pid = fork();
221 if (pid == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700222 PLOG(ERROR) << "fork failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700223
224 // Fall back to traditional logging mode without timestamps.
225 // If these fail, there's not really anywhere to complain...
226 freopen(filename, "a", stdout); setbuf(stdout, NULL);
227 freopen(filename, "a", stderr); setbuf(stderr, NULL);
228
229 return;
230 }
231
232 if (pid == 0) {
233 /// Close the unused write end.
234 close(pipefd[1]);
235
236 auto start = std::chrono::steady_clock::now();
237
238 // Child logger to actually write to the log file.
239 FILE* log_fp = fopen(filename, "a");
240 if (log_fp == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700241 PLOG(ERROR) << "fopen \"" << filename << "\" failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700242 close(pipefd[0]);
243 _exit(1);
244 }
245
246 FILE* pipe_fp = fdopen(pipefd[0], "r");
247 if (pipe_fp == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700248 PLOG(ERROR) << "fdopen failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700249 check_and_fclose(log_fp, filename);
250 close(pipefd[0]);
251 _exit(1);
252 }
253
254 char* line = nullptr;
255 size_t len = 0;
256 while (getline(&line, &len, pipe_fp) != -1) {
257 auto now = std::chrono::steady_clock::now();
258 double duration = std::chrono::duration_cast<std::chrono::duration<double>>(
259 now - start).count();
260 if (line[0] == '\n') {
261 fprintf(log_fp, "[%12.6lf]\n", duration);
262 } else {
263 fprintf(log_fp, "[%12.6lf] %s", duration, line);
264 }
265 fflush(log_fp);
266 }
267
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700268 PLOG(ERROR) << "getline failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700269
270 free(line);
271 check_and_fclose(log_fp, filename);
272 close(pipefd[0]);
273 _exit(1);
274 } else {
275 // Redirect stdout/stderr to the logger process.
276 // Close the unused read end.
277 close(pipefd[0]);
278
279 setbuf(stdout, nullptr);
280 setbuf(stderr, nullptr);
281
282 if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700283 PLOG(ERROR) << "dup2 stdout failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700284 }
285 if (dup2(pipefd[1], STDERR_FILENO) == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700286 PLOG(ERROR) << "dup2 stderr failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700287 }
288
289 close(pipefd[1]);
290 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800291}
292
293// command line args come from, in decreasing precedence:
294// - the actual command line
295// - the bootloader control block (one per line, after "recovery")
296// - the contents of COMMAND_FILE (one per line)
Tao Baof0ed1592016-12-02 11:32:19 -0800297static std::vector<std::string> get_args(const int argc, char** const argv) {
298 CHECK_GT(argc, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800299
Tao Baof0ed1592016-12-02 11:32:19 -0800300 bootloader_message boot = {};
301 std::string err;
302 if (!read_bootloader_message(&boot, &err)) {
303 LOG(ERROR) << err;
304 // If fails, leave a zeroed bootloader_message.
305 boot = {};
306 }
307 stage = strndup(boot.stage, sizeof(boot.stage));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308
Tao Baof0ed1592016-12-02 11:32:19 -0800309 if (boot.command[0] != 0) {
310 std::string boot_command = std::string(boot.command, sizeof(boot.command));
311 LOG(INFO) << "Boot command: " << boot_command;
312 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313
Tao Baof0ed1592016-12-02 11:32:19 -0800314 if (boot.status[0] != 0) {
315 std::string boot_status = std::string(boot.status, sizeof(boot.status));
316 LOG(INFO) << "Boot status: " << boot_status;
317 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800318
Tao Baof0ed1592016-12-02 11:32:19 -0800319 std::vector<std::string> args(argv, argv + argc);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800320
Tao Baof0ed1592016-12-02 11:32:19 -0800321 // --- if arguments weren't supplied, look in the bootloader control block
322 if (argc == 1) {
323 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
324 std::string boot_recovery(boot.recovery);
325 std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n");
326 if (!tokens.empty() && tokens[0] == "recovery") {
327 for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
328 // Skip empty and '\0'-filled tokens.
329 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
330 }
331 LOG(INFO) << "Got " << args.size() << " arguments from boot message";
332 } else if (boot.recovery[0] != 0) {
333 LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\"";
334 }
335 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800336
Tao Baof0ed1592016-12-02 11:32:19 -0800337 // --- if that doesn't work, try the command file (if we have /cache).
338 if (argc == 1 && has_cache) {
339 std::string content;
Tao Bao7d34fa12016-12-08 18:10:48 -0800340 if (ensure_path_mounted(COMMAND_FILE) == 0 &&
341 android::base::ReadFileToString(COMMAND_FILE, &content)) {
Tao Baof0ed1592016-12-02 11:32:19 -0800342 std::vector<std::string> tokens = android::base::Split(content, "\n");
Tao Bao7d34fa12016-12-08 18:10:48 -0800343 // All the arguments in COMMAND_FILE are needed (unlike the BCB message,
344 // COMMAND_FILE doesn't use filename as the first argument).
345 for (auto it = tokens.begin(); it != tokens.end(); it++) {
Tao Baof0ed1592016-12-02 11:32:19 -0800346 // Skip empty and '\0'-filled tokens.
347 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
348 }
349 LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800350 }
Tao Baof0ed1592016-12-02 11:32:19 -0800351 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352
Tao Baof0ed1592016-12-02 11:32:19 -0800353 // Write the arguments (excluding the filename in args[0]) back into the
354 // bootloader control block. So the device will always boot into recovery to
355 // finish the pending work, until finish_recovery() is called.
356 std::vector<std::string> options(args.cbegin() + 1, args.cend());
Tao Bao2292db82016-12-13 21:53:31 -0800357 if (!update_bootloader_message(options, &err)) {
358 LOG(ERROR) << "Failed to set BCB message: " << err;
Tao Baof0ed1592016-12-02 11:32:19 -0800359 }
360
361 return args;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800362}
363
Tao Bao2292db82016-12-13 21:53:31 -0800364// Set the BCB to reboot back into recovery (it won't resume the install from
365// sdcard though).
366static void set_sdcard_update_bootloader_message() {
367 std::vector<std::string> options;
368 std::string err;
369 if (!update_bootloader_message(options, &err)) {
370 LOG(ERROR) << "Failed to set BCB message: " << err;
371 }
Doug Zongker34c98df2009-08-18 12:05:45 -0700372}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800373
Tao Baobef39712015-05-04 18:50:27 -0700374// Read from kernel log into buffer and write out to file.
375static void save_kernel_log(const char* destination) {
376 int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800377 if (klog_buf_len <= 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700378 PLOG(ERROR) << "Error getting klog size";
Patrick Tjincd055ee2014-12-09 11:26:40 -0800379 return;
380 }
381
Tao Baobef39712015-05-04 18:50:27 -0700382 std::string buffer(klog_buf_len, 0);
383 int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len);
384 if (n == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700385 PLOG(ERROR) << "Error in reading klog";
Patrick Tjincd055ee2014-12-09 11:26:40 -0800386 return;
387 }
Tao Baobef39712015-05-04 18:50:27 -0700388 buffer.resize(n);
389 android::base::WriteStringToFile(buffer, destination);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800390}
391
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800392// write content to the current pmsg session.
393static ssize_t __pmsg_write(const char *filename, const char *buf, size_t len) {
394 return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO,
395 filename, buf, len);
396}
397
398static void copy_log_file_to_pmsg(const char* source, const char* destination) {
399 std::string content;
400 android::base::ReadFileToString(source, &content);
401 __pmsg_write(destination, content.c_str(), content.length());
402}
403
Tao Baof0124322015-04-11 02:04:11 +0000404// How much of the temp log we have copied to the copy in cache.
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700405static off_t tmplog_offset = 0;
Tao Baof0124322015-04-11 02:04:11 +0000406
Tao Baobef39712015-05-04 18:50:27 -0700407static void copy_log_file(const char* source, const char* destination, bool append) {
408 FILE* dest_fp = fopen_path(destination, append ? "a" : "w");
409 if (dest_fp == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700410 PLOG(ERROR) << "Can't open " << destination;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700411 } else {
Tao Baobef39712015-05-04 18:50:27 -0700412 FILE* source_fp = fopen(source, "r");
413 if (source_fp != nullptr) {
Tao Baof0124322015-04-11 02:04:11 +0000414 if (append) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700415 fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write
Doug Zongker2c3539e2010-09-29 13:21:30 -0700416 }
417 char buf[4096];
Tao Baobef39712015-05-04 18:50:27 -0700418 size_t bytes;
419 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
420 fwrite(buf, 1, bytes, dest_fp);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700421 }
Tao Baobef39712015-05-04 18:50:27 -0700422 if (append) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700423 tmplog_offset = ftello(source_fp);
Tao Baobef39712015-05-04 18:50:27 -0700424 }
425 check_and_fclose(source_fp, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700426 }
Tao Baobef39712015-05-04 18:50:27 -0700427 check_and_fclose(dest_fp, destination);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700428 }
429}
430
Tao Bao682c34b2015-04-07 17:16:35 -0700431static void copy_logs() {
432 // We only rotate and record the log of the current session if there are
433 // actual attempts to modify the flash, such as wipes, installs from BCB
434 // or menu selections. This is to avoid unnecessary rotation (and
435 // possible deletion) of log files, if it does not do anything loggable.
436 if (!modified_flash) {
437 return;
438 }
439
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800440 // Always write to pmsg, this allows the OTA logs to be caught in logcat -L
441 copy_log_file_to_pmsg(TEMPORARY_LOG_FILE, LAST_LOG_FILE);
442 copy_log_file_to_pmsg(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE);
443
444 // We can do nothing for now if there's no /cache partition.
445 if (!has_cache) {
446 return;
447 }
448
Tianjie Xue113e4d2016-10-21 17:46:13 -0700449 ensure_path_mounted(LAST_LOG_FILE);
450 ensure_path_mounted(LAST_KMSG_FILE);
451 rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
Tao Bao682c34b2015-04-07 17:16:35 -0700452
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700453 // Copy logs to cache so the system can find out what happened.
Tao Baof0124322015-04-11 02:04:11 +0000454 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
455 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
456 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
457 save_kernel_log(LAST_KMSG_FILE);
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700458 chmod(LOG_FILE, 0600);
459 chown(LOG_FILE, 1000, 1000); // system user
Tao Baof0124322015-04-11 02:04:11 +0000460 chmod(LAST_KMSG_FILE, 0600);
461 chown(LAST_KMSG_FILE, 1000, 1000); // system user
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700462 chmod(LAST_LOG_FILE, 0640);
463 chmod(LAST_INSTALL_FILE, 0644);
464 sync();
465}
466
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800467// clear the recovery command and prepare to boot a (hopefully working) system,
Tianjie Xuc14d95d2016-03-24 11:50:34 -0700468// copy our log file to cache as well (for the system to read). This function is
469// idempotent: call it as many times as you like.
Tao Baoac9d94d2016-11-03 11:37:15 -0700470static void finish_recovery() {
Doug Zongker4f33e552012-08-23 13:16:12 -0700471 // Save the locale to cache, so if recovery is next started up
472 // without a --locale argument (eg, directly from the bootloader)
473 // it will use the last-known locale.
Tao Baoac9d94d2016-11-03 11:37:15 -0700474 if (!locale.empty() && has_cache) {
475 LOG(INFO) << "Saving locale \"" << locale << "\"";
476
477 FILE* fp = fopen_path(LOCALE_FILE, "w");
478 if (!android::base::WriteStringToFd(locale, fileno(fp))) {
479 PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800480 }
Tao Baoac9d94d2016-11-03 11:37:15 -0700481 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker4f33e552012-08-23 13:16:12 -0700482 }
483
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700484 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800485
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700486 // Reset to normal system boot so recovery won't cycle indefinitely.
Yabin Cui8b309f62016-06-24 18:22:02 -0700487 std::string err;
Tianjie Xubd56f152016-09-28 16:32:10 -0700488 if (!clear_bootloader_message(&err)) {
Tao Bao2292db82016-12-13 21:53:31 -0800489 LOG(ERROR) << "Failed to clear BCB message: " << err;
Yabin Cui8b309f62016-06-24 18:22:02 -0700490 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800491
492 // Remove the command file, so recovery won't repeat indefinitely.
Tao Bao26112e52016-02-25 12:29:40 -0800493 if (has_cache) {
494 if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700495 LOG(WARNING) << "Can't unlink " << COMMAND_FILE;
Tao Bao26112e52016-02-25 12:29:40 -0800496 }
497 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800498 }
499
500 sync(); // For good measure.
501}
502
Tao Bao3f5a3822016-12-13 11:14:37 -0800503struct saved_log_file {
504 std::string name;
505 struct stat sb;
506 std::string data;
507};
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700508
Elliott Hughes945548e2015-06-05 17:59:56 -0700509static bool erase_volume(const char* volume) {
Tao Bao3f5a3822016-12-13 11:14:37 -0800510 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
511 bool is_data = (strcmp(volume, DATA_ROOT) == 0);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700512
Tao Bao3f5a3822016-12-13 11:14:37 -0800513 ui->SetBackground(RecoveryUI::ERASING);
514 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700515
Tao Bao3f5a3822016-12-13 11:14:37 -0800516 std::vector<saved_log_file> log_files;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700517
Tao Bao3f5a3822016-12-13 11:14:37 -0800518 if (is_cache) {
519 // If we're reformatting /cache, we load any past logs
520 // (i.e. "/cache/recovery/last_*") and the current log
521 // ("/cache/recovery/log") into memory, so we can restore them after
522 // the reformat.
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700523
Tao Bao3f5a3822016-12-13 11:14:37 -0800524 ensure_path_mounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700525
Tao Bao3f5a3822016-12-13 11:14:37 -0800526 struct dirent* de;
527 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(CACHE_LOG_DIR), closedir);
528 if (d) {
529 while ((de = readdir(d.get())) != nullptr) {
530 if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) {
531 std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name);
532
533 struct stat sb;
534 if (stat(path.c_str(), &sb) == 0) {
535 // truncate files to 512kb
536 if (sb.st_size > (1 << 19)) {
537 sb.st_size = 1 << 19;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700538 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800539
540 std::string data(sb.st_size, '\0');
541 FILE* f = fopen(path.c_str(), "rb");
542 fread(&data[0], 1, data.size(), f);
543 fclose(f);
544
545 log_files.emplace_back(saved_log_file{ path, sb, data });
546 }
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700547 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800548 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800549 } else {
Tao Bao3f5a3822016-12-13 11:14:37 -0800550 if (errno != ENOENT) {
551 PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR;
552 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800553 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800554 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700555
Tao Bao3f5a3822016-12-13 11:14:37 -0800556 ui->Print("Formatting %s...\n", volume);
557
558 ensure_path_unmounted(volume);
559
560 int result;
561
562 if (is_data && reason && strcmp(reason, "convert_fbe") == 0) {
563 // Create convert_fbe breadcrumb file to signal to init
564 // to convert to file based encryption, not full disk encryption
565 if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
566 ui->Print("Failed to make convert_fbe dir %s\n", strerror(errno));
567 return true;
568 }
569 FILE* f = fopen(CONVERT_FBE_FILE, "wb");
570 if (!f) {
571 ui->Print("Failed to convert to file encryption %s\n", strerror(errno));
572 return true;
573 }
574 fclose(f);
575 result = format_volume(volume, CONVERT_FBE_DIR);
576 remove(CONVERT_FBE_FILE);
577 rmdir(CONVERT_FBE_DIR);
578 } else {
579 result = format_volume(volume);
580 }
581
582 if (is_cache) {
583 // Re-create the log dir and write back the log entries.
584 if (ensure_path_mounted(CACHE_LOG_DIR) == 0 &&
585 dirCreateHierarchy(CACHE_LOG_DIR, 0777, nullptr, false, sehandle) == 0) {
586 for (const auto& log : log_files) {
587 if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid,
588 log.sb.st_gid)) {
589 PLOG(ERROR) << "Failed to write to " << log.name;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700590 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800591 }
592 } else {
593 PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700594 }
595
Tao Bao3f5a3822016-12-13 11:14:37 -0800596 // Any part of the log we'd copied to cache is now gone.
597 // Reset the pointer so we copy from the beginning of the temp
598 // log.
599 tmplog_offset = 0;
600 copy_logs();
601 }
602
603 return (result == 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800604}
605
Doug Zongkerf93d8162009-09-22 15:16:02 -0700606static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700607get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700608 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700609 // throw away keys pressed previously, so user doesn't
610 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700611 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700612
Doug Zongker211aebc2011-10-28 15:13:10 -0700613 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700614 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800615 int chosen_item = -1;
616
Doug Zongkerf93d8162009-09-22 15:16:02 -0700617 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700618 int key = ui->WaitKey();
619 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800620
Doug Zongker5cae4452011-01-25 13:15:30 -0800621 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700622 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800623 continue;
624 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700625 LOG(INFO) << "timed out waiting for key input; rebooting.";
Doug Zongker211aebc2011-10-28 15:13:10 -0700626 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700627 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800628 }
629 }
630
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700631 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700632
633 if (action < 0) {
634 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700635 case Device::kHighlightUp:
Elliott Hughes642aaa72015-04-10 12:47:46 -0700636 selected = ui->SelectMenu(--selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700637 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700638 case Device::kHighlightDown:
Elliott Hughes642aaa72015-04-10 12:47:46 -0700639 selected = ui->SelectMenu(++selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700640 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700641 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700642 chosen_item = selected;
643 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700644 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700645 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800646 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700647 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700648 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800649 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700650 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800651
Doug Zongker211aebc2011-10-28 15:13:10 -0700652 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700653 return chosen_item;
654}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800655
Doug Zongker8674a722010-09-15 11:08:23 -0700656static int compare_string(const void* a, const void* b) {
657 return strcmp(*(const char**)a, *(const char**)b);
658}
659
Doug Zongker93950222014-07-08 14:10:23 -0700660// Returns a malloc'd path, or NULL.
Elliott Hughes30694c92015-03-25 15:16:51 -0700661static char* browse_directory(const char* path, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700662 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700663
Elliott Hughes30694c92015-03-25 15:16:51 -0700664 DIR* d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700665 if (d == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700666 PLOG(ERROR) << "error opening " << path;
Doug Zongker93950222014-07-08 14:10:23 -0700667 return NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700668 }
669
Doug Zongker8674a722010-09-15 11:08:23 -0700670 int d_size = 0;
671 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700672 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700673 int z_size = 1;
674 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700675 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700676 zips[0] = strdup("../");
677
Elliott Hughes30694c92015-03-25 15:16:51 -0700678 struct dirent* de;
Doug Zongker8674a722010-09-15 11:08:23 -0700679 while ((de = readdir(d)) != NULL) {
680 int name_len = strlen(de->d_name);
681
682 if (de->d_type == DT_DIR) {
683 // skip "." and ".." entries
684 if (name_len == 1 && de->d_name[0] == '.') continue;
685 if (name_len == 2 && de->d_name[0] == '.' &&
686 de->d_name[1] == '.') continue;
687
688 if (d_size >= d_alloc) {
689 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700690 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700691 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700692 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700693 strcpy(dirs[d_size], de->d_name);
694 dirs[d_size][name_len] = '/';
695 dirs[d_size][name_len+1] = '\0';
696 ++d_size;
697 } else if (de->d_type == DT_REG &&
698 name_len >= 4 &&
699 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
700 if (z_size >= z_alloc) {
701 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700702 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700703 }
704 zips[z_size++] = strdup(de->d_name);
705 }
706 }
707 closedir(d);
708
709 qsort(dirs, d_size, sizeof(char*), compare_string);
710 qsort(zips, z_size, sizeof(char*), compare_string);
711
712 // append dirs to the zips list
713 if (d_size + z_size + 1 > z_alloc) {
714 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700715 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700716 }
717 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
718 free(dirs);
719 z_size += d_size;
720 zips[z_size] = NULL;
721
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700722 const char* headers[] = { "Choose a package to install:", path, NULL };
Elliott Hughes30694c92015-03-25 15:16:51 -0700723
Doug Zongker93950222014-07-08 14:10:23 -0700724 char* result;
Doug Zongker8674a722010-09-15 11:08:23 -0700725 int chosen_item = 0;
Doug Zongker93950222014-07-08 14:10:23 -0700726 while (true) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700727 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700728
729 char* item = zips[chosen_item];
730 int item_len = strlen(item);
731 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700732 // go up but continue browsing (if the caller is update_directory)
Doug Zongker93950222014-07-08 14:10:23 -0700733 result = NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700734 break;
735 }
Doug Zongker93950222014-07-08 14:10:23 -0700736
737 char new_path[PATH_MAX];
738 strlcpy(new_path, path, PATH_MAX);
739 strlcat(new_path, "/", PATH_MAX);
740 strlcat(new_path, item, PATH_MAX);
741
742 if (item[item_len-1] == '/') {
743 // recurse down into a subdirectory
744 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
745 result = browse_directory(new_path, device);
746 if (result) break;
747 } else {
748 // selected a zip file: return the malloc'd path to the caller.
749 result = strdup(new_path);
750 break;
751 }
752 }
Doug Zongker8674a722010-09-15 11:08:23 -0700753
Elliott Hughes30694c92015-03-25 15:16:51 -0700754 for (int i = 0; i < z_size; ++i) free(zips[i]);
Doug Zongker8674a722010-09-15 11:08:23 -0700755 free(zips);
Doug Zongker8674a722010-09-15 11:08:23 -0700756
757 return result;
758}
759
Elliott Hughes30694c92015-03-25 15:16:51 -0700760static bool yes_no(Device* device, const char* question1, const char* question2) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700761 const char* headers[] = { question1, question2, NULL };
Elliott Hughes30694c92015-03-25 15:16:51 -0700762 const char* items[] = { " No", " Yes", NULL };
Doug Zongkerddd6a282009-06-09 12:22:33 -0700763
Elliott Hughes30694c92015-03-25 15:16:51 -0700764 int chosen_item = get_menu_selection(headers, items, 1, 0, device);
765 return (chosen_item == 1);
766}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800767
Tao Baoe39a9bc2015-03-31 12:19:05 -0700768// Return true on success.
769static bool wipe_data(int should_confirm, Device* device) {
770 if (should_confirm && !yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!")) {
771 return false;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700772 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700773
Tao Bao682c34b2015-04-07 17:16:35 -0700774 modified_flash = true;
775
Doug Zongker211aebc2011-10-28 15:13:10 -0700776 ui->Print("\n-- Wiping data...\n");
Elliott Hughes945548e2015-06-05 17:59:56 -0700777 bool success =
778 device->PreWipeData() &&
779 erase_volume("/data") &&
Tao Bao26112e52016-02-25 12:29:40 -0800780 (has_cache ? erase_volume("/cache") : true) &&
Elliott Hughes945548e2015-06-05 17:59:56 -0700781 device->PostWipeData();
782 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
783 return success;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700784}
785
Tao Baoe39a9bc2015-03-31 12:19:05 -0700786// Return true on success.
787static bool wipe_cache(bool should_confirm, Device* device) {
Tao Bao26112e52016-02-25 12:29:40 -0800788 if (!has_cache) {
789 ui->Print("No /cache partition found.\n");
790 return false;
791 }
792
Elliott Hughes30694c92015-03-25 15:16:51 -0700793 if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) {
Tao Baoe39a9bc2015-03-31 12:19:05 -0700794 return false;
Elliott Hughes30694c92015-03-25 15:16:51 -0700795 }
796
Tao Bao682c34b2015-04-07 17:16:35 -0700797 modified_flash = true;
798
Elliott Hughes30694c92015-03-25 15:16:51 -0700799 ui->Print("\n-- Wiping cache...\n");
Elliott Hughes945548e2015-06-05 17:59:56 -0700800 bool success = erase_volume("/cache");
801 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
802 return success;
Elliott Hughes30694c92015-03-25 15:16:51 -0700803}
804
Tao Bao862a4c12016-06-02 11:16:50 -0700805// Secure-wipe a given partition. It uses BLKSECDISCARD, if supported.
806// Otherwise, it goes with BLKDISCARD (if device supports BLKDISCARDZEROES) or
807// BLKZEROOUT.
808static bool secure_wipe_partition(const std::string& partition) {
809 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY)));
810 if (fd == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700811 PLOG(ERROR) << "failed to open \"" << partition << "\"";
Tao Bao862a4c12016-06-02 11:16:50 -0700812 return false;
813 }
814
815 uint64_t range[2] = {0, 0};
816 if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700817 PLOG(ERROR) << "failed to get partition size";
Tao Bao862a4c12016-06-02 11:16:50 -0700818 return false;
819 }
820 printf("Secure-wiping \"%s\" from %" PRIu64 " to %" PRIu64 ".\n",
821 partition.c_str(), range[0], range[1]);
822
823 printf("Trying BLKSECDISCARD...\t");
824 if (ioctl(fd, BLKSECDISCARD, &range) == -1) {
825 printf("failed: %s\n", strerror(errno));
826
827 // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT.
828 unsigned int zeroes;
829 if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) {
830 printf("Trying BLKDISCARD...\t");
831 if (ioctl(fd, BLKDISCARD, &range) == -1) {
832 printf("failed: %s\n", strerror(errno));
833 return false;
834 }
835 } else {
836 printf("Trying BLKZEROOUT...\t");
837 if (ioctl(fd, BLKZEROOUT, &range) == -1) {
838 printf("failed: %s\n", strerror(errno));
839 return false;
840 }
841 }
842 }
843
844 printf("done\n");
845 return true;
846}
847
Yabin Cuifd99a312016-06-09 14:09:39 -0700848// Check if the wipe package matches expectation:
849// 1. verify the package.
850// 2. check metadata (ota-type, pre-device and serial number if having one).
851static bool check_wipe_package(size_t wipe_package_size) {
852 if (wipe_package_size == 0) {
853 LOG(ERROR) << "wipe_package_size is zero";
854 return false;
855 }
856 std::string wipe_package;
857 std::string err_str;
858 if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
859 PLOG(ERROR) << "Failed to read wipe package";
860 return false;
861 }
862 if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()),
863 wipe_package.size())) {
864 LOG(ERROR) << "Failed to verify package";
865 return false;
866 }
867
868 // Extract metadata
869 ZipArchiveHandle zip;
870 int err = OpenArchiveFromMemory(reinterpret_cast<void*>(&wipe_package[0]),
871 wipe_package.size(), "wipe_package", &zip);
872 if (err != 0) {
873 LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err);
874 return false;
875 }
876 std::string metadata;
877 if (!read_metadata_from_package(&zip, &metadata)) {
878 CloseArchive(zip);
879 return false;
880 }
881 CloseArchive(zip);
882
883 // Check metadata
884 std::vector<std::string> lines = android::base::Split(metadata, "\n");
885 bool ota_type_matched = false;
886 bool device_type_matched = false;
887 bool has_serial_number = false;
888 bool serial_number_matched = false;
889 for (const auto& line : lines) {
890 if (line == "ota-type=BRICK") {
891 ota_type_matched = true;
892 } else if (android::base::StartsWith(line, "pre-device=")) {
893 std::string device_type = line.substr(strlen("pre-device="));
894 char real_device_type[PROPERTY_VALUE_MAX];
895 property_get("ro.build.product", real_device_type, "");
896 device_type_matched = (device_type == real_device_type);
897 } else if (android::base::StartsWith(line, "serialno=")) {
898 std::string serial_no = line.substr(strlen("serialno="));
899 char real_serial_no[PROPERTY_VALUE_MAX];
900 property_get("ro.serialno", real_serial_no, "");
901 has_serial_number = true;
902 serial_number_matched = (serial_no == real_serial_no);
903 }
904 }
905 return ota_type_matched && device_type_matched && (!has_serial_number || serial_number_matched);
906}
907
Yabin Cui8b309f62016-06-24 18:22:02 -0700908// Wipe the current A/B device, with a secure wipe of all the partitions in
909// RECOVERY_WIPE.
Yabin Cuifd99a312016-06-09 14:09:39 -0700910static bool wipe_ab_device(size_t wipe_package_size) {
Tao Bao862a4c12016-06-02 11:16:50 -0700911 ui->SetBackground(RecoveryUI::ERASING);
912 ui->SetProgressType(RecoveryUI::INDETERMINATE);
913
Yabin Cuifd99a312016-06-09 14:09:39 -0700914 if (!check_wipe_package(wipe_package_size)) {
915 LOG(ERROR) << "Failed to verify wipe package";
916 return false;
917 }
Tao Bao862a4c12016-06-02 11:16:50 -0700918 std::string partition_list;
Yabin Cui8b309f62016-06-24 18:22:02 -0700919 if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) {
920 LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\"";
Tao Bao862a4c12016-06-02 11:16:50 -0700921 return false;
922 }
923
924 std::vector<std::string> lines = android::base::Split(partition_list, "\n");
925 for (const std::string& line : lines) {
926 std::string partition = android::base::Trim(line);
927 // Ignore '#' comment or empty lines.
928 if (android::base::StartsWith(partition, "#") || partition.empty()) {
929 continue;
930 }
931
932 // Proceed anyway even if it fails to wipe some partition.
933 secure_wipe_partition(partition);
934 }
935 return true;
936}
937
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700938static void choose_recovery_file(Device* device) {
Elliott Hughesc0491632015-05-06 12:40:05 -0700939 // "Back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry
940 char* entries[1 + KEEP_LOG_COUNT * 2 + 1];
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700941 memset(entries, 0, sizeof(entries));
942
Tao Baobef39712015-05-04 18:50:27 -0700943 unsigned int n = 0;
Patrick Tjincd055ee2014-12-09 11:26:40 -0800944
Tianjie Xua54f75e2016-08-17 12:02:46 -0700945 if (has_cache) {
946 // Add LAST_LOG_FILE + LAST_LOG_FILE.x
947 // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
948 for (int i = 0; i < KEEP_LOG_COUNT; i++) {
949 char* log_file;
950 int ret;
951 ret = (i == 0) ? asprintf(&log_file, "%s", LAST_LOG_FILE) :
952 asprintf(&log_file, "%s.%d", LAST_LOG_FILE, i);
953 if (ret == -1) {
954 // memory allocation failure - return early. Should never happen.
955 return;
956 }
957 if ((ensure_path_mounted(log_file) != 0) || (access(log_file, R_OK) == -1)) {
958 free(log_file);
959 } else {
960 entries[n++] = log_file;
961 }
Tao Baobef39712015-05-04 18:50:27 -0700962
Tianjie Xua54f75e2016-08-17 12:02:46 -0700963 char* kmsg_file;
964 ret = (i == 0) ? asprintf(&kmsg_file, "%s", LAST_KMSG_FILE) :
965 asprintf(&kmsg_file, "%s.%d", LAST_KMSG_FILE, i);
966 if (ret == -1) {
967 // memory allocation failure - return early. Should never happen.
968 return;
969 }
970 if ((ensure_path_mounted(kmsg_file) != 0) || (access(kmsg_file, R_OK) == -1)) {
971 free(kmsg_file);
972 } else {
973 entries[n++] = kmsg_file;
974 }
Tao Baobef39712015-05-04 18:50:27 -0700975 }
Tianjie Xua54f75e2016-08-17 12:02:46 -0700976 } else {
977 // If cache partition is not found, view /tmp/recovery.log instead.
978 ui->Print("No /cache partition found.\n");
979 if (access(TEMPORARY_LOG_FILE, R_OK) == -1) {
980 return;
981 } else{
982 entries[n++] = strdup(TEMPORARY_LOG_FILE);
Tao Baobef39712015-05-04 18:50:27 -0700983 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700984 }
985
Elliott Hughesc0491632015-05-06 12:40:05 -0700986 entries[n++] = strdup("Back");
987
Tao Baobef39712015-05-04 18:50:27 -0700988 const char* headers[] = { "Select file to view", nullptr };
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700989
Elliott Hughes8de52072015-04-08 20:06:50 -0700990 while (true) {
Elliott Hughes30694c92015-03-25 15:16:51 -0700991 int chosen_item = get_menu_selection(headers, entries, 1, 0, device);
Elliott Hughesc0491632015-05-06 12:40:05 -0700992 if (strcmp(entries[chosen_item], "Back") == 0) break;
Elliott Hughes8de52072015-04-08 20:06:50 -0700993
Elliott Hughes8de52072015-04-08 20:06:50 -0700994 ui->ShowFile(entries[chosen_item]);
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700995 }
996
Tao Baobef39712015-05-04 18:50:27 -0700997 for (size_t i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) {
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700998 free(entries[i]);
999 }
1000}
1001
Elliott Hughes498cda62016-04-14 16:49:04 -07001002static void run_graphics_test(Device* device) {
1003 // Switch to graphics screen.
1004 ui->ShowText(false);
1005
1006 ui->SetProgressType(RecoveryUI::INDETERMINATE);
1007 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
1008 sleep(1);
1009
1010 ui->SetBackground(RecoveryUI::ERROR);
1011 sleep(1);
1012
1013 ui->SetBackground(RecoveryUI::NO_COMMAND);
1014 sleep(1);
1015
1016 ui->SetBackground(RecoveryUI::ERASING);
1017 sleep(1);
1018
1019 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
1020
1021 ui->SetProgressType(RecoveryUI::DETERMINATE);
1022 ui->ShowProgress(1.0, 10.0);
1023 float fraction = 0.0;
1024 for (size_t i = 0; i < 100; ++i) {
1025 fraction += .01;
1026 ui->SetProgress(fraction);
1027 usleep(100000);
1028 }
1029
1030 ui->ShowText(true);
1031}
1032
Tao Baocdcf28f2016-01-13 15:05:20 -08001033// How long (in seconds) we wait for the fuse-provided package file to
1034// appear, before timing out.
1035#define SDCARD_INSTALL_TIMEOUT 10
1036
Tao Bao145d8612015-03-25 15:51:15 -07001037static int apply_from_sdcard(Device* device, bool* wipe_cache) {
Tao Bao682c34b2015-04-07 17:16:35 -07001038 modified_flash = true;
1039
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001040 if (ensure_path_mounted(SDCARD_ROOT) != 0) {
1041 ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT);
1042 return INSTALL_ERROR;
1043 }
1044
1045 char* path = browse_directory(SDCARD_ROOT, device);
1046 if (path == NULL) {
Elliott Hughes018ed312015-04-08 16:51:36 -07001047 ui->Print("\n-- No package file selected.\n");
caozhiyuanb4effb92015-06-10 16:46:38 +08001048 ensure_path_unmounted(SDCARD_ROOT);
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001049 return INSTALL_ERROR;
1050 }
1051
1052 ui->Print("\n-- Install %s ...\n", path);
1053 set_sdcard_update_bootloader_message();
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001054
Tao Baocdcf28f2016-01-13 15:05:20 -08001055 // We used to use fuse in a thread as opposed to a process. Since accessing
1056 // through fuse involves going from kernel to userspace to kernel, it leads
1057 // to deadlock when a page fault occurs. (Bug: 26313124)
1058 pid_t child;
1059 if ((child = fork()) == 0) {
1060 bool status = start_sdcard_fuse(path);
1061
1062 _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
1063 }
1064
1065 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
1066 // process is ready.
1067 int result = INSTALL_ERROR;
1068 int status;
1069 bool waited = false;
1070 for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
1071 if (waitpid(child, &status, WNOHANG) == -1) {
1072 result = INSTALL_ERROR;
1073 waited = true;
1074 break;
1075 }
1076
1077 struct stat sb;
1078 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
1079 if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
1080 sleep(1);
1081 continue;
1082 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001083 LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
Tao Baocdcf28f2016-01-13 15:05:20 -08001084 result = INSTALL_ERROR;
1085 kill(child, SIGKILL);
1086 break;
1087 }
1088 }
1089
1090 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache,
Tianjie Xu16255832016-04-30 11:49:59 -07001091 TEMPORARY_INSTALL_FILE, false, 0/*retry_count*/);
Tao Baocdcf28f2016-01-13 15:05:20 -08001092 break;
1093 }
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001094
Tao Baocdcf28f2016-01-13 15:05:20 -08001095 if (!waited) {
1096 // Calling stat() on this magic filename signals the fuse
1097 // filesystem to shut down.
1098 struct stat sb;
1099 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
1100
1101 waitpid(child, &status, 0);
1102 }
1103
1104 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001105 LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
Tao Baocdcf28f2016-01-13 15:05:20 -08001106 }
1107
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001108 ensure_path_unmounted(SDCARD_ROOT);
Tao Baocdcf28f2016-01-13 15:05:20 -08001109 return result;
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001110}
1111
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001112// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
1113// means to take the default, which is to reboot or shutdown depending
1114// on if the --shutdown_after flag was passed to recovery.
1115static Device::BuiltinAction
Doug Zongker6c8553d2012-09-24 10:40:47 -07001116prompt_and_wait(Device* device, int status) {
Doug Zongkerf93d8162009-09-22 15:16:02 -07001117 for (;;) {
Tianjie Xuc14d95d2016-03-24 11:50:34 -07001118 finish_recovery();
Doug Zongker6c8553d2012-09-24 10:40:47 -07001119 switch (status) {
1120 case INSTALL_SUCCESS:
1121 case INSTALL_NONE:
1122 ui->SetBackground(RecoveryUI::NO_COMMAND);
1123 break;
1124
1125 case INSTALL_ERROR:
1126 case INSTALL_CORRUPT:
1127 ui->SetBackground(RecoveryUI::ERROR);
1128 break;
1129 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001130 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -07001131
Elliott Hughes8fd86d72015-04-13 14:36:02 -07001132 int chosen_item = get_menu_selection(nullptr, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -07001133
1134 // device-specific code may take some action here. It may
1135 // return one of the core actions handled in the switch
1136 // statement below.
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001137 Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -07001138
Elliott Hughes30694c92015-03-25 15:16:51 -07001139 bool should_wipe_cache = false;
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001140 switch (chosen_action) {
1141 case Device::NO_ACTION:
1142 break;
1143
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001144 case Device::REBOOT:
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001145 case Device::SHUTDOWN:
1146 case Device::REBOOT_BOOTLOADER:
1147 return chosen_action;
Doug Zongkerf93d8162009-09-22 15:16:02 -07001148
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001149 case Device::WIPE_DATA:
1150 wipe_data(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001151 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -07001152 break;
1153
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001154 case Device::WIPE_CACHE:
Elliott Hughes30694c92015-03-25 15:16:51 -07001155 wipe_cache(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001156 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -07001157 break;
1158
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001159 case Device::APPLY_ADB_SIDELOAD:
Elliott Hughesec283402015-04-10 10:01:53 -07001160 case Device::APPLY_SDCARD:
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001161 {
1162 bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
1163 if (adb) {
Elliott Hughes30694c92015-03-25 15:16:51 -07001164 status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001165 } else {
Elliott Hughes30694c92015-03-25 15:16:51 -07001166 status = apply_from_sdcard(device, &should_wipe_cache);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001167 }
Doug Zongker945fc682014-07-10 10:50:39 -07001168
Elliott Hughes30694c92015-03-25 15:16:51 -07001169 if (status == INSTALL_SUCCESS && should_wipe_cache) {
Tao Baoe39a9bc2015-03-31 12:19:05 -07001170 if (!wipe_cache(false, device)) {
1171 status = INSTALL_ERROR;
1172 }
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001173 }
1174
Tao Baoc679f932015-03-30 09:43:49 -07001175 if (status != INSTALL_SUCCESS) {
1176 ui->SetBackground(RecoveryUI::ERROR);
1177 ui->Print("Installation aborted.\n");
1178 copy_logs();
1179 } else if (!ui->IsTextVisible()) {
1180 return Device::NO_ACTION; // reboot if logs aren't visible
1181 } else {
1182 ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
Doug Zongker8674a722010-09-15 11:08:23 -07001183 }
Doug Zongkerf93d8162009-09-22 15:16:02 -07001184 }
1185 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001186
Elliott Hughesec283402015-04-10 10:01:53 -07001187 case Device::VIEW_RECOVERY_LOGS:
1188 choose_recovery_file(device);
Michael Ward9d2629c2011-06-23 22:14:24 -07001189 break;
Doug Zongker9270a202012-01-09 15:16:13 -08001190
Elliott Hughes498cda62016-04-14 16:49:04 -07001191 case Device::RUN_GRAPHICS_TEST:
1192 run_graphics_test(device);
1193 break;
1194
Elliott Hughesec283402015-04-10 10:01:53 -07001195 case Device::MOUNT_SYSTEM:
Tao Baoabb8f772015-07-30 14:43:27 -07001196 // For a system image built with the root directory (i.e.
1197 // system_root_image == "true"), we mount it to /system_root, and symlink /system
1198 // to /system_root/system to make adb shell work (the symlink is created through
1199 // the build system).
1200 // Bug: 22855115
Elliott Hughescb220402016-09-23 15:30:55 -07001201 if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
Tao Baoabb8f772015-07-30 14:43:27 -07001202 if (ensure_path_mounted_at("/", "/system_root") != -1) {
1203 ui->Print("Mounted /system.\n");
1204 }
1205 } else {
1206 if (ensure_path_mounted("/system") != -1) {
1207 ui->Print("Mounted /system.\n");
1208 }
Elliott Hughesec283402015-04-10 10:01:53 -07001209 }
Tao Baoabb8f772015-07-30 14:43:27 -07001210
Nick Kralevicha9ad0322014-10-22 18:38:48 -07001211 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001212 }
1213 }
1214}
1215
1216static void
Oscar Montemayor05231562009-11-30 08:40:57 -08001217print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -07001218 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001219}
1220
Tao Baoac9d94d2016-11-03 11:37:15 -07001221static std::string load_locale_from_cache() {
1222 if (ensure_path_mounted(LOCALE_FILE) != 0) {
1223 LOG(ERROR) << "Can't mount " << LOCALE_FILE;
1224 return "";
Doug Zongker02ec6b82012-08-22 17:26:40 -07001225 }
Tao Baoac9d94d2016-11-03 11:37:15 -07001226
1227 std::string content;
1228 if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
1229 PLOG(ERROR) << "Can't read " << LOCALE_FILE;
1230 return "";
1231 }
1232
1233 return android::base::Trim(content);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001234}
1235
Tao Baoac9d94d2016-11-03 11:37:15 -07001236void ui_print(const char* format, ...) {
1237 std::string buffer;
Doug Zongker7c3ae452013-05-14 11:03:02 -07001238 va_list ap;
1239 va_start(ap, format);
Tao Baoac9d94d2016-11-03 11:37:15 -07001240 android::base::StringAppendV(&buffer, format, ap);
Doug Zongker7c3ae452013-05-14 11:03:02 -07001241 va_end(ap);
1242
Tao Baoac9d94d2016-11-03 11:37:15 -07001243 if (ui != nullptr) {
1244 ui->Print("%s", buffer.c_str());
Doug Zongker7c3ae452013-05-14 11:03:02 -07001245 } else {
Tao Baoac9d94d2016-11-03 11:37:15 -07001246 fputs(buffer.c_str(), stdout);
Doug Zongker7c3ae452013-05-14 11:03:02 -07001247 }
1248}
1249
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001250static constexpr char log_characters[] = "VDIWEF";
1251
1252void UiLogger(android::base::LogId id, android::base::LogSeverity severity,
1253 const char* tag, const char* file, unsigned int line,
1254 const char* message) {
Tao Baoac9d94d2016-11-03 11:37:15 -07001255 if (severity >= android::base::ERROR && ui != nullptr) {
1256 ui->Print("E:%s\n", message);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001257 } else {
1258 fprintf(stdout, "%c:%s\n", log_characters[severity], message);
1259 }
1260}
1261
Yabin Cui99281df2016-02-17 12:21:52 -08001262static bool is_battery_ok() {
1263 struct healthd_config healthd_config = {
1264 .batteryStatusPath = android::String8(android::String8::kEmptyString),
1265 .batteryHealthPath = android::String8(android::String8::kEmptyString),
1266 .batteryPresentPath = android::String8(android::String8::kEmptyString),
1267 .batteryCapacityPath = android::String8(android::String8::kEmptyString),
1268 .batteryVoltagePath = android::String8(android::String8::kEmptyString),
1269 .batteryTemperaturePath = android::String8(android::String8::kEmptyString),
1270 .batteryTechnologyPath = android::String8(android::String8::kEmptyString),
1271 .batteryCurrentNowPath = android::String8(android::String8::kEmptyString),
1272 .batteryCurrentAvgPath = android::String8(android::String8::kEmptyString),
1273 .batteryChargeCounterPath = android::String8(android::String8::kEmptyString),
1274 .batteryFullChargePath = android::String8(android::String8::kEmptyString),
1275 .batteryCycleCountPath = android::String8(android::String8::kEmptyString),
1276 .energyCounter = NULL,
1277 .boot_min_cap = 0,
1278 .screen_on = NULL
1279 };
1280 healthd_board_init(&healthd_config);
1281
1282 android::BatteryMonitor monitor;
1283 monitor.init(&healthd_config);
1284
1285 int wait_second = 0;
1286 while (true) {
1287 int charge_status = monitor.getChargeStatus();
1288 // Treat unknown status as charged.
1289 bool charged = (charge_status != android::BATTERY_STATUS_DISCHARGING &&
1290 charge_status != android::BATTERY_STATUS_NOT_CHARGING);
1291 android::BatteryProperty capacity;
1292 android::status_t status = monitor.getProperty(android::BATTERY_PROP_CAPACITY, &capacity);
1293 ui_print("charge_status %d, charged %d, status %d, capacity %lld\n", charge_status,
1294 charged, status, capacity.valueInt64);
1295 // At startup, the battery drivers in devices like N5X/N6P take some time to load
1296 // the battery profile. Before the load finishes, it reports value 50 as a fake
1297 // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected
1298 // to finish loading the battery profile earlier than 10 seconds after kernel startup.
1299 if (status == 0 && capacity.valueInt64 == 50) {
1300 if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) {
1301 sleep(1);
1302 wait_second++;
1303 continue;
1304 }
1305 }
1306 // If we can't read battery percentage, it may be a device without battery. In this
1307 // situation, use 100 as a fake battery percentage.
1308 if (status != 0) {
1309 capacity.valueInt64 = 100;
1310 }
1311 return (charged && capacity.valueInt64 >= BATTERY_WITH_CHARGER_OK_PERCENTAGE) ||
1312 (!charged && capacity.valueInt64 >= BATTERY_OK_PERCENTAGE);
1313 }
1314}
1315
Tianjie Xu3c62b672016-02-05 18:25:58 -08001316static void set_retry_bootloader_message(int retry_count, int argc, char** argv) {
Tianjie Xubd56f152016-09-28 16:32:10 -07001317 std::vector<std::string> options;
Tianjie Xu3c62b672016-02-05 18:25:58 -08001318 for (int i = 1; i < argc; ++i) {
1319 if (strstr(argv[i], "retry_count") == nullptr) {
Tianjie Xubd56f152016-09-28 16:32:10 -07001320 options.push_back(argv[i]);
Tianjie Xu3c62b672016-02-05 18:25:58 -08001321 }
1322 }
1323
Tianjie Xubd56f152016-09-28 16:32:10 -07001324 // Increment the retry counter by 1.
1325 options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count+1));
Yabin Cui8b309f62016-06-24 18:22:02 -07001326 std::string err;
Tao Bao2292db82016-12-13 21:53:31 -08001327 if (!update_bootloader_message(options, &err)) {
Yabin Cui8b309f62016-06-24 18:22:02 -07001328 LOG(ERROR) << err;
1329 }
Tianjie Xu3c62b672016-02-05 18:25:58 -08001330}
1331
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001332static bool bootreason_in_blacklist() {
1333 char bootreason[PROPERTY_VALUE_MAX];
1334 if (property_get("ro.boot.bootreason", bootreason, nullptr) > 0) {
1335 for (const auto& str : bootreason_blacklist) {
1336 if (strcasecmp(str.c_str(), bootreason) == 0) {
1337 return true;
1338 }
1339 }
1340 }
1341 return false;
1342}
1343
1344static void log_failure_code(ErrorCode code, const char *update_package) {
1345 std::vector<std::string> log_buffer = {
1346 update_package,
1347 "0", // install result
1348 "error: " + std::to_string(code),
1349 };
1350 std::string log_content = android::base::Join(log_buffer, "\n");
1351 if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) {
Tao Baoac9d94d2016-11-03 11:37:15 -07001352 PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001353 }
1354
1355 // Also write the info into last_log.
1356 LOG(INFO) << log_content;
1357}
1358
Yabin Cui99281df2016-02-17 12:21:52 -08001359int main(int argc, char **argv) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001360 // We don't have logcat yet under recovery; so we'll print error on screen and
1361 // log to stdout (which is redirected to recovery.log) as we used to do.
1362 android::base::InitLogging(argv, &UiLogger);
1363
Mark Salyzyna4f701a2016-03-09 14:58:16 -08001364 // Take last pmsg contents and rewrite it to the current pmsg session.
1365 static const char filter[] = "recovery/";
1366 // Do we need to rotate?
1367 bool doRotate = false;
Tianjie Xue113e4d2016-10-21 17:46:13 -07001368
Mark Salyzyna4f701a2016-03-09 14:58:16 -08001369 __android_log_pmsg_file_read(
1370 LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
1371 logbasename, &doRotate);
1372 // Take action to refresh pmsg contents
1373 __android_log_pmsg_file_read(
1374 LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
1375 logrotate, &doRotate);
1376
Doug Zongker9270a202012-01-09 15:16:13 -08001377 // If this binary is started with the single argument "--adbd",
1378 // instead of being the normal recovery binary, it turns into kind
1379 // of a stripped-down version of adbd that only supports the
1380 // 'sideload' command. Note this must be a real argument, not
1381 // anything in the command file or bootloader control block; the
1382 // only way recovery should be run with this argument is when it
1383 // starts a copy of itself from the apply_from_adb() function.
1384 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
Josh Gaoacb2a2f2016-08-26 18:24:34 -07001385 minadbd_main();
Doug Zongker9270a202012-01-09 15:16:13 -08001386 return 0;
1387 }
1388
Tao Bao04ca4262015-09-10 15:32:24 -07001389 time_t start = time(NULL);
1390
1391 // redirect_stdio should be called only in non-sideload mode. Otherwise
1392 // we may have two logger instances with different timestamps.
1393 redirect_stdio(TEMPORARY_LOG_FILE);
1394
Doug Zongker19a8e242014-01-21 09:25:41 -08001395 printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001396
Doug Zongkerd4208f92010-09-20 12:16:13 -07001397 load_volume_table();
Tao Bao26112e52016-02-25 12:29:40 -08001398 has_cache = volume_for_path(CACHE_ROOT) != nullptr;
1399
Tao Baof0ed1592016-12-02 11:32:19 -08001400 std::vector<std::string> args = get_args(argc, argv);
1401 std::vector<char*> args_to_parse(args.size());
1402 std::transform(args.cbegin(), args.cend(), args_to_parse.begin(),
1403 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001404
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001405 const char *update_package = NULL;
Tao Baoe39a9bc2015-03-31 12:19:05 -07001406 bool should_wipe_data = false;
Tao Baoc679f932015-03-30 09:43:49 -07001407 bool should_wipe_cache = false;
Yabin Cui8b309f62016-06-24 18:22:02 -07001408 bool should_wipe_ab = false;
Yabin Cuifd99a312016-06-09 14:09:39 -07001409 size_t wipe_package_size = 0;
Tao Bao145d8612015-03-25 15:51:15 -07001410 bool show_text = false;
Tao Baoc679f932015-03-30 09:43:49 -07001411 bool sideload = false;
1412 bool sideload_auto_reboot = false;
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001413 bool just_exit = false;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001414 bool shutdown_after = false;
Tianjie Xu3c62b672016-02-05 18:25:58 -08001415 int retry_count = 0;
Tianjie Xu35926c42016-04-28 18:06:26 -07001416 bool security_update = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001417
1418 int arg;
Tao Bao862a4c12016-06-02 11:16:50 -07001419 int option_index;
Tao Baof0ed1592016-12-02 11:32:19 -08001420 while ((arg = getopt_long(args_to_parse.size(), args_to_parse.data(), "", OPTIONS,
1421 &option_index)) != -1) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001422 switch (arg) {
Tianjie Xu3c62b672016-02-05 18:25:58 -08001423 case 'n': android::base::ParseInt(optarg, &retry_count, 0); break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001424 case 'u': update_package = optarg; break;
Tao Baoe39a9bc2015-03-31 12:19:05 -07001425 case 'w': should_wipe_data = true; break;
Tao Baoc679f932015-03-30 09:43:49 -07001426 case 'c': should_wipe_cache = true; break;
Tao Bao145d8612015-03-25 15:51:15 -07001427 case 't': show_text = true; break;
Tao Baoc679f932015-03-30 09:43:49 -07001428 case 's': sideload = true; break;
1429 case 'a': sideload = true; sideload_auto_reboot = true; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001430 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -07001431 case 'l': locale = optarg; break;
Doug Zongkerc87bab12013-11-25 13:53:25 -08001432 case 'g': {
1433 if (stage == NULL || *stage == '\0') {
1434 char buffer[20] = "1/";
1435 strncat(buffer, optarg, sizeof(buffer)-3);
1436 stage = strdup(buffer);
1437 }
1438 break;
1439 }
Doug Zongkerb1d12632014-03-18 10:32:12 -07001440 case 'p': shutdown_after = true; break;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -07001441 case 'r': reason = optarg; break;
Tianjie Xu35926c42016-04-28 18:06:26 -07001442 case 'e': security_update = true; break;
Tao Bao862a4c12016-06-02 11:16:50 -07001443 case 0: {
Yabin Cui8b309f62016-06-24 18:22:02 -07001444 if (strcmp(OPTIONS[option_index].name, "wipe_ab") == 0) {
1445 should_wipe_ab = true;
Tao Bao862a4c12016-06-02 11:16:50 -07001446 break;
Yabin Cuifd99a312016-06-09 14:09:39 -07001447 } else if (strcmp(OPTIONS[option_index].name, "wipe_package_size") == 0) {
1448 android::base::ParseUint(optarg, &wipe_package_size);
1449 break;
Tao Bao862a4c12016-06-02 11:16:50 -07001450 }
1451 break;
1452 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001453 case '?':
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001454 LOG(ERROR) << "Invalid command argument";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001455 continue;
1456 }
1457 }
1458
Tao Baoac9d94d2016-11-03 11:37:15 -07001459 if (locale.empty()) {
1460 if (has_cache) {
1461 locale = load_locale_from_cache();
1462 }
1463
1464 if (locale.empty()) {
1465 locale = DEFAULT_LOCALE;
1466 }
Doug Zongker02ec6b82012-08-22 17:26:40 -07001467 }
Tao Baoac9d94d2016-11-03 11:37:15 -07001468
1469 printf("locale is [%s]\n", locale.c_str());
Doug Zongker0d32f252014-02-13 15:07:56 -08001470 printf("stage is [%s]\n", stage);
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -07001471 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001472
1473 Device* device = make_device();
1474 ui = device->GetUI();
1475
Sen Jiangd5304492016-12-09 16:20:49 -08001476 if (!ui->Init()) {
1477 printf("Failed to initialize UI, use stub UI instead.");
1478 ui = new StubRecoveryUI();
1479 }
Tao Baoac9d94d2016-11-03 11:37:15 -07001480 ui->SetLocale(locale.c_str());
Tianjie Xu35926c42016-04-28 18:06:26 -07001481 // Set background string to "installing security update" for security update,
1482 // otherwise set it to "installing system update".
1483 ui->SetSystemUpdateText(security_update);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001484
1485 int st_cur, st_max;
1486 if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
1487 ui->SetStage(st_cur, st_max);
1488 }
1489
Doug Zongker02ec6b82012-08-22 17:26:40 -07001490 ui->SetBackground(RecoveryUI::NONE);
1491 if (show_text) ui->ShowText(true);
1492
Stephen Smalley779701d2012-02-09 14:13:23 -05001493 struct selinux_opt seopts[] = {
Tao Baoac9d94d2016-11-03 11:37:15 -07001494 { SELABEL_OPT_PATH, "/file_contexts" }
Stephen Smalley779701d2012-02-09 14:13:23 -05001495 };
1496
1497 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
1498
1499 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001500 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -05001501 }
Stephen Smalley779701d2012-02-09 14:13:23 -05001502
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001503 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -08001504
Doug Zongker56c51052010-07-01 09:18:44 -07001505 printf("Command:");
Tao Baof0ed1592016-12-02 11:32:19 -08001506 for (const auto& arg : args) {
1507 printf(" \"%s\"", arg.c_str());
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001508 }
Tao Baof038ca02016-12-10 09:24:50 -08001509 printf("\n\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001510
1511 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -07001512 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001513
Elliott Hughesbb78d622015-04-09 20:51:08 -07001514 ui->Print("Supported API: %d\n", RECOVERY_API_VERSION);
1515
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001516 int status = INSTALL_SUCCESS;
1517
Doug Zongker540d57f2011-01-18 13:36:58 -08001518 if (update_package != NULL) {
Tao Bao56870012016-04-29 15:37:05 -07001519 // It's not entirely true that we will modify the flash. But we want
1520 // to log the update attempt since update_package is non-NULL.
1521 modified_flash = true;
1522
Yabin Cui99281df2016-02-17 12:21:52 -08001523 if (!is_battery_ok()) {
1524 ui->Print("battery capacity is not enough for installing package, needed is %d%%\n",
1525 BATTERY_OK_PERCENTAGE);
Tianjie Xu16255832016-04-30 11:49:59 -07001526 // Log the error code to last_install when installation skips due to
1527 // low battery.
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001528 log_failure_code(kLowBattery, update_package);
1529 status = INSTALL_SKIPPED;
1530 } else if (bootreason_in_blacklist()) {
1531 // Skip update-on-reboot when bootreason is kernel_panic or similar
1532 ui->Print("bootreason is in the blacklist; skip OTA installation\n");
1533 log_failure_code(kBootreasonInBlacklist, update_package);
Yabin Cui99281df2016-02-17 12:21:52 -08001534 status = INSTALL_SKIPPED;
1535 } else {
1536 status = install_package(update_package, &should_wipe_cache,
Tianjie Xu16255832016-04-30 11:49:59 -07001537 TEMPORARY_INSTALL_FILE, true, retry_count);
Yabin Cui99281df2016-02-17 12:21:52 -08001538 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1539 wipe_cache(false, device);
1540 }
1541 if (status != INSTALL_SUCCESS) {
1542 ui->Print("Installation aborted.\n");
Tianjie Xu3c62b672016-02-05 18:25:58 -08001543 // When I/O error happens, reboot and retry installation EIO_RETRY_COUNT
1544 // times before we abandon this OTA update.
1545 if (status == INSTALL_RETRY && retry_count < EIO_RETRY_COUNT) {
1546 copy_logs();
1547 set_retry_bootloader_message(retry_count, argc, argv);
1548 // Print retry count on screen.
1549 ui->Print("Retry attempt %d\n", retry_count);
Doug Zongker7c3ae452013-05-14 11:03:02 -07001550
Tianjie Xu3c62b672016-02-05 18:25:58 -08001551 // Reboot and retry the update
Elliott Hughescb220402016-09-23 15:30:55 -07001552 if (!android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,recovery")) {
Tianjie Xu3c62b672016-02-05 18:25:58 -08001553 ui->Print("Reboot failed\n");
1554 } else {
1555 while (true) {
1556 pause();
1557 }
1558 }
1559 }
Yabin Cui99281df2016-02-17 12:21:52 -08001560 // If this is an eng or userdebug build, then automatically
1561 // turn the text display on if the script fails so the error
1562 // message is visible.
1563 if (is_ro_debuggable()) {
1564 ui->ShowText(true);
1565 }
Doug Zongker7c3ae452013-05-14 11:03:02 -07001566 }
1567 }
Tao Baoe39a9bc2015-03-31 12:19:05 -07001568 } else if (should_wipe_data) {
1569 if (!wipe_data(false, device)) {
1570 status = INSTALL_ERROR;
1571 }
Tao Baoc679f932015-03-30 09:43:49 -07001572 } else if (should_wipe_cache) {
Tao Baoe39a9bc2015-03-31 12:19:05 -07001573 if (!wipe_cache(false, device)) {
1574 status = INSTALL_ERROR;
1575 }
Yabin Cui8b309f62016-06-24 18:22:02 -07001576 } else if (should_wipe_ab) {
Yabin Cuifd99a312016-06-09 14:09:39 -07001577 if (!wipe_ab_device(wipe_package_size)) {
Tao Bao862a4c12016-06-02 11:16:50 -07001578 status = INSTALL_ERROR;
1579 }
Tao Baoc679f932015-03-30 09:43:49 -07001580 } else if (sideload) {
1581 // 'adb reboot sideload' acts the same as user presses key combinations
1582 // to enter the sideload mode. When 'sideload-auto-reboot' is used, text
1583 // display will NOT be turned on by default. And it will reboot after
1584 // sideload finishes even if there are errors. Unless one turns on the
1585 // text display during the installation. This is to enable automated
1586 // testing.
1587 if (!sideload_auto_reboot) {
1588 ui->ShowText(true);
1589 }
1590 status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE);
1591 if (status == INSTALL_SUCCESS && should_wipe_cache) {
Tao Baoe39a9bc2015-03-31 12:19:05 -07001592 if (!wipe_cache(false, device)) {
1593 status = INSTALL_ERROR;
1594 }
Tao Baoc679f932015-03-30 09:43:49 -07001595 }
1596 ui->Print("\nInstall from ADB complete (status: %d).\n", status);
1597 if (sideload_auto_reboot) {
1598 ui->Print("Rebooting automatically.\n");
1599 }
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001600 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001601 status = INSTALL_NONE; // No command specified
1602 ui->SetBackground(RecoveryUI::NO_COMMAND);
Tao Bao785d22c2015-05-04 09:34:29 -07001603
1604 // http://b/17489952
1605 // If this is an eng or userdebug build, automatically turn on the
1606 // text display if no command is specified.
1607 if (is_ro_debuggable()) {
1608 ui->ShowText(true);
1609 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001610 }
1611
Tao Baoc679f932015-03-30 09:43:49 -07001612 if (!sideload_auto_reboot && (status == INSTALL_ERROR || status == INSTALL_CORRUPT)) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001613 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001614 ui->SetBackground(RecoveryUI::ERROR);
1615 }
Tao Baoc679f932015-03-30 09:43:49 -07001616
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001617 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
Yabin Cui99281df2016-02-17 12:21:52 -08001618 if ((status != INSTALL_SUCCESS && status != INSTALL_SKIPPED && !sideload_auto_reboot) ||
1619 ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001620 Device::BuiltinAction temp = prompt_and_wait(device, status);
Tao Baoc679f932015-03-30 09:43:49 -07001621 if (temp != Device::NO_ACTION) {
1622 after = temp;
1623 }
Doug Zongker8674a722010-09-15 11:08:23 -07001624 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001625
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001626 // Save logs and clean up before rebooting or shutting down.
Tianjie Xuc14d95d2016-03-24 11:50:34 -07001627 finish_recovery();
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001628
1629 switch (after) {
1630 case Device::SHUTDOWN:
1631 ui->Print("Shutting down...\n");
Elliott Hughescb220402016-09-23 15:30:55 -07001632 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,");
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001633 break;
1634
1635 case Device::REBOOT_BOOTLOADER:
1636 ui->Print("Rebooting to bootloader...\n");
Elliott Hughescb220402016-09-23 15:30:55 -07001637 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001638 break;
1639
1640 default:
1641 ui->Print("Rebooting...\n");
Elliott Hughescb220402016-09-23 15:30:55 -07001642 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,");
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001643 break;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001644 }
Tao Bao75238632015-05-27 14:46:17 -07001645 while (true) {
Tao Baoac9d94d2016-11-03 11:37:15 -07001646 pause();
Tao Bao75238632015-05-27 14:46:17 -07001647 }
1648 // Should be unreachable.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001649 return EXIT_SUCCESS;
1650}