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 | |
| 17 | #include <ctype.h> |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 18 | #include <dirent.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <getopt.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <limits.h> |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 24 | #include <linux/fs.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <linux/input.h> |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 26 | #include <stdarg.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Patrick Tjin | cd055ee | 2014-12-09 11:26:40 -0800 | [diff] [blame] | 30 | #include <sys/klog.h> |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 31 | #include <sys/stat.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | #include <sys/types.h> |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 33 | #include <sys/wait.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | #include <time.h> |
| 35 | #include <unistd.h> |
| 36 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 37 | #include <algorithm> |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 38 | #include <chrono> |
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> |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 51 | #include <cutils/android_reboot.h> |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 52 | #include <cutils/properties.h> /* for property_list */ |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 53 | #include <healthd/BatteryMonitor.h> |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 54 | #include <private/android_logger.h> /* private pmsg functions */ |
Tao Bao | e1905ad | 2017-03-22 14:57:04 -0700 | [diff] [blame] | 55 | #include <private/android_filesystem_config.h> /* for AID_SYSTEM */ |
Jeff Vander Stoep | e35926e | 2017-06-14 15:30:39 -0700 | [diff] [blame] | 56 | #include <selinux/android.h> |
Elliott Hughes | 4bbd5bf | 2016-04-01 18:24:39 -0700 | [diff] [blame] | 57 | #include <selinux/label.h> |
| 58 | #include <selinux/selinux.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 59 | #include <ziparchive/zip_archive.h> |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 60 | |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 61 | #include "adb_install.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 62 | #include "common.h" |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 63 | #include "device.h" |
| 64 | #include "fuse_sdcard_provider.h" |
| 65 | #include "fuse_sideload.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 66 | #include "install.h" |
Josh Gao | acb2a2f | 2016-08-26 18:24:34 -0700 | [diff] [blame] | 67 | #include "minadbd/minadbd.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 68 | #include "minui/minui.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 69 | #include "otautil/DirUtil.h" |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 70 | #include "otautil/error_code.h" |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 71 | #include "otautil/paths.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 72 | #include "roots.h" |
Tianjie Xu | e113e4d | 2016-10-21 17:46:13 -0700 | [diff] [blame] | 73 | #include "rotate_logs.h" |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 74 | #include "screen_ui.h" |
Sen Jiang | d530449 | 2016-12-09 16:20:49 -0800 | [diff] [blame] | 75 | #include "stub_ui.h" |
Tianjie Xu | e113e4d | 2016-10-21 17:46:13 -0700 | [diff] [blame] | 76 | #include "ui.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 77 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 78 | static constexpr const char* CACHE_LOG_DIR = "/cache/recovery"; |
| 79 | static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; |
| 80 | static constexpr const char* LOG_FILE = "/cache/recovery/log"; |
| 81 | static constexpr const char* LAST_INSTALL_FILE = "/cache/recovery/last_install"; |
| 82 | static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; |
| 83 | static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log"; |
| 84 | static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale"; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 85 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 86 | static constexpr const char* CACHE_ROOT = "/cache"; |
| 87 | static constexpr const char* DATA_ROOT = "/data"; |
| 88 | static constexpr const char* METADATA_ROOT = "/metadata"; |
| 89 | static constexpr const char* SDCARD_ROOT = "/sdcard"; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 90 | |
Tao Bao | bd0ddcd | 2017-05-04 13:03:18 -0700 | [diff] [blame] | 91 | // We define RECOVERY_API_VERSION in Android.mk, which will be picked up by build system and packed |
| 92 | // into target_files.zip. Assert the version defined in code and in Android.mk are consistent. |
| 93 | static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions."); |
| 94 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 95 | static std::string locale; |
Dan Albert | 8584fcf | 2016-10-27 03:08:08 +0000 | [diff] [blame] | 96 | static bool has_cache = false; |
Tao Bao | c0319b6 | 2016-10-13 15:17:04 -0700 | [diff] [blame] | 97 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 98 | RecoveryUI* ui = nullptr; |
| 99 | bool modified_flash = false; |
Tao Bao | a8d72bc | 2016-12-25 18:46:50 -0800 | [diff] [blame] | 100 | std::string stage; |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 101 | const char* reason = nullptr; |
| 102 | struct selabel_handle* sehandle; |
| 103 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 104 | /* |
| 105 | * The recovery tool communicates with the main system through /cache files. |
| 106 | * /cache/recovery/command - INPUT - command line for tool, one arg per line |
| 107 | * /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] | 108 | * |
| 109 | * The arguments which may be supplied in the recovery.command file: |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 110 | * --update_package=path - verify install an OTA package file |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 111 | * --wipe_data - erase user data (and cache), then reboot |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 112 | * --prompt_and_wipe_data - prompt the user that data is corrupt, with their consent erase user |
| 113 | * data (and cache), then reboot |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 114 | * --wipe_cache - wipe cache (but not user data), then reboot |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 115 | * --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] | 116 | * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs |
Doug Zongker | e5d5ac7 | 2012-04-12 11:01:22 -0700 | [diff] [blame] | 117 | * --just_exit - do nothing; exit and reboot |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 118 | * |
| 119 | * After completing, we remove /cache/recovery/command and reboot. |
| 120 | * Arguments may also be supplied in the bootloader control block (BCB). |
| 121 | * These important scenarios must be safely restartable at any point: |
| 122 | * |
| 123 | * FACTORY RESET |
| 124 | * 1. user selects "factory reset" |
| 125 | * 2. main system writes "--wipe_data" to /cache/recovery/command |
| 126 | * 3. main system reboots into recovery |
| 127 | * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data" |
| 128 | * -- after this, rebooting will restart the erase -- |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 129 | * 5. erase_volume() reformats /data |
| 130 | * 6. erase_volume() reformats /cache |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 131 | * 7. finish_recovery() erases BCB |
| 132 | * -- after this, rebooting will restart the main system -- |
| 133 | * 8. main() calls reboot() to boot main system |
| 134 | * |
| 135 | * OTA INSTALL |
| 136 | * 1. main system downloads OTA package to /cache/some-filename.zip |
Doug Zongker | 9b125b0 | 2010-09-22 12:01:37 -0700 | [diff] [blame] | 137 | * 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] | 138 | * 3. main system reboots into recovery |
| 139 | * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..." |
| 140 | * -- after this, rebooting will attempt to reinstall the update -- |
| 141 | * 5. install_package() attempts to install the update |
| 142 | * NOTE: the package install must itself be restartable from any point |
| 143 | * 6. finish_recovery() erases BCB |
| 144 | * -- after this, rebooting will (try to) restart the main system -- |
| 145 | * 7. ** if install failed ** |
| 146 | * 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] | 147 | * 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] | 148 | */ |
| 149 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 150 | // Open a given path, mounting partitions as necessary. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 151 | FILE* fopen_path(const std::string& path, const char* mode) { |
| 152 | if (ensure_path_mounted(path.c_str()) != 0) { |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 153 | LOG(ERROR) << "Can't mount " << path; |
| 154 | return nullptr; |
| 155 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 156 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 157 | // When writing, try to create the containing directory, if necessary. Use generous permissions, |
| 158 | // the system (init.rc) will reset them. |
| 159 | if (strchr("wa", mode[0])) { |
| 160 | mkdir_recursively(path, 0777, true, sehandle); |
| 161 | } |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 162 | return fopen(path.c_str(), mode); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 165 | // close a file, log an error if the error indicator is set |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 166 | static void check_and_fclose(FILE* fp, const std::string& name) { |
| 167 | fflush(fp); |
| 168 | if (fsync(fileno(fp)) == -1) { |
| 169 | PLOG(ERROR) << "Failed to fsync " << name; |
| 170 | } |
| 171 | if (ferror(fp)) { |
| 172 | PLOG(ERROR) << "Error in " << name; |
| 173 | } |
| 174 | fclose(fp); |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Elliott Hughes | f14af80 | 2015-02-10 14:46:14 -0800 | [diff] [blame] | 177 | bool is_ro_debuggable() { |
Elliott Hughes | cb22040 | 2016-09-23 15:30:55 -0700 | [diff] [blame] | 178 | return android::base::GetBoolProperty("ro.debuggable", false); |
Elliott Hughes | f14af80 | 2015-02-10 14:46:14 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Dmitri Plotnikov | 8706a98 | 2017-04-18 08:28:26 -0700 | [diff] [blame] | 181 | bool reboot(const std::string& command) { |
| 182 | std::string cmd = command; |
| 183 | if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { |
| 184 | cmd += ",quiescent"; |
| 185 | } |
| 186 | return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd); |
| 187 | } |
| 188 | |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 189 | static void redirect_stdio(const char* filename) { |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 190 | int pipefd[2]; |
| 191 | if (pipe(pipefd) == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 192 | PLOG(ERROR) << "pipe failed"; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 193 | |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 194 | // Fall back to traditional logging mode without timestamps. |
| 195 | // If these fail, there's not really anywhere to complain... |
| 196 | freopen(filename, "a", stdout); setbuf(stdout, NULL); |
| 197 | freopen(filename, "a", stderr); setbuf(stderr, NULL); |
| 198 | |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | pid_t pid = fork(); |
| 203 | if (pid == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 204 | PLOG(ERROR) << "fork failed"; |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 205 | |
| 206 | // Fall back to traditional logging mode without timestamps. |
| 207 | // If these fail, there's not really anywhere to complain... |
| 208 | freopen(filename, "a", stdout); setbuf(stdout, NULL); |
| 209 | freopen(filename, "a", stderr); setbuf(stderr, NULL); |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (pid == 0) { |
| 215 | /// Close the unused write end. |
| 216 | close(pipefd[1]); |
| 217 | |
| 218 | auto start = std::chrono::steady_clock::now(); |
| 219 | |
| 220 | // Child logger to actually write to the log file. |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 221 | FILE* log_fp = fopen(filename, "ae"); |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 222 | if (log_fp == nullptr) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 223 | PLOG(ERROR) << "fopen \"" << filename << "\" failed"; |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 224 | close(pipefd[0]); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 225 | _exit(EXIT_FAILURE); |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | FILE* pipe_fp = fdopen(pipefd[0], "r"); |
| 229 | if (pipe_fp == nullptr) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 230 | PLOG(ERROR) << "fdopen failed"; |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 231 | check_and_fclose(log_fp, filename); |
| 232 | close(pipefd[0]); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 233 | _exit(EXIT_FAILURE); |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | char* line = nullptr; |
| 237 | size_t len = 0; |
| 238 | while (getline(&line, &len, pipe_fp) != -1) { |
| 239 | auto now = std::chrono::steady_clock::now(); |
| 240 | double duration = std::chrono::duration_cast<std::chrono::duration<double>>( |
| 241 | now - start).count(); |
| 242 | if (line[0] == '\n') { |
| 243 | fprintf(log_fp, "[%12.6lf]\n", duration); |
| 244 | } else { |
| 245 | fprintf(log_fp, "[%12.6lf] %s", duration, line); |
| 246 | } |
| 247 | fflush(log_fp); |
| 248 | } |
| 249 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 250 | PLOG(ERROR) << "getline failed"; |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 251 | |
| 252 | free(line); |
| 253 | check_and_fclose(log_fp, filename); |
| 254 | close(pipefd[0]); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 255 | _exit(EXIT_FAILURE); |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 256 | } else { |
| 257 | // Redirect stdout/stderr to the logger process. |
| 258 | // Close the unused read end. |
| 259 | close(pipefd[0]); |
| 260 | |
| 261 | setbuf(stdout, nullptr); |
| 262 | setbuf(stderr, nullptr); |
| 263 | |
| 264 | if (dup2(pipefd[1], STDOUT_FILENO) == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 265 | PLOG(ERROR) << "dup2 stdout failed"; |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 266 | } |
| 267 | if (dup2(pipefd[1], STDERR_FILENO) == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 268 | PLOG(ERROR) << "dup2 stderr failed"; |
Tao Bao | 04ca426 | 2015-09-10 15:32:24 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | close(pipefd[1]); |
| 272 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // command line args come from, in decreasing precedence: |
| 276 | // - the actual command line |
| 277 | // - the bootloader control block (one per line, after "recovery") |
| 278 | // - the contents of COMMAND_FILE (one per line) |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 279 | static std::vector<std::string> get_args(const int argc, char** const argv) { |
| 280 | CHECK_GT(argc, 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 281 | |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 282 | bootloader_message boot = {}; |
| 283 | std::string err; |
| 284 | if (!read_bootloader_message(&boot, &err)) { |
| 285 | LOG(ERROR) << err; |
| 286 | // If fails, leave a zeroed bootloader_message. |
| 287 | boot = {}; |
| 288 | } |
Tao Bao | a8d72bc | 2016-12-25 18:46:50 -0800 | [diff] [blame] | 289 | stage = std::string(boot.stage); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 290 | |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 291 | if (boot.command[0] != 0) { |
| 292 | std::string boot_command = std::string(boot.command, sizeof(boot.command)); |
| 293 | LOG(INFO) << "Boot command: " << boot_command; |
| 294 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 295 | |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 296 | if (boot.status[0] != 0) { |
| 297 | std::string boot_status = std::string(boot.status, sizeof(boot.status)); |
| 298 | LOG(INFO) << "Boot status: " << boot_status; |
| 299 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 300 | |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 301 | std::vector<std::string> args(argv, argv + argc); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 302 | |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 303 | // --- if arguments weren't supplied, look in the bootloader control block |
Tao Bao | 570af9d | 2017-01-09 10:29:59 -0800 | [diff] [blame] | 304 | if (args.size() == 1) { |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 305 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 306 | std::string boot_recovery(boot.recovery); |
| 307 | std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n"); |
| 308 | if (!tokens.empty() && tokens[0] == "recovery") { |
| 309 | for (auto it = tokens.begin() + 1; it != tokens.end(); it++) { |
| 310 | // Skip empty and '\0'-filled tokens. |
| 311 | if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it)); |
| 312 | } |
| 313 | LOG(INFO) << "Got " << args.size() << " arguments from boot message"; |
| 314 | } else if (boot.recovery[0] != 0) { |
| 315 | LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\""; |
| 316 | } |
| 317 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 318 | |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 319 | // --- if that doesn't work, try the command file (if we have /cache). |
Tao Bao | 570af9d | 2017-01-09 10:29:59 -0800 | [diff] [blame] | 320 | if (args.size() == 1 && has_cache) { |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 321 | std::string content; |
Tao Bao | 7d34fa1 | 2016-12-08 18:10:48 -0800 | [diff] [blame] | 322 | if (ensure_path_mounted(COMMAND_FILE) == 0 && |
| 323 | android::base::ReadFileToString(COMMAND_FILE, &content)) { |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 324 | std::vector<std::string> tokens = android::base::Split(content, "\n"); |
Tao Bao | 7d34fa1 | 2016-12-08 18:10:48 -0800 | [diff] [blame] | 325 | // All the arguments in COMMAND_FILE are needed (unlike the BCB message, |
| 326 | // COMMAND_FILE doesn't use filename as the first argument). |
| 327 | for (auto it = tokens.begin(); it != tokens.end(); it++) { |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 328 | // Skip empty and '\0'-filled tokens. |
| 329 | if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it)); |
| 330 | } |
| 331 | LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 332 | } |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 333 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 334 | |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 335 | // Write the arguments (excluding the filename in args[0]) back into the |
| 336 | // bootloader control block. So the device will always boot into recovery to |
| 337 | // finish the pending work, until finish_recovery() is called. |
| 338 | std::vector<std::string> options(args.cbegin() + 1, args.cend()); |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 339 | if (!update_bootloader_message(options, &err)) { |
| 340 | LOG(ERROR) << "Failed to set BCB message: " << err; |
Tao Bao | f0ed159 | 2016-12-02 11:32:19 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return args; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Tao Bao | 2292db8 | 2016-12-13 21:53:31 -0800 | [diff] [blame] | 346 | // Set the BCB to reboot back into recovery (it won't resume the install from |
| 347 | // sdcard though). |
| 348 | static void set_sdcard_update_bootloader_message() { |
| 349 | std::vector<std::string> options; |
| 350 | std::string err; |
| 351 | if (!update_bootloader_message(options, &err)) { |
| 352 | LOG(ERROR) << "Failed to set BCB message: " << err; |
| 353 | } |
Doug Zongker | 34c98df | 2009-08-18 12:05:45 -0700 | [diff] [blame] | 354 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 355 | |
Tao Bao | bef3971 | 2015-05-04 18:50:27 -0700 | [diff] [blame] | 356 | // Read from kernel log into buffer and write out to file. |
| 357 | static void save_kernel_log(const char* destination) { |
| 358 | int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0); |
Patrick Tjin | cd055ee | 2014-12-09 11:26:40 -0800 | [diff] [blame] | 359 | if (klog_buf_len <= 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 360 | PLOG(ERROR) << "Error getting klog size"; |
Patrick Tjin | cd055ee | 2014-12-09 11:26:40 -0800 | [diff] [blame] | 361 | return; |
| 362 | } |
| 363 | |
Tao Bao | bef3971 | 2015-05-04 18:50:27 -0700 | [diff] [blame] | 364 | std::string buffer(klog_buf_len, 0); |
| 365 | int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len); |
| 366 | if (n == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 367 | PLOG(ERROR) << "Error in reading klog"; |
Patrick Tjin | cd055ee | 2014-12-09 11:26:40 -0800 | [diff] [blame] | 368 | return; |
| 369 | } |
Tao Bao | bef3971 | 2015-05-04 18:50:27 -0700 | [diff] [blame] | 370 | buffer.resize(n); |
| 371 | android::base::WriteStringToFile(buffer, destination); |
Patrick Tjin | cd055ee | 2014-12-09 11:26:40 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 374 | // Writes content to the current pmsg session. |
| 375 | static ssize_t __pmsg_write(const std::string& filename, const std::string& buf) { |
| 376 | return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filename.c_str(), |
| 377 | buf.data(), buf.size()); |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 380 | static void copy_log_file_to_pmsg(const std::string& source, const std::string& destination) { |
| 381 | std::string content; |
| 382 | android::base::ReadFileToString(source, &content); |
| 383 | __pmsg_write(destination, content); |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Tao Bao | f012432 | 2015-04-11 02:04:11 +0000 | [diff] [blame] | 386 | // How much of the temp log we have copied to the copy in cache. |
Chih-Hung Hsieh | 54a2747 | 2016-04-18 11:30:55 -0700 | [diff] [blame] | 387 | static off_t tmplog_offset = 0; |
Tao Bao | f012432 | 2015-04-11 02:04:11 +0000 | [diff] [blame] | 388 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 389 | static void copy_log_file(const std::string& source, const std::string& destination, bool append) { |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 390 | FILE* dest_fp = fopen_path(destination, append ? "ae" : "we"); |
| 391 | if (dest_fp == nullptr) { |
| 392 | PLOG(ERROR) << "Can't open " << destination; |
| 393 | } else { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 394 | FILE* source_fp = fopen(source.c_str(), "re"); |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 395 | if (source_fp != nullptr) { |
| 396 | if (append) { |
| 397 | fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write |
| 398 | } |
| 399 | char buf[4096]; |
| 400 | size_t bytes; |
| 401 | while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) { |
| 402 | fwrite(buf, 1, bytes, dest_fp); |
| 403 | } |
| 404 | if (append) { |
| 405 | tmplog_offset = ftello(source_fp); |
| 406 | } |
| 407 | check_and_fclose(source_fp, source); |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 408 | } |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 409 | check_and_fclose(dest_fp, destination); |
| 410 | } |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 413 | static void copy_logs() { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 414 | // We only rotate and record the log of the current session if there are actual attempts to modify |
| 415 | // the flash, such as wipes, installs from BCB or menu selections. This is to avoid unnecessary |
| 416 | // rotation (and possible deletion) of log files, if it does not do anything loggable. |
| 417 | if (!modified_flash) { |
| 418 | return; |
| 419 | } |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 420 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 421 | // Always write to pmsg, this allows the OTA logs to be caught in `logcat -L`. |
| 422 | copy_log_file_to_pmsg(Paths::Get().temporary_log_file(), LAST_LOG_FILE); |
| 423 | copy_log_file_to_pmsg(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE); |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 424 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 425 | // We can do nothing for now if there's no /cache partition. |
| 426 | if (!has_cache) { |
| 427 | return; |
| 428 | } |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 429 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 430 | ensure_path_mounted(LAST_LOG_FILE); |
| 431 | ensure_path_mounted(LAST_KMSG_FILE); |
| 432 | rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 433 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 434 | // Copy logs to cache so the system can find out what happened. |
| 435 | copy_log_file(Paths::Get().temporary_log_file(), LOG_FILE, true); |
| 436 | copy_log_file(Paths::Get().temporary_log_file(), LAST_LOG_FILE, false); |
| 437 | copy_log_file(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE, false); |
| 438 | save_kernel_log(LAST_KMSG_FILE); |
| 439 | chmod(LOG_FILE, 0600); |
| 440 | chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM); |
| 441 | chmod(LAST_KMSG_FILE, 0600); |
| 442 | chown(LAST_KMSG_FILE, AID_SYSTEM, AID_SYSTEM); |
| 443 | chmod(LAST_LOG_FILE, 0640); |
| 444 | chmod(LAST_INSTALL_FILE, 0644); |
| 445 | sync(); |
Doug Zongker | f24fd7e | 2013-07-02 11:43:25 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 448 | // Clear the recovery command and prepare to boot a (hopefully working) system, |
Tianjie Xu | c14d95d | 2016-03-24 11:50:34 -0700 | [diff] [blame] | 449 | // copy our log file to cache as well (for the system to read). This function is |
| 450 | // idempotent: call it as many times as you like. |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 451 | static void finish_recovery() { |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 452 | // Save the locale to cache, so if recovery is next started up without a '--locale' argument |
| 453 | // (e.g., directly from the bootloader) it will use the last-known locale. |
| 454 | if (!locale.empty() && has_cache) { |
| 455 | LOG(INFO) << "Saving locale \"" << locale << "\""; |
| 456 | if (ensure_path_mounted(LOCALE_FILE) != 0) { |
| 457 | LOG(ERROR) << "Failed to mount " << LOCALE_FILE; |
| 458 | } else if (!android::base::WriteStringToFile(locale, LOCALE_FILE)) { |
| 459 | PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE; |
Doug Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 460 | } |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 461 | } |
Doug Zongker | 4f33e55 | 2012-08-23 13:16:12 -0700 | [diff] [blame] | 462 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 463 | copy_logs(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 464 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 465 | // Reset to normal system boot so recovery won't cycle indefinitely. |
| 466 | std::string err; |
| 467 | if (!clear_bootloader_message(&err)) { |
| 468 | LOG(ERROR) << "Failed to clear BCB message: " << err; |
| 469 | } |
| 470 | |
| 471 | // Remove the command file, so recovery won't repeat indefinitely. |
| 472 | if (has_cache) { |
| 473 | if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) { |
| 474 | LOG(WARNING) << "Can't unlink " << COMMAND_FILE; |
Yabin Cui | 8b309f6 | 2016-06-24 18:22:02 -0700 | [diff] [blame] | 475 | } |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 476 | ensure_path_unmounted(CACHE_ROOT); |
| 477 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 478 | |
Tao Bao | ec57903 | 2017-07-21 12:13:15 -0700 | [diff] [blame] | 479 | sync(); // For good measure. |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 482 | struct saved_log_file { |
| 483 | std::string name; |
| 484 | struct stat sb; |
| 485 | std::string data; |
| 486 | }; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 487 | |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 488 | static bool erase_volume(const char* volume) { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 489 | bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); |
| 490 | bool is_data = (strcmp(volume, DATA_ROOT) == 0); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 491 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 492 | ui->SetBackground(RecoveryUI::ERASING); |
| 493 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 494 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 495 | std::vector<saved_log_file> log_files; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 496 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 497 | if (is_cache) { |
| 498 | // If we're reformatting /cache, we load any past logs |
| 499 | // (i.e. "/cache/recovery/last_*") and the current log |
| 500 | // ("/cache/recovery/log") into memory, so we can restore them after |
| 501 | // the reformat. |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 502 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 503 | ensure_path_mounted(volume); |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 504 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 505 | struct dirent* de; |
| 506 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(CACHE_LOG_DIR), closedir); |
| 507 | if (d) { |
| 508 | while ((de = readdir(d.get())) != nullptr) { |
| 509 | if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) { |
| 510 | std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name); |
| 511 | |
| 512 | struct stat sb; |
| 513 | if (stat(path.c_str(), &sb) == 0) { |
| 514 | // truncate files to 512kb |
| 515 | if (sb.st_size > (1 << 19)) { |
| 516 | sb.st_size = 1 << 19; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 517 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 518 | |
| 519 | std::string data(sb.st_size, '\0'); |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 520 | FILE* f = fopen(path.c_str(), "rbe"); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 521 | fread(&data[0], 1, data.size(), f); |
| 522 | fclose(f); |
| 523 | |
| 524 | log_files.emplace_back(saved_log_file{ path, sb, data }); |
| 525 | } |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 526 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 527 | } |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 528 | } else { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 529 | if (errno != ENOENT) { |
| 530 | PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR; |
| 531 | } |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 532 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 533 | } |
Doug Zongker | d0181b8 | 2011-10-19 10:51:12 -0700 | [diff] [blame] | 534 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 535 | ui->Print("Formatting %s...\n", volume); |
| 536 | |
| 537 | ensure_path_unmounted(volume); |
| 538 | |
| 539 | int result; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 540 | if (is_data && reason && strcmp(reason, "convert_fbe") == 0) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 541 | static constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe"; |
| 542 | static constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe"; |
| 543 | // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not |
| 544 | // full disk encryption. |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 545 | if (mkdir(CONVERT_FBE_DIR, 0700) != 0) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 546 | PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR; |
| 547 | return false; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 548 | } |
Tianjie Xu | de6735e | 2017-07-10 15:13:33 -0700 | [diff] [blame] | 549 | FILE* f = fopen(CONVERT_FBE_FILE, "wbe"); |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 550 | if (!f) { |
Tao Bao | 406a6ff | 2018-04-30 10:05:57 -0700 | [diff] [blame] | 551 | PLOG(ERROR) << "Failed to convert to file encryption"; |
| 552 | return false; |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 553 | } |
| 554 | fclose(f); |
| 555 | result = format_volume(volume, CONVERT_FBE_DIR); |
| 556 | remove(CONVERT_FBE_FILE); |
| 557 | rmdir(CONVERT_FBE_DIR); |
| 558 | } else { |
| 559 | result = format_volume(volume); |
| 560 | } |
| 561 | |
| 562 | if (is_cache) { |
| 563 | // Re-create the log dir and write back the log entries. |
| 564 | if (ensure_path_mounted(CACHE_LOG_DIR) == 0 && |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 565 | mkdir_recursively(CACHE_LOG_DIR, 0777, false, sehandle) == 0) { |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 566 | for (const auto& log : log_files) { |
| 567 | if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid, |
| 568 | log.sb.st_gid)) { |
| 569 | PLOG(ERROR) << "Failed to write to " << log.name; |
Doug Zongker | 6d0d7ac | 2013-07-09 13:34:55 -0700 | [diff] [blame] | 570 | } |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 571 | } |
| 572 | } else { |
| 573 | PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR; |
Doug Zongker | 2c3539e | 2010-09-29 13:21:30 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Tao Bao | 3f5a382 | 2016-12-13 11:14:37 -0800 | [diff] [blame] | 576 | // Any part of the log we'd copied to cache is now gone. |
| 577 | // Reset the pointer so we copy from the beginning of the temp |
| 578 | // log. |
| 579 | tmplog_offset = 0; |
| 580 | copy_logs(); |
| 581 | } |
| 582 | |
| 583 | return (result == 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 586 | // Display a menu with the specified 'headers' and 'items'. Device specific HandleMenuKey() may |
| 587 | // return a positive number beyond the given range. Caller sets 'menu_only' to true to ensure only |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 588 | // a menu item gets selected. 'initial_selection' controls the initial cursor location. Returns the |
| 589 | // (non-negative) chosen item number, or -1 if timed out waiting for input. |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 590 | static int get_menu_selection(const char* const* headers, const char* const* items, bool menu_only, |
| 591 | int initial_selection, Device* device) { |
| 592 | // Throw away keys pressed previously, so user doesn't accidentally trigger menu items. |
| 593 | ui->FlushKeys(); |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 594 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 595 | ui->StartMenu(headers, items, initial_selection); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 596 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 597 | int selected = initial_selection; |
| 598 | int chosen_item = -1; |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 599 | while (chosen_item < 0) { |
| 600 | int key = ui->WaitKey(); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 601 | if (key == -1) { // WaitKey() timed out. |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 602 | if (ui->WasTextEverVisible()) { |
| 603 | continue; |
| 604 | } else { |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 605 | LOG(INFO) << "Timed out waiting for key input; rebooting."; |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 606 | ui->EndMenu(); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 607 | return -1; |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 608 | } |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 609 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 610 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 611 | bool visible = ui->IsTextVisible(); |
| 612 | int action = device->HandleMenuKey(key, visible); |
| 613 | |
| 614 | if (action < 0) { |
| 615 | switch (action) { |
| 616 | case Device::kHighlightUp: |
| 617 | selected = ui->SelectMenu(--selected); |
| 618 | break; |
| 619 | case Device::kHighlightDown: |
| 620 | selected = ui->SelectMenu(++selected); |
| 621 | break; |
| 622 | case Device::kInvokeItem: |
| 623 | chosen_item = selected; |
| 624 | break; |
| 625 | case Device::kNoAction: |
| 626 | break; |
| 627 | } |
| 628 | } else if (!menu_only) { |
| 629 | chosen_item = action; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | ui->EndMenu(); |
| 634 | return chosen_item; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 635 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 636 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 637 | // Returns the selected filename, or an empty string. |
| 638 | static std::string browse_directory(const std::string& path, Device* device) { |
| 639 | ensure_path_mounted(path.c_str()); |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 640 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 641 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir); |
| 642 | if (!d) { |
| 643 | PLOG(ERROR) << "error opening " << path; |
| 644 | return ""; |
| 645 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 646 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 647 | std::vector<std::string> dirs; |
| 648 | std::vector<std::string> zips = { "../" }; // "../" is always the first entry. |
| 649 | |
| 650 | dirent* de; |
| 651 | while ((de = readdir(d.get())) != nullptr) { |
| 652 | std::string name(de->d_name); |
| 653 | |
| 654 | if (de->d_type == DT_DIR) { |
| 655 | // Skip "." and ".." entries. |
| 656 | if (name == "." || name == "..") continue; |
| 657 | dirs.push_back(name + "/"); |
| 658 | } else if (de->d_type == DT_REG && android::base::EndsWithIgnoreCase(name, ".zip")) { |
| 659 | zips.push_back(name); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | std::sort(dirs.begin(), dirs.end()); |
| 664 | std::sort(zips.begin(), zips.end()); |
| 665 | |
| 666 | // Append dirs to the zips list. |
| 667 | zips.insert(zips.end(), dirs.begin(), dirs.end()); |
| 668 | |
| 669 | const char* entries[zips.size() + 1]; |
| 670 | entries[zips.size()] = nullptr; |
| 671 | for (size_t i = 0; i < zips.size(); i++) { |
| 672 | entries[i] = zips[i].c_str(); |
| 673 | } |
| 674 | |
| 675 | const char* headers[] = { "Choose a package to install:", path.c_str(), nullptr }; |
| 676 | |
| 677 | int chosen_item = 0; |
| 678 | while (true) { |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 679 | chosen_item = get_menu_selection(headers, entries, true, chosen_item, device); |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 680 | |
| 681 | const std::string& item = zips[chosen_item]; |
| 682 | if (chosen_item == 0) { |
| 683 | // Go up but continue browsing (if the caller is browse_directory). |
| 684 | return ""; |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 687 | std::string new_path = path + "/" + item; |
| 688 | if (new_path.back() == '/') { |
| 689 | // Recurse down into a subdirectory. |
| 690 | new_path.pop_back(); |
| 691 | std::string result = browse_directory(new_path, device); |
| 692 | if (!result.empty()) return result; |
| 693 | } else { |
| 694 | // Selected a zip file: return the path to the caller. |
| 695 | return new_path; |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 696 | } |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 697 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 698 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 699 | // Unreachable. |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 702 | static bool yes_no(Device* device, const char* question1, const char* question2) { |
Elliott Hughes | 8fd86d7 | 2015-04-13 14:36:02 -0700 | [diff] [blame] | 703 | const char* headers[] = { question1, question2, NULL }; |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 704 | const char* items[] = { " No", " Yes", NULL }; |
Doug Zongker | ddd6a28 | 2009-06-09 12:22:33 -0700 | [diff] [blame] | 705 | |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 706 | int chosen_item = get_menu_selection(headers, items, true, 0, device); |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 707 | return (chosen_item == 1); |
| 708 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 709 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 710 | static bool ask_to_wipe_data(Device* device) { |
| 711 | return yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!"); |
| 712 | } |
Doug Zongker | 1066d2c | 2009-04-01 13:57:40 -0700 | [diff] [blame] | 713 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 714 | // Return true on success. |
| 715 | static bool wipe_data(Device* device) { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 716 | modified_flash = true; |
| 717 | |
Doug Zongker | 211aebc | 2011-10-28 15:13:10 -0700 | [diff] [blame] | 718 | ui->Print("\n-- Wiping data...\n"); |
Paul Crowley | 3b4d516 | 2016-06-08 13:51:41 -0700 | [diff] [blame] | 719 | bool success = device->PreWipeData(); |
| 720 | if (success) { |
| 721 | success &= erase_volume(DATA_ROOT); |
| 722 | if (has_cache) { |
| 723 | success &= erase_volume(CACHE_ROOT); |
| 724 | } |
| 725 | if (volume_for_mount_point(METADATA_ROOT) != nullptr) { |
| 726 | success &= erase_volume(METADATA_ROOT); |
| 727 | } |
| 728 | } |
| 729 | if (success) { |
| 730 | success &= device->PostWipeData(); |
| 731 | } |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 732 | ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); |
| 733 | return success; |
Doug Zongker | f93d816 | 2009-09-22 15:16:02 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 736 | static bool prompt_and_wipe_data(Device* device) { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 737 | // Use a single string and let ScreenRecoveryUI handles the wrapping. |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 738 | const char* const headers[] = { |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 739 | "Can't load Android system. Your data may be corrupt. " |
| 740 | "If you continue to get this message, you may need to " |
| 741 | "perform a factory data reset and erase all user data " |
Paul Crowley | 31ac0c6 | 2017-03-23 12:32:40 -0700 | [diff] [blame] | 742 | "stored on this device.", |
Tao Bao | 2bbc6d6 | 2017-08-13 23:48:55 -0700 | [diff] [blame] | 743 | nullptr |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 744 | }; |
| 745 | const char* const items[] = { |
Paul Crowley | 31ac0c6 | 2017-03-23 12:32:40 -0700 | [diff] [blame] | 746 | "Try again", |
| 747 | "Factory data reset", |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 748 | NULL |
| 749 | }; |
| 750 | for (;;) { |
| 751 | int chosen_item = get_menu_selection(headers, items, true, 0, device); |
| 752 | if (chosen_item != 1) { |
| 753 | 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] | 754 | } |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 755 | if (ask_to_wipe_data(device)) { |
| 756 | return wipe_data(device); |
| 757 | } |
| 758 | } |
Paul Crowley | 08404b4 | 2016-12-19 13:04:23 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Tao Bao | e39a9bc | 2015-03-31 12:19:05 -0700 | [diff] [blame] | 761 | // Return true on success. |
| 762 | static bool wipe_cache(bool should_confirm, Device* device) { |
Tao Bao | 26112e5 | 2016-02-25 12:29:40 -0800 | [diff] [blame] | 763 | if (!has_cache) { |
| 764 | ui->Print("No /cache partition found.\n"); |
| 765 | return false; |
| 766 | } |
| 767 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 768 | 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] | 769 | return false; |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 772 | modified_flash = true; |
| 773 | |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 774 | ui->Print("\n-- Wiping cache...\n"); |
Elliott Hughes | 945548e | 2015-06-05 17:59:56 -0700 | [diff] [blame] | 775 | bool success = erase_volume("/cache"); |
| 776 | ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); |
| 777 | return success; |
Elliott Hughes | 30694c9 | 2015-03-25 15:16:51 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 780 | // Secure-wipe a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with |
| 781 | // BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT. |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 782 | static bool secure_wipe_partition(const std::string& partition) { |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 783 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY))); |
| 784 | if (fd == -1) { |
| 785 | PLOG(ERROR) << "Failed to open \"" << partition << "\""; |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | uint64_t range[2] = { 0, 0 }; |
| 790 | if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) { |
| 791 | PLOG(ERROR) << "Failed to get partition size"; |
| 792 | return false; |
| 793 | } |
| 794 | LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1]; |
| 795 | |
| 796 | LOG(INFO) << " Trying BLKSECDISCARD..."; |
| 797 | if (ioctl(fd, BLKSECDISCARD, &range) == -1) { |
| 798 | PLOG(WARNING) << " Failed"; |
| 799 | |
| 800 | // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT. |
| 801 | unsigned int zeroes; |
| 802 | if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) { |
| 803 | LOG(INFO) << " Trying BLKDISCARD..."; |
| 804 | if (ioctl(fd, BLKDISCARD, &range) == -1) { |
| 805 | PLOG(ERROR) << " Failed"; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 806 | return false; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 807 | } |
| 808 | } else { |
| 809 | LOG(INFO) << " Trying BLKZEROOUT..."; |
| 810 | if (ioctl(fd, BLKZEROOUT, &range) == -1) { |
| 811 | PLOG(ERROR) << " Failed"; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 812 | return false; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 813 | } |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 814 | } |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 815 | } |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 816 | |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 817 | LOG(INFO) << " Done"; |
| 818 | return true; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 821 | // Check if the wipe package matches expectation: |
| 822 | // 1. verify the package. |
| 823 | // 2. check metadata (ota-type, pre-device and serial number if having one). |
| 824 | static bool check_wipe_package(size_t wipe_package_size) { |
| 825 | if (wipe_package_size == 0) { |
| 826 | LOG(ERROR) << "wipe_package_size is zero"; |
| 827 | return false; |
| 828 | } |
| 829 | std::string wipe_package; |
| 830 | std::string err_str; |
| 831 | if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) { |
| 832 | PLOG(ERROR) << "Failed to read wipe package"; |
| 833 | return false; |
| 834 | } |
| 835 | if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()), |
| 836 | wipe_package.size())) { |
| 837 | LOG(ERROR) << "Failed to verify package"; |
| 838 | return false; |
| 839 | } |
| 840 | |
| 841 | // Extract metadata |
| 842 | ZipArchiveHandle zip; |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 843 | int err = OpenArchiveFromMemory(static_cast<void*>(&wipe_package[0]), wipe_package.size(), |
| 844 | "wipe_package", &zip); |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 845 | if (err != 0) { |
| 846 | LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err); |
| 847 | return false; |
| 848 | } |
| 849 | std::string metadata; |
Tao Bao | 1b2a98b | 2017-03-24 10:45:34 -0700 | [diff] [blame] | 850 | if (!read_metadata_from_package(zip, &metadata)) { |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 851 | CloseArchive(zip); |
| 852 | return false; |
| 853 | } |
| 854 | CloseArchive(zip); |
| 855 | |
| 856 | // Check metadata |
| 857 | std::vector<std::string> lines = android::base::Split(metadata, "\n"); |
| 858 | bool ota_type_matched = false; |
| 859 | bool device_type_matched = false; |
| 860 | bool has_serial_number = false; |
| 861 | bool serial_number_matched = false; |
| 862 | for (const auto& line : lines) { |
| 863 | if (line == "ota-type=BRICK") { |
| 864 | ota_type_matched = true; |
| 865 | } else if (android::base::StartsWith(line, "pre-device=")) { |
| 866 | std::string device_type = line.substr(strlen("pre-device=")); |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 867 | std::string real_device_type = android::base::GetProperty("ro.build.product", ""); |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 868 | device_type_matched = (device_type == real_device_type); |
| 869 | } else if (android::base::StartsWith(line, "serialno=")) { |
| 870 | std::string serial_no = line.substr(strlen("serialno=")); |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 871 | std::string real_serial_no = android::base::GetProperty("ro.serialno", ""); |
Yabin Cui | fd99a31 | 2016-06-09 14:09:39 -0700 | [diff] [blame] | 872 | has_serial_number = true; |
| 873 | serial_number_matched = (serial_no == real_serial_no); |
| 874 | } |
| 875 | } |
| 876 | return ota_type_matched && device_type_matched && (!has_serial_number || serial_number_matched); |
| 877 | } |
| 878 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 879 | // 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] | 880 | static bool wipe_ab_device(size_t wipe_package_size) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 881 | ui->SetBackground(RecoveryUI::ERASING); |
| 882 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 883 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 884 | if (!check_wipe_package(wipe_package_size)) { |
| 885 | LOG(ERROR) << "Failed to verify wipe package"; |
| 886 | return false; |
| 887 | } |
| 888 | static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe"; |
| 889 | std::string partition_list; |
| 890 | if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) { |
| 891 | LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\""; |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | std::vector<std::string> lines = android::base::Split(partition_list, "\n"); |
| 896 | for (const std::string& line : lines) { |
| 897 | std::string partition = android::base::Trim(line); |
| 898 | // Ignore '#' comment or empty lines. |
| 899 | if (android::base::StartsWith(partition, "#") || partition.empty()) { |
| 900 | continue; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 903 | // Proceed anyway even if it fails to wipe some partition. |
| 904 | secure_wipe_partition(partition); |
| 905 | } |
| 906 | return true; |
Tao Bao | 862a4c1 | 2016-06-02 11:16:50 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 909 | static void choose_recovery_file(Device* device) { |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 910 | std::vector<std::string> entries; |
| 911 | if (has_cache) { |
| 912 | for (int i = 0; i < KEEP_LOG_COUNT; i++) { |
| 913 | auto add_to_entries = [&](const char* filename) { |
| 914 | std::string log_file(filename); |
| 915 | if (i > 0) { |
| 916 | log_file += "." + std::to_string(i); |
Tao Bao | bef3971 | 2015-05-04 18:50:27 -0700 | [diff] [blame] | 917 | } |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 918 | |
| 919 | if (ensure_path_mounted(log_file.c_str()) == 0 && access(log_file.c_str(), R_OK) == 0) { |
| 920 | entries.push_back(std::move(log_file)); |
| 921 | } |
| 922 | }; |
| 923 | |
| 924 | // Add LAST_LOG_FILE + LAST_LOG_FILE.x |
| 925 | add_to_entries(LAST_LOG_FILE); |
| 926 | |
| 927 | // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x |
| 928 | add_to_entries(LAST_KMSG_FILE); |
| 929 | } |
| 930 | } else { |
| 931 | // If cache partition is not found, view /tmp/recovery.log instead. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 932 | if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) { |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 933 | return; |
Tianjie Xu | a54f75e | 2016-08-17 12:02:46 -0700 | [diff] [blame] | 934 | } else { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 935 | entries.push_back(Paths::Get().temporary_log_file()); |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 936 | } |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 937 | } |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 938 | |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 939 | entries.push_back("Back"); |
Elliott Hughes | c049163 | 2015-05-06 12:40:05 -0700 | [diff] [blame] | 940 | |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 941 | std::vector<const char*> menu_entries(entries.size()); |
| 942 | std::transform(entries.cbegin(), entries.cend(), menu_entries.begin(), |
| 943 | [](const std::string& entry) { return entry.c_str(); }); |
| 944 | menu_entries.push_back(nullptr); |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 945 | |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 946 | const char* headers[] = { "Select file to view", nullptr }; |
Elliott Hughes | 8de5207 | 2015-04-08 20:06:50 -0700 | [diff] [blame] | 947 | |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 948 | int chosen_item = 0; |
| 949 | while (true) { |
Tao Bao | fc5499f | 2017-02-23 19:06:53 -0800 | [diff] [blame] | 950 | chosen_item = get_menu_selection(headers, menu_entries.data(), true, chosen_item, device); |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 951 | if (entries[chosen_item] == "Back") break; |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 952 | |
Tao Bao | 08fc6be | 2017-03-07 00:56:27 -0800 | [diff] [blame] | 953 | ui->ShowFile(entries[chosen_item].c_str()); |
| 954 | } |
Nick Kralevich | a9ad032 | 2014-10-22 18:38:48 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 957 | static void run_graphics_test() { |
| 958 | // Switch to graphics screen. |
| 959 | ui->ShowText(false); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 960 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 961 | ui->SetProgressType(RecoveryUI::INDETERMINATE); |
| 962 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 963 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 964 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 965 | ui->SetBackground(RecoveryUI::ERROR); |
| 966 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 967 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 968 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 969 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 970 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 971 | ui->SetBackground(RecoveryUI::ERASING); |
| 972 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 973 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 974 | // Calling SetBackground() after SetStage() to trigger a redraw. |
| 975 | ui->SetStage(1, 3); |
| 976 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 977 | sleep(1); |
| 978 | ui->SetStage(2, 3); |
| 979 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 980 | sleep(1); |
| 981 | ui->SetStage(3, 3); |
| 982 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
| 983 | sleep(1); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 984 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 985 | ui->SetStage(-1, -1); |
| 986 | ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 987 | |
Tao Bao | db7e898 | 2017-03-06 23:53:16 -0800 | [diff] [blame] | 988 | ui->SetProgressType(RecoveryUI::DETERMINATE); |
| 989 | ui->ShowProgress(1.0, 10.0); |
| 990 | float fraction = 0.0; |
| 991 | for (size_t i = 0; i < 100; ++i) { |
| 992 | fraction += .01; |
| 993 | ui->SetProgress(fraction); |
| 994 | usleep(100000); |
| 995 | } |
| 996 | |
| 997 | ui->ShowText(true); |
Elliott Hughes | 498cda6 | 2016-04-14 16:49:04 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 1000 | // How long (in seconds) we wait for the fuse-provided package file to |
| 1001 | // appear, before timing out. |
| 1002 | #define SDCARD_INSTALL_TIMEOUT 10 |
| 1003 | |
Tao Bao | 145d861 | 2015-03-25 15:51:15 -0700 | [diff] [blame] | 1004 | static int apply_from_sdcard(Device* device, bool* wipe_cache) { |
Tao Bao | 682c34b | 2015-04-07 17:16:35 -0700 | [diff] [blame] | 1005 | modified_flash = true; |
| 1006 | |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 1007 | if (ensure_path_mounted(SDCARD_ROOT) != 0) { |
| 1008 | ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT); |
| 1009 | return INSTALL_ERROR; |
| 1010 | } |
| 1011 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 1012 | std::string path = browse_directory(SDCARD_ROOT, device); |
| 1013 | if (path.empty()) { |
Elliott Hughes | 018ed31 | 2015-04-08 16:51:36 -0700 | [diff] [blame] | 1014 | ui->Print("\n-- No package file selected.\n"); |
caozhiyuan | b4effb9 | 2015-06-10 16:46:38 +0800 | [diff] [blame] | 1015 | ensure_path_unmounted(SDCARD_ROOT); |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 1016 | return INSTALL_ERROR; |
| 1017 | } |
| 1018 | |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 1019 | ui->Print("\n-- Install %s ...\n", path.c_str()); |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 1020 | set_sdcard_update_bootloader_message(); |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 1021 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 1022 | // We used to use fuse in a thread as opposed to a process. Since accessing |
| 1023 | // through fuse involves going from kernel to userspace to kernel, it leads |
| 1024 | // to deadlock when a page fault occurs. (Bug: 26313124) |
| 1025 | pid_t child; |
| 1026 | if ((child = fork()) == 0) { |
Tao Bao | c4a18ef | 2017-02-10 00:13:30 -0800 | [diff] [blame] | 1027 | bool status = start_sdcard_fuse(path.c_str()); |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 1028 | |
| 1029 | _exit(status ? EXIT_SUCCESS : EXIT_FAILURE); |
| 1030 | } |
| 1031 | |
| 1032 | // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child |
| 1033 | // process is ready. |
| 1034 | int result = INSTALL_ERROR; |
| 1035 | int status; |
| 1036 | bool waited = false; |
| 1037 | for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) { |
| 1038 | if (waitpid(child, &status, WNOHANG) == -1) { |
| 1039 | result = INSTALL_ERROR; |
| 1040 | waited = true; |
| 1041 | break; |
| 1042 | } |
| 1043 | |
| 1044 | struct stat sb; |
| 1045 | if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) { |
| 1046 | if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) { |
| 1047 | sleep(1); |
| 1048 | continue; |
| 1049 | } else { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 1050 | LOG(ERROR) << "Timed out waiting for the fuse-provided package."; |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 1051 | result = INSTALL_ERROR; |
| 1052 | kill(child, SIGKILL); |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1057 | 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] | 1058 | break; |
| 1059 | } |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 1060 | |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 1061 | if (!waited) { |
| 1062 | // Calling stat() on this magic filename signals the fuse |
| 1063 | // filesystem to shut down. |
| 1064 | struct stat sb; |
| 1065 | stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb); |
| 1066 | |
| 1067 | waitpid(child, &status, 0); |
| 1068 | } |
| 1069 | |
| 1070 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 1071 | LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status); |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 1074 | ensure_path_unmounted(SDCARD_ROOT); |
Tao Bao | cdcf28f | 2016-01-13 15:05:20 -0800 | [diff] [blame] | 1075 | return result; |
Christian Poetzsch | 4ec58a4 | 2015-02-19 10:42:39 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 1078 | // Returns REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default, |
| 1079 | // which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery. |
| 1080 | static Device::BuiltinAction prompt_and_wait(Device* device, int status) { |
| 1081 | for (;;) { |
| 1082 | finish_recovery(); |
| 1083 | switch (status) { |
| 1084 | case INSTALL_SUCCESS: |
| 1085 | case INSTALL_NONE: |
| 1086 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 1087 | break; |
Doug Zongker | 6c8553d | 2012-09-24 10:40:47 -0700 | [diff] [blame] | 1088 | |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 1089 | case INSTALL_ERROR: |
| 1090 | case INSTALL_CORRUPT: |
| 1091 | ui->SetBackground(RecoveryUI::ERROR); |
| 1092 | break; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1093 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 1094 | ui->SetProgressType(RecoveryUI::EMPTY); |
| 1095 | |
| 1096 | int chosen_item = get_menu_selection(nullptr, device->GetMenuItems(), false, 0, device); |
| 1097 | |
| 1098 | // Device-specific code may take some action here. It may return one of the core actions |
| 1099 | // handled in the switch statement below. |
| 1100 | Device::BuiltinAction chosen_action = |
| 1101 | (chosen_item == -1) ? Device::REBOOT : device->InvokeMenuItem(chosen_item); |
| 1102 | |
| 1103 | bool should_wipe_cache = false; |
| 1104 | switch (chosen_action) { |
| 1105 | case Device::NO_ACTION: |
| 1106 | break; |
| 1107 | |
| 1108 | case Device::REBOOT: |
| 1109 | case Device::SHUTDOWN: |
| 1110 | case Device::REBOOT_BOOTLOADER: |
| 1111 | return chosen_action; |
| 1112 | |
| 1113 | case Device::WIPE_DATA: |
| 1114 | if (ui->IsTextVisible()) { |
| 1115 | if (ask_to_wipe_data(device)) { |
| 1116 | wipe_data(device); |
| 1117 | } |
| 1118 | } else { |
| 1119 | wipe_data(device); |
| 1120 | return Device::NO_ACTION; |
| 1121 | } |
| 1122 | break; |
| 1123 | |
| 1124 | case Device::WIPE_CACHE: |
| 1125 | wipe_cache(ui->IsTextVisible(), device); |
| 1126 | if (!ui->IsTextVisible()) return Device::NO_ACTION; |
| 1127 | break; |
| 1128 | |
| 1129 | case Device::APPLY_ADB_SIDELOAD: |
| 1130 | case Device::APPLY_SDCARD: |
| 1131 | { |
| 1132 | bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); |
| 1133 | if (adb) { |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1134 | status = apply_from_adb(&should_wipe_cache); |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 1135 | } else { |
| 1136 | status = apply_from_sdcard(device, &should_wipe_cache); |
| 1137 | } |
| 1138 | |
| 1139 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 1140 | if (!wipe_cache(false, device)) { |
| 1141 | status = INSTALL_ERROR; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if (status != INSTALL_SUCCESS) { |
| 1146 | ui->SetBackground(RecoveryUI::ERROR); |
| 1147 | ui->Print("Installation aborted.\n"); |
| 1148 | copy_logs(); |
| 1149 | } else if (!ui->IsTextVisible()) { |
| 1150 | return Device::NO_ACTION; // reboot if logs aren't visible |
| 1151 | } else { |
| 1152 | ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card"); |
| 1153 | } |
| 1154 | } |
| 1155 | break; |
| 1156 | |
| 1157 | case Device::VIEW_RECOVERY_LOGS: |
| 1158 | choose_recovery_file(device); |
| 1159 | break; |
| 1160 | |
| 1161 | case Device::RUN_GRAPHICS_TEST: |
| 1162 | run_graphics_test(); |
| 1163 | break; |
| 1164 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 1165 | case Device::RUN_LOCALE_TEST: { |
| 1166 | ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui); |
| 1167 | screen_ui->CheckBackgroundTextImages(locale); |
| 1168 | break; |
| 1169 | } |
Tao Bao | 50dd532 | 2017-03-07 14:57:04 -0800 | [diff] [blame] | 1170 | case Device::MOUNT_SYSTEM: |
| 1171 | // For a system image built with the root directory (i.e. system_root_image == "true"), we |
| 1172 | // mount it to /system_root, and symlink /system to /system_root/system to make adb shell |
| 1173 | // work (the symlink is created through the build system). (Bug: 22855115) |
| 1174 | if (android::base::GetBoolProperty("ro.build.system_root_image", false)) { |
| 1175 | if (ensure_path_mounted_at("/", "/system_root") != -1) { |
| 1176 | ui->Print("Mounted /system.\n"); |
| 1177 | } |
| 1178 | } else { |
| 1179 | if (ensure_path_mounted("/system") != -1) { |
| 1180 | ui->Print("Mounted /system.\n"); |
| 1181 | } |
| 1182 | } |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 1188 | static void print_property(const char* key, const char* name, void* /* cookie */) { |
| 1189 | printf("%s=%s\n", key, name); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1192 | static std::string load_locale_from_cache() { |
| 1193 | if (ensure_path_mounted(LOCALE_FILE) != 0) { |
| 1194 | LOG(ERROR) << "Can't mount " << LOCALE_FILE; |
| 1195 | return ""; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1196 | } |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1197 | |
| 1198 | std::string content; |
| 1199 | if (!android::base::ReadFileToString(LOCALE_FILE, &content)) { |
| 1200 | PLOG(ERROR) << "Can't read " << LOCALE_FILE; |
| 1201 | return ""; |
| 1202 | } |
| 1203 | |
| 1204 | return android::base::Trim(content); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1207 | void ui_print(const char* format, ...) { |
| 1208 | std::string buffer; |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 1209 | va_list ap; |
| 1210 | va_start(ap, format); |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1211 | android::base::StringAppendV(&buffer, format, ap); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 1212 | va_end(ap); |
| 1213 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1214 | if (ui != nullptr) { |
| 1215 | ui->Print("%s", buffer.c_str()); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 1216 | } else { |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1217 | fputs(buffer.c_str(), stdout); |
Doug Zongker | 7c3ae45 | 2013-05-14 11:03:02 -0700 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 1221 | static constexpr char log_characters[] = "VDIWEF"; |
| 1222 | |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 1223 | void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity, |
| 1224 | const char* /* tag */, const char* /* file */, unsigned int /* line */, |
| 1225 | const char* message) { |
| 1226 | if (severity >= android::base::ERROR && ui != nullptr) { |
| 1227 | ui->Print("E:%s\n", message); |
| 1228 | } else { |
| 1229 | fprintf(stdout, "%c:%s\n", log_characters[severity], message); |
| 1230 | } |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 1233 | static bool is_battery_ok(int* required_battery_level) { |
| 1234 | struct healthd_config healthd_config = { |
| 1235 | .batteryStatusPath = android::String8(android::String8::kEmptyString), |
| 1236 | .batteryHealthPath = android::String8(android::String8::kEmptyString), |
| 1237 | .batteryPresentPath = android::String8(android::String8::kEmptyString), |
| 1238 | .batteryCapacityPath = android::String8(android::String8::kEmptyString), |
| 1239 | .batteryVoltagePath = android::String8(android::String8::kEmptyString), |
| 1240 | .batteryTemperaturePath = android::String8(android::String8::kEmptyString), |
| 1241 | .batteryTechnologyPath = android::String8(android::String8::kEmptyString), |
| 1242 | .batteryCurrentNowPath = android::String8(android::String8::kEmptyString), |
| 1243 | .batteryCurrentAvgPath = android::String8(android::String8::kEmptyString), |
| 1244 | .batteryChargeCounterPath = android::String8(android::String8::kEmptyString), |
| 1245 | .batteryFullChargePath = android::String8(android::String8::kEmptyString), |
| 1246 | .batteryCycleCountPath = android::String8(android::String8::kEmptyString), |
| 1247 | .energyCounter = nullptr, |
| 1248 | .boot_min_cap = 0, |
| 1249 | .screen_on = nullptr |
| 1250 | }; |
| 1251 | healthd_board_init(&healthd_config); |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 1252 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 1253 | android::BatteryMonitor monitor; |
| 1254 | monitor.init(&healthd_config); |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 1255 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 1256 | static constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10; |
| 1257 | int wait_second = 0; |
| 1258 | while (true) { |
| 1259 | int charge_status = monitor.getChargeStatus(); |
| 1260 | // Treat unknown status as charged. |
| 1261 | bool charged = (charge_status != android::BATTERY_STATUS_DISCHARGING && |
| 1262 | charge_status != android::BATTERY_STATUS_NOT_CHARGING); |
| 1263 | android::BatteryProperty capacity; |
| 1264 | android::status_t status = monitor.getProperty(android::BATTERY_PROP_CAPACITY, &capacity); |
| 1265 | ui_print("charge_status %d, charged %d, status %d, capacity %" PRId64 "\n", charge_status, |
| 1266 | charged, status, capacity.valueInt64); |
| 1267 | // At startup, the battery drivers in devices like N5X/N6P take some time to load |
| 1268 | // the battery profile. Before the load finishes, it reports value 50 as a fake |
| 1269 | // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected |
| 1270 | // to finish loading the battery profile earlier than 10 seconds after kernel startup. |
| 1271 | if (status == 0 && capacity.valueInt64 == 50) { |
| 1272 | if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) { |
| 1273 | sleep(1); |
| 1274 | wait_second++; |
| 1275 | continue; |
| 1276 | } |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 1277 | } |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 1278 | // If we can't read battery percentage, it may be a device without battery. In this situation, |
| 1279 | // use 100 as a fake battery percentage. |
| 1280 | if (status != 0) { |
| 1281 | capacity.valueInt64 = 100; |
| 1282 | } |
| 1283 | |
| 1284 | // GmsCore enters recovery mode to install package when having enough battery percentage. |
| 1285 | // Normally, the threshold is 40% without charger and 20% with charger. So we should check |
| 1286 | // battery with a slightly lower limitation. |
| 1287 | static constexpr int BATTERY_OK_PERCENTAGE = 20; |
| 1288 | static constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; |
| 1289 | *required_battery_level = charged ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE; |
| 1290 | return capacity.valueInt64 >= *required_battery_level; |
| 1291 | } |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1294 | // Set the retry count to |retry_count| in BCB. |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 1295 | static void set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) { |
| 1296 | std::vector<std::string> options; |
| 1297 | for (const auto& arg : args) { |
| 1298 | if (!android::base::StartsWith(arg, "--retry_count")) { |
| 1299 | options.push_back(arg); |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 1300 | } |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 1301 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 1302 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1303 | // Update the retry counter in BCB. |
| 1304 | options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count)); |
Tianjie Xu | 72449c9 | 2017-05-16 18:07:31 -0700 | [diff] [blame] | 1305 | std::string err; |
| 1306 | if (!update_bootloader_message(options, &err)) { |
| 1307 | LOG(ERROR) << err; |
| 1308 | } |
Tianjie Xu | 3c62b67 | 2016-02-05 18:25:58 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 1311 | static bool bootreason_in_blacklist() { |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 1312 | std::string bootreason = android::base::GetProperty("ro.boot.bootreason", ""); |
| 1313 | if (!bootreason.empty()) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 1314 | // More bootreasons can be found in "system/core/bootstat/bootstat.cpp". |
| 1315 | static const std::vector<std::string> kBootreasonBlacklist{ |
| 1316 | "kernel_panic", |
| 1317 | "Panic", |
| 1318 | }; |
| 1319 | for (const auto& str : kBootreasonBlacklist) { |
| 1320 | if (android::base::EqualsIgnoreCase(str, bootreason)) return true; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 1321 | } |
Tao Bao | efc3559 | 2017-01-08 22:45:47 -0800 | [diff] [blame] | 1322 | } |
| 1323 | return false; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1326 | static void log_failure_code(ErrorCode code, const std::string& update_package) { |
| 1327 | std::vector<std::string> log_buffer = { |
| 1328 | update_package, |
| 1329 | "0", // install result |
| 1330 | "error: " + std::to_string(code), |
| 1331 | }; |
| 1332 | std::string log_content = android::base::Join(log_buffer, "\n"); |
| 1333 | const std::string& install_file = Paths::Get().temporary_install_file(); |
| 1334 | if (!android::base::WriteStringToFile(log_content, install_file)) { |
| 1335 | PLOG(ERROR) << "Failed to write " << install_file; |
| 1336 | } |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 1337 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1338 | // Also write the info into last_log. |
| 1339 | LOG(INFO) << log_content; |
Tianjie Xu | 06e57ac | 2016-07-11 14:04:08 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Yabin Cui | 99281df | 2016-02-17 12:21:52 -0800 | [diff] [blame] | 1342 | int main(int argc, char **argv) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1343 | // We don't have logcat yet under recovery; so we'll print error on screen and |
| 1344 | // log to stdout (which is redirected to recovery.log) as we used to do. |
| 1345 | android::base::InitLogging(argv, &UiLogger); |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 1346 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1347 | // Take last pmsg contents and rewrite it to the current pmsg session. |
| 1348 | static const char filter[] = "recovery/"; |
| 1349 | // Do we need to rotate? |
| 1350 | bool doRotate = false; |
Tianjie Xu | e113e4d | 2016-10-21 17:46:13 -0700 | [diff] [blame] | 1351 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1352 | __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &doRotate); |
| 1353 | // Take action to refresh pmsg contents |
| 1354 | __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &doRotate); |
Mark Salyzyn | a4f701a | 2016-03-09 14:58:16 -0800 | [diff] [blame] | 1355 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1356 | // If this binary is started with the single argument "--adbd", |
| 1357 | // instead of being the normal recovery binary, it turns into kind |
| 1358 | // of a stripped-down version of adbd that only supports the |
| 1359 | // 'sideload' command. Note this must be a real argument, not |
| 1360 | // anything in the command file or bootloader control block; the |
| 1361 | // only way recovery should be run with this argument is when it |
| 1362 | // starts a copy of itself from the apply_from_adb() function. |
| 1363 | if (argc == 2 && strcmp(argv[1], "--adbd") == 0) { |
| 1364 | minadbd_main(); |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | time_t start = time(nullptr); |
| 1369 | |
| 1370 | // redirect_stdio should be called only in non-sideload mode. Otherwise |
| 1371 | // we may have two logger instances with different timestamps. |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1372 | redirect_stdio(Paths::Get().temporary_log_file().c_str()); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1373 | |
| 1374 | printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start)); |
| 1375 | |
| 1376 | load_volume_table(); |
| 1377 | has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr; |
| 1378 | |
| 1379 | std::vector<std::string> args = get_args(argc, argv); |
| 1380 | std::vector<char*> args_to_parse(args.size()); |
| 1381 | std::transform(args.cbegin(), args.cend(), args_to_parse.begin(), |
| 1382 | [](const std::string& arg) { return const_cast<char*>(arg.c_str()); }); |
| 1383 | |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 1384 | static constexpr struct option OPTIONS[] = { |
| 1385 | { "just_exit", no_argument, nullptr, 'x' }, |
| 1386 | { "locale", required_argument, nullptr, 0 }, |
| 1387 | { "prompt_and_wipe_data", no_argument, nullptr, 0 }, |
| 1388 | { "reason", required_argument, nullptr, 0 }, |
| 1389 | { "retry_count", required_argument, nullptr, 0 }, |
| 1390 | { "security", no_argument, nullptr, 0 }, |
| 1391 | { "show_text", no_argument, nullptr, 't' }, |
| 1392 | { "shutdown_after", no_argument, nullptr, 0 }, |
| 1393 | { "sideload", no_argument, nullptr, 0 }, |
| 1394 | { "sideload_auto_reboot", no_argument, nullptr, 0 }, |
| 1395 | { "update_package", required_argument, nullptr, 0 }, |
| 1396 | { "wipe_ab", no_argument, nullptr, 0 }, |
| 1397 | { "wipe_cache", no_argument, nullptr, 0 }, |
| 1398 | { "wipe_data", no_argument, nullptr, 0 }, |
| 1399 | { "wipe_package_size", required_argument, nullptr, 0 }, |
| 1400 | { nullptr, 0, nullptr, 0 }, |
| 1401 | }; |
| 1402 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1403 | const char* update_package = nullptr; |
| 1404 | bool should_wipe_data = false; |
| 1405 | bool should_prompt_and_wipe_data = false; |
| 1406 | bool should_wipe_cache = false; |
| 1407 | bool should_wipe_ab = false; |
| 1408 | size_t wipe_package_size = 0; |
| 1409 | bool show_text = false; |
| 1410 | bool sideload = false; |
| 1411 | bool sideload_auto_reboot = false; |
| 1412 | bool just_exit = false; |
| 1413 | bool shutdown_after = false; |
| 1414 | int retry_count = 0; |
| 1415 | bool security_update = false; |
| 1416 | |
| 1417 | int arg; |
| 1418 | int option_index; |
| 1419 | while ((arg = getopt_long(args_to_parse.size(), args_to_parse.data(), "", OPTIONS, |
| 1420 | &option_index)) != -1) { |
| 1421 | switch (arg) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1422 | case 't': |
| 1423 | show_text = true; |
| 1424 | break; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1425 | case 'x': |
| 1426 | just_exit = true; |
| 1427 | break; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1428 | case 0: { |
| 1429 | std::string option = OPTIONS[option_index].name; |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 1430 | if (option == "locale") { |
| 1431 | locale = optarg; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1432 | } else if (option == "prompt_and_wipe_data") { |
| 1433 | should_prompt_and_wipe_data = true; |
Tao Bao | f9f1734 | 2018-04-27 10:44:04 -0700 | [diff] [blame] | 1434 | } else if (option == "reason") { |
| 1435 | reason = optarg; |
| 1436 | } else if (option == "retry_count") { |
| 1437 | android::base::ParseInt(optarg, &retry_count, 0); |
| 1438 | } else if (option == "security") { |
| 1439 | security_update = true; |
| 1440 | } else if (option == "sideload") { |
| 1441 | sideload = true; |
| 1442 | } else if (option == "sideload_auto_reboot") { |
| 1443 | sideload = true; |
| 1444 | sideload_auto_reboot = true; |
| 1445 | } else if (option == "shutdown_after") { |
| 1446 | shutdown_after = true; |
| 1447 | } else if (option == "update_package") { |
| 1448 | update_package = optarg; |
| 1449 | } else if (option == "wipe_ab") { |
| 1450 | should_wipe_ab = true; |
| 1451 | } else if (option == "wipe_cache") { |
| 1452 | should_wipe_cache = true; |
| 1453 | } else if (option == "wipe_data") { |
| 1454 | should_wipe_data = true; |
| 1455 | } else if (option == "wipe_package_size") { |
| 1456 | android::base::ParseUint(optarg, &wipe_package_size); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1457 | } |
| 1458 | break; |
| 1459 | } |
| 1460 | case '?': |
| 1461 | LOG(ERROR) << "Invalid command argument"; |
| 1462 | continue; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1463 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1464 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1465 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1466 | if (locale.empty()) { |
| 1467 | if (has_cache) { |
| 1468 | locale = load_locale_from_cache(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1471 | if (locale.empty()) { |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 1472 | static constexpr const char* DEFAULT_LOCALE = "en-US"; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1473 | locale = DEFAULT_LOCALE; |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1474 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1475 | } |
Tao Bao | ac9d94d | 2016-11-03 11:37:15 -0700 | [diff] [blame] | 1476 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1477 | printf("locale is [%s]\n", locale.c_str()); |
| 1478 | printf("stage is [%s]\n", stage.c_str()); |
| 1479 | printf("reason is [%s]\n", reason); |
Doug Zongker | 02ec6b8 | 2012-08-22 17:26:40 -0700 | [diff] [blame] | 1480 | |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1481 | Device* device = make_device(); |
| 1482 | if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { |
| 1483 | printf("Quiescent recovery mode.\n"); |
| 1484 | ui = new StubRecoveryUI(); |
| 1485 | } else { |
| 1486 | ui = device->GetUI(); |
| 1487 | |
| 1488 | if (!ui->Init(locale)) { |
| 1489 | printf("Failed to initialize UI, use stub UI instead.\n"); |
| 1490 | ui = new StubRecoveryUI(); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | // Set background string to "installing security update" for security update, |
| 1495 | // otherwise set it to "installing system update". |
| 1496 | ui->SetSystemUpdateText(security_update); |
| 1497 | |
| 1498 | int st_cur, st_max; |
| 1499 | if (!stage.empty() && sscanf(stage.c_str(), "%d/%d", &st_cur, &st_max) == 2) { |
| 1500 | ui->SetStage(st_cur, st_max); |
| 1501 | } |
| 1502 | |
| 1503 | ui->SetBackground(RecoveryUI::NONE); |
| 1504 | if (show_text) ui->ShowText(true); |
| 1505 | |
| 1506 | sehandle = selinux_android_file_context_handle(); |
| 1507 | selinux_android_set_sehandle(sehandle); |
| 1508 | if (!sehandle) { |
| 1509 | ui->Print("Warning: No file_contexts\n"); |
| 1510 | } |
| 1511 | |
| 1512 | device->StartRecovery(); |
| 1513 | |
| 1514 | printf("Command:"); |
| 1515 | for (const auto& arg : args) { |
| 1516 | printf(" \"%s\"", arg.c_str()); |
| 1517 | } |
| 1518 | printf("\n\n"); |
| 1519 | |
| 1520 | property_list(print_property, nullptr); |
| 1521 | printf("\n"); |
| 1522 | |
| 1523 | ui->Print("Supported API: %d\n", kRecoveryApiVersion); |
| 1524 | |
| 1525 | int status = INSTALL_SUCCESS; |
| 1526 | |
| 1527 | if (update_package != nullptr) { |
| 1528 | // It's not entirely true that we will modify the flash. But we want |
| 1529 | // to log the update attempt since update_package is non-NULL. |
| 1530 | modified_flash = true; |
| 1531 | |
Tao Bao | f2ea6d7 | 2018-04-26 10:40:36 -0700 | [diff] [blame] | 1532 | int required_battery_level; |
| 1533 | if (retry_count == 0 && !is_battery_ok(&required_battery_level)) { |
| 1534 | ui->Print("battery capacity is not enough for installing package: %d%% needed\n", |
| 1535 | required_battery_level); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1536 | // Log the error code to last_install when installation skips due to |
| 1537 | // low battery. |
| 1538 | log_failure_code(kLowBattery, update_package); |
| 1539 | status = INSTALL_SKIPPED; |
Tianjie Xu | a6f49bd | 2018-03-26 14:32:11 -0700 | [diff] [blame] | 1540 | } else if (retry_count == 0 && bootreason_in_blacklist()) { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1541 | // Skip update-on-reboot when bootreason is kernel_panic or similar |
| 1542 | ui->Print("bootreason is in the blacklist; skip OTA installation\n"); |
| 1543 | log_failure_code(kBootreasonInBlacklist, update_package); |
| 1544 | status = INSTALL_SKIPPED; |
Dmitri Plotnikov | 8706a98 | 2017-04-18 08:28:26 -0700 | [diff] [blame] | 1545 | } else { |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1546 | // It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later |
| 1547 | // identify the interrupted update due to unexpected reboots. |
| 1548 | if (retry_count == 0) { |
| 1549 | set_retry_bootloader_message(retry_count + 1, args); |
Tao Bao | 7022f33 | 2017-07-25 09:52:36 -0700 | [diff] [blame] | 1550 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1551 | |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1552 | status = install_package(update_package, &should_wipe_cache, true, retry_count); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1553 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 1554 | wipe_cache(false, device); |
| 1555 | } |
| 1556 | if (status != INSTALL_SUCCESS) { |
| 1557 | ui->Print("Installation aborted.\n"); |
Tao Bao | aac9d9f | 2018-04-29 23:38:59 -0700 | [diff] [blame^] | 1558 | |
| 1559 | // When I/O error or bspatch/imgpatch error happens, reboot and retry installation |
| 1560 | // RETRY_LIMIT times before we abandon this OTA update. |
| 1561 | static constexpr int RETRY_LIMIT = 4; |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1562 | if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) { |
| 1563 | copy_logs(); |
| 1564 | retry_count += 1; |
| 1565 | set_retry_bootloader_message(retry_count, args); |
| 1566 | // Print retry count on screen. |
| 1567 | ui->Print("Retry attempt %d\n", retry_count); |
| 1568 | |
| 1569 | // Reboot and retry the update |
| 1570 | if (!reboot("reboot,recovery")) { |
| 1571 | ui->Print("Reboot failed\n"); |
| 1572 | } else { |
| 1573 | while (true) { |
| 1574 | pause(); |
| 1575 | } |
| 1576 | } |
Tianjie Xu | d9d1629 | 2017-04-20 18:08:21 -0700 | [diff] [blame] | 1577 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1578 | // If this is an eng or userdebug build, then automatically |
| 1579 | // turn the text display on if the script fails so the error |
| 1580 | // message is visible. |
| 1581 | if (is_ro_debuggable()) { |
| 1582 | ui->ShowText(true); |
Tao Bao | c679f93 | 2015-03-30 09:43:49 -0700 | [diff] [blame] | 1583 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1584 | } |
Doug Zongker | 8674a72 | 2010-09-15 11:08:23 -0700 | [diff] [blame] | 1585 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1586 | } else if (should_wipe_data) { |
| 1587 | if (!wipe_data(device)) { |
| 1588 | status = INSTALL_ERROR; |
Doug Zongker | b1d1263 | 2014-03-18 10:32:12 -0700 | [diff] [blame] | 1589 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1590 | } else if (should_prompt_and_wipe_data) { |
| 1591 | ui->ShowText(true); |
| 1592 | ui->SetBackground(RecoveryUI::ERROR); |
| 1593 | if (!prompt_and_wipe_data(device)) { |
| 1594 | status = INSTALL_ERROR; |
Tao Bao | 7523863 | 2015-05-27 14:46:17 -0700 | [diff] [blame] | 1595 | } |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1596 | ui->ShowText(false); |
| 1597 | } else if (should_wipe_cache) { |
| 1598 | if (!wipe_cache(false, device)) { |
| 1599 | status = INSTALL_ERROR; |
| 1600 | } |
| 1601 | } else if (should_wipe_ab) { |
| 1602 | if (!wipe_ab_device(wipe_package_size)) { |
| 1603 | status = INSTALL_ERROR; |
| 1604 | } |
| 1605 | } else if (sideload) { |
| 1606 | // 'adb reboot sideload' acts the same as user presses key combinations |
| 1607 | // to enter the sideload mode. When 'sideload-auto-reboot' is used, text |
| 1608 | // display will NOT be turned on by default. And it will reboot after |
| 1609 | // sideload finishes even if there are errors. Unless one turns on the |
| 1610 | // text display during the installation. This is to enable automated |
| 1611 | // testing. |
| 1612 | if (!sideload_auto_reboot) { |
| 1613 | ui->ShowText(true); |
| 1614 | } |
Tao Bao | 641fa97 | 2018-04-25 18:59:40 -0700 | [diff] [blame] | 1615 | status = apply_from_adb(&should_wipe_cache); |
Tianjie Xu | 99b73be | 2017-11-28 17:23:06 -0800 | [diff] [blame] | 1616 | if (status == INSTALL_SUCCESS && should_wipe_cache) { |
| 1617 | if (!wipe_cache(false, device)) { |
| 1618 | status = INSTALL_ERROR; |
| 1619 | } |
| 1620 | } |
| 1621 | ui->Print("\nInstall from ADB complete (status: %d).\n", status); |
| 1622 | if (sideload_auto_reboot) { |
| 1623 | ui->Print("Rebooting automatically.\n"); |
| 1624 | } |
| 1625 | } else if (!just_exit) { |
| 1626 | // If this is an eng or userdebug build, automatically turn on the text display if no command |
| 1627 | // is specified. Note that this should be called before setting the background to avoid |
| 1628 | // flickering the background image. |
| 1629 | if (is_ro_debuggable()) { |
| 1630 | ui->ShowText(true); |
| 1631 | } |
| 1632 | status = INSTALL_NONE; // No command specified |
| 1633 | ui->SetBackground(RecoveryUI::NO_COMMAND); |
| 1634 | } |
| 1635 | |
| 1636 | if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) { |
| 1637 | ui->SetBackground(RecoveryUI::ERROR); |
| 1638 | if (!ui->IsTextVisible()) { |
| 1639 | sleep(5); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT; |
| 1644 | // 1. If the recovery menu is visible, prompt and wait for commands. |
| 1645 | // 2. If the state is INSTALL_NONE, wait for commands. (i.e. In user build, manually reboot into |
| 1646 | // recovery to sideload a package.) |
| 1647 | // 3. sideload_auto_reboot is an option only available in user-debug build, reboot the device |
| 1648 | // without waiting. |
| 1649 | // 4. In all other cases, reboot the device. Therefore, normal users will observe the device |
| 1650 | // reboot after it shows the "error" screen for 5s. |
| 1651 | if ((status == INSTALL_NONE && !sideload_auto_reboot) || ui->IsTextVisible()) { |
| 1652 | Device::BuiltinAction temp = prompt_and_wait(device, status); |
| 1653 | if (temp != Device::NO_ACTION) { |
| 1654 | after = temp; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | // Save logs and clean up before rebooting or shutting down. |
| 1659 | finish_recovery(); |
| 1660 | |
| 1661 | switch (after) { |
| 1662 | case Device::SHUTDOWN: |
| 1663 | ui->Print("Shutting down...\n"); |
| 1664 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,"); |
| 1665 | break; |
| 1666 | |
| 1667 | case Device::REBOOT_BOOTLOADER: |
| 1668 | ui->Print("Rebooting to bootloader...\n"); |
| 1669 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
| 1670 | break; |
| 1671 | |
| 1672 | default: |
| 1673 | ui->Print("Rebooting...\n"); |
| 1674 | reboot("reboot,"); |
| 1675 | break; |
| 1676 | } |
| 1677 | while (true) { |
| 1678 | pause(); |
| 1679 | } |
| 1680 | // Should be unreachable. |
| 1681 | return EXIT_SUCCESS; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1682 | } |