The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Zhang | 6a64804 | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 17 | #include "recovery.h" |
Tao Bao | 2ac56af | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 18 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <getopt.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | #include <limits.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 25 | #include <linux/fs.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | #include <linux/input.h> |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 27 | #include <stdarg.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 31 | #include <sys/stat.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | #include <sys/types.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 35 | #include <algorithm> |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 36 | #include <functional> |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 37 | #include <memory> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 38 | #include <string> |
| 39 | #include <vector> |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 41 | #include <android-base/file.h> |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 42 | #include <android-base/logging.h> |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 43 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 44 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 45 | #include <android-base/stringprintf.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 46 | #include <android-base/strings.h> |
| 47 | #include <android-base/unique_fd.h> |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 48 | #include <bootloader_message/bootloader_message.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 49 | #include <cutils/properties.h> /* for property_list */ |
Yifan Hong | 056538c | 2018-07-11 17:04:12 -0700 | [diff] [blame] | 50 | #include <healthhalutils/HealthHalUtils.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 51 | #include <ziparchive/zip_archive.h> |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 52 | |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 53 | #include "adb_install.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 54 | #include "common.h" |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 55 | #include "fsck_unshare_blocks.h" |
xunchang | ea2912f | 2019-03-17 16:45:12 -0700 | [diff] [blame] | 56 | #include "fuse_sdcard_install.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 57 | #include "install.h" |
Jerry Zhang | cadf4ed | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 58 | #include "logging.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 59 | #include "otautil/dirutil.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 60 | #include "otautil/error_code.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 61 | #include "otautil/paths.h" |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 62 | #include "otautil/sysutil.h" |
xunchang | f07ed2e | 2019-02-25 14:14:01 -0800 | [diff] [blame] | 63 | #include "package.h" |
Tianjie Xu | b5108c3 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 64 | #include "recovery_ui/screen_ui.h" |
| 65 | #include "recovery_ui/ui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 66 | #include "roots.h" |
| 67 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 68 | static constexpr const char* CACHE_LOG_DIR = "/cache/recovery"; |
| 69 | static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 70 | static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; |
| 71 | static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 72 | static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale"; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 73 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 74 | static constexpr const char* CACHE_ROOT = "/cache"; |
| 75 | static constexpr const char* DATA_ROOT = "/data"; |
| 76 | static constexpr const char* METADATA_ROOT = "/metadata"; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 77 | |
Tao Bao | bd0ddcd | 2017-05-04 13:03:18 -0700 | [diff] [blame] | 78 | // We define RECOVERY_API_VERSION in Android.mk, which will be picked up by build system and packed |
| 79 | // into target_files.zip. Assert the version defined in code and in Android.mk are consistent. |
| 80 | static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions."); |
| 81 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 82 | bool modified_flash = false; |
Tao Bao | a8d72bc | 2016-12-25 18:46:50 -0800 | [diff] [blame] | 83 | std::string stage; |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 84 | const char* reason = nullptr; |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 85 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 86 | /* |
| 87 | * The recovery tool communicates with the main system through /cache files. |
| 88 | * /cache/recovery/command - INPUT - command line for tool, one arg per line |
| 89 | * /cache/recovery/log - OUTPUT - combined log file from recovery run(s) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | * |
| 91 | * The arguments which may be supplied in the recovery.command file: |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 92 | * --update_package=path - verify install an OTA package file |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 93 | * --wipe_data - erase user data (and cache), then reboot |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 94 | * --prompt_and_wipe_data - prompt the user that data is corrupt, with their consent erase user |
| 95 | * data (and cache), then reboot |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 96 | * --wipe_cache - wipe cache (but not user data), then reboot |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 97 | * --show_text - show the recovery text menu, used by some bootloader (e.g. http://b/36872519). |
Oscar Montemayor | 0523156 | 2009-11-30 08:40:57 -0800 | [diff] [blame] | 98 | * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 99 | * --just_exit - do nothing; exit and reboot |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 100 | * |
| 101 | * After completing, we remove /cache/recovery/command and reboot. |
| 102 | * Arguments may also be supplied in the bootloader control block (BCB). |
| 103 | * These important scenarios must be safely restartable at any point: |
| 104 | * |
| 105 | * FACTORY RESET |
| 106 | * 1. user selects "factory reset" |
| 107 | * 2. main system writes "--wipe_data" to /cache/recovery/command |
| 108 | * 3. main system reboots into recovery |
| 109 | * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data" |
| 110 | * -- after this, rebooting will restart the erase -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 111 | * 5. erase_volume() reformats /data |
| 112 | * 6. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 113 | * 7. finish_recovery() erases BCB |
| 114 | * -- after this, rebooting will restart the main system -- |
| 115 | * 8. main() calls reboot() to boot main system |
| 116 | * |
| 117 | * OTA INSTALL |
| 118 | * 1. main system downloads OTA package to /cache/some-filename.zip |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 119 | * 2. main system writes "--update_package=/cache/some-filename.zip" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 120 | * 3. main system reboots into recovery |
| 121 | * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..." |
| 122 | * -- after this, rebooting will attempt to reinstall the update -- |
| 123 | * 5. install_package() attempts to install the update |
| 124 | * NOTE: the package install must itself be restartable from any point |
| 125 | * 6. finish_recovery() erases BCB |
| 126 | * -- after this, rebooting will (try to) restart the main system -- |
| 127 | * 7. ** if install failed ** |
| 128 | * 7a. prompt_and_wait() shows an error icon and waits for the user |
Tao Bao | c033639 | 2016-12-13 22:29:49 -0800 | [diff] [blame] | 129 | * 7b. the user reboots (pulling the battery, etc) into the main system |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | */ |
| 131 | |
Elliott Hughes | f14af80 | 2015-02-10 14:46:14 -0800 | [diff] [blame] | 132 | bool is_ro_debuggable() { |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 133 | return android::base::GetBoolProperty("ro.debuggable", false); |
Elliott Hughes | f14af80 | 2015-02-10 14:46:14 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 136 | // Clear the recovery command and prepare to boot a (hopefully working) system, |
Tianjie Xu | c14d95d | 2016-03-24 11:50:34 -0700 | [diff] [blame] | 137 | // copy our log file to cache as well (for the system to read). This function is |
| 138 | // idempotent: call it as many times as you like. |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 139 | static void finish_recovery() { |
| 140 | std::string locale = ui->GetLocale(); |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 141 | // Save the locale to cache, so if recovery is next started up without a '--locale' argument |
| 142 | // (e.g., directly from the bootloader) it will use the last-known locale. |
| 143 | if (!locale.empty() && has_cache) { |
| 144 | LOG(INFO) << "Saving locale \"" << locale << "\""; |
| 145 | if (ensure_path_mounted(LOCALE_FILE) != 0) { |
| 146 | LOG(ERROR) << "Failed to mount " << LOCALE_FILE; |
| 147 | } else if (!android::base::WriteStringToFile(locale, LOCALE_FILE)) { |
| 148 | PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE; |
Doug Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 149 | } |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 150 | } |
Doug Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 151 | |
Jerry Zhang | cadf4ed | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 152 | copy_logs(modified_flash, has_cache); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 153 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 154 | // Reset to normal system boot so recovery won't cycle indefinitely. |
| 155 | std::string err; |
| 156 | if (!clear_bootloader_message(&err)) { |
| 157 | LOG(ERROR) << "Failed to clear BCB message: " << err; |
| 158 | } |
| 159 | |
| 160 | // Remove the command file, so recovery won't repeat indefinitely. |
| 161 | if (has_cache) { |
| 162 | if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) { |
| 163 | LOG(WARNING) << "Can't unlink " << COMMAND_FILE; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 164 | } |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 165 | ensure_path_unmounted(CACHE_ROOT); |
| 166 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 167 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 168 | sync(); // For good measure. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 171 | struct saved_log_file { |
| 172 | std::string name; |
| 173 | struct stat sb; |
| 174 | std::string data; |
| 175 | }; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 176 | |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 177 | static bool erase_volume(const char* volume) { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 178 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
| 179 | bool is_data = (strcmp(volume, DATA_ROOT) == 0); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 180 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 181 | ui->SetBackground(RecoveryUI::ERASING); |
| 182 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 183 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 184 | std::vector<saved_log_file> log_files; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 185 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 186 | if (is_cache) { |
| 187 | // If we're reformatting /cache, we load any past logs |
| 188 | // (i.e. "/cache/recovery/last_*") and the current log |
| 189 | // ("/cache/recovery/log") into memory, so we can restore them after |
| 190 | // the reformat. |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 191 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 192 | ensure_path_mounted(volume); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 193 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 194 | struct dirent* de; |
| 195 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(CACHE_LOG_DIR), closedir); |
| 196 | if (d) { |
| 197 | while ((de = readdir(d.get())) != nullptr) { |
| 198 | if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) { |
| 199 | std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name); |
| 200 | |
| 201 | struct stat sb; |
| 202 | if (stat(path.c_str(), &sb) == 0) { |
| 203 | // truncate files to 512kb |
| 204 | if (sb.st_size > (1 << 19)) { |
| 205 | sb.st_size = 1 << 19; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 206 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 207 | |
| 208 | std::string data(sb.st_size, '\0'); |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 209 | FILE* f = fopen(path.c_str(), "rbe"); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 210 | fread(&data[0], 1, data.size(), f); |
| 211 | fclose(f); |
| 212 | |
| 213 | log_files.emplace_back(saved_log_file{ path, sb, data }); |
| 214 | } |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 215 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 216 | } |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 217 | } else { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 218 | if (errno != ENOENT) { |
| 219 | PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR; |
| 220 | } |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 221 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 222 | } |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 223 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 224 | ui->Print("Formatting %s...\n", volume); |
| 225 | |
| 226 | ensure_path_unmounted(volume); |
| 227 | |
| 228 | int result; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 229 | if (is_data && reason && strcmp(reason, "convert_fbe") == 0) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 230 | static constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe"; |
| 231 | static constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe"; |
| 232 | // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not |
| 233 | // full disk encryption. |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 234 | if (mkdir(CONVERT_FBE_DIR, 0700) != 0) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 235 | PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR; |
| 236 | return false; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 237 | } |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 238 | FILE* f = fopen(CONVERT_FBE_FILE, "wbe"); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 239 | if (!f) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 240 | PLOG(ERROR) << "Failed to convert to file encryption"; |
| 241 | return false; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 242 | } |
| 243 | fclose(f); |
| 244 | result = format_volume(volume, CONVERT_FBE_DIR); |
| 245 | remove(CONVERT_FBE_FILE); |
| 246 | rmdir(CONVERT_FBE_DIR); |
| 247 | } else { |
| 248 | result = format_volume(volume); |
| 249 | } |
| 250 | |
| 251 | if (is_cache) { |
| 252 | // Re-create the log dir and write back the log entries. |
| 253 | if (ensure_path_mounted(CACHE_LOG_DIR) == 0 && |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 254 | mkdir_recursively(CACHE_LOG_DIR, 0777, false, sehandle) == 0) { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 255 | for (const auto& log : log_files) { |
| 256 | if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid, |
| 257 | log.sb.st_gid)) { |
| 258 | PLOG(ERROR) << "Failed to write to " << log.name; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 259 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 260 | } |
| 261 | } else { |
| 262 | PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR; |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 265 | // Any part of the log we'd copied to cache is now gone. |
| 266 | // Reset the pointer so we copy from the beginning of the temp |
| 267 | // log. |
Jerry Zhang | cadf4ed | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 268 | reset_tmplog_offset(); |
| 269 | copy_logs(modified_flash, has_cache); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return (result == 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Hridya Valsaraju | e4ef453 | 2018-08-31 11:57:51 -0700 | [diff] [blame] | 275 | // Sets the usb config to 'state' |
| 276 | bool SetUsbConfig(const std::string& state) { |
| 277 | android::base::SetProperty("sys.usb.config", state); |
| 278 | return android::base::WaitForProperty("sys.usb.state", state); |
| 279 | } |
| 280 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 281 | static bool yes_no(Device* device, const char* question1, const char* question2) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 282 | std::vector<std::string> headers{ question1, question2 }; |
| 283 | std::vector<std::string> items{ " No", " Yes" }; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 284 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 285 | size_t chosen_item = ui->ShowMenu( |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 286 | headers, items, 0, true, |
| 287 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
| 288 | return (chosen_item == 1); |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 289 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 290 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 291 | static bool ask_to_wipe_data(Device* device) { |
Tianjie Xu | 1a0a30a | 2018-10-25 15:22:07 -0700 | [diff] [blame] | 292 | std::vector<std::string> headers{ "Wipe all user data?", " THIS CAN NOT BE UNDONE!" }; |
| 293 | std::vector<std::string> items{ " Cancel", " Factory data reset" }; |
| 294 | |
| 295 | size_t chosen_item = ui->ShowPromptWipeDataConfirmationMenu( |
| 296 | headers, items, |
| 297 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
| 298 | |
| 299 | return (chosen_item == 1); |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 300 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 301 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 302 | // Return true on success. |
| 303 | static bool wipe_data(Device* device) { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 304 | modified_flash = true; |
| 305 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 306 | ui->Print("\n-- Wiping data...\n"); |
Paul Crowley | 3b4d516 | 2016-06-08 13:51:41 -0700 | [diff] [blame] | 307 | bool success = device->PreWipeData(); |
| 308 | if (success) { |
| 309 | success &= erase_volume(DATA_ROOT); |
| 310 | if (has_cache) { |
| 311 | success &= erase_volume(CACHE_ROOT); |
| 312 | } |
| 313 | if (volume_for_mount_point(METADATA_ROOT) != nullptr) { |
| 314 | success &= erase_volume(METADATA_ROOT); |
| 315 | } |
| 316 | } |
| 317 | if (success) { |
| 318 | success &= device->PostWipeData(); |
| 319 | } |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 320 | ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); |
| 321 | return success; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Hridya Valsaraju | eb6f13a | 2018-09-12 10:25:01 -0700 | [diff] [blame] | 324 | static InstallResult prompt_and_wipe_data(Device* device) { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 325 | // Use a single string and let ScreenRecoveryUI handles the wrapping. |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 326 | std::vector<std::string> wipe_data_menu_headers{ |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 327 | "Can't load Android system. Your data may be corrupt. " |
| 328 | "If you continue to get this message, you may need to " |
| 329 | "perform a factory data reset and erase all user data " |
Paul Crowley | 31ac0c6 | 2017-03-23 12:32:40 -0700 | [diff] [blame] | 330 | "stored on this device.", |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 331 | }; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 332 | // clang-format off |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 333 | std::vector<std::string> wipe_data_menu_items { |
Paul Crowley | 31ac0c6 | 2017-03-23 12:32:40 -0700 | [diff] [blame] | 334 | "Try again", |
| 335 | "Factory data reset", |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 336 | }; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 337 | // clang-format on |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 338 | for (;;) { |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 339 | size_t chosen_item = ui->ShowPromptWipeDataMenu( |
| 340 | wipe_data_menu_headers, wipe_data_menu_items, |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 341 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 342 | // If ShowMenu() returned RecoveryUI::KeyError::INTERRUPTED, WaitKey() was interrupted. |
| 343 | if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) { |
Hridya Valsaraju | eb6f13a | 2018-09-12 10:25:01 -0700 | [diff] [blame] | 344 | return INSTALL_KEY_INTERRUPTED; |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 345 | } |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 346 | if (chosen_item != 1) { |
Hridya Valsaraju | eb6f13a | 2018-09-12 10:25:01 -0700 | [diff] [blame] | 347 | return INSTALL_SUCCESS; // Just reboot, no wipe; not a failure, user asked for it |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 348 | } |
Tianjie Xu | b99e606 | 2018-10-16 15:13:09 -0700 | [diff] [blame] | 349 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 350 | if (ask_to_wipe_data(device)) { |
Hridya Valsaraju | eb6f13a | 2018-09-12 10:25:01 -0700 | [diff] [blame] | 351 | if (wipe_data(device)) { |
| 352 | return INSTALL_SUCCESS; |
| 353 | } else { |
| 354 | return INSTALL_ERROR; |
| 355 | } |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Tao Bao | e39a9bc | 2015-03-31 12:19:05 -0700 | [diff] [blame] | 360 | // Return true on success. |
| 361 | static bool wipe_cache(bool should_confirm, Device* device) { |
Tao Bao | 26112e5 | 2016-02-25 12:29:40 -0800 | [diff] [blame] | 362 | if (!has_cache) { |
| 363 | ui->Print("No /cache partition found.\n"); |
| 364 | return false; |
| 365 | } |
| 366 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 367 | if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) { |
Tao Bao | e39a9bc | 2015-03-31 12:19:05 -0700 | [diff] [blame] | 368 | return false; |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 371 | modified_flash = true; |
| 372 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 373 | ui->Print("\n-- Wiping cache...\n"); |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 374 | bool success = erase_volume("/cache"); |
| 375 | ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); |
| 376 | return success; |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 379 | // Secure-wipe a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with |
| 380 | // BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT. |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 381 | static bool secure_wipe_partition(const std::string& partition) { |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 382 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY))); |
| 383 | if (fd == -1) { |
| 384 | PLOG(ERROR) << "Failed to open \"" << partition << "\""; |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | uint64_t range[2] = { 0, 0 }; |
| 389 | if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) { |
| 390 | PLOG(ERROR) << "Failed to get partition size"; |
| 391 | return false; |
| 392 | } |
| 393 | LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1]; |
| 394 | |
| 395 | LOG(INFO) << " Trying BLKSECDISCARD..."; |
| 396 | if (ioctl(fd, BLKSECDISCARD, &range) == -1) { |
| 397 | PLOG(WARNING) << " Failed"; |
| 398 | |
| 399 | // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT. |
| 400 | unsigned int zeroes; |
| 401 | if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) { |
| 402 | LOG(INFO) << " Trying BLKDISCARD..."; |
| 403 | if (ioctl(fd, BLKDISCARD, &range) == -1) { |
| 404 | PLOG(ERROR) << " Failed"; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 405 | return false; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 406 | } |
| 407 | } else { |
| 408 | LOG(INFO) << " Trying BLKZEROOUT..."; |
| 409 | if (ioctl(fd, BLKZEROOUT, &range) == -1) { |
| 410 | PLOG(ERROR) << " Failed"; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 411 | return false; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 412 | } |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 413 | } |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 414 | } |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 415 | |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 416 | LOG(INFO) << " Done"; |
| 417 | return true; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 418 | } |
| 419 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 420 | static std::unique_ptr<Package> ReadWipePackage(size_t wipe_package_size) { |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 421 | if (wipe_package_size == 0) { |
| 422 | LOG(ERROR) << "wipe_package_size is zero"; |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 423 | return nullptr; |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | std::string wipe_package; |
| 427 | std::string err_str; |
| 428 | if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) { |
| 429 | PLOG(ERROR) << "Failed to read wipe package" << err_str; |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 430 | return nullptr; |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 431 | } |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 432 | |
| 433 | return Package::CreateMemoryPackage( |
| 434 | std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr); |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // Checks if the wipe package matches expectation. If the check passes, reads the list of |
| 438 | // partitions to wipe from the package. Checks include |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 439 | // 1. verify the package. |
| 440 | // 2. check metadata (ota-type, pre-device and serial number if having one). |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 441 | static bool CheckWipePackage(Package* wipe_package) { |
| 442 | if (!verify_package(wipe_package)) { |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 443 | LOG(ERROR) << "Failed to verify package"; |
| 444 | return false; |
| 445 | } |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 446 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 447 | ZipArchiveHandle zip = wipe_package->GetZipArchiveHandle(); |
| 448 | if (!zip) { |
| 449 | LOG(ERROR) << "Failed to get ZipArchiveHandle"; |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | |
| 453 | std::map<std::string, std::string> metadata; |
| 454 | if (!ReadMetadataFromPackage(zip, &metadata)) { |
| 455 | LOG(ERROR) << "Failed to parse metadata in the zip file"; |
| 456 | return false; |
| 457 | } |
| 458 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 459 | return CheckPackageMetadata(metadata, OtaType::BRICK) == 0; |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 460 | } |
| 461 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 462 | std::vector<std::string> GetWipePartitionList(Package* wipe_package) { |
| 463 | ZipArchiveHandle zip = wipe_package->GetZipArchiveHandle(); |
| 464 | if (!zip) { |
| 465 | LOG(ERROR) << "Failed to get ZipArchiveHandle"; |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 466 | return {}; |
| 467 | } |
| 468 | |
| 469 | static constexpr const char* RECOVERY_WIPE_ENTRY_NAME = "recovery.wipe"; |
| 470 | |
| 471 | std::string partition_list_content; |
| 472 | ZipString path(RECOVERY_WIPE_ENTRY_NAME); |
| 473 | ZipEntry entry; |
| 474 | if (FindEntry(zip, path, &entry) == 0) { |
| 475 | uint32_t length = entry.uncompressed_length; |
| 476 | partition_list_content = std::string(length, '\0'); |
| 477 | if (auto err = ExtractToMemory( |
| 478 | zip, &entry, reinterpret_cast<uint8_t*>(partition_list_content.data()), length); |
| 479 | err != 0) { |
| 480 | LOG(ERROR) << "Failed to extract " << RECOVERY_WIPE_ENTRY_NAME << ": " |
| 481 | << ErrorCodeString(err); |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 482 | return {}; |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 483 | } |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 484 | } else { |
| 485 | LOG(INFO) << "Failed to find " << RECOVERY_WIPE_ENTRY_NAME |
| 486 | << ", falling back to use the partition list on device."; |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 487 | |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 488 | static constexpr const char* RECOVERY_WIPE_ON_DEVICE = "/etc/recovery.wipe"; |
| 489 | if (!android::base::ReadFileToString(RECOVERY_WIPE_ON_DEVICE, &partition_list_content)) { |
| 490 | PLOG(ERROR) << "failed to read \"" << RECOVERY_WIPE_ON_DEVICE << "\""; |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 491 | return {}; |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 492 | } |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 493 | } |
Tianjie Xu | 93b5bf2 | 2018-10-25 10:39:01 -0700 | [diff] [blame] | 494 | |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 495 | std::vector<std::string> result; |
| 496 | std::vector<std::string> lines = android::base::Split(partition_list_content, "\n"); |
| 497 | for (const std::string& line : lines) { |
| 498 | std::string partition = android::base::Trim(line); |
| 499 | // Ignore '#' comment or empty lines. |
| 500 | if (android::base::StartsWith(partition, "#") || partition.empty()) { |
| 501 | continue; |
| 502 | } |
| 503 | result.push_back(line); |
| 504 | } |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 505 | |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 506 | return result; |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 509 | // Wipes the current A/B device, with a secure wipe of all the partitions in RECOVERY_WIPE. |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 510 | static bool wipe_ab_device(size_t wipe_package_size) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 511 | ui->SetBackground(RecoveryUI::ERASING); |
| 512 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 513 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 514 | auto wipe_package = ReadWipePackage(wipe_package_size); |
| 515 | if (!wipe_package) { |
| 516 | LOG(ERROR) << "Failed to open wipe package"; |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 520 | if (!CheckWipePackage(wipe_package.get())) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 521 | LOG(ERROR) << "Failed to verify wipe package"; |
| 522 | return false; |
| 523 | } |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 524 | |
xunchang | 55e3d22 | 2019-03-11 11:28:41 -0700 | [diff] [blame] | 525 | auto partition_list = GetWipePartitionList(wipe_package.get()); |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 526 | if (partition_list.empty()) { |
| 527 | LOG(ERROR) << "Empty wipe ab partition list"; |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | |
xunchang | e0d991c | 2019-03-05 14:50:51 -0800 | [diff] [blame] | 531 | for (const auto& partition : partition_list) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 532 | // Proceed anyway even if it fails to wipe some partition. |
| 533 | secure_wipe_partition(partition); |
| 534 | } |
| 535 | return true; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 538 | static void choose_recovery_file(Device* device) { |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 539 | std::vector<std::string> entries; |
| 540 | if (has_cache) { |
| 541 | for (int i = 0; i < KEEP_LOG_COUNT; i++) { |
| 542 | auto add_to_entries = [&](const char* filename) { |
| 543 | std::string log_file(filename); |
| 544 | if (i > 0) { |
| 545 | log_file += "." + std::to_string(i); |
Tao Bao | bef3971 | 2015-05-04 18:50:27 -0700 | [diff] [blame] | 546 | } |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 547 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 548 | if (ensure_path_mounted(log_file) == 0 && access(log_file.c_str(), R_OK) == 0) { |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 549 | entries.push_back(std::move(log_file)); |
| 550 | } |
| 551 | }; |
| 552 | |
| 553 | // Add LAST_LOG_FILE + LAST_LOG_FILE.x |
| 554 | add_to_entries(LAST_LOG_FILE); |
| 555 | |
| 556 | // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x |
| 557 | add_to_entries(LAST_KMSG_FILE); |
| 558 | } |
| 559 | } else { |
| 560 | // If cache partition is not found, view /tmp/recovery.log instead. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 561 | if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) { |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 562 | return; |
Tianjie Xu | a54f75e | 2016-08-17 12:02:46 -0700 | [diff] [blame] | 563 | } else { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 564 | entries.push_back(Paths::Get().temporary_log_file()); |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 565 | } |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 566 | } |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 567 | |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 568 | entries.push_back("Back"); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 569 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 570 | std::vector<std::string> headers{ "Select file to view" }; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 571 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 572 | size_t chosen_item = 0; |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 573 | while (true) { |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 574 | chosen_item = ui->ShowMenu( |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 575 | headers, entries, chosen_item, true, |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 576 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 577 | |
| 578 | // Handle WaitKey() interrupt. |
| 579 | if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) { |
| 580 | break; |
| 581 | } |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 582 | if (entries[chosen_item] == "Back") break; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 583 | |
Tao Bao | 1d156b9 | 2018-05-02 12:43:18 -0700 | [diff] [blame] | 584 | ui->ShowFile(entries[chosen_item]); |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 585 | } |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 588 | static void run_graphics_test() { |
| 589 | // Switch to graphics screen. |
| 590 | ui->ShowText(false); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 591 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 592 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
| 593 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 594 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 595 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 596 | ui->SetBackground(RecoveryUI::ERROR); |
| 597 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 598 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 599 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 600 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 601 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 602 | ui->SetBackground(RecoveryUI::ERASING); |
| 603 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 604 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 605 | // Calling SetBackground() after SetStage() to trigger a redraw. |
| 606 | ui->SetStage(1, 3); |
| 607 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 608 | sleep(1); |
| 609 | ui->SetStage(2, 3); |
| 610 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 611 | sleep(1); |
| 612 | ui->SetStage(3, 3); |
| 613 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 614 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 615 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 616 | ui->SetStage(-1, -1); |
| 617 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 618 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 619 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 620 | ui->ShowProgress(1.0, 10.0); |
| 621 | float fraction = 0.0; |
| 622 | for (size_t i = 0; i < 100; ++i) { |
| 623 | fraction += .01; |
| 624 | ui->SetProgress(fraction); |
| 625 | usleep(100000); |
| 626 | } |
| 627 | |
| 628 | ui->ShowText(true); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 631 | // Returns REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default, |
| 632 | // which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery. |
| 633 | static Device::BuiltinAction prompt_and_wait(Device* device, int status) { |
| 634 | for (;;) { |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 635 | finish_recovery(); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 636 | switch (status) { |
| 637 | case INSTALL_SUCCESS: |
| 638 | case INSTALL_NONE: |
| 639 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 640 | break; |
Doug Zongker | 6c8553d | 2012-09-24 10:40:47 -0700 | [diff] [blame] | 641 | |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 642 | case INSTALL_ERROR: |
| 643 | case INSTALL_CORRUPT: |
| 644 | ui->SetBackground(RecoveryUI::ERROR); |
| 645 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 646 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 647 | ui->SetProgressType(RecoveryUI::EMPTY); |
| 648 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 649 | size_t chosen_item = ui->ShowMenu( |
| 650 | {}, device->GetMenuItems(), 0, false, |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 651 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 652 | // Handle Interrupt key |
| 653 | if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) { |
| 654 | return Device::KEY_INTERRUPTED; |
| 655 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 656 | // Device-specific code may take some action here. It may return one of the core actions |
| 657 | // handled in the switch statement below. |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 658 | Device::BuiltinAction chosen_action = |
| 659 | (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT)) |
| 660 | ? Device::REBOOT |
| 661 | : device->InvokeMenuItem(chosen_item); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 662 | |
| 663 | bool should_wipe_cache = false; |
| 664 | switch (chosen_action) { |
| 665 | case Device::NO_ACTION: |
| 666 | break; |
| 667 | |
| 668 | case Device::REBOOT: |
| 669 | case Device::SHUTDOWN: |
| 670 | case Device::REBOOT_BOOTLOADER: |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 671 | case Device::ENTER_FASTBOOT: |
| 672 | case Device::ENTER_RECOVERY: |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 673 | return chosen_action; |
| 674 | |
| 675 | case Device::WIPE_DATA: |
| 676 | if (ui->IsTextVisible()) { |
| 677 | if (ask_to_wipe_data(device)) { |
| 678 | wipe_data(device); |
| 679 | } |
| 680 | } else { |
| 681 | wipe_data(device); |
| 682 | return Device::NO_ACTION; |
| 683 | } |
| 684 | break; |
| 685 | |
| 686 | case Device::WIPE_CACHE: |
| 687 | wipe_cache(ui->IsTextVisible(), device); |
| 688 | if (!ui->IsTextVisible()) return Device::NO_ACTION; |
| 689 | break; |
| 690 | |
| 691 | case Device::APPLY_ADB_SIDELOAD: |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 692 | case Device::APPLY_SDCARD: { |
| 693 | modified_flash = true; |
| 694 | bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); |
| 695 | if (adb) { |
| 696 | status = apply_from_adb(&should_wipe_cache); |
| 697 | } else { |
| 698 | status = ApplyFromSdcard(device, &should_wipe_cache, ui); |
| 699 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 700 | |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 701 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 702 | if (!wipe_cache(false, device)) { |
| 703 | status = INSTALL_ERROR; |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 704 | } |
| 705 | } |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 706 | |
| 707 | if (status != INSTALL_SUCCESS) { |
| 708 | ui->SetBackground(RecoveryUI::ERROR); |
| 709 | ui->Print("Installation aborted.\n"); |
| 710 | copy_logs(modified_flash, has_cache); |
| 711 | } else if (!ui->IsTextVisible()) { |
| 712 | return Device::NO_ACTION; // reboot if logs aren't visible |
| 713 | } else { |
| 714 | ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card"); |
| 715 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 716 | break; |
xunchang | 3cc23d5 | 2019-03-18 15:03:33 -0700 | [diff] [blame] | 717 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 718 | |
| 719 | case Device::VIEW_RECOVERY_LOGS: |
| 720 | choose_recovery_file(device); |
| 721 | break; |
| 722 | |
| 723 | case Device::RUN_GRAPHICS_TEST: |
| 724 | run_graphics_test(); |
| 725 | break; |
| 726 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 727 | case Device::RUN_LOCALE_TEST: { |
| 728 | ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui); |
Tao Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 729 | screen_ui->CheckBackgroundTextImages(); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 730 | break; |
| 731 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 732 | case Device::MOUNT_SYSTEM: |
Jiyong Park | 8b7af4c | 2018-06-01 11:58:54 +0900 | [diff] [blame] | 733 | // the system partition is mounted at /mnt/system |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 734 | if (ensure_path_mounted_at(get_system_root(), "/mnt/system") != -1) { |
Yifan Hong | 4932780 | 2018-11-26 14:59:09 -0800 | [diff] [blame] | 735 | ui->Print("Mounted /system.\n"); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 736 | } |
| 737 | break; |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 738 | |
| 739 | case Device::KEY_INTERRUPTED: |
| 740 | return Device::KEY_INTERRUPTED; |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 741 | } |
| 742 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 745 | static void print_property(const char* key, const char* name, void* /* cookie */) { |
| 746 | printf("%s=%s\n", key, name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 749 | void ui_print(const char* format, ...) { |
| 750 | std::string buffer; |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 751 | va_list ap; |
| 752 | va_start(ap, format); |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 753 | android::base::StringAppendV(&buffer, format, ap); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 754 | va_end(ap); |
| 755 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 756 | if (ui != nullptr) { |
| 757 | ui->Print("%s", buffer.c_str()); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 758 | } else { |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 759 | fputs(buffer.c_str(), stdout); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Tao Bao | 6d90a9d | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 763 | static bool is_battery_ok(int* required_battery_level) { |
Yifan Hong | e8e4c40 | 2017-11-08 14:56:03 -0800 | [diff] [blame] | 764 | using android::hardware::health::V1_0::BatteryStatus; |
Yifan Hong | 056538c | 2018-07-11 17:04:12 -0700 | [diff] [blame] | 765 | using android::hardware::health::V2_0::get_health_service; |
| 766 | using android::hardware::health::V2_0::IHealth; |
Yifan Hong | e8e4c40 | 2017-11-08 14:56:03 -0800 | [diff] [blame] | 767 | using android::hardware::health::V2_0::Result; |
| 768 | using android::hardware::health::V2_0::toString; |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 769 | |
Yifan Hong | 056538c | 2018-07-11 17:04:12 -0700 | [diff] [blame] | 770 | android::sp<IHealth> health = get_health_service(); |
Yifan Hong | e8e4c40 | 2017-11-08 14:56:03 -0800 | [diff] [blame] | 771 | |
Tao Bao | 6d90a9d | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 772 | static constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10; |
Yifan Hong | e8e4c40 | 2017-11-08 14:56:03 -0800 | [diff] [blame] | 773 | int wait_second = 0; |
| 774 | while (true) { |
| 775 | auto charge_status = BatteryStatus::UNKNOWN; |
Yifan Hong | 056538c | 2018-07-11 17:04:12 -0700 | [diff] [blame] | 776 | |
| 777 | if (health == nullptr) { |
| 778 | LOG(WARNING) << "no health implementation is found, assuming defaults"; |
| 779 | } else { |
| 780 | health |
| 781 | ->getChargeStatus([&charge_status](auto res, auto out_status) { |
| 782 | if (res == Result::SUCCESS) { |
| 783 | charge_status = out_status; |
| 784 | } |
| 785 | }) |
| 786 | .isOk(); // should not have transport error |
| 787 | } |
Yifan Hong | e8e4c40 | 2017-11-08 14:56:03 -0800 | [diff] [blame] | 788 | |
| 789 | // Treat unknown status as charged. |
| 790 | bool charged = (charge_status != BatteryStatus::DISCHARGING && |
| 791 | charge_status != BatteryStatus::NOT_CHARGING); |
| 792 | |
| 793 | Result res = Result::UNKNOWN; |
| 794 | int32_t capacity = INT32_MIN; |
Yifan Hong | 056538c | 2018-07-11 17:04:12 -0700 | [diff] [blame] | 795 | if (health != nullptr) { |
| 796 | health |
| 797 | ->getCapacity([&res, &capacity](auto out_res, auto out_capacity) { |
| 798 | res = out_res; |
| 799 | capacity = out_capacity; |
| 800 | }) |
| 801 | .isOk(); // should not have transport error |
| 802 | } |
Yifan Hong | e8e4c40 | 2017-11-08 14:56:03 -0800 | [diff] [blame] | 803 | |
Yifan Hong | 056538c | 2018-07-11 17:04:12 -0700 | [diff] [blame] | 804 | LOG(INFO) << "charge_status " << toString(charge_status) << ", charged " << charged |
| 805 | << ", status " << toString(res) << ", capacity " << capacity; |
Yifan Hong | e8e4c40 | 2017-11-08 14:56:03 -0800 | [diff] [blame] | 806 | // At startup, the battery drivers in devices like N5X/N6P take some time to load |
| 807 | // the battery profile. Before the load finishes, it reports value 50 as a fake |
| 808 | // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected |
| 809 | // to finish loading the battery profile earlier than 10 seconds after kernel startup. |
| 810 | if (res == Result::SUCCESS && capacity == 50) { |
| 811 | if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) { |
| 812 | sleep(1); |
| 813 | wait_second++; |
| 814 | continue; |
| 815 | } |
| 816 | } |
| 817 | // If we can't read battery percentage, it may be a device without battery. In this |
| 818 | // situation, use 100 as a fake battery percentage. |
| 819 | if (res != Result::SUCCESS) { |
| 820 | capacity = 100; |
| 821 | } |
Tao Bao | 6d90a9d | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 822 | |
| 823 | // GmsCore enters recovery mode to install package when having enough battery percentage. |
| 824 | // Normally, the threshold is 40% without charger and 20% with charger. So we should check |
| 825 | // battery with a slightly lower limitation. |
| 826 | static constexpr int BATTERY_OK_PERCENTAGE = 20; |
| 827 | static constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; |
| 828 | *required_battery_level = charged ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE; |
| 829 | return capacity >= *required_battery_level; |
| 830 | } |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 833 | // Set the retry count to |retry_count| in BCB. |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 834 | static void set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) { |
| 835 | std::vector<std::string> options; |
| 836 | for (const auto& arg : args) { |
| 837 | if (!android::base::StartsWith(arg, "--retry_count")) { |
| 838 | options.push_back(arg); |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 839 | } |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 840 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 841 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 842 | // Update the retry counter in BCB. |
| 843 | options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count)); |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 844 | std::string err; |
| 845 | if (!update_bootloader_message(options, &err)) { |
| 846 | LOG(ERROR) << err; |
| 847 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 850 | static bool bootreason_in_blacklist() { |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 851 | std::string bootreason = android::base::GetProperty("ro.boot.bootreason", ""); |
| 852 | if (!bootreason.empty()) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 853 | // More bootreasons can be found in "system/core/bootstat/bootstat.cpp". |
| 854 | static const std::vector<std::string> kBootreasonBlacklist{ |
| 855 | "kernel_panic", |
| 856 | "Panic", |
| 857 | }; |
| 858 | for (const auto& str : kBootreasonBlacklist) { |
| 859 | if (android::base::EqualsIgnoreCase(str, bootreason)) return true; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 860 | } |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 861 | } |
| 862 | return false; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 865 | static void log_failure_code(ErrorCode code, const std::string& update_package) { |
| 866 | std::vector<std::string> log_buffer = { |
| 867 | update_package, |
| 868 | "0", // install result |
| 869 | "error: " + std::to_string(code), |
| 870 | }; |
| 871 | std::string log_content = android::base::Join(log_buffer, "\n"); |
| 872 | const std::string& install_file = Paths::Get().temporary_install_file(); |
| 873 | if (!android::base::WriteStringToFile(log_content, install_file)) { |
| 874 | PLOG(ERROR) << "Failed to write " << install_file; |
| 875 | } |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 876 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 877 | // Also write the info into last_log. |
| 878 | LOG(INFO) << log_content; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Jerry Zhang | 6a64804 | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 881 | Device::BuiltinAction start_recovery(Device* device, const std::vector<std::string>& args) { |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 882 | static constexpr struct option OPTIONS[] = { |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 883 | { "fastboot", no_argument, nullptr, 0 }, |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 884 | { "fsck_unshare_blocks", no_argument, nullptr, 0 }, |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 885 | { "just_exit", no_argument, nullptr, 'x' }, |
| 886 | { "locale", required_argument, nullptr, 0 }, |
| 887 | { "prompt_and_wipe_data", no_argument, nullptr, 0 }, |
| 888 | { "reason", required_argument, nullptr, 0 }, |
| 889 | { "retry_count", required_argument, nullptr, 0 }, |
| 890 | { "security", no_argument, nullptr, 0 }, |
| 891 | { "show_text", no_argument, nullptr, 't' }, |
| 892 | { "shutdown_after", no_argument, nullptr, 0 }, |
| 893 | { "sideload", no_argument, nullptr, 0 }, |
| 894 | { "sideload_auto_reboot", no_argument, nullptr, 0 }, |
| 895 | { "update_package", required_argument, nullptr, 0 }, |
| 896 | { "wipe_ab", no_argument, nullptr, 0 }, |
| 897 | { "wipe_cache", no_argument, nullptr, 0 }, |
| 898 | { "wipe_data", no_argument, nullptr, 0 }, |
| 899 | { "wipe_package_size", required_argument, nullptr, 0 }, |
| 900 | { nullptr, 0, nullptr, 0 }, |
| 901 | }; |
| 902 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 903 | const char* update_package = nullptr; |
| 904 | bool should_wipe_data = false; |
| 905 | bool should_prompt_and_wipe_data = false; |
| 906 | bool should_wipe_cache = false; |
| 907 | bool should_wipe_ab = false; |
| 908 | size_t wipe_package_size = 0; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 909 | bool sideload = false; |
| 910 | bool sideload_auto_reboot = false; |
| 911 | bool just_exit = false; |
| 912 | bool shutdown_after = false; |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 913 | bool fsck_unshare_blocks = false; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 914 | int retry_count = 0; |
| 915 | bool security_update = false; |
Jerry Zhang | 2dea53e | 2018-05-02 17:15:03 -0700 | [diff] [blame] | 916 | std::string locale; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 917 | |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 918 | auto args_to_parse = StringVectorToNullTerminatedArray(args); |
| 919 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 920 | int arg; |
| 921 | int option_index; |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 922 | // Parse everything before the last element (which must be a nullptr). getopt_long(3) expects a |
| 923 | // null-terminated char* array, but without counting null as an arg (i.e. argv[argc] should be |
| 924 | // nullptr). |
| 925 | while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS, |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 926 | &option_index)) != -1) { |
| 927 | switch (arg) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 928 | case 't': |
Jerry Zhang | 6a64804 | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 929 | // Handled in recovery_main.cpp |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 930 | break; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 931 | case 'x': |
| 932 | just_exit = true; |
| 933 | break; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 934 | case 0: { |
| 935 | std::string option = OPTIONS[option_index].name; |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 936 | if (option == "fsck_unshare_blocks") { |
| 937 | fsck_unshare_blocks = true; |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 938 | } else if (option == "locale" || option == "fastboot") { |
Jerry Zhang | 6a64804 | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 939 | // Handled in recovery_main.cpp |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 940 | } else if (option == "prompt_and_wipe_data") { |
| 941 | should_prompt_and_wipe_data = true; |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 942 | } else if (option == "reason") { |
| 943 | reason = optarg; |
| 944 | } else if (option == "retry_count") { |
| 945 | android::base::ParseInt(optarg, &retry_count, 0); |
| 946 | } else if (option == "security") { |
| 947 | security_update = true; |
| 948 | } else if (option == "sideload") { |
| 949 | sideload = true; |
| 950 | } else if (option == "sideload_auto_reboot") { |
| 951 | sideload = true; |
| 952 | sideload_auto_reboot = true; |
| 953 | } else if (option == "shutdown_after") { |
| 954 | shutdown_after = true; |
| 955 | } else if (option == "update_package") { |
| 956 | update_package = optarg; |
| 957 | } else if (option == "wipe_ab") { |
| 958 | should_wipe_ab = true; |
| 959 | } else if (option == "wipe_cache") { |
| 960 | should_wipe_cache = true; |
| 961 | } else if (option == "wipe_data") { |
| 962 | should_wipe_data = true; |
| 963 | } else if (option == "wipe_package_size") { |
| 964 | android::base::ParseUint(optarg, &wipe_package_size); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 965 | } |
| 966 | break; |
| 967 | } |
| 968 | case '?': |
| 969 | LOG(ERROR) << "Invalid command argument"; |
| 970 | continue; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 971 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 972 | } |
Jerry Zhang | 49fd5d2 | 2018-05-17 12:54:41 -0700 | [diff] [blame] | 973 | optind = 1; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 974 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 975 | printf("stage is [%s]\n", stage.c_str()); |
| 976 | printf("reason is [%s]\n", reason); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 977 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 978 | // Set background string to "installing security update" for security update, |
| 979 | // otherwise set it to "installing system update". |
| 980 | ui->SetSystemUpdateText(security_update); |
| 981 | |
| 982 | int st_cur, st_max; |
| 983 | if (!stage.empty() && sscanf(stage.c_str(), "%d/%d", &st_cur, &st_max) == 2) { |
| 984 | ui->SetStage(st_cur, st_max); |
| 985 | } |
| 986 | |
Jerry Zhang | 0e577ee | 2018-05-07 11:21:10 -0700 | [diff] [blame] | 987 | std::vector<std::string> title_lines = |
| 988 | android::base::Split(android::base::GetProperty("ro.bootimage.build.fingerprint", ""), ":"); |
| 989 | title_lines.insert(std::begin(title_lines), "Android Recovery"); |
| 990 | ui->SetTitle(title_lines); |
| 991 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 992 | ui->ResetKeyInterruptStatus(); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 993 | device->StartRecovery(); |
| 994 | |
| 995 | printf("Command:"); |
| 996 | for (const auto& arg : args) { |
| 997 | printf(" \"%s\"", arg.c_str()); |
| 998 | } |
| 999 | printf("\n\n"); |
| 1000 | |
| 1001 | property_list(print_property, nullptr); |
| 1002 | printf("\n"); |
| 1003 | |
| 1004 | ui->Print("Supported API: %d\n", kRecoveryApiVersion); |
| 1005 | |
| 1006 | int status = INSTALL_SUCCESS; |
| 1007 | |
| 1008 | if (update_package != nullptr) { |
| 1009 | // It's not entirely true that we will modify the flash. But we want |
| 1010 | // to log the update attempt since update_package is non-NULL. |
| 1011 | modified_flash = true; |
| 1012 | |
Tao Bao | 6d90a9d | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 1013 | int required_battery_level; |
| 1014 | if (retry_count == 0 && !is_battery_ok(&required_battery_level)) { |
| 1015 | ui->Print("battery capacity is not enough for installing package: %d%% needed\n", |
| 1016 | required_battery_level); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1017 | // Log the error code to last_install when installation skips due to |
| 1018 | // low battery. |
| 1019 | log_failure_code(kLowBattery, update_package); |
| 1020 | status = INSTALL_SKIPPED; |
Tianjie Xu | a6f49bd | 2018-03-26 14:32:11 -0700 | [diff] [blame] | 1021 | } else if (retry_count == 0 && bootreason_in_blacklist()) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1022 | // Skip update-on-reboot when bootreason is kernel_panic or similar |
| 1023 | ui->Print("bootreason is in the blacklist; skip OTA installation\n"); |
| 1024 | log_failure_code(kBootreasonInBlacklist, update_package); |
| 1025 | status = INSTALL_SKIPPED; |
Dmitri Plotnikov | 8706a98 | 2017-04-18 08:28:26 -0700 | [diff] [blame] | 1026 | } else { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1027 | // It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later |
| 1028 | // identify the interrupted update due to unexpected reboots. |
| 1029 | if (retry_count == 0) { |
| 1030 | set_retry_bootloader_message(retry_count + 1, args); |
Tao Bao | 7022f33 | 2017-07-25 09:52:36 -0700 | [diff] [blame] | 1031 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1032 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1033 | status = install_package(update_package, &should_wipe_cache, true, retry_count); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1034 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 1035 | wipe_cache(false, device); |
| 1036 | } |
| 1037 | if (status != INSTALL_SUCCESS) { |
| 1038 | ui->Print("Installation aborted.\n"); |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 1039 | |
| 1040 | // When I/O error or bspatch/imgpatch error happens, reboot and retry installation |
| 1041 | // RETRY_LIMIT times before we abandon this OTA update. |
| 1042 | static constexpr int RETRY_LIMIT = 4; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1043 | if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) { |
Jerry Zhang | cadf4ed | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 1044 | copy_logs(modified_flash, has_cache); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1045 | retry_count += 1; |
| 1046 | set_retry_bootloader_message(retry_count, args); |
| 1047 | // Print retry count on screen. |
| 1048 | ui->Print("Retry attempt %d\n", retry_count); |
| 1049 | |
| 1050 | // Reboot and retry the update |
| 1051 | if (!reboot("reboot,recovery")) { |
| 1052 | ui->Print("Reboot failed\n"); |
| 1053 | } else { |
| 1054 | while (true) { |
| 1055 | pause(); |
| 1056 | } |
| 1057 | } |
Tianjie Xu | d9d1629 | 2017-04-20 18:08:21 -0700 | [diff] [blame] | 1058 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1059 | // If this is an eng or userdebug build, then automatically |
| 1060 | // turn the text display on if the script fails so the error |
| 1061 | // message is visible. |
| 1062 | if (is_ro_debuggable()) { |
| 1063 | ui->ShowText(true); |
Tao Bao | c679f93 | 2015-03-30 09:43:49 -0700 | [diff] [blame] | 1064 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1065 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 1066 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1067 | } else if (should_wipe_data) { |
| 1068 | if (!wipe_data(device)) { |
| 1069 | status = INSTALL_ERROR; |
Doug Zongker | b1d1263 | 2014-03-18 10:32:12 -0700 | [diff] [blame] | 1070 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1071 | } else if (should_prompt_and_wipe_data) { |
Tao Bao | a5bbcb9 | 2018-09-17 14:32:47 -0700 | [diff] [blame] | 1072 | // Trigger the logging to capture the cause, even if user chooses to not wipe data. |
| 1073 | modified_flash = true; |
| 1074 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1075 | ui->ShowText(true); |
| 1076 | ui->SetBackground(RecoveryUI::ERROR); |
Hridya Valsaraju | eb6f13a | 2018-09-12 10:25:01 -0700 | [diff] [blame] | 1077 | status = prompt_and_wipe_data(device); |
| 1078 | if (status != INSTALL_KEY_INTERRUPTED) { |
| 1079 | ui->ShowText(false); |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 1080 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1081 | } else if (should_wipe_cache) { |
| 1082 | if (!wipe_cache(false, device)) { |
| 1083 | status = INSTALL_ERROR; |
| 1084 | } |
| 1085 | } else if (should_wipe_ab) { |
| 1086 | if (!wipe_ab_device(wipe_package_size)) { |
| 1087 | status = INSTALL_ERROR; |
| 1088 | } |
| 1089 | } else if (sideload) { |
| 1090 | // 'adb reboot sideload' acts the same as user presses key combinations |
| 1091 | // to enter the sideload mode. When 'sideload-auto-reboot' is used, text |
| 1092 | // display will NOT be turned on by default. And it will reboot after |
| 1093 | // sideload finishes even if there are errors. Unless one turns on the |
| 1094 | // text display during the installation. This is to enable automated |
| 1095 | // testing. |
| 1096 | if (!sideload_auto_reboot) { |
| 1097 | ui->ShowText(true); |
| 1098 | } |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1099 | status = apply_from_adb(&should_wipe_cache); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1100 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 1101 | if (!wipe_cache(false, device)) { |
| 1102 | status = INSTALL_ERROR; |
| 1103 | } |
| 1104 | } |
| 1105 | ui->Print("\nInstall from ADB complete (status: %d).\n", status); |
| 1106 | if (sideload_auto_reboot) { |
| 1107 | ui->Print("Rebooting automatically.\n"); |
| 1108 | } |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame] | 1109 | } else if (fsck_unshare_blocks) { |
| 1110 | if (!do_fsck_unshare_blocks()) { |
| 1111 | status = INSTALL_ERROR; |
| 1112 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1113 | } else if (!just_exit) { |
| 1114 | // If this is an eng or userdebug build, automatically turn on the text display if no command |
| 1115 | // is specified. Note that this should be called before setting the background to avoid |
| 1116 | // flickering the background image. |
| 1117 | if (is_ro_debuggable()) { |
| 1118 | ui->ShowText(true); |
| 1119 | } |
| 1120 | status = INSTALL_NONE; // No command specified |
| 1121 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 1122 | } |
| 1123 | |
| 1124 | if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) { |
| 1125 | ui->SetBackground(RecoveryUI::ERROR); |
| 1126 | if (!ui->IsTextVisible()) { |
| 1127 | sleep(5); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT; |
| 1132 | // 1. If the recovery menu is visible, prompt and wait for commands. |
| 1133 | // 2. If the state is INSTALL_NONE, wait for commands. (i.e. In user build, manually reboot into |
| 1134 | // recovery to sideload a package.) |
| 1135 | // 3. sideload_auto_reboot is an option only available in user-debug build, reboot the device |
| 1136 | // without waiting. |
| 1137 | // 4. In all other cases, reboot the device. Therefore, normal users will observe the device |
| 1138 | // reboot after it shows the "error" screen for 5s. |
| 1139 | if ((status == INSTALL_NONE && !sideload_auto_reboot) || ui->IsTextVisible()) { |
| 1140 | Device::BuiltinAction temp = prompt_and_wait(device, status); |
| 1141 | if (temp != Device::NO_ACTION) { |
| 1142 | after = temp; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | // Save logs and clean up before rebooting or shutting down. |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 1147 | finish_recovery(); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1148 | |
Jerry Zhang | 6a64804 | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 1149 | return after; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1150 | } |