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 | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 17 | #include "recovery.h" |
Tao Bao | 6d99d4b | 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> |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 20 | #include <dirent.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <getopt.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 24 | #include <inttypes.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <limits.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 26 | #include <linux/fs.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | #include <linux/input.h> |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 28 | #include <stdarg.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 32 | #include <sys/stat.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | #include <sys/types.h> |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 34 | #include <sys/wait.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 37 | #include <algorithm> |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 38 | #include <functional> |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 39 | #include <memory> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 40 | #include <string> |
| 41 | #include <vector> |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 43 | #include <android-base/file.h> |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 44 | #include <android-base/logging.h> |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 45 | #include <android-base/parseint.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 46 | #include <android-base/properties.h> |
Elliott Hughes | 4b166f0 | 2015-12-04 15:30:20 -0800 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 48 | #include <android-base/strings.h> |
| 49 | #include <android-base/unique_fd.h> |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 50 | #include <bootloader_message/bootloader_message.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 51 | #include <cutils/properties.h> /* for property_list */ |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 52 | #include <healthd/BatteryMonitor.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 53 | #include <ziparchive/zip_archive.h> |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 54 | |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 55 | #include "adb_install.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 56 | #include "common.h" |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 57 | #include "device.h" |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame^] | 58 | #include "fsck_unshare_blocks.h" |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 59 | #include "fuse_sdcard_provider.h" |
| 60 | #include "fuse_sideload.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 61 | #include "install.h" |
Jerry Zhang | 152933a | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 62 | #include "logging.h" |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 63 | #include "otautil/dirutil.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 64 | #include "otautil/error_code.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 65 | #include "otautil/paths.h" |
Tao Bao | 2c52639 | 2018-05-03 23:01:13 -0700 | [diff] [blame] | 66 | #include "otautil/sysutil.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 67 | #include "roots.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 68 | #include "screen_ui.h" |
Tianjie Xu | e113e4d | 2016-10-21 17:46:13 -0700 | [diff] [blame] | 69 | #include "ui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 70 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 71 | static constexpr const char* CACHE_LOG_DIR = "/cache/recovery"; |
| 72 | static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 73 | static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; |
| 74 | static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 75 | static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale"; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 76 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 77 | static constexpr const char* CACHE_ROOT = "/cache"; |
| 78 | static constexpr const char* DATA_ROOT = "/data"; |
| 79 | static constexpr const char* METADATA_ROOT = "/metadata"; |
| 80 | static constexpr const char* SDCARD_ROOT = "/sdcard"; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 81 | |
Tao Bao | bd0ddcd | 2017-05-04 13:03:18 -0700 | [diff] [blame] | 82 | // 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. |
| 84 | static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions."); |
| 85 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 86 | bool modified_flash = false; |
Tao Bao | a8d72bc | 2016-12-25 18:46:50 -0800 | [diff] [blame] | 87 | std::string stage; |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 88 | const char* reason = nullptr; |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 89 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | /* |
| 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 94 | * |
| 95 | * The arguments which may be supplied in the recovery.command file: |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 96 | * --update_package=path - verify install an OTA package file |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 97 | * --wipe_data - erase user data (and cache), then reboot |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 98 | * --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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 100 | * --wipe_cache - wipe cache (but not user data), then reboot |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 101 | * --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] | 102 | * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 103 | * --just_exit - do nothing; exit and reboot |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 104 | * |
| 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 Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 115 | * 5. erase_volume() reformats /data |
| 116 | * 6. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 117 | * 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 Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 123 | * 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] | 124 | * 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 Bao | c033639 | 2016-12-13 22:29:49 -0800 | [diff] [blame] | 133 | * 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] | 134 | */ |
| 135 | |
Elliott Hughes | f14af80 | 2015-02-10 14:46:14 -0800 | [diff] [blame] | 136 | bool is_ro_debuggable() { |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 137 | return android::base::GetBoolProperty("ro.debuggable", false); |
Elliott Hughes | f14af80 | 2015-02-10 14:46:14 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 140 | // Set the BCB to reboot back into recovery (it won't resume the install from |
| 141 | // sdcard though). |
| 142 | static 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 Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 148 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 149 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 150 | // Clear the recovery command and prepare to boot a (hopefully working) system, |
Tianjie Xu | c14d95d | 2016-03-24 11:50:34 -0700 | [diff] [blame] | 151 | // 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 Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 153 | static void finish_recovery() { |
| 154 | std::string locale = ui->GetLocale(); |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 155 | // 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 Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 163 | } |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 164 | } |
Doug Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 165 | |
Jerry Zhang | 152933a | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 166 | copy_logs(modified_flash, has_cache); |
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 | // 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 Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 178 | } |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 179 | ensure_path_unmounted(CACHE_ROOT); |
| 180 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 181 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 182 | sync(); // For good measure. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 185 | struct saved_log_file { |
| 186 | std::string name; |
| 187 | struct stat sb; |
| 188 | std::string data; |
| 189 | }; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 190 | |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 191 | static bool erase_volume(const char* volume) { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 192 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
| 193 | bool is_data = (strcmp(volume, DATA_ROOT) == 0); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 194 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 195 | ui->SetBackground(RecoveryUI::ERASING); |
| 196 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 197 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 198 | std::vector<saved_log_file> log_files; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 199 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 200 | 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 Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 205 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 206 | ensure_path_mounted(volume); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 207 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 208 | 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 Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 220 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 221 | |
| 222 | std::string data(sb.st_size, '\0'); |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 223 | FILE* f = fopen(path.c_str(), "rbe"); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 224 | fread(&data[0], 1, data.size(), f); |
| 225 | fclose(f); |
| 226 | |
| 227 | log_files.emplace_back(saved_log_file{ path, sb, data }); |
| 228 | } |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 229 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 230 | } |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 231 | } else { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 232 | if (errno != ENOENT) { |
| 233 | PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR; |
| 234 | } |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 235 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 236 | } |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 237 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 238 | ui->Print("Formatting %s...\n", volume); |
| 239 | |
| 240 | ensure_path_unmounted(volume); |
| 241 | |
| 242 | int result; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 243 | if (is_data && reason && strcmp(reason, "convert_fbe") == 0) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 244 | 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 Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 248 | if (mkdir(CONVERT_FBE_DIR, 0700) != 0) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 249 | PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR; |
| 250 | return false; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 251 | } |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 252 | FILE* f = fopen(CONVERT_FBE_FILE, "wbe"); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 253 | if (!f) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 254 | PLOG(ERROR) << "Failed to convert to file encryption"; |
| 255 | return false; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 256 | } |
| 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 Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 268 | mkdir_recursively(CACHE_LOG_DIR, 0777, false, sehandle) == 0) { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 269 | 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 Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 273 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 274 | } |
| 275 | } else { |
| 276 | PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR; |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 279 | // 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 Zhang | 152933a | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 282 | reset_tmplog_offset(); |
| 283 | copy_logs(modified_flash, has_cache); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return (result == 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 289 | // Returns the selected filename, or an empty string. |
| 290 | static std::string browse_directory(const std::string& path, Device* device) { |
| 291 | ensure_path_mounted(path.c_str()); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 292 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 293 | 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 Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 298 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 299 | std::vector<std::string> dirs; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 300 | std::vector<std::string> entries{ "../" }; // "../" is always the first entry. |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 301 | |
| 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 Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 311 | entries.push_back(name); |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | std::sort(dirs.begin(), dirs.end()); |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 316 | std::sort(entries.begin(), entries.end()); |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 317 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 318 | // Append dirs to the entries list. |
| 319 | entries.insert(entries.end(), dirs.begin(), dirs.end()); |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 320 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 321 | std::vector<std::string> headers{ "Choose a package to install:", path }; |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 322 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 323 | size_t chosen_item = 0; |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 324 | while (true) { |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 325 | chosen_item = ui->ShowMenu( |
| 326 | headers, entries, chosen_item, true, |
| 327 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 328 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 329 | const std::string& item = entries[chosen_item]; |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 330 | if (chosen_item == 0) { |
| 331 | // Go up but continue browsing (if the caller is browse_directory). |
| 332 | return ""; |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 335 | std::string new_path = path + "/" + item; |
| 336 | if (new_path.back() == '/') { |
| 337 | // Recurse down into a subdirectory. |
| 338 | new_path.pop_back(); |
| 339 | std::string result = browse_directory(new_path, device); |
| 340 | if (!result.empty()) return result; |
| 341 | } else { |
| 342 | // Selected a zip file: return the path to the caller. |
| 343 | return new_path; |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 344 | } |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 345 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 346 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 347 | // Unreachable. |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 350 | static bool yes_no(Device* device, const char* question1, const char* question2) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 351 | std::vector<std::string> headers{ question1, question2 }; |
| 352 | std::vector<std::string> items{ " No", " Yes" }; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 353 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 354 | size_t chosen_item = ui->ShowMenu( |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 355 | headers, items, 0, true, |
| 356 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
| 357 | return (chosen_item == 1); |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 358 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 359 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 360 | static bool ask_to_wipe_data(Device* device) { |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 361 | return yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!"); |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 362 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 363 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 364 | // Return true on success. |
| 365 | static bool wipe_data(Device* device) { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 366 | modified_flash = true; |
| 367 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 368 | ui->Print("\n-- Wiping data...\n"); |
Paul Crowley | 3b4d516 | 2016-06-08 13:51:41 -0700 | [diff] [blame] | 369 | bool success = device->PreWipeData(); |
| 370 | if (success) { |
| 371 | success &= erase_volume(DATA_ROOT); |
| 372 | if (has_cache) { |
| 373 | success &= erase_volume(CACHE_ROOT); |
| 374 | } |
| 375 | if (volume_for_mount_point(METADATA_ROOT) != nullptr) { |
| 376 | success &= erase_volume(METADATA_ROOT); |
| 377 | } |
| 378 | } |
| 379 | if (success) { |
| 380 | success &= device->PostWipeData(); |
| 381 | } |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 382 | ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); |
| 383 | return success; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 386 | static bool prompt_and_wipe_data(Device* device) { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 387 | // Use a single string and let ScreenRecoveryUI handles the wrapping. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 388 | std::vector<std::string> headers{ |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 389 | "Can't load Android system. Your data may be corrupt. " |
| 390 | "If you continue to get this message, you may need to " |
| 391 | "perform a factory data reset and erase all user data " |
Paul Crowley | 31ac0c6 | 2017-03-23 12:32:40 -0700 | [diff] [blame] | 392 | "stored on this device.", |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 393 | }; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 394 | // clang-format off |
| 395 | std::vector<std::string> items { |
Paul Crowley | 31ac0c6 | 2017-03-23 12:32:40 -0700 | [diff] [blame] | 396 | "Try again", |
| 397 | "Factory data reset", |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 398 | }; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 399 | // clang-format on |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 400 | for (;;) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 401 | size_t chosen_item = ui->ShowMenu( |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 402 | headers, items, 0, true, |
| 403 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 404 | if (chosen_item != 1) { |
| 405 | return true; // Just reboot, no wipe; not a failure, user asked for it |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 406 | } |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 407 | if (ask_to_wipe_data(device)) { |
| 408 | return wipe_data(device); |
| 409 | } |
| 410 | } |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Tao Bao | e39a9bc | 2015-03-31 12:19:05 -0700 | [diff] [blame] | 413 | // Return true on success. |
| 414 | static bool wipe_cache(bool should_confirm, Device* device) { |
Tao Bao | 26112e5 | 2016-02-25 12:29:40 -0800 | [diff] [blame] | 415 | if (!has_cache) { |
| 416 | ui->Print("No /cache partition found.\n"); |
| 417 | return false; |
| 418 | } |
| 419 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 420 | 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] | 421 | return false; |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 424 | modified_flash = true; |
| 425 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 426 | ui->Print("\n-- Wiping cache...\n"); |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 427 | bool success = erase_volume("/cache"); |
| 428 | ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); |
| 429 | return success; |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 432 | // Secure-wipe a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with |
| 433 | // BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT. |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 434 | static bool secure_wipe_partition(const std::string& partition) { |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 435 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY))); |
| 436 | if (fd == -1) { |
| 437 | PLOG(ERROR) << "Failed to open \"" << partition << "\""; |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | uint64_t range[2] = { 0, 0 }; |
| 442 | if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) { |
| 443 | PLOG(ERROR) << "Failed to get partition size"; |
| 444 | return false; |
| 445 | } |
| 446 | LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1]; |
| 447 | |
| 448 | LOG(INFO) << " Trying BLKSECDISCARD..."; |
| 449 | if (ioctl(fd, BLKSECDISCARD, &range) == -1) { |
| 450 | PLOG(WARNING) << " Failed"; |
| 451 | |
| 452 | // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT. |
| 453 | unsigned int zeroes; |
| 454 | if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) { |
| 455 | LOG(INFO) << " Trying BLKDISCARD..."; |
| 456 | if (ioctl(fd, BLKDISCARD, &range) == -1) { |
| 457 | PLOG(ERROR) << " Failed"; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 458 | return false; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 459 | } |
| 460 | } else { |
| 461 | LOG(INFO) << " Trying BLKZEROOUT..."; |
| 462 | if (ioctl(fd, BLKZEROOUT, &range) == -1) { |
| 463 | PLOG(ERROR) << " Failed"; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 464 | return false; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 465 | } |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 466 | } |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 467 | } |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 468 | |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 469 | LOG(INFO) << " Done"; |
| 470 | return true; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 473 | // Check if the wipe package matches expectation: |
| 474 | // 1. verify the package. |
| 475 | // 2. check metadata (ota-type, pre-device and serial number if having one). |
| 476 | static bool check_wipe_package(size_t wipe_package_size) { |
| 477 | if (wipe_package_size == 0) { |
| 478 | LOG(ERROR) << "wipe_package_size is zero"; |
| 479 | return false; |
| 480 | } |
| 481 | std::string wipe_package; |
| 482 | std::string err_str; |
| 483 | if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) { |
| 484 | PLOG(ERROR) << "Failed to read wipe package"; |
| 485 | return false; |
| 486 | } |
| 487 | if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()), |
| 488 | wipe_package.size())) { |
| 489 | LOG(ERROR) << "Failed to verify package"; |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | // Extract metadata |
| 494 | ZipArchiveHandle zip; |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 495 | int err = OpenArchiveFromMemory(static_cast<void*>(&wipe_package[0]), wipe_package.size(), |
| 496 | "wipe_package", &zip); |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 497 | if (err != 0) { |
| 498 | LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err); |
| 499 | return false; |
| 500 | } |
| 501 | std::string metadata; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 502 | if (!read_metadata_from_package(zip, &metadata)) { |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 503 | CloseArchive(zip); |
| 504 | return false; |
| 505 | } |
| 506 | CloseArchive(zip); |
| 507 | |
| 508 | // Check metadata |
| 509 | std::vector<std::string> lines = android::base::Split(metadata, "\n"); |
| 510 | bool ota_type_matched = false; |
| 511 | bool device_type_matched = false; |
| 512 | bool has_serial_number = false; |
| 513 | bool serial_number_matched = false; |
| 514 | for (const auto& line : lines) { |
| 515 | if (line == "ota-type=BRICK") { |
| 516 | ota_type_matched = true; |
| 517 | } else if (android::base::StartsWith(line, "pre-device=")) { |
| 518 | std::string device_type = line.substr(strlen("pre-device=")); |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 519 | std::string real_device_type = android::base::GetProperty("ro.build.product", ""); |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 520 | device_type_matched = (device_type == real_device_type); |
| 521 | } else if (android::base::StartsWith(line, "serialno=")) { |
| 522 | std::string serial_no = line.substr(strlen("serialno=")); |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 523 | std::string real_serial_no = android::base::GetProperty("ro.serialno", ""); |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 524 | has_serial_number = true; |
| 525 | serial_number_matched = (serial_no == real_serial_no); |
| 526 | } |
| 527 | } |
| 528 | return ota_type_matched && device_type_matched && (!has_serial_number || serial_number_matched); |
| 529 | } |
| 530 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 531 | // 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] | 532 | static bool wipe_ab_device(size_t wipe_package_size) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 533 | ui->SetBackground(RecoveryUI::ERASING); |
| 534 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 535 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 536 | if (!check_wipe_package(wipe_package_size)) { |
| 537 | LOG(ERROR) << "Failed to verify wipe package"; |
| 538 | return false; |
| 539 | } |
| 540 | static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe"; |
| 541 | std::string partition_list; |
| 542 | if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) { |
| 543 | LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\""; |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | std::vector<std::string> lines = android::base::Split(partition_list, "\n"); |
| 548 | for (const std::string& line : lines) { |
| 549 | std::string partition = android::base::Trim(line); |
| 550 | // Ignore '#' comment or empty lines. |
| 551 | if (android::base::StartsWith(partition, "#") || partition.empty()) { |
| 552 | continue; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 555 | // Proceed anyway even if it fails to wipe some partition. |
| 556 | secure_wipe_partition(partition); |
| 557 | } |
| 558 | return true; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 561 | static void choose_recovery_file(Device* device) { |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 562 | std::vector<std::string> entries; |
| 563 | if (has_cache) { |
| 564 | for (int i = 0; i < KEEP_LOG_COUNT; i++) { |
| 565 | auto add_to_entries = [&](const char* filename) { |
| 566 | std::string log_file(filename); |
| 567 | if (i > 0) { |
| 568 | log_file += "." + std::to_string(i); |
Tao Bao | bef3971 | 2015-05-04 18:50:27 -0700 | [diff] [blame] | 569 | } |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 570 | |
| 571 | if (ensure_path_mounted(log_file.c_str()) == 0 && access(log_file.c_str(), R_OK) == 0) { |
| 572 | entries.push_back(std::move(log_file)); |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | // Add LAST_LOG_FILE + LAST_LOG_FILE.x |
| 577 | add_to_entries(LAST_LOG_FILE); |
| 578 | |
| 579 | // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x |
| 580 | add_to_entries(LAST_KMSG_FILE); |
| 581 | } |
| 582 | } else { |
| 583 | // If cache partition is not found, view /tmp/recovery.log instead. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 584 | if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) { |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 585 | return; |
Tianjie Xu | a54f75e | 2016-08-17 12:02:46 -0700 | [diff] [blame] | 586 | } else { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 587 | entries.push_back(Paths::Get().temporary_log_file()); |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 588 | } |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 589 | } |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 590 | |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 591 | entries.push_back("Back"); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 592 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 593 | std::vector<std::string> headers{ "Select file to view" }; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 594 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 595 | size_t chosen_item = 0; |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 596 | while (true) { |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 597 | chosen_item = ui->ShowMenu( |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 598 | headers, entries, chosen_item, true, |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 599 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 600 | if (entries[chosen_item] == "Back") break; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 601 | |
Tao Bao | 1d156b9 | 2018-05-02 12:43:18 -0700 | [diff] [blame] | 602 | ui->ShowFile(entries[chosen_item]); |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 603 | } |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 606 | static void run_graphics_test() { |
| 607 | // Switch to graphics screen. |
| 608 | ui->ShowText(false); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 609 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 610 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
| 611 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 612 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 613 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 614 | ui->SetBackground(RecoveryUI::ERROR); |
| 615 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 616 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 617 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 618 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 619 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 620 | ui->SetBackground(RecoveryUI::ERASING); |
| 621 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 622 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 623 | // Calling SetBackground() after SetStage() to trigger a redraw. |
| 624 | ui->SetStage(1, 3); |
| 625 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 626 | sleep(1); |
| 627 | ui->SetStage(2, 3); |
| 628 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 629 | sleep(1); |
| 630 | ui->SetStage(3, 3); |
| 631 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 632 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 633 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 634 | ui->SetStage(-1, -1); |
| 635 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 636 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 637 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 638 | ui->ShowProgress(1.0, 10.0); |
| 639 | float fraction = 0.0; |
| 640 | for (size_t i = 0; i < 100; ++i) { |
| 641 | fraction += .01; |
| 642 | ui->SetProgress(fraction); |
| 643 | usleep(100000); |
| 644 | } |
| 645 | |
| 646 | ui->ShowText(true); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 649 | // How long (in seconds) we wait for the fuse-provided package file to |
| 650 | // appear, before timing out. |
| 651 | #define SDCARD_INSTALL_TIMEOUT 10 |
| 652 | |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 653 | static int apply_from_sdcard(Device* device, bool* wipe_cache) { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 654 | modified_flash = true; |
| 655 | |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 656 | if (ensure_path_mounted(SDCARD_ROOT) != 0) { |
| 657 | ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT); |
| 658 | return INSTALL_ERROR; |
| 659 | } |
| 660 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 661 | std::string path = browse_directory(SDCARD_ROOT, device); |
| 662 | if (path.empty()) { |
Elliott Hughes | 018ed31 | 2015-04-08 16:51:36 -0700 | [diff] [blame] | 663 | ui->Print("\n-- No package file selected.\n"); |
caozhiyuan | b4effb9 | 2015-06-10 16:46:38 +0800 | [diff] [blame] | 664 | ensure_path_unmounted(SDCARD_ROOT); |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 665 | return INSTALL_ERROR; |
| 666 | } |
| 667 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 668 | ui->Print("\n-- Install %s ...\n", path.c_str()); |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 669 | set_sdcard_update_bootloader_message(); |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 670 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 671 | // We used to use fuse in a thread as opposed to a process. Since accessing |
| 672 | // through fuse involves going from kernel to userspace to kernel, it leads |
| 673 | // to deadlock when a page fault occurs. (Bug: 26313124) |
| 674 | pid_t child; |
| 675 | if ((child = fork()) == 0) { |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 676 | bool status = start_sdcard_fuse(path.c_str()); |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 677 | |
| 678 | _exit(status ? EXIT_SUCCESS : EXIT_FAILURE); |
| 679 | } |
| 680 | |
| 681 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child |
| 682 | // process is ready. |
| 683 | int result = INSTALL_ERROR; |
| 684 | int status; |
| 685 | bool waited = false; |
| 686 | for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) { |
| 687 | if (waitpid(child, &status, WNOHANG) == -1) { |
| 688 | result = INSTALL_ERROR; |
| 689 | waited = true; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | struct stat sb; |
| 694 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) { |
| 695 | if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) { |
| 696 | sleep(1); |
| 697 | continue; |
| 698 | } else { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 699 | LOG(ERROR) << "Timed out waiting for the fuse-provided package."; |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 700 | result = INSTALL_ERROR; |
| 701 | kill(child, SIGKILL); |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 706 | result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/); |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 707 | break; |
| 708 | } |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 709 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 710 | if (!waited) { |
| 711 | // Calling stat() on this magic filename signals the fuse |
| 712 | // filesystem to shut down. |
| 713 | struct stat sb; |
| 714 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb); |
| 715 | |
| 716 | waitpid(child, &status, 0); |
| 717 | } |
| 718 | |
| 719 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 720 | LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status); |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 723 | ensure_path_unmounted(SDCARD_ROOT); |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 724 | return result; |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 727 | // Returns REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default, |
| 728 | // which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery. |
| 729 | static Device::BuiltinAction prompt_and_wait(Device* device, int status) { |
| 730 | for (;;) { |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 731 | finish_recovery(); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 732 | switch (status) { |
| 733 | case INSTALL_SUCCESS: |
| 734 | case INSTALL_NONE: |
| 735 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 736 | break; |
Doug Zongker | 6c8553d | 2012-09-24 10:40:47 -0700 | [diff] [blame] | 737 | |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 738 | case INSTALL_ERROR: |
| 739 | case INSTALL_CORRUPT: |
| 740 | ui->SetBackground(RecoveryUI::ERROR); |
| 741 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 742 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 743 | ui->SetProgressType(RecoveryUI::EMPTY); |
| 744 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 745 | size_t chosen_item = ui->ShowMenu( |
| 746 | {}, device->GetMenuItems(), 0, false, |
Tao Bao | 3aec696 | 2018-04-20 09:24:58 -0700 | [diff] [blame] | 747 | std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 748 | |
| 749 | // Device-specific code may take some action here. It may return one of the core actions |
| 750 | // handled in the switch statement below. |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 751 | Device::BuiltinAction chosen_action = (chosen_item == static_cast<size_t>(-1)) |
| 752 | ? Device::REBOOT |
| 753 | : device->InvokeMenuItem(chosen_item); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 754 | |
| 755 | bool should_wipe_cache = false; |
| 756 | switch (chosen_action) { |
| 757 | case Device::NO_ACTION: |
| 758 | break; |
| 759 | |
| 760 | case Device::REBOOT: |
| 761 | case Device::SHUTDOWN: |
| 762 | case Device::REBOOT_BOOTLOADER: |
| 763 | return chosen_action; |
| 764 | |
| 765 | case Device::WIPE_DATA: |
| 766 | if (ui->IsTextVisible()) { |
| 767 | if (ask_to_wipe_data(device)) { |
| 768 | wipe_data(device); |
| 769 | } |
| 770 | } else { |
| 771 | wipe_data(device); |
| 772 | return Device::NO_ACTION; |
| 773 | } |
| 774 | break; |
| 775 | |
| 776 | case Device::WIPE_CACHE: |
| 777 | wipe_cache(ui->IsTextVisible(), device); |
| 778 | if (!ui->IsTextVisible()) return Device::NO_ACTION; |
| 779 | break; |
| 780 | |
| 781 | case Device::APPLY_ADB_SIDELOAD: |
| 782 | case Device::APPLY_SDCARD: |
| 783 | { |
| 784 | bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); |
| 785 | if (adb) { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 786 | status = apply_from_adb(&should_wipe_cache); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 787 | } else { |
| 788 | status = apply_from_sdcard(device, &should_wipe_cache); |
| 789 | } |
| 790 | |
| 791 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 792 | if (!wipe_cache(false, device)) { |
| 793 | status = INSTALL_ERROR; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | if (status != INSTALL_SUCCESS) { |
| 798 | ui->SetBackground(RecoveryUI::ERROR); |
| 799 | ui->Print("Installation aborted.\n"); |
Jerry Zhang | 152933a | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 800 | copy_logs(modified_flash, has_cache); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 801 | } else if (!ui->IsTextVisible()) { |
| 802 | return Device::NO_ACTION; // reboot if logs aren't visible |
| 803 | } else { |
| 804 | ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card"); |
| 805 | } |
| 806 | } |
| 807 | break; |
| 808 | |
| 809 | case Device::VIEW_RECOVERY_LOGS: |
| 810 | choose_recovery_file(device); |
| 811 | break; |
| 812 | |
| 813 | case Device::RUN_GRAPHICS_TEST: |
| 814 | run_graphics_test(); |
| 815 | break; |
| 816 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 817 | case Device::RUN_LOCALE_TEST: { |
| 818 | ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui); |
Tao Bao | 39c4918 | 2018-05-07 22:50:33 -0700 | [diff] [blame] | 819 | screen_ui->CheckBackgroundTextImages(); |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 820 | break; |
| 821 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 822 | case Device::MOUNT_SYSTEM: |
| 823 | // For a system image built with the root directory (i.e. system_root_image == "true"), we |
| 824 | // mount it to /system_root, and symlink /system to /system_root/system to make adb shell |
| 825 | // work (the symlink is created through the build system). (Bug: 22855115) |
| 826 | if (android::base::GetBoolProperty("ro.build.system_root_image", false)) { |
| 827 | if (ensure_path_mounted_at("/", "/system_root") != -1) { |
| 828 | ui->Print("Mounted /system.\n"); |
| 829 | } |
| 830 | } else { |
| 831 | if (ensure_path_mounted("/system") != -1) { |
| 832 | ui->Print("Mounted /system.\n"); |
| 833 | } |
| 834 | } |
| 835 | break; |
| 836 | } |
| 837 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 840 | static void print_property(const char* key, const char* name, void* /* cookie */) { |
| 841 | printf("%s=%s\n", key, name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 842 | } |
| 843 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 844 | void ui_print(const char* format, ...) { |
| 845 | std::string buffer; |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 846 | va_list ap; |
| 847 | va_start(ap, format); |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 848 | android::base::StringAppendV(&buffer, format, ap); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 849 | va_end(ap); |
| 850 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 851 | if (ui != nullptr) { |
| 852 | ui->Print("%s", buffer.c_str()); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 853 | } else { |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 854 | fputs(buffer.c_str(), stdout); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 858 | static bool is_battery_ok(int* required_battery_level) { |
| 859 | struct healthd_config healthd_config = { |
| 860 | .batteryStatusPath = android::String8(android::String8::kEmptyString), |
| 861 | .batteryHealthPath = android::String8(android::String8::kEmptyString), |
| 862 | .batteryPresentPath = android::String8(android::String8::kEmptyString), |
| 863 | .batteryCapacityPath = android::String8(android::String8::kEmptyString), |
| 864 | .batteryVoltagePath = android::String8(android::String8::kEmptyString), |
| 865 | .batteryTemperaturePath = android::String8(android::String8::kEmptyString), |
| 866 | .batteryTechnologyPath = android::String8(android::String8::kEmptyString), |
| 867 | .batteryCurrentNowPath = android::String8(android::String8::kEmptyString), |
| 868 | .batteryCurrentAvgPath = android::String8(android::String8::kEmptyString), |
| 869 | .batteryChargeCounterPath = android::String8(android::String8::kEmptyString), |
| 870 | .batteryFullChargePath = android::String8(android::String8::kEmptyString), |
| 871 | .batteryCycleCountPath = android::String8(android::String8::kEmptyString), |
| 872 | .energyCounter = nullptr, |
| 873 | .boot_min_cap = 0, |
| 874 | .screen_on = nullptr |
| 875 | }; |
| 876 | healthd_board_init(&healthd_config); |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 877 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 878 | android::BatteryMonitor monitor; |
| 879 | monitor.init(&healthd_config); |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 880 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 881 | static constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10; |
| 882 | int wait_second = 0; |
| 883 | while (true) { |
| 884 | int charge_status = monitor.getChargeStatus(); |
| 885 | // Treat unknown status as charged. |
| 886 | bool charged = (charge_status != android::BATTERY_STATUS_DISCHARGING && |
| 887 | charge_status != android::BATTERY_STATUS_NOT_CHARGING); |
| 888 | android::BatteryProperty capacity; |
| 889 | android::status_t status = monitor.getProperty(android::BATTERY_PROP_CAPACITY, &capacity); |
| 890 | ui_print("charge_status %d, charged %d, status %d, capacity %" PRId64 "\n", charge_status, |
| 891 | charged, status, capacity.valueInt64); |
| 892 | // At startup, the battery drivers in devices like N5X/N6P take some time to load |
| 893 | // the battery profile. Before the load finishes, it reports value 50 as a fake |
| 894 | // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected |
| 895 | // to finish loading the battery profile earlier than 10 seconds after kernel startup. |
| 896 | if (status == 0 && capacity.valueInt64 == 50) { |
| 897 | if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) { |
| 898 | sleep(1); |
| 899 | wait_second++; |
| 900 | continue; |
| 901 | } |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 902 | } |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 903 | // If we can't read battery percentage, it may be a device without battery. In this situation, |
| 904 | // use 100 as a fake battery percentage. |
| 905 | if (status != 0) { |
| 906 | capacity.valueInt64 = 100; |
| 907 | } |
| 908 | |
| 909 | // GmsCore enters recovery mode to install package when having enough battery percentage. |
| 910 | // Normally, the threshold is 40% without charger and 20% with charger. So we should check |
| 911 | // battery with a slightly lower limitation. |
| 912 | static constexpr int BATTERY_OK_PERCENTAGE = 20; |
| 913 | static constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; |
| 914 | *required_battery_level = charged ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE; |
| 915 | return capacity.valueInt64 >= *required_battery_level; |
| 916 | } |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 919 | // Set the retry count to |retry_count| in BCB. |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 920 | static void set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) { |
| 921 | std::vector<std::string> options; |
| 922 | for (const auto& arg : args) { |
| 923 | if (!android::base::StartsWith(arg, "--retry_count")) { |
| 924 | options.push_back(arg); |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 925 | } |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 926 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 927 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 928 | // Update the retry counter in BCB. |
| 929 | options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count)); |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 930 | std::string err; |
| 931 | if (!update_bootloader_message(options, &err)) { |
| 932 | LOG(ERROR) << err; |
| 933 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 936 | static bool bootreason_in_blacklist() { |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 937 | std::string bootreason = android::base::GetProperty("ro.boot.bootreason", ""); |
| 938 | if (!bootreason.empty()) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 939 | // More bootreasons can be found in "system/core/bootstat/bootstat.cpp". |
| 940 | static const std::vector<std::string> kBootreasonBlacklist{ |
| 941 | "kernel_panic", |
| 942 | "Panic", |
| 943 | }; |
| 944 | for (const auto& str : kBootreasonBlacklist) { |
| 945 | if (android::base::EqualsIgnoreCase(str, bootreason)) return true; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 946 | } |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 947 | } |
| 948 | return false; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 951 | static void log_failure_code(ErrorCode code, const std::string& update_package) { |
| 952 | std::vector<std::string> log_buffer = { |
| 953 | update_package, |
| 954 | "0", // install result |
| 955 | "error: " + std::to_string(code), |
| 956 | }; |
| 957 | std::string log_content = android::base::Join(log_buffer, "\n"); |
| 958 | const std::string& install_file = Paths::Get().temporary_install_file(); |
| 959 | if (!android::base::WriteStringToFile(log_content, install_file)) { |
| 960 | PLOG(ERROR) << "Failed to write " << install_file; |
| 961 | } |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 962 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 963 | // Also write the info into last_log. |
| 964 | LOG(INFO) << log_content; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 967 | Device::BuiltinAction start_recovery(Device* device, const std::vector<std::string>& args) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 968 | std::vector<char*> args_to_parse(args.size()); |
| 969 | std::transform(args.cbegin(), args.cend(), args_to_parse.begin(), |
| 970 | [](const std::string& arg) { return const_cast<char*>(arg.c_str()); }); |
| 971 | |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 972 | static constexpr struct option OPTIONS[] = { |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame^] | 973 | { "fsck_unshare_blocks", no_argument, nullptr, 0 }, |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 974 | { "just_exit", no_argument, nullptr, 'x' }, |
| 975 | { "locale", required_argument, nullptr, 0 }, |
| 976 | { "prompt_and_wipe_data", no_argument, nullptr, 0 }, |
| 977 | { "reason", required_argument, nullptr, 0 }, |
| 978 | { "retry_count", required_argument, nullptr, 0 }, |
| 979 | { "security", no_argument, nullptr, 0 }, |
| 980 | { "show_text", no_argument, nullptr, 't' }, |
| 981 | { "shutdown_after", no_argument, nullptr, 0 }, |
| 982 | { "sideload", no_argument, nullptr, 0 }, |
| 983 | { "sideload_auto_reboot", no_argument, nullptr, 0 }, |
| 984 | { "update_package", required_argument, nullptr, 0 }, |
| 985 | { "wipe_ab", no_argument, nullptr, 0 }, |
| 986 | { "wipe_cache", no_argument, nullptr, 0 }, |
| 987 | { "wipe_data", no_argument, nullptr, 0 }, |
| 988 | { "wipe_package_size", required_argument, nullptr, 0 }, |
| 989 | { nullptr, 0, nullptr, 0 }, |
| 990 | }; |
| 991 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 992 | const char* update_package = nullptr; |
| 993 | bool should_wipe_data = false; |
| 994 | bool should_prompt_and_wipe_data = false; |
| 995 | bool should_wipe_cache = false; |
| 996 | bool should_wipe_ab = false; |
| 997 | size_t wipe_package_size = 0; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 998 | bool sideload = false; |
| 999 | bool sideload_auto_reboot = false; |
| 1000 | bool just_exit = false; |
| 1001 | bool shutdown_after = false; |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame^] | 1002 | bool fsck_unshare_blocks = false; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1003 | int retry_count = 0; |
| 1004 | bool security_update = false; |
Jerry Zhang | 2dea53e | 2018-05-02 17:15:03 -0700 | [diff] [blame] | 1005 | std::string locale; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1006 | |
| 1007 | int arg; |
| 1008 | int option_index; |
| 1009 | while ((arg = getopt_long(args_to_parse.size(), args_to_parse.data(), "", OPTIONS, |
| 1010 | &option_index)) != -1) { |
| 1011 | switch (arg) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1012 | case 't': |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 1013 | // Handled in recovery_main.cpp |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1014 | break; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1015 | case 'x': |
| 1016 | just_exit = true; |
| 1017 | break; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1018 | case 0: { |
| 1019 | std::string option = OPTIONS[option_index].name; |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame^] | 1020 | if (option == "fsck_unshare_blocks") { |
| 1021 | fsck_unshare_blocks = true; |
| 1022 | } else if (option == "locale") { |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 1023 | // Handled in recovery_main.cpp |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1024 | } else if (option == "prompt_and_wipe_data") { |
| 1025 | should_prompt_and_wipe_data = true; |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 1026 | } else if (option == "reason") { |
| 1027 | reason = optarg; |
| 1028 | } else if (option == "retry_count") { |
| 1029 | android::base::ParseInt(optarg, &retry_count, 0); |
| 1030 | } else if (option == "security") { |
| 1031 | security_update = true; |
| 1032 | } else if (option == "sideload") { |
| 1033 | sideload = true; |
| 1034 | } else if (option == "sideload_auto_reboot") { |
| 1035 | sideload = true; |
| 1036 | sideload_auto_reboot = true; |
| 1037 | } else if (option == "shutdown_after") { |
| 1038 | shutdown_after = true; |
| 1039 | } else if (option == "update_package") { |
| 1040 | update_package = optarg; |
| 1041 | } else if (option == "wipe_ab") { |
| 1042 | should_wipe_ab = true; |
| 1043 | } else if (option == "wipe_cache") { |
| 1044 | should_wipe_cache = true; |
| 1045 | } else if (option == "wipe_data") { |
| 1046 | should_wipe_data = true; |
| 1047 | } else if (option == "wipe_package_size") { |
| 1048 | android::base::ParseUint(optarg, &wipe_package_size); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1049 | } |
| 1050 | break; |
| 1051 | } |
| 1052 | case '?': |
| 1053 | LOG(ERROR) << "Invalid command argument"; |
| 1054 | continue; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1055 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1056 | } |
Jerry Zhang | 49fd5d2 | 2018-05-17 12:54:41 -0700 | [diff] [blame] | 1057 | optind = 1; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1058 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1059 | printf("stage is [%s]\n", stage.c_str()); |
| 1060 | printf("reason is [%s]\n", reason); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1061 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1062 | // Set background string to "installing security update" for security update, |
| 1063 | // otherwise set it to "installing system update". |
| 1064 | ui->SetSystemUpdateText(security_update); |
| 1065 | |
| 1066 | int st_cur, st_max; |
| 1067 | if (!stage.empty() && sscanf(stage.c_str(), "%d/%d", &st_cur, &st_max) == 2) { |
| 1068 | ui->SetStage(st_cur, st_max); |
| 1069 | } |
| 1070 | |
Jerry Zhang | 0e577ee | 2018-05-07 11:21:10 -0700 | [diff] [blame] | 1071 | std::vector<std::string> title_lines = |
| 1072 | android::base::Split(android::base::GetProperty("ro.bootimage.build.fingerprint", ""), ":"); |
| 1073 | title_lines.insert(std::begin(title_lines), "Android Recovery"); |
| 1074 | ui->SetTitle(title_lines); |
| 1075 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1076 | device->StartRecovery(); |
| 1077 | |
| 1078 | printf("Command:"); |
| 1079 | for (const auto& arg : args) { |
| 1080 | printf(" \"%s\"", arg.c_str()); |
| 1081 | } |
| 1082 | printf("\n\n"); |
| 1083 | |
| 1084 | property_list(print_property, nullptr); |
| 1085 | printf("\n"); |
| 1086 | |
| 1087 | ui->Print("Supported API: %d\n", kRecoveryApiVersion); |
| 1088 | |
| 1089 | int status = INSTALL_SUCCESS; |
| 1090 | |
| 1091 | if (update_package != nullptr) { |
| 1092 | // It's not entirely true that we will modify the flash. But we want |
| 1093 | // to log the update attempt since update_package is non-NULL. |
| 1094 | modified_flash = true; |
| 1095 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 1096 | int required_battery_level; |
| 1097 | if (retry_count == 0 && !is_battery_ok(&required_battery_level)) { |
| 1098 | ui->Print("battery capacity is not enough for installing package: %d%% needed\n", |
| 1099 | required_battery_level); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1100 | // Log the error code to last_install when installation skips due to |
| 1101 | // low battery. |
| 1102 | log_failure_code(kLowBattery, update_package); |
| 1103 | status = INSTALL_SKIPPED; |
Tianjie Xu | a6f49bd | 2018-03-26 14:32:11 -0700 | [diff] [blame] | 1104 | } else if (retry_count == 0 && bootreason_in_blacklist()) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1105 | // Skip update-on-reboot when bootreason is kernel_panic or similar |
| 1106 | ui->Print("bootreason is in the blacklist; skip OTA installation\n"); |
| 1107 | log_failure_code(kBootreasonInBlacklist, update_package); |
| 1108 | status = INSTALL_SKIPPED; |
Dmitri Plotnikov | 8706a98 | 2017-04-18 08:28:26 -0700 | [diff] [blame] | 1109 | } else { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1110 | // It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later |
| 1111 | // identify the interrupted update due to unexpected reboots. |
| 1112 | if (retry_count == 0) { |
| 1113 | set_retry_bootloader_message(retry_count + 1, args); |
Tao Bao | 7022f33 | 2017-07-25 09:52:36 -0700 | [diff] [blame] | 1114 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1115 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1116 | status = install_package(update_package, &should_wipe_cache, true, retry_count); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1117 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 1118 | wipe_cache(false, device); |
| 1119 | } |
| 1120 | if (status != INSTALL_SUCCESS) { |
| 1121 | ui->Print("Installation aborted.\n"); |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame] | 1122 | |
| 1123 | // When I/O error or bspatch/imgpatch error happens, reboot and retry installation |
| 1124 | // RETRY_LIMIT times before we abandon this OTA update. |
| 1125 | static constexpr int RETRY_LIMIT = 4; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1126 | if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) { |
Jerry Zhang | 152933a | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 1127 | copy_logs(modified_flash, has_cache); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1128 | retry_count += 1; |
| 1129 | set_retry_bootloader_message(retry_count, args); |
| 1130 | // Print retry count on screen. |
| 1131 | ui->Print("Retry attempt %d\n", retry_count); |
| 1132 | |
| 1133 | // Reboot and retry the update |
| 1134 | if (!reboot("reboot,recovery")) { |
| 1135 | ui->Print("Reboot failed\n"); |
| 1136 | } else { |
| 1137 | while (true) { |
| 1138 | pause(); |
| 1139 | } |
| 1140 | } |
Tianjie Xu | d9d1629 | 2017-04-20 18:08:21 -0700 | [diff] [blame] | 1141 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1142 | // If this is an eng or userdebug build, then automatically |
| 1143 | // turn the text display on if the script fails so the error |
| 1144 | // message is visible. |
| 1145 | if (is_ro_debuggable()) { |
| 1146 | ui->ShowText(true); |
Tao Bao | c679f93 | 2015-03-30 09:43:49 -0700 | [diff] [blame] | 1147 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1148 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 1149 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1150 | } else if (should_wipe_data) { |
| 1151 | if (!wipe_data(device)) { |
| 1152 | status = INSTALL_ERROR; |
Doug Zongker | b1d1263 | 2014-03-18 10:32:12 -0700 | [diff] [blame] | 1153 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1154 | } else if (should_prompt_and_wipe_data) { |
| 1155 | ui->ShowText(true); |
| 1156 | ui->SetBackground(RecoveryUI::ERROR); |
| 1157 | if (!prompt_and_wipe_data(device)) { |
| 1158 | status = INSTALL_ERROR; |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 1159 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1160 | ui->ShowText(false); |
| 1161 | } else if (should_wipe_cache) { |
| 1162 | if (!wipe_cache(false, device)) { |
| 1163 | status = INSTALL_ERROR; |
| 1164 | } |
| 1165 | } else if (should_wipe_ab) { |
| 1166 | if (!wipe_ab_device(wipe_package_size)) { |
| 1167 | status = INSTALL_ERROR; |
| 1168 | } |
| 1169 | } else if (sideload) { |
| 1170 | // 'adb reboot sideload' acts the same as user presses key combinations |
| 1171 | // to enter the sideload mode. When 'sideload-auto-reboot' is used, text |
| 1172 | // display will NOT be turned on by default. And it will reboot after |
| 1173 | // sideload finishes even if there are errors. Unless one turns on the |
| 1174 | // text display during the installation. This is to enable automated |
| 1175 | // testing. |
| 1176 | if (!sideload_auto_reboot) { |
| 1177 | ui->ShowText(true); |
| 1178 | } |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1179 | status = apply_from_adb(&should_wipe_cache); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1180 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 1181 | if (!wipe_cache(false, device)) { |
| 1182 | status = INSTALL_ERROR; |
| 1183 | } |
| 1184 | } |
| 1185 | ui->Print("\nInstall from ADB complete (status: %d).\n", status); |
| 1186 | if (sideload_auto_reboot) { |
| 1187 | ui->Print("Rebooting automatically.\n"); |
| 1188 | } |
David Anderson | edee836 | 2018-05-16 13:43:22 -0700 | [diff] [blame^] | 1189 | } else if (fsck_unshare_blocks) { |
| 1190 | if (!do_fsck_unshare_blocks()) { |
| 1191 | status = INSTALL_ERROR; |
| 1192 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1193 | } else if (!just_exit) { |
| 1194 | // If this is an eng or userdebug build, automatically turn on the text display if no command |
| 1195 | // is specified. Note that this should be called before setting the background to avoid |
| 1196 | // flickering the background image. |
| 1197 | if (is_ro_debuggable()) { |
| 1198 | ui->ShowText(true); |
| 1199 | } |
| 1200 | status = INSTALL_NONE; // No command specified |
| 1201 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 1202 | } |
| 1203 | |
| 1204 | if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) { |
| 1205 | ui->SetBackground(RecoveryUI::ERROR); |
| 1206 | if (!ui->IsTextVisible()) { |
| 1207 | sleep(5); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT; |
| 1212 | // 1. If the recovery menu is visible, prompt and wait for commands. |
| 1213 | // 2. If the state is INSTALL_NONE, wait for commands. (i.e. In user build, manually reboot into |
| 1214 | // recovery to sideload a package.) |
| 1215 | // 3. sideload_auto_reboot is an option only available in user-debug build, reboot the device |
| 1216 | // without waiting. |
| 1217 | // 4. In all other cases, reboot the device. Therefore, normal users will observe the device |
| 1218 | // reboot after it shows the "error" screen for 5s. |
| 1219 | if ((status == INSTALL_NONE && !sideload_auto_reboot) || ui->IsTextVisible()) { |
| 1220 | Device::BuiltinAction temp = prompt_and_wait(device, status); |
| 1221 | if (temp != Device::NO_ACTION) { |
| 1222 | after = temp; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | // Save logs and clean up before rebooting or shutting down. |
Tao Bao | 551d2c3 | 2018-05-09 20:53:13 -0700 | [diff] [blame] | 1227 | finish_recovery(); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1228 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 1229 | return after; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1230 | } |