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