blob: 247cbf9686258cac15155209896e65f0bc8a344a [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
Jerry Zhang6a648042018-05-04 11:24:10 -070017#include "recovery.h"
Tao Bao2ac56af2018-04-25 16:47:04 -070018
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <ctype.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070020#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <errno.h>
22#include <fcntl.h>
23#include <getopt.h>
Tao Bao862a4c12016-06-02 11:16:50 -070024#include <inttypes.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <limits.h>
Tao Bao862a4c12016-06-02 11:16:50 -070026#include <linux/fs.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <linux/input.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070028#include <stdarg.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070032#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include <sys/types.h>
Tao Baocdcf28f2016-01-13 15:05:20 -080034#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include <unistd.h>
36
Tao Baoc4a18ef2017-02-10 00:13:30 -080037#include <algorithm>
Tao Bao3aec6962018-04-20 09:24:58 -070038#include <functional>
Tao Baoc4a18ef2017-02-10 00:13:30 -080039#include <memory>
Tao Bao862a4c12016-06-02 11:16:50 -070040#include <string>
41#include <vector>
Tao Bao04ca4262015-09-10 15:32:24 -070042
Elliott Hughes4b166f02015-12-04 15:30:20 -080043#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070044#include <android-base/logging.h>
Tianjie Xu3c62b672016-02-05 18:25:58 -080045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Bao862a4c12016-06-02 11:16:50 -070048#include <android-base/strings.h>
49#include <android-base/unique_fd.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070050#include <bootloader_message/bootloader_message.h>
Elliott Hughescb220402016-09-23 15:30:55 -070051#include <cutils/properties.h> /* for property_list */
Yifan Hong056538c2018-07-11 17:04:12 -070052#include <healthhalutils/HealthHalUtils.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070053#include <ziparchive/zip_archive.h>
Yabin Cui99281df2016-02-17 12:21:52 -080054
Tao Bao75238632015-05-27 14:46:17 -070055#include "adb_install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056#include "common.h"
Tao Bao75238632015-05-27 14:46:17 -070057#include "device.h"
David Andersonedee8362018-05-16 13:43:22 -070058#include "fsck_unshare_blocks.h"
Tao Bao75238632015-05-27 14:46:17 -070059#include "fuse_sdcard_provider.h"
60#include "fuse_sideload.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061#include "install.h"
Jerry Zhangcadf4ed2018-05-02 16:56:00 -070062#include "logging.h"
Tao Bao17054c02018-05-03 22:41:23 -070063#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070064#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070065#include "otautil/paths.h"
Tao Bao2c526392018-05-03 23:01:13 -070066#include "otautil/sysutil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067#include "roots.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070068#include "screen_ui.h"
Tianjie Xue113e4d2016-10-21 17:46:13 -070069#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070
Tao Baoaac9d9f2018-04-29 23:38:59 -070071static constexpr const char* CACHE_LOG_DIR = "/cache/recovery";
72static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
Tao Baoaac9d9f2018-04-29 23:38:59 -070073static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
74static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log";
75static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
Tianjie Xu06e57ac2016-07-11 14:04:08 -070076
Tao Baoaac9d9f2018-04-29 23:38:59 -070077static constexpr const char* CACHE_ROOT = "/cache";
78static constexpr const char* DATA_ROOT = "/data";
79static constexpr const char* METADATA_ROOT = "/metadata";
80static constexpr const char* SDCARD_ROOT = "/sdcard";
Nick Kralevicha9ad0322014-10-22 18:38:48 -070081
Tao Baobd0ddcd2017-05-04 13:03:18 -070082// We define RECOVERY_API_VERSION in Android.mk, which will be picked up by build system and packed
83// into target_files.zip. Assert the version defined in code and in Android.mk are consistent.
84static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions.");
85
Tao Baoac9d94d2016-11-03 11:37:15 -070086bool modified_flash = false;
Tao Baoa8d72bc2016-12-25 18:46:50 -080087std::string stage;
Tao Baoac9d94d2016-11-03 11:37:15 -070088const char* reason = nullptr;
Tao Baoac9d94d2016-11-03 11:37:15 -070089
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090/*
91 * The recovery tool communicates with the main system through /cache files.
92 * /cache/recovery/command - INPUT - command line for tool, one arg per line
93 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094 *
95 * The arguments which may be supplied in the recovery.command file:
Doug Zongkerd4208f92010-09-20 12:16:13 -070096 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 * --wipe_data - erase user data (and cache), then reboot
Tao Baof9f17342018-04-27 10:44:04 -070098 * --prompt_and_wipe_data - prompt the user that data is corrupt, with their consent erase user
99 * data (and cache), then reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 * --wipe_cache - wipe cache (but not user data), then reboot
Tao Baof9f17342018-04-27 10:44:04 -0700101 * --show_text - show the recovery text menu, used by some bootloader (e.g. http://b/36872519).
Oscar Montemayor05231562009-11-30 08:40:57 -0800102 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700103 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 *
105 * After completing, we remove /cache/recovery/command and reboot.
106 * Arguments may also be supplied in the bootloader control block (BCB).
107 * These important scenarios must be safely restartable at any point:
108 *
109 * FACTORY RESET
110 * 1. user selects "factory reset"
111 * 2. main system writes "--wipe_data" to /cache/recovery/command
112 * 3. main system reboots into recovery
113 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
114 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700115 * 5. erase_volume() reformats /data
116 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 * 7. finish_recovery() erases BCB
118 * -- after this, rebooting will restart the main system --
119 * 8. main() calls reboot() to boot main system
120 *
121 * OTA INSTALL
122 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700123 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124 * 3. main system reboots into recovery
125 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
126 * -- after this, rebooting will attempt to reinstall the update --
127 * 5. install_package() attempts to install the update
128 * NOTE: the package install must itself be restartable from any point
129 * 6. finish_recovery() erases BCB
130 * -- after this, rebooting will (try to) restart the main system --
131 * 7. ** if install failed **
132 * 7a. prompt_and_wait() shows an error icon and waits for the user
Tao Baoc0336392016-12-13 22:29:49 -0800133 * 7b. the user reboots (pulling the battery, etc) into the main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134 */
135
Elliott Hughesf14af802015-02-10 14:46:14 -0800136bool is_ro_debuggable() {
Elliott Hughescb220402016-09-23 15:30:55 -0700137 return android::base::GetBoolProperty("ro.debuggable", false);
Elliott Hughesf14af802015-02-10 14:46:14 -0800138}
139
Tao Bao2292db82016-12-13 21:53:31 -0800140// Set the BCB to reboot back into recovery (it won't resume the install from
141// sdcard though).
142static void set_sdcard_update_bootloader_message() {
143 std::vector<std::string> options;
144 std::string err;
145 if (!update_bootloader_message(options, &err)) {
146 LOG(ERROR) << "Failed to set BCB message: " << err;
147 }
Doug Zongker34c98df2009-08-18 12:05:45 -0700148}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149
Tao Baoec579032017-07-21 12:13:15 -0700150// Clear the recovery command and prepare to boot a (hopefully working) system,
Tianjie Xuc14d95d2016-03-24 11:50:34 -0700151// copy our log file to cache as well (for the system to read). This function is
152// idempotent: call it as many times as you like.
Tao Bao551d2c32018-05-09 20:53:13 -0700153static void finish_recovery() {
154 std::string locale = ui->GetLocale();
Tao Baoec579032017-07-21 12:13:15 -0700155 // Save the locale to cache, so if recovery is next started up without a '--locale' argument
156 // (e.g., directly from the bootloader) it will use the last-known locale.
157 if (!locale.empty() && has_cache) {
158 LOG(INFO) << "Saving locale \"" << locale << "\"";
159 if (ensure_path_mounted(LOCALE_FILE) != 0) {
160 LOG(ERROR) << "Failed to mount " << LOCALE_FILE;
161 } else if (!android::base::WriteStringToFile(locale, LOCALE_FILE)) {
162 PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
Doug Zongker4f33e552012-08-23 13:16:12 -0700163 }
Tao Baoec579032017-07-21 12:13:15 -0700164 }
Doug Zongker4f33e552012-08-23 13:16:12 -0700165
Jerry Zhangcadf4ed2018-05-02 16:56:00 -0700166 copy_logs(modified_flash, has_cache);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800167
Tao Baoec579032017-07-21 12:13:15 -0700168 // Reset to normal system boot so recovery won't cycle indefinitely.
169 std::string err;
170 if (!clear_bootloader_message(&err)) {
171 LOG(ERROR) << "Failed to clear BCB message: " << err;
172 }
173
174 // Remove the command file, so recovery won't repeat indefinitely.
175 if (has_cache) {
176 if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) {
177 LOG(WARNING) << "Can't unlink " << COMMAND_FILE;
Yabin Cui8b309f62016-06-24 18:22:02 -0700178 }
Tao Baoec579032017-07-21 12:13:15 -0700179 ensure_path_unmounted(CACHE_ROOT);
180 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800181
Tao Baoec579032017-07-21 12:13:15 -0700182 sync(); // For good measure.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800183}
184
Tao Bao3f5a3822016-12-13 11:14:37 -0800185struct saved_log_file {
186 std::string name;
187 struct stat sb;
188 std::string data;
189};
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700190
Elliott Hughes945548e2015-06-05 17:59:56 -0700191static bool erase_volume(const char* volume) {
Tao Bao3f5a3822016-12-13 11:14:37 -0800192 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
193 bool is_data = (strcmp(volume, DATA_ROOT) == 0);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700194
Tao Bao3f5a3822016-12-13 11:14:37 -0800195 ui->SetBackground(RecoveryUI::ERASING);
196 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700197
Tao Bao3f5a3822016-12-13 11:14:37 -0800198 std::vector<saved_log_file> log_files;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700199
Tao Bao3f5a3822016-12-13 11:14:37 -0800200 if (is_cache) {
201 // If we're reformatting /cache, we load any past logs
202 // (i.e. "/cache/recovery/last_*") and the current log
203 // ("/cache/recovery/log") into memory, so we can restore them after
204 // the reformat.
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700205
Tao Bao3f5a3822016-12-13 11:14:37 -0800206 ensure_path_mounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700207
Tao Bao3f5a3822016-12-13 11:14:37 -0800208 struct dirent* de;
209 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(CACHE_LOG_DIR), closedir);
210 if (d) {
211 while ((de = readdir(d.get())) != nullptr) {
212 if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) {
213 std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name);
214
215 struct stat sb;
216 if (stat(path.c_str(), &sb) == 0) {
217 // truncate files to 512kb
218 if (sb.st_size > (1 << 19)) {
219 sb.st_size = 1 << 19;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700220 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800221
222 std::string data(sb.st_size, '\0');
Tianjie Xude6735e2017-07-10 15:13:33 -0700223 FILE* f = fopen(path.c_str(), "rbe");
Tao Bao3f5a3822016-12-13 11:14:37 -0800224 fread(&data[0], 1, data.size(), f);
225 fclose(f);
226
227 log_files.emplace_back(saved_log_file{ path, sb, data });
228 }
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700229 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800230 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800231 } else {
Tao Bao3f5a3822016-12-13 11:14:37 -0800232 if (errno != ENOENT) {
233 PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR;
234 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800235 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800236 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700237
Tao Bao3f5a3822016-12-13 11:14:37 -0800238 ui->Print("Formatting %s...\n", volume);
239
240 ensure_path_unmounted(volume);
241
242 int result;
Tao Bao3f5a3822016-12-13 11:14:37 -0800243 if (is_data && reason && strcmp(reason, "convert_fbe") == 0) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700244 static constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe";
245 static constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
246 // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not
247 // full disk encryption.
Tao Bao3f5a3822016-12-13 11:14:37 -0800248 if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700249 PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR;
250 return false;
Tao Bao3f5a3822016-12-13 11:14:37 -0800251 }
Tianjie Xude6735e2017-07-10 15:13:33 -0700252 FILE* f = fopen(CONVERT_FBE_FILE, "wbe");
Tao Bao3f5a3822016-12-13 11:14:37 -0800253 if (!f) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700254 PLOG(ERROR) << "Failed to convert to file encryption";
255 return false;
Tao Bao3f5a3822016-12-13 11:14:37 -0800256 }
257 fclose(f);
258 result = format_volume(volume, CONVERT_FBE_DIR);
259 remove(CONVERT_FBE_FILE);
260 rmdir(CONVERT_FBE_DIR);
261 } else {
262 result = format_volume(volume);
263 }
264
265 if (is_cache) {
266 // Re-create the log dir and write back the log entries.
267 if (ensure_path_mounted(CACHE_LOG_DIR) == 0 &&
Tao Baoac3d1ed2017-07-23 00:01:02 -0700268 mkdir_recursively(CACHE_LOG_DIR, 0777, false, sehandle) == 0) {
Tao Bao3f5a3822016-12-13 11:14:37 -0800269 for (const auto& log : log_files) {
270 if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid,
271 log.sb.st_gid)) {
272 PLOG(ERROR) << "Failed to write to " << log.name;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700273 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800274 }
275 } else {
276 PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700277 }
278
Tao Bao3f5a3822016-12-13 11:14:37 -0800279 // Any part of the log we'd copied to cache is now gone.
280 // Reset the pointer so we copy from the beginning of the temp
281 // log.
Jerry Zhangcadf4ed2018-05-02 16:56:00 -0700282 reset_tmplog_offset();
283 copy_logs(modified_flash, has_cache);
Tao Bao3f5a3822016-12-13 11:14:37 -0800284 }
285
286 return (result == 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287}
288
Tao Baoc4a18ef2017-02-10 00:13:30 -0800289// Returns the selected filename, or an empty string.
290static std::string browse_directory(const std::string& path, Device* device) {
291 ensure_path_mounted(path.c_str());
Doug Zongker8674a722010-09-15 11:08:23 -0700292
Tao Baoc4a18ef2017-02-10 00:13:30 -0800293 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir);
294 if (!d) {
295 PLOG(ERROR) << "error opening " << path;
296 return "";
297 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700298
Tao Baoc4a18ef2017-02-10 00:13:30 -0800299 std::vector<std::string> dirs;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700300 std::vector<std::string> entries{ "../" }; // "../" is always the first entry.
Tao Baoc4a18ef2017-02-10 00:13:30 -0800301
302 dirent* de;
303 while ((de = readdir(d.get())) != nullptr) {
304 std::string name(de->d_name);
305
306 if (de->d_type == DT_DIR) {
307 // Skip "." and ".." entries.
308 if (name == "." || name == "..") continue;
309 dirs.push_back(name + "/");
310 } else if (de->d_type == DT_REG && android::base::EndsWithIgnoreCase(name, ".zip")) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700311 entries.push_back(name);
Tao Baoc4a18ef2017-02-10 00:13:30 -0800312 }
313 }
314
315 std::sort(dirs.begin(), dirs.end());
Tao Bao1fe1afe2018-05-01 15:56:05 -0700316 std::sort(entries.begin(), entries.end());
Tao Baoc4a18ef2017-02-10 00:13:30 -0800317
Tao Bao1fe1afe2018-05-01 15:56:05 -0700318 // Append dirs to the entries list.
319 entries.insert(entries.end(), dirs.begin(), dirs.end());
Tao Baoc4a18ef2017-02-10 00:13:30 -0800320
Tao Bao1fe1afe2018-05-01 15:56:05 -0700321 std::vector<std::string> headers{ "Choose a package to install:", path };
Tao Baoc4a18ef2017-02-10 00:13:30 -0800322
Tao Bao1fe1afe2018-05-01 15:56:05 -0700323 size_t chosen_item = 0;
Tao Baoc4a18ef2017-02-10 00:13:30 -0800324 while (true) {
Tao Bao3aec6962018-04-20 09:24:58 -0700325 chosen_item = ui->ShowMenu(
326 headers, entries, chosen_item, true,
327 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Tao Baoc4a18ef2017-02-10 00:13:30 -0800328
Jerry Zhangb76af932018-05-22 12:08:35 -0700329 // Return if WaitKey() was interrupted.
330 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
331 return "";
332 }
333
Tao Bao1fe1afe2018-05-01 15:56:05 -0700334 const std::string& item = entries[chosen_item];
Tao Baoc4a18ef2017-02-10 00:13:30 -0800335 if (chosen_item == 0) {
336 // Go up but continue browsing (if the caller is browse_directory).
337 return "";
Doug Zongker8674a722010-09-15 11:08:23 -0700338 }
339
Tao Baoc4a18ef2017-02-10 00:13:30 -0800340 std::string new_path = path + "/" + item;
341 if (new_path.back() == '/') {
342 // Recurse down into a subdirectory.
343 new_path.pop_back();
344 std::string result = browse_directory(new_path, device);
345 if (!result.empty()) return result;
346 } else {
347 // Selected a zip file: return the path to the caller.
348 return new_path;
Doug Zongker8674a722010-09-15 11:08:23 -0700349 }
Tao Baoc4a18ef2017-02-10 00:13:30 -0800350 }
Doug Zongker8674a722010-09-15 11:08:23 -0700351
Tao Baoc4a18ef2017-02-10 00:13:30 -0800352 // Unreachable.
Doug Zongker8674a722010-09-15 11:08:23 -0700353}
354
Elliott Hughes30694c92015-03-25 15:16:51 -0700355static bool yes_no(Device* device, const char* question1, const char* question2) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700356 std::vector<std::string> headers{ question1, question2 };
357 std::vector<std::string> items{ " No", " Yes" };
Doug Zongkerddd6a282009-06-09 12:22:33 -0700358
Tao Bao1fe1afe2018-05-01 15:56:05 -0700359 size_t chosen_item = ui->ShowMenu(
Tao Bao3aec6962018-04-20 09:24:58 -0700360 headers, items, 0, true,
361 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
362 return (chosen_item == 1);
Elliott Hughes30694c92015-03-25 15:16:51 -0700363}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800364
Paul Crowley08404b42016-12-19 13:04:23 -0800365static bool ask_to_wipe_data(Device* device) {
Tao Bao3aec6962018-04-20 09:24:58 -0700366 return yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!");
Paul Crowley08404b42016-12-19 13:04:23 -0800367}
Doug Zongker1066d2c2009-04-01 13:57:40 -0700368
Paul Crowley08404b42016-12-19 13:04:23 -0800369// Return true on success.
370static bool wipe_data(Device* device) {
Tao Bao682c34b2015-04-07 17:16:35 -0700371 modified_flash = true;
372
Doug Zongker211aebc2011-10-28 15:13:10 -0700373 ui->Print("\n-- Wiping data...\n");
Paul Crowley3b4d5162016-06-08 13:51:41 -0700374 bool success = device->PreWipeData();
375 if (success) {
376 success &= erase_volume(DATA_ROOT);
377 if (has_cache) {
378 success &= erase_volume(CACHE_ROOT);
379 }
380 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
381 success &= erase_volume(METADATA_ROOT);
382 }
383 }
384 if (success) {
385 success &= device->PostWipeData();
386 }
Elliott Hughes945548e2015-06-05 17:59:56 -0700387 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
388 return success;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700389}
390
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700391static InstallResult prompt_and_wipe_data(Device* device) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700392 // Use a single string and let ScreenRecoveryUI handles the wrapping.
Tao Bao1fe1afe2018-05-01 15:56:05 -0700393 std::vector<std::string> headers{
Tao Bao2bbc6d62017-08-13 23:48:55 -0700394 "Can't load Android system. Your data may be corrupt. "
395 "If you continue to get this message, you may need to "
396 "perform a factory data reset and erase all user data "
Paul Crowley31ac0c62017-03-23 12:32:40 -0700397 "stored on this device.",
Tao Baofc5499f2017-02-23 19:06:53 -0800398 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700399 // clang-format off
400 std::vector<std::string> items {
Paul Crowley31ac0c62017-03-23 12:32:40 -0700401 "Try again",
402 "Factory data reset",
Tao Baofc5499f2017-02-23 19:06:53 -0800403 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700404 // clang-format on
Tao Baofc5499f2017-02-23 19:06:53 -0800405 for (;;) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700406 size_t chosen_item = ui->ShowMenu(
Tao Bao3aec6962018-04-20 09:24:58 -0700407 headers, items, 0, true,
408 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Jerry Zhangb76af932018-05-22 12:08:35 -0700409
410 // If ShowMenu() returned RecoveryUI::KeyError::INTERRUPTED, WaitKey() was interrupted.
411 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700412 return INSTALL_KEY_INTERRUPTED;
Jerry Zhangb76af932018-05-22 12:08:35 -0700413 }
Tao Baofc5499f2017-02-23 19:06:53 -0800414 if (chosen_item != 1) {
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700415 return INSTALL_SUCCESS; // Just reboot, no wipe; not a failure, user asked for it
Paul Crowley08404b42016-12-19 13:04:23 -0800416 }
Tao Baofc5499f2017-02-23 19:06:53 -0800417 if (ask_to_wipe_data(device)) {
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700418 if (wipe_data(device)) {
419 return INSTALL_SUCCESS;
420 } else {
421 return INSTALL_ERROR;
422 }
Tao Baofc5499f2017-02-23 19:06:53 -0800423 }
424 }
Paul Crowley08404b42016-12-19 13:04:23 -0800425}
426
Tao Baoe39a9bc2015-03-31 12:19:05 -0700427// Return true on success.
428static bool wipe_cache(bool should_confirm, Device* device) {
Tao Bao26112e52016-02-25 12:29:40 -0800429 if (!has_cache) {
430 ui->Print("No /cache partition found.\n");
431 return false;
432 }
433
Elliott Hughes30694c92015-03-25 15:16:51 -0700434 if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) {
Tao Baoe39a9bc2015-03-31 12:19:05 -0700435 return false;
Elliott Hughes30694c92015-03-25 15:16:51 -0700436 }
437
Tao Bao682c34b2015-04-07 17:16:35 -0700438 modified_flash = true;
439
Elliott Hughes30694c92015-03-25 15:16:51 -0700440 ui->Print("\n-- Wiping cache...\n");
Elliott Hughes945548e2015-06-05 17:59:56 -0700441 bool success = erase_volume("/cache");
442 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
443 return success;
Elliott Hughes30694c92015-03-25 15:16:51 -0700444}
445
Tao Bao1b2a98b2017-03-24 10:45:34 -0700446// Secure-wipe a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with
447// BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT.
Tao Bao862a4c12016-06-02 11:16:50 -0700448static bool secure_wipe_partition(const std::string& partition) {
Tao Bao1b2a98b2017-03-24 10:45:34 -0700449 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY)));
450 if (fd == -1) {
451 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
452 return false;
453 }
454
455 uint64_t range[2] = { 0, 0 };
456 if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) {
457 PLOG(ERROR) << "Failed to get partition size";
458 return false;
459 }
460 LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1];
461
462 LOG(INFO) << " Trying BLKSECDISCARD...";
463 if (ioctl(fd, BLKSECDISCARD, &range) == -1) {
464 PLOG(WARNING) << " Failed";
465
466 // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT.
467 unsigned int zeroes;
468 if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) {
469 LOG(INFO) << " Trying BLKDISCARD...";
470 if (ioctl(fd, BLKDISCARD, &range) == -1) {
471 PLOG(ERROR) << " Failed";
Tao Bao862a4c12016-06-02 11:16:50 -0700472 return false;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700473 }
474 } else {
475 LOG(INFO) << " Trying BLKZEROOUT...";
476 if (ioctl(fd, BLKZEROOUT, &range) == -1) {
477 PLOG(ERROR) << " Failed";
Tao Bao862a4c12016-06-02 11:16:50 -0700478 return false;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700479 }
Tao Bao862a4c12016-06-02 11:16:50 -0700480 }
Tao Bao1b2a98b2017-03-24 10:45:34 -0700481 }
Tao Bao862a4c12016-06-02 11:16:50 -0700482
Tao Bao1b2a98b2017-03-24 10:45:34 -0700483 LOG(INFO) << " Done";
484 return true;
Tao Bao862a4c12016-06-02 11:16:50 -0700485}
486
Yabin Cuifd99a312016-06-09 14:09:39 -0700487// Check if the wipe package matches expectation:
488// 1. verify the package.
489// 2. check metadata (ota-type, pre-device and serial number if having one).
490static bool check_wipe_package(size_t wipe_package_size) {
491 if (wipe_package_size == 0) {
492 LOG(ERROR) << "wipe_package_size is zero";
493 return false;
494 }
495 std::string wipe_package;
496 std::string err_str;
497 if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
498 PLOG(ERROR) << "Failed to read wipe package";
499 return false;
500 }
501 if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()),
502 wipe_package.size())) {
503 LOG(ERROR) << "Failed to verify package";
504 return false;
505 }
506
507 // Extract metadata
508 ZipArchiveHandle zip;
Tao Baoefc35592017-01-08 22:45:47 -0800509 int err = OpenArchiveFromMemory(static_cast<void*>(&wipe_package[0]), wipe_package.size(),
510 "wipe_package", &zip);
Yabin Cuifd99a312016-06-09 14:09:39 -0700511 if (err != 0) {
512 LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err);
513 return false;
514 }
515 std::string metadata;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700516 if (!read_metadata_from_package(zip, &metadata)) {
Yabin Cuifd99a312016-06-09 14:09:39 -0700517 CloseArchive(zip);
518 return false;
519 }
520 CloseArchive(zip);
521
522 // Check metadata
523 std::vector<std::string> lines = android::base::Split(metadata, "\n");
524 bool ota_type_matched = false;
525 bool device_type_matched = false;
526 bool has_serial_number = false;
527 bool serial_number_matched = false;
528 for (const auto& line : lines) {
529 if (line == "ota-type=BRICK") {
530 ota_type_matched = true;
531 } else if (android::base::StartsWith(line, "pre-device=")) {
532 std::string device_type = line.substr(strlen("pre-device="));
Tao Baoefc35592017-01-08 22:45:47 -0800533 std::string real_device_type = android::base::GetProperty("ro.build.product", "");
Yabin Cuifd99a312016-06-09 14:09:39 -0700534 device_type_matched = (device_type == real_device_type);
535 } else if (android::base::StartsWith(line, "serialno=")) {
536 std::string serial_no = line.substr(strlen("serialno="));
Tao Baoefc35592017-01-08 22:45:47 -0800537 std::string real_serial_no = android::base::GetProperty("ro.serialno", "");
Yabin Cuifd99a312016-06-09 14:09:39 -0700538 has_serial_number = true;
539 serial_number_matched = (serial_no == real_serial_no);
540 }
541 }
542 return ota_type_matched && device_type_matched && (!has_serial_number || serial_number_matched);
543}
544
Tao Baoaac9d9f2018-04-29 23:38:59 -0700545// Wipes the current A/B device, with a secure wipe of all the partitions in RECOVERY_WIPE.
Yabin Cuifd99a312016-06-09 14:09:39 -0700546static bool wipe_ab_device(size_t wipe_package_size) {
Tao Baoaac9d9f2018-04-29 23:38:59 -0700547 ui->SetBackground(RecoveryUI::ERASING);
548 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Tao Bao862a4c12016-06-02 11:16:50 -0700549
Tao Baoaac9d9f2018-04-29 23:38:59 -0700550 if (!check_wipe_package(wipe_package_size)) {
551 LOG(ERROR) << "Failed to verify wipe package";
552 return false;
553 }
554 static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe";
555 std::string partition_list;
556 if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) {
557 LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\"";
558 return false;
559 }
560
561 std::vector<std::string> lines = android::base::Split(partition_list, "\n");
562 for (const std::string& line : lines) {
563 std::string partition = android::base::Trim(line);
564 // Ignore '#' comment or empty lines.
565 if (android::base::StartsWith(partition, "#") || partition.empty()) {
566 continue;
Tao Bao862a4c12016-06-02 11:16:50 -0700567 }
568
Tao Baoaac9d9f2018-04-29 23:38:59 -0700569 // Proceed anyway even if it fails to wipe some partition.
570 secure_wipe_partition(partition);
571 }
572 return true;
Tao Bao862a4c12016-06-02 11:16:50 -0700573}
574
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700575static void choose_recovery_file(Device* device) {
Tao Bao08fc6be2017-03-07 00:56:27 -0800576 std::vector<std::string> entries;
577 if (has_cache) {
578 for (int i = 0; i < KEEP_LOG_COUNT; i++) {
579 auto add_to_entries = [&](const char* filename) {
580 std::string log_file(filename);
581 if (i > 0) {
582 log_file += "." + std::to_string(i);
Tao Baobef39712015-05-04 18:50:27 -0700583 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800584
585 if (ensure_path_mounted(log_file.c_str()) == 0 && access(log_file.c_str(), R_OK) == 0) {
586 entries.push_back(std::move(log_file));
587 }
588 };
589
590 // Add LAST_LOG_FILE + LAST_LOG_FILE.x
591 add_to_entries(LAST_LOG_FILE);
592
593 // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
594 add_to_entries(LAST_KMSG_FILE);
595 }
596 } else {
597 // If cache partition is not found, view /tmp/recovery.log instead.
Tao Bao641fa972018-04-25 18:59:40 -0700598 if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) {
Tao Bao08fc6be2017-03-07 00:56:27 -0800599 return;
Tianjie Xua54f75e2016-08-17 12:02:46 -0700600 } else {
Tao Bao641fa972018-04-25 18:59:40 -0700601 entries.push_back(Paths::Get().temporary_log_file());
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700602 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800603 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700604
Tao Bao08fc6be2017-03-07 00:56:27 -0800605 entries.push_back("Back");
Elliott Hughesc0491632015-05-06 12:40:05 -0700606
Tao Bao1fe1afe2018-05-01 15:56:05 -0700607 std::vector<std::string> headers{ "Select file to view" };
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700608
Tao Bao1fe1afe2018-05-01 15:56:05 -0700609 size_t chosen_item = 0;
Tao Bao08fc6be2017-03-07 00:56:27 -0800610 while (true) {
Tao Bao3aec6962018-04-20 09:24:58 -0700611 chosen_item = ui->ShowMenu(
Tao Bao1fe1afe2018-05-01 15:56:05 -0700612 headers, entries, chosen_item, true,
Tao Bao3aec6962018-04-20 09:24:58 -0700613 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Jerry Zhangb76af932018-05-22 12:08:35 -0700614
615 // Handle WaitKey() interrupt.
616 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
617 break;
618 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800619 if (entries[chosen_item] == "Back") break;
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700620
Tao Bao1d156b92018-05-02 12:43:18 -0700621 ui->ShowFile(entries[chosen_item]);
Tao Bao08fc6be2017-03-07 00:56:27 -0800622 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700623}
624
Tao Baodb7e8982017-03-06 23:53:16 -0800625static void run_graphics_test() {
626 // Switch to graphics screen.
627 ui->ShowText(false);
Elliott Hughes498cda62016-04-14 16:49:04 -0700628
Tao Baodb7e8982017-03-06 23:53:16 -0800629 ui->SetProgressType(RecoveryUI::INDETERMINATE);
630 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
631 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700632
Tao Baodb7e8982017-03-06 23:53:16 -0800633 ui->SetBackground(RecoveryUI::ERROR);
634 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700635
Tao Baodb7e8982017-03-06 23:53:16 -0800636 ui->SetBackground(RecoveryUI::NO_COMMAND);
637 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700638
Tao Baodb7e8982017-03-06 23:53:16 -0800639 ui->SetBackground(RecoveryUI::ERASING);
640 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700641
Tao Baodb7e8982017-03-06 23:53:16 -0800642 // Calling SetBackground() after SetStage() to trigger a redraw.
643 ui->SetStage(1, 3);
644 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
645 sleep(1);
646 ui->SetStage(2, 3);
647 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
648 sleep(1);
649 ui->SetStage(3, 3);
650 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
651 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700652
Tao Baodb7e8982017-03-06 23:53:16 -0800653 ui->SetStage(-1, -1);
654 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Elliott Hughes498cda62016-04-14 16:49:04 -0700655
Tao Baodb7e8982017-03-06 23:53:16 -0800656 ui->SetProgressType(RecoveryUI::DETERMINATE);
657 ui->ShowProgress(1.0, 10.0);
658 float fraction = 0.0;
659 for (size_t i = 0; i < 100; ++i) {
660 fraction += .01;
661 ui->SetProgress(fraction);
662 usleep(100000);
663 }
664
665 ui->ShowText(true);
Elliott Hughes498cda62016-04-14 16:49:04 -0700666}
667
Tao Baocdcf28f2016-01-13 15:05:20 -0800668// How long (in seconds) we wait for the fuse-provided package file to
669// appear, before timing out.
670#define SDCARD_INSTALL_TIMEOUT 10
671
Tao Bao145d8612015-03-25 15:51:15 -0700672static int apply_from_sdcard(Device* device, bool* wipe_cache) {
Tao Bao682c34b2015-04-07 17:16:35 -0700673 modified_flash = true;
674
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000675 if (ensure_path_mounted(SDCARD_ROOT) != 0) {
676 ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT);
677 return INSTALL_ERROR;
678 }
679
Tao Baoc4a18ef2017-02-10 00:13:30 -0800680 std::string path = browse_directory(SDCARD_ROOT, device);
681 if (path.empty()) {
Elliott Hughes018ed312015-04-08 16:51:36 -0700682 ui->Print("\n-- No package file selected.\n");
caozhiyuanb4effb92015-06-10 16:46:38 +0800683 ensure_path_unmounted(SDCARD_ROOT);
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000684 return INSTALL_ERROR;
685 }
686
Tao Baoc4a18ef2017-02-10 00:13:30 -0800687 ui->Print("\n-- Install %s ...\n", path.c_str());
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000688 set_sdcard_update_bootloader_message();
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000689
Tao Baocdcf28f2016-01-13 15:05:20 -0800690 // We used to use fuse in a thread as opposed to a process. Since accessing
691 // through fuse involves going from kernel to userspace to kernel, it leads
692 // to deadlock when a page fault occurs. (Bug: 26313124)
693 pid_t child;
694 if ((child = fork()) == 0) {
Tao Baoc4a18ef2017-02-10 00:13:30 -0800695 bool status = start_sdcard_fuse(path.c_str());
Tao Baocdcf28f2016-01-13 15:05:20 -0800696
697 _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
698 }
699
700 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
701 // process is ready.
702 int result = INSTALL_ERROR;
703 int status;
704 bool waited = false;
705 for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
706 if (waitpid(child, &status, WNOHANG) == -1) {
707 result = INSTALL_ERROR;
708 waited = true;
709 break;
710 }
711
712 struct stat sb;
713 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
714 if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
715 sleep(1);
716 continue;
717 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700718 LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
Tao Baocdcf28f2016-01-13 15:05:20 -0800719 result = INSTALL_ERROR;
720 kill(child, SIGKILL);
721 break;
722 }
723 }
724
Tao Bao641fa972018-04-25 18:59:40 -0700725 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/);
Tao Baocdcf28f2016-01-13 15:05:20 -0800726 break;
727 }
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000728
Tao Baocdcf28f2016-01-13 15:05:20 -0800729 if (!waited) {
730 // Calling stat() on this magic filename signals the fuse
731 // filesystem to shut down.
732 struct stat sb;
733 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
734
735 waitpid(child, &status, 0);
736 }
737
738 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700739 LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
Tao Baocdcf28f2016-01-13 15:05:20 -0800740 }
741
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000742 ensure_path_unmounted(SDCARD_ROOT);
Tao Baocdcf28f2016-01-13 15:05:20 -0800743 return result;
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000744}
745
Tao Bao50dd5322017-03-07 14:57:04 -0800746// Returns REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default,
747// which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery.
748static Device::BuiltinAction prompt_and_wait(Device* device, int status) {
749 for (;;) {
Tao Bao551d2c32018-05-09 20:53:13 -0700750 finish_recovery();
Tao Bao50dd5322017-03-07 14:57:04 -0800751 switch (status) {
752 case INSTALL_SUCCESS:
753 case INSTALL_NONE:
754 ui->SetBackground(RecoveryUI::NO_COMMAND);
755 break;
Doug Zongker6c8553d2012-09-24 10:40:47 -0700756
Tao Bao50dd5322017-03-07 14:57:04 -0800757 case INSTALL_ERROR:
758 case INSTALL_CORRUPT:
759 ui->SetBackground(RecoveryUI::ERROR);
760 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800761 }
Tao Bao50dd5322017-03-07 14:57:04 -0800762 ui->SetProgressType(RecoveryUI::EMPTY);
763
Tao Bao1fe1afe2018-05-01 15:56:05 -0700764 size_t chosen_item = ui->ShowMenu(
765 {}, device->GetMenuItems(), 0, false,
Tao Bao3aec6962018-04-20 09:24:58 -0700766 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Jerry Zhangb76af932018-05-22 12:08:35 -0700767 // Handle Interrupt key
768 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
769 return Device::KEY_INTERRUPTED;
770 }
Tao Bao50dd5322017-03-07 14:57:04 -0800771 // Device-specific code may take some action here. It may return one of the core actions
772 // handled in the switch statement below.
Jerry Zhangb76af932018-05-22 12:08:35 -0700773 Device::BuiltinAction chosen_action =
774 (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT))
775 ? Device::REBOOT
776 : device->InvokeMenuItem(chosen_item);
Tao Bao50dd5322017-03-07 14:57:04 -0800777
778 bool should_wipe_cache = false;
779 switch (chosen_action) {
780 case Device::NO_ACTION:
781 break;
782
783 case Device::REBOOT:
784 case Device::SHUTDOWN:
785 case Device::REBOOT_BOOTLOADER:
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700786 case Device::ENTER_FASTBOOT:
787 case Device::ENTER_RECOVERY:
Tao Bao50dd5322017-03-07 14:57:04 -0800788 return chosen_action;
789
790 case Device::WIPE_DATA:
791 if (ui->IsTextVisible()) {
792 if (ask_to_wipe_data(device)) {
793 wipe_data(device);
794 }
795 } else {
796 wipe_data(device);
797 return Device::NO_ACTION;
798 }
799 break;
800
801 case Device::WIPE_CACHE:
802 wipe_cache(ui->IsTextVisible(), device);
803 if (!ui->IsTextVisible()) return Device::NO_ACTION;
804 break;
805
806 case Device::APPLY_ADB_SIDELOAD:
807 case Device::APPLY_SDCARD:
808 {
809 bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
810 if (adb) {
Tao Bao641fa972018-04-25 18:59:40 -0700811 status = apply_from_adb(&should_wipe_cache);
Tao Bao50dd5322017-03-07 14:57:04 -0800812 } else {
813 status = apply_from_sdcard(device, &should_wipe_cache);
814 }
815
816 if (status == INSTALL_SUCCESS && should_wipe_cache) {
817 if (!wipe_cache(false, device)) {
818 status = INSTALL_ERROR;
819 }
820 }
821
822 if (status != INSTALL_SUCCESS) {
823 ui->SetBackground(RecoveryUI::ERROR);
824 ui->Print("Installation aborted.\n");
Jerry Zhangcadf4ed2018-05-02 16:56:00 -0700825 copy_logs(modified_flash, has_cache);
Tao Bao50dd5322017-03-07 14:57:04 -0800826 } else if (!ui->IsTextVisible()) {
827 return Device::NO_ACTION; // reboot if logs aren't visible
828 } else {
829 ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
830 }
831 }
832 break;
833
834 case Device::VIEW_RECOVERY_LOGS:
835 choose_recovery_file(device);
836 break;
837
838 case Device::RUN_GRAPHICS_TEST:
839 run_graphics_test();
840 break;
841
Tianjie Xu29d55752017-09-20 17:53:46 -0700842 case Device::RUN_LOCALE_TEST: {
843 ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui);
Tao Bao39c49182018-05-07 22:50:33 -0700844 screen_ui->CheckBackgroundTextImages();
Tianjie Xu29d55752017-09-20 17:53:46 -0700845 break;
846 }
Tao Bao50dd5322017-03-07 14:57:04 -0800847 case Device::MOUNT_SYSTEM:
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900848 // the system partition is mounted at /mnt/system
Tao Bao50dd5322017-03-07 14:57:04 -0800849 if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900850 if (ensure_path_mounted_at("/", "/mnt/system") != -1) {
Tao Bao50dd5322017-03-07 14:57:04 -0800851 ui->Print("Mounted /system.\n");
852 }
853 } else {
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900854 if (ensure_path_mounted_at("/system", "/mnt/system") != -1) {
Tao Bao50dd5322017-03-07 14:57:04 -0800855 ui->Print("Mounted /system.\n");
856 }
857 }
858 break;
Jerry Zhangb76af932018-05-22 12:08:35 -0700859
860 case Device::KEY_INTERRUPTED:
861 return Device::KEY_INTERRUPTED;
Tao Bao50dd5322017-03-07 14:57:04 -0800862 }
863 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800864}
865
Tao Bao99f0d9e2016-10-13 12:46:38 -0700866static void print_property(const char* key, const char* name, void* /* cookie */) {
867 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800868}
869
Tao Baoac9d94d2016-11-03 11:37:15 -0700870void ui_print(const char* format, ...) {
871 std::string buffer;
Doug Zongker7c3ae452013-05-14 11:03:02 -0700872 va_list ap;
873 va_start(ap, format);
Tao Baoac9d94d2016-11-03 11:37:15 -0700874 android::base::StringAppendV(&buffer, format, ap);
Doug Zongker7c3ae452013-05-14 11:03:02 -0700875 va_end(ap);
876
Tao Baoac9d94d2016-11-03 11:37:15 -0700877 if (ui != nullptr) {
878 ui->Print("%s", buffer.c_str());
Doug Zongker7c3ae452013-05-14 11:03:02 -0700879 } else {
Tao Baoac9d94d2016-11-03 11:37:15 -0700880 fputs(buffer.c_str(), stdout);
Doug Zongker7c3ae452013-05-14 11:03:02 -0700881 }
882}
883
Tao Bao6d90a9d2018-04-26 10:40:36 -0700884static bool is_battery_ok(int* required_battery_level) {
Yifan Honge8e4c402017-11-08 14:56:03 -0800885 using android::hardware::health::V1_0::BatteryStatus;
Yifan Hong056538c2018-07-11 17:04:12 -0700886 using android::hardware::health::V2_0::get_health_service;
887 using android::hardware::health::V2_0::IHealth;
Yifan Honge8e4c402017-11-08 14:56:03 -0800888 using android::hardware::health::V2_0::Result;
889 using android::hardware::health::V2_0::toString;
Yabin Cui99281df2016-02-17 12:21:52 -0800890
Yifan Hong056538c2018-07-11 17:04:12 -0700891 android::sp<IHealth> health = get_health_service();
Yifan Honge8e4c402017-11-08 14:56:03 -0800892
Tao Bao6d90a9d2018-04-26 10:40:36 -0700893 static constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10;
Yifan Honge8e4c402017-11-08 14:56:03 -0800894 int wait_second = 0;
895 while (true) {
896 auto charge_status = BatteryStatus::UNKNOWN;
Yifan Hong056538c2018-07-11 17:04:12 -0700897
898 if (health == nullptr) {
899 LOG(WARNING) << "no health implementation is found, assuming defaults";
900 } else {
901 health
902 ->getChargeStatus([&charge_status](auto res, auto out_status) {
903 if (res == Result::SUCCESS) {
904 charge_status = out_status;
905 }
906 })
907 .isOk(); // should not have transport error
908 }
Yifan Honge8e4c402017-11-08 14:56:03 -0800909
910 // Treat unknown status as charged.
911 bool charged = (charge_status != BatteryStatus::DISCHARGING &&
912 charge_status != BatteryStatus::NOT_CHARGING);
913
914 Result res = Result::UNKNOWN;
915 int32_t capacity = INT32_MIN;
Yifan Hong056538c2018-07-11 17:04:12 -0700916 if (health != nullptr) {
917 health
918 ->getCapacity([&res, &capacity](auto out_res, auto out_capacity) {
919 res = out_res;
920 capacity = out_capacity;
921 })
922 .isOk(); // should not have transport error
923 }
Yifan Honge8e4c402017-11-08 14:56:03 -0800924
Yifan Hong056538c2018-07-11 17:04:12 -0700925 LOG(INFO) << "charge_status " << toString(charge_status) << ", charged " << charged
926 << ", status " << toString(res) << ", capacity " << capacity;
Yifan Honge8e4c402017-11-08 14:56:03 -0800927 // At startup, the battery drivers in devices like N5X/N6P take some time to load
928 // the battery profile. Before the load finishes, it reports value 50 as a fake
929 // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected
930 // to finish loading the battery profile earlier than 10 seconds after kernel startup.
931 if (res == Result::SUCCESS && capacity == 50) {
932 if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) {
933 sleep(1);
934 wait_second++;
935 continue;
936 }
937 }
938 // If we can't read battery percentage, it may be a device without battery. In this
939 // situation, use 100 as a fake battery percentage.
940 if (res != Result::SUCCESS) {
941 capacity = 100;
942 }
Tao Bao6d90a9d2018-04-26 10:40:36 -0700943
944 // GmsCore enters recovery mode to install package when having enough battery percentage.
945 // Normally, the threshold is 40% without charger and 20% with charger. So we should check
946 // battery with a slightly lower limitation.
947 static constexpr int BATTERY_OK_PERCENTAGE = 20;
948 static constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
949 *required_battery_level = charged ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE;
950 return capacity >= *required_battery_level;
951 }
Yabin Cui99281df2016-02-17 12:21:52 -0800952}
953
Tianjie Xu99b73be2017-11-28 17:23:06 -0800954// Set the retry count to |retry_count| in BCB.
Tianjie Xu72449c92017-05-16 18:07:31 -0700955static void set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) {
956 std::vector<std::string> options;
957 for (const auto& arg : args) {
958 if (!android::base::StartsWith(arg, "--retry_count")) {
959 options.push_back(arg);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800960 }
Tianjie Xu72449c92017-05-16 18:07:31 -0700961 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800962
Tianjie Xu99b73be2017-11-28 17:23:06 -0800963 // Update the retry counter in BCB.
964 options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count));
Tianjie Xu72449c92017-05-16 18:07:31 -0700965 std::string err;
966 if (!update_bootloader_message(options, &err)) {
967 LOG(ERROR) << err;
968 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800969}
970
Tianjie Xu06e57ac2016-07-11 14:04:08 -0700971static bool bootreason_in_blacklist() {
Tao Baoefc35592017-01-08 22:45:47 -0800972 std::string bootreason = android::base::GetProperty("ro.boot.bootreason", "");
973 if (!bootreason.empty()) {
Tao Baoaac9d9f2018-04-29 23:38:59 -0700974 // More bootreasons can be found in "system/core/bootstat/bootstat.cpp".
975 static const std::vector<std::string> kBootreasonBlacklist{
976 "kernel_panic",
977 "Panic",
978 };
979 for (const auto& str : kBootreasonBlacklist) {
980 if (android::base::EqualsIgnoreCase(str, bootreason)) return true;
Tianjie Xu06e57ac2016-07-11 14:04:08 -0700981 }
Tao Baoefc35592017-01-08 22:45:47 -0800982 }
983 return false;
Tianjie Xu06e57ac2016-07-11 14:04:08 -0700984}
985
Tao Bao641fa972018-04-25 18:59:40 -0700986static void log_failure_code(ErrorCode code, const std::string& update_package) {
987 std::vector<std::string> log_buffer = {
988 update_package,
989 "0", // install result
990 "error: " + std::to_string(code),
991 };
992 std::string log_content = android::base::Join(log_buffer, "\n");
993 const std::string& install_file = Paths::Get().temporary_install_file();
994 if (!android::base::WriteStringToFile(log_content, install_file)) {
995 PLOG(ERROR) << "Failed to write " << install_file;
996 }
Tianjie Xu06e57ac2016-07-11 14:04:08 -0700997
Tao Bao641fa972018-04-25 18:59:40 -0700998 // Also write the info into last_log.
999 LOG(INFO) << log_content;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001000}
1001
Jerry Zhang6a648042018-05-04 11:24:10 -07001002Device::BuiltinAction start_recovery(Device* device, const std::vector<std::string>& args) {
Tao Baof9f17342018-04-27 10:44:04 -07001003 static constexpr struct option OPTIONS[] = {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -07001004 { "fastboot", no_argument, nullptr, 0 },
David Andersonedee8362018-05-16 13:43:22 -07001005 { "fsck_unshare_blocks", no_argument, nullptr, 0 },
Tao Baof9f17342018-04-27 10:44:04 -07001006 { "just_exit", no_argument, nullptr, 'x' },
1007 { "locale", required_argument, nullptr, 0 },
1008 { "prompt_and_wipe_data", no_argument, nullptr, 0 },
1009 { "reason", required_argument, nullptr, 0 },
1010 { "retry_count", required_argument, nullptr, 0 },
1011 { "security", no_argument, nullptr, 0 },
1012 { "show_text", no_argument, nullptr, 't' },
1013 { "shutdown_after", no_argument, nullptr, 0 },
1014 { "sideload", no_argument, nullptr, 0 },
1015 { "sideload_auto_reboot", no_argument, nullptr, 0 },
1016 { "update_package", required_argument, nullptr, 0 },
1017 { "wipe_ab", no_argument, nullptr, 0 },
1018 { "wipe_cache", no_argument, nullptr, 0 },
1019 { "wipe_data", no_argument, nullptr, 0 },
1020 { "wipe_package_size", required_argument, nullptr, 0 },
1021 { nullptr, 0, nullptr, 0 },
1022 };
1023
Tianjie Xu99b73be2017-11-28 17:23:06 -08001024 const char* update_package = nullptr;
1025 bool should_wipe_data = false;
1026 bool should_prompt_and_wipe_data = false;
1027 bool should_wipe_cache = false;
1028 bool should_wipe_ab = false;
1029 size_t wipe_package_size = 0;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001030 bool sideload = false;
1031 bool sideload_auto_reboot = false;
1032 bool just_exit = false;
1033 bool shutdown_after = false;
David Andersonedee8362018-05-16 13:43:22 -07001034 bool fsck_unshare_blocks = false;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001035 int retry_count = 0;
1036 bool security_update = false;
Jerry Zhang2dea53e2018-05-02 17:15:03 -07001037 std::string locale;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001038
Tao Bao1700cc42018-07-16 22:09:59 -07001039 auto args_to_parse = StringVectorToNullTerminatedArray(args);
1040
Tianjie Xu99b73be2017-11-28 17:23:06 -08001041 int arg;
1042 int option_index;
Tao Bao1700cc42018-07-16 22:09:59 -07001043 // Parse everything before the last element (which must be a nullptr). getopt_long(3) expects a
1044 // null-terminated char* array, but without counting null as an arg (i.e. argv[argc] should be
1045 // nullptr).
1046 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
Tianjie Xu99b73be2017-11-28 17:23:06 -08001047 &option_index)) != -1) {
1048 switch (arg) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001049 case 't':
Jerry Zhang6a648042018-05-04 11:24:10 -07001050 // Handled in recovery_main.cpp
Tianjie Xu99b73be2017-11-28 17:23:06 -08001051 break;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001052 case 'x':
1053 just_exit = true;
1054 break;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001055 case 0: {
1056 std::string option = OPTIONS[option_index].name;
David Andersonedee8362018-05-16 13:43:22 -07001057 if (option == "fsck_unshare_blocks") {
1058 fsck_unshare_blocks = true;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -07001059 } else if (option == "locale" || option == "fastboot") {
Jerry Zhang6a648042018-05-04 11:24:10 -07001060 // Handled in recovery_main.cpp
Tianjie Xu99b73be2017-11-28 17:23:06 -08001061 } else if (option == "prompt_and_wipe_data") {
1062 should_prompt_and_wipe_data = true;
Tao Baof9f17342018-04-27 10:44:04 -07001063 } else if (option == "reason") {
1064 reason = optarg;
1065 } else if (option == "retry_count") {
1066 android::base::ParseInt(optarg, &retry_count, 0);
1067 } else if (option == "security") {
1068 security_update = true;
1069 } else if (option == "sideload") {
1070 sideload = true;
1071 } else if (option == "sideload_auto_reboot") {
1072 sideload = true;
1073 sideload_auto_reboot = true;
1074 } else if (option == "shutdown_after") {
1075 shutdown_after = true;
1076 } else if (option == "update_package") {
1077 update_package = optarg;
1078 } else if (option == "wipe_ab") {
1079 should_wipe_ab = true;
1080 } else if (option == "wipe_cache") {
1081 should_wipe_cache = true;
1082 } else if (option == "wipe_data") {
1083 should_wipe_data = true;
1084 } else if (option == "wipe_package_size") {
1085 android::base::ParseUint(optarg, &wipe_package_size);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001086 }
1087 break;
1088 }
1089 case '?':
1090 LOG(ERROR) << "Invalid command argument";
1091 continue;
Doug Zongker9270a202012-01-09 15:16:13 -08001092 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001093 }
Jerry Zhang49fd5d22018-05-17 12:54:41 -07001094 optind = 1;
Doug Zongker9270a202012-01-09 15:16:13 -08001095
Tianjie Xu99b73be2017-11-28 17:23:06 -08001096 printf("stage is [%s]\n", stage.c_str());
1097 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001098
Tianjie Xu99b73be2017-11-28 17:23:06 -08001099 // Set background string to "installing security update" for security update,
1100 // otherwise set it to "installing system update".
1101 ui->SetSystemUpdateText(security_update);
1102
1103 int st_cur, st_max;
1104 if (!stage.empty() && sscanf(stage.c_str(), "%d/%d", &st_cur, &st_max) == 2) {
1105 ui->SetStage(st_cur, st_max);
1106 }
1107
Jerry Zhang0e577ee2018-05-07 11:21:10 -07001108 std::vector<std::string> title_lines =
1109 android::base::Split(android::base::GetProperty("ro.bootimage.build.fingerprint", ""), ":");
1110 title_lines.insert(std::begin(title_lines), "Android Recovery");
1111 ui->SetTitle(title_lines);
1112
Jerry Zhangb76af932018-05-22 12:08:35 -07001113 ui->ResetKeyInterruptStatus();
Tianjie Xu99b73be2017-11-28 17:23:06 -08001114 device->StartRecovery();
1115
1116 printf("Command:");
1117 for (const auto& arg : args) {
1118 printf(" \"%s\"", arg.c_str());
1119 }
1120 printf("\n\n");
1121
1122 property_list(print_property, nullptr);
1123 printf("\n");
1124
1125 ui->Print("Supported API: %d\n", kRecoveryApiVersion);
1126
1127 int status = INSTALL_SUCCESS;
1128
1129 if (update_package != nullptr) {
1130 // It's not entirely true that we will modify the flash. But we want
1131 // to log the update attempt since update_package is non-NULL.
1132 modified_flash = true;
1133
Tao Bao6d90a9d2018-04-26 10:40:36 -07001134 int required_battery_level;
1135 if (retry_count == 0 && !is_battery_ok(&required_battery_level)) {
1136 ui->Print("battery capacity is not enough for installing package: %d%% needed\n",
1137 required_battery_level);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001138 // Log the error code to last_install when installation skips due to
1139 // low battery.
1140 log_failure_code(kLowBattery, update_package);
1141 status = INSTALL_SKIPPED;
Tianjie Xua6f49bd2018-03-26 14:32:11 -07001142 } else if (retry_count == 0 && bootreason_in_blacklist()) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001143 // Skip update-on-reboot when bootreason is kernel_panic or similar
1144 ui->Print("bootreason is in the blacklist; skip OTA installation\n");
1145 log_failure_code(kBootreasonInBlacklist, update_package);
1146 status = INSTALL_SKIPPED;
Dmitri Plotnikov8706a982017-04-18 08:28:26 -07001147 } else {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001148 // It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later
1149 // identify the interrupted update due to unexpected reboots.
1150 if (retry_count == 0) {
1151 set_retry_bootloader_message(retry_count + 1, args);
Tao Bao7022f332017-07-25 09:52:36 -07001152 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001153
Tao Bao641fa972018-04-25 18:59:40 -07001154 status = install_package(update_package, &should_wipe_cache, true, retry_count);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001155 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1156 wipe_cache(false, device);
1157 }
1158 if (status != INSTALL_SUCCESS) {
1159 ui->Print("Installation aborted.\n");
Tao Baoaac9d9f2018-04-29 23:38:59 -07001160
1161 // When I/O error or bspatch/imgpatch error happens, reboot and retry installation
1162 // RETRY_LIMIT times before we abandon this OTA update.
1163 static constexpr int RETRY_LIMIT = 4;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001164 if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) {
Jerry Zhangcadf4ed2018-05-02 16:56:00 -07001165 copy_logs(modified_flash, has_cache);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001166 retry_count += 1;
1167 set_retry_bootloader_message(retry_count, args);
1168 // Print retry count on screen.
1169 ui->Print("Retry attempt %d\n", retry_count);
1170
1171 // Reboot and retry the update
1172 if (!reboot("reboot,recovery")) {
1173 ui->Print("Reboot failed\n");
1174 } else {
1175 while (true) {
1176 pause();
1177 }
1178 }
Tianjie Xud9d16292017-04-20 18:08:21 -07001179 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001180 // If this is an eng or userdebug build, then automatically
1181 // turn the text display on if the script fails so the error
1182 // message is visible.
1183 if (is_ro_debuggable()) {
1184 ui->ShowText(true);
Tao Baoc679f932015-03-30 09:43:49 -07001185 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001186 }
Doug Zongker8674a722010-09-15 11:08:23 -07001187 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001188 } else if (should_wipe_data) {
1189 if (!wipe_data(device)) {
1190 status = INSTALL_ERROR;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001191 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001192 } else if (should_prompt_and_wipe_data) {
1193 ui->ShowText(true);
1194 ui->SetBackground(RecoveryUI::ERROR);
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -07001195 status = prompt_and_wipe_data(device);
1196 if (status != INSTALL_KEY_INTERRUPTED) {
1197 ui->ShowText(false);
Tao Bao75238632015-05-27 14:46:17 -07001198 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001199 } else if (should_wipe_cache) {
1200 if (!wipe_cache(false, device)) {
1201 status = INSTALL_ERROR;
1202 }
1203 } else if (should_wipe_ab) {
1204 if (!wipe_ab_device(wipe_package_size)) {
1205 status = INSTALL_ERROR;
1206 }
1207 } else if (sideload) {
1208 // 'adb reboot sideload' acts the same as user presses key combinations
1209 // to enter the sideload mode. When 'sideload-auto-reboot' is used, text
1210 // display will NOT be turned on by default. And it will reboot after
1211 // sideload finishes even if there are errors. Unless one turns on the
1212 // text display during the installation. This is to enable automated
1213 // testing.
1214 if (!sideload_auto_reboot) {
1215 ui->ShowText(true);
1216 }
Tao Bao641fa972018-04-25 18:59:40 -07001217 status = apply_from_adb(&should_wipe_cache);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001218 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1219 if (!wipe_cache(false, device)) {
1220 status = INSTALL_ERROR;
1221 }
1222 }
1223 ui->Print("\nInstall from ADB complete (status: %d).\n", status);
1224 if (sideload_auto_reboot) {
1225 ui->Print("Rebooting automatically.\n");
1226 }
David Andersonedee8362018-05-16 13:43:22 -07001227 } else if (fsck_unshare_blocks) {
1228 if (!do_fsck_unshare_blocks()) {
1229 status = INSTALL_ERROR;
1230 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001231 } else if (!just_exit) {
1232 // If this is an eng or userdebug build, automatically turn on the text display if no command
1233 // is specified. Note that this should be called before setting the background to avoid
1234 // flickering the background image.
1235 if (is_ro_debuggable()) {
1236 ui->ShowText(true);
1237 }
1238 status = INSTALL_NONE; // No command specified
1239 ui->SetBackground(RecoveryUI::NO_COMMAND);
1240 }
1241
1242 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
1243 ui->SetBackground(RecoveryUI::ERROR);
1244 if (!ui->IsTextVisible()) {
1245 sleep(5);
1246 }
1247 }
1248
1249 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
1250 // 1. If the recovery menu is visible, prompt and wait for commands.
1251 // 2. If the state is INSTALL_NONE, wait for commands. (i.e. In user build, manually reboot into
1252 // recovery to sideload a package.)
1253 // 3. sideload_auto_reboot is an option only available in user-debug build, reboot the device
1254 // without waiting.
1255 // 4. In all other cases, reboot the device. Therefore, normal users will observe the device
1256 // reboot after it shows the "error" screen for 5s.
1257 if ((status == INSTALL_NONE && !sideload_auto_reboot) || ui->IsTextVisible()) {
1258 Device::BuiltinAction temp = prompt_and_wait(device, status);
1259 if (temp != Device::NO_ACTION) {
1260 after = temp;
1261 }
1262 }
1263
1264 // Save logs and clean up before rebooting or shutting down.
Tao Bao551d2c32018-05-09 20:53:13 -07001265 finish_recovery();
Tianjie Xu99b73be2017-11-28 17:23:06 -08001266
Jerry Zhang6a648042018-05-04 11:24:10 -07001267 return after;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001268}