Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Tao Bao | 42c45e2 | 2018-07-31 09:37:12 -0700 | [diff] [blame] | 17 | #include <dlfcn.h> |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <getopt.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <limits.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <stdarg.h> |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <time.h> |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 33 | #include <atomic> |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 34 | #include <string> |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 35 | #include <thread> |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 36 | #include <vector> |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 37 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 38 | #include <android-base/file.h> |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 39 | #include <android-base/logging.h> |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 40 | #include <android-base/properties.h> |
| 41 | #include <android-base/strings.h> |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 42 | #include <android-base/unique_fd.h> |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 43 | #include <bootloader_message/bootloader_message.h> |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 44 | #include <cutils/sockets.h> |
Tao Bao | f90d9a1 | 2019-05-10 10:40:59 -0700 | [diff] [blame] | 45 | #include <fs_mgr/roots.h> |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 46 | #include <private/android_logger.h> /* private pmsg functions */ |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 47 | #include <selinux/android.h> |
| 48 | #include <selinux/label.h> |
| 49 | #include <selinux/selinux.h> |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 50 | |
| 51 | #include "common.h" |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 52 | #include "fastboot/fastboot.h" |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 53 | #include "install/wipe_data.h" |
| 54 | #include "otautil/logging.h" |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 55 | #include "otautil/paths.h" |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 56 | #include "otautil/roots.h" |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 57 | #include "otautil/sysutil.h" |
| 58 | #include "recovery.h" |
Tianjie Xu | 8f39730 | 2018-08-20 13:40:47 -0700 | [diff] [blame] | 59 | #include "recovery_ui/device.h" |
| 60 | #include "recovery_ui/stub_ui.h" |
| 61 | #include "recovery_ui/ui.h" |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 62 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 63 | static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; |
| 64 | static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale"; |
| 65 | |
| 66 | static constexpr const char* CACHE_ROOT = "/cache"; |
| 67 | |
| 68 | bool has_cache = false; |
| 69 | |
| 70 | RecoveryUI* ui = nullptr; |
| 71 | struct selabel_handle* sehandle; |
| 72 | |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 73 | static void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity, |
| 74 | const char* /* tag */, const char* /* file */, unsigned int /* line */, |
| 75 | const char* message) { |
| 76 | static constexpr char log_characters[] = "VDIWEF"; |
| 77 | if (severity >= android::base::ERROR && ui != nullptr) { |
| 78 | ui->Print("E:%s\n", message); |
| 79 | } else { |
| 80 | fprintf(stdout, "%c:%s\n", log_characters[severity], message); |
| 81 | } |
| 82 | } |
| 83 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 84 | // command line args come from, in decreasing precedence: |
| 85 | // - the actual command line |
| 86 | // - the bootloader control block (one per line, after "recovery") |
| 87 | // - the contents of COMMAND_FILE (one per line) |
| 88 | static std::vector<std::string> get_args(const int argc, char** const argv) { |
| 89 | CHECK_GT(argc, 0); |
| 90 | |
| 91 | bootloader_message boot = {}; |
| 92 | std::string err; |
| 93 | if (!read_bootloader_message(&boot, &err)) { |
| 94 | LOG(ERROR) << err; |
| 95 | // If fails, leave a zeroed bootloader_message. |
| 96 | boot = {}; |
| 97 | } |
| 98 | stage = std::string(boot.stage); |
| 99 | |
David Anderson | eee4e26 | 2018-08-21 13:10:45 -0700 | [diff] [blame] | 100 | std::string boot_command; |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 101 | if (boot.command[0] != 0) { |
David Anderson | eee4e26 | 2018-08-21 13:10:45 -0700 | [diff] [blame] | 102 | if (memchr(boot.command, '\0', sizeof(boot.command))) { |
| 103 | boot_command = std::string(boot.command); |
| 104 | } else { |
| 105 | boot_command = std::string(boot.command, sizeof(boot.command)); |
| 106 | } |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 107 | LOG(INFO) << "Boot command: " << boot_command; |
| 108 | } |
| 109 | |
| 110 | if (boot.status[0] != 0) { |
| 111 | std::string boot_status = std::string(boot.status, sizeof(boot.status)); |
| 112 | LOG(INFO) << "Boot status: " << boot_status; |
| 113 | } |
| 114 | |
| 115 | std::vector<std::string> args(argv, argv + argc); |
| 116 | |
| 117 | // --- if arguments weren't supplied, look in the bootloader control block |
| 118 | if (args.size() == 1) { |
| 119 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 120 | std::string boot_recovery(boot.recovery); |
| 121 | std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n"); |
| 122 | if (!tokens.empty() && tokens[0] == "recovery") { |
| 123 | for (auto it = tokens.begin() + 1; it != tokens.end(); it++) { |
| 124 | // Skip empty and '\0'-filled tokens. |
| 125 | if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it)); |
| 126 | } |
| 127 | LOG(INFO) << "Got " << args.size() << " arguments from boot message"; |
| 128 | } else if (boot.recovery[0] != 0) { |
| 129 | LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\""; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // --- if that doesn't work, try the command file (if we have /cache). |
| 134 | if (args.size() == 1 && has_cache) { |
| 135 | std::string content; |
| 136 | if (ensure_path_mounted(COMMAND_FILE) == 0 && |
| 137 | android::base::ReadFileToString(COMMAND_FILE, &content)) { |
| 138 | std::vector<std::string> tokens = android::base::Split(content, "\n"); |
| 139 | // All the arguments in COMMAND_FILE are needed (unlike the BCB message, |
| 140 | // COMMAND_FILE doesn't use filename as the first argument). |
| 141 | for (auto it = tokens.begin(); it != tokens.end(); it++) { |
| 142 | // Skip empty and '\0'-filled tokens. |
| 143 | if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it)); |
| 144 | } |
| 145 | LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Write the arguments (excluding the filename in args[0]) back into the |
| 150 | // bootloader control block. So the device will always boot into recovery to |
| 151 | // finish the pending work, until finish_recovery() is called. |
| 152 | std::vector<std::string> options(args.cbegin() + 1, args.cend()); |
| 153 | if (!update_bootloader_message(options, &err)) { |
| 154 | LOG(ERROR) << "Failed to set BCB message: " << err; |
| 155 | } |
| 156 | |
David Anderson | eee4e26 | 2018-08-21 13:10:45 -0700 | [diff] [blame] | 157 | // Finally, if no arguments were specified, check whether we should boot |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 158 | // into fastboot or rescue mode. |
David Anderson | eee4e26 | 2018-08-21 13:10:45 -0700 | [diff] [blame] | 159 | if (args.size() == 1 && boot_command == "boot-fastboot") { |
| 160 | args.emplace_back("--fastboot"); |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 161 | } else if (args.size() == 1 && boot_command == "boot-rescue") { |
| 162 | args.emplace_back("--rescue"); |
David Anderson | eee4e26 | 2018-08-21 13:10:45 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 165 | return args; |
| 166 | } |
| 167 | |
| 168 | static std::string load_locale_from_cache() { |
| 169 | if (ensure_path_mounted(LOCALE_FILE) != 0) { |
| 170 | LOG(ERROR) << "Can't mount " << LOCALE_FILE; |
| 171 | return ""; |
| 172 | } |
| 173 | |
| 174 | std::string content; |
| 175 | if (!android::base::ReadFileToString(LOCALE_FILE, &content)) { |
| 176 | PLOG(ERROR) << "Can't read " << LOCALE_FILE; |
| 177 | return ""; |
| 178 | } |
| 179 | |
| 180 | return android::base::Trim(content); |
| 181 | } |
| 182 | |
Tao Bao | e0cfab3 | 2019-03-29 15:53:23 -0700 | [diff] [blame] | 183 | // Sets the usb config to 'state'. |
| 184 | static bool SetUsbConfig(const std::string& state) { |
| 185 | android::base::SetProperty("sys.usb.config", state); |
| 186 | return android::base::WaitForProperty("sys.usb.state", state); |
| 187 | } |
| 188 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 189 | static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) { |
| 190 | android::base::unique_fd sock_fd(android_get_control_socket("recovery")); |
| 191 | if (sock_fd < 0) { |
| 192 | PLOG(ERROR) << "Failed to open recovery socket"; |
| 193 | return; |
| 194 | } |
| 195 | listen(sock_fd, 4); |
| 196 | |
| 197 | while (true) { |
| 198 | android::base::unique_fd connection_fd; |
| 199 | connection_fd.reset(accept(sock_fd, nullptr, nullptr)); |
| 200 | if (connection_fd < 0) { |
| 201 | PLOG(ERROR) << "Failed to accept socket connection"; |
| 202 | continue; |
| 203 | } |
| 204 | char msg; |
| 205 | constexpr char kSwitchToFastboot = 'f'; |
| 206 | constexpr char kSwitchToRecovery = 'r'; |
| 207 | ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg))); |
| 208 | if (ret != sizeof(msg)) { |
| 209 | PLOG(ERROR) << "Couldn't read from socket"; |
| 210 | continue; |
| 211 | } |
| 212 | switch (msg) { |
| 213 | case kSwitchToRecovery: |
| 214 | action = Device::BuiltinAction::ENTER_RECOVERY; |
| 215 | break; |
| 216 | case kSwitchToFastboot: |
| 217 | action = Device::BuiltinAction::ENTER_FASTBOOT; |
| 218 | break; |
| 219 | default: |
| 220 | LOG(ERROR) << "Unrecognized char from socket " << msg; |
| 221 | continue; |
| 222 | } |
| 223 | ui->InterruptKey(); |
| 224 | } |
| 225 | } |
| 226 | |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 227 | static void redirect_stdio(const char* filename) { |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 228 | android::base::unique_fd pipe_read, pipe_write; |
| 229 | // Create a pipe that allows parent process sending logs over. |
| 230 | if (!android::base::Pipe(&pipe_read, &pipe_write)) { |
| 231 | PLOG(ERROR) << "Failed to create pipe for redirecting stdio"; |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 232 | |
| 233 | // Fall back to traditional logging mode without timestamps. If these fail, there's not really |
| 234 | // anywhere to complain... |
| 235 | freopen(filename, "a", stdout); |
| 236 | setbuf(stdout, nullptr); |
| 237 | freopen(filename, "a", stderr); |
| 238 | setbuf(stderr, nullptr); |
| 239 | |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | pid_t pid = fork(); |
| 244 | if (pid == -1) { |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 245 | PLOG(ERROR) << "Failed to fork for redirecting stdio"; |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 246 | |
| 247 | // Fall back to traditional logging mode without timestamps. If these fail, there's not really |
| 248 | // anywhere to complain... |
| 249 | freopen(filename, "a", stdout); |
| 250 | setbuf(stdout, nullptr); |
| 251 | freopen(filename, "a", stderr); |
| 252 | setbuf(stderr, nullptr); |
| 253 | |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (pid == 0) { |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 258 | // Child process reads the incoming logs and doesn't write to the pipe. |
| 259 | pipe_write.reset(); |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 260 | |
| 261 | auto start = std::chrono::steady_clock::now(); |
| 262 | |
| 263 | // Child logger to actually write to the log file. |
| 264 | FILE* log_fp = fopen(filename, "ae"); |
| 265 | if (log_fp == nullptr) { |
| 266 | PLOG(ERROR) << "fopen \"" << filename << "\" failed"; |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 267 | _exit(EXIT_FAILURE); |
| 268 | } |
| 269 | |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 270 | FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), "r"); |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 271 | if (pipe_fp == nullptr) { |
| 272 | PLOG(ERROR) << "fdopen failed"; |
| 273 | check_and_fclose(log_fp, filename); |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 274 | _exit(EXIT_FAILURE); |
| 275 | } |
| 276 | |
| 277 | char* line = nullptr; |
| 278 | size_t len = 0; |
| 279 | while (getline(&line, &len, pipe_fp) != -1) { |
| 280 | auto now = std::chrono::steady_clock::now(); |
| 281 | double duration = |
| 282 | std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count(); |
| 283 | if (line[0] == '\n') { |
| 284 | fprintf(log_fp, "[%12.6lf]\n", duration); |
| 285 | } else { |
| 286 | fprintf(log_fp, "[%12.6lf] %s", duration, line); |
| 287 | } |
| 288 | fflush(log_fp); |
| 289 | } |
| 290 | |
| 291 | PLOG(ERROR) << "getline failed"; |
| 292 | |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 293 | fclose(pipe_fp); |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 294 | free(line); |
| 295 | check_and_fclose(log_fp, filename); |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 296 | _exit(EXIT_FAILURE); |
| 297 | } else { |
| 298 | // Redirect stdout/stderr to the logger process. Close the unused read end. |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 299 | pipe_read.reset(); |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 300 | |
| 301 | setbuf(stdout, nullptr); |
| 302 | setbuf(stderr, nullptr); |
| 303 | |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 304 | if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) { |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 305 | PLOG(ERROR) << "dup2 stdout failed"; |
| 306 | } |
Tao Bao | 6fcd208 | 2019-01-16 09:29:17 -0800 | [diff] [blame] | 307 | if (dup2(pipe_write.get(), STDERR_FILENO) == -1) { |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 308 | PLOG(ERROR) << "dup2 stderr failed"; |
| 309 | } |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | int main(int argc, char** argv) { |
| 314 | // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout |
| 315 | // (which is redirected to recovery.log) as we used to do. |
| 316 | android::base::InitLogging(argv, &UiLogger); |
| 317 | |
| 318 | // Take last pmsg contents and rewrite it to the current pmsg session. |
| 319 | static constexpr const char filter[] = "recovery/"; |
| 320 | // Do we need to rotate? |
| 321 | bool do_rotate = false; |
| 322 | |
| 323 | __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate); |
| 324 | // Take action to refresh pmsg contents |
| 325 | __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate); |
| 326 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 327 | time_t start = time(nullptr); |
| 328 | |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 329 | // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger |
| 330 | // instances with different timestamps. |
| 331 | redirect_stdio(Paths::Get().temporary_log_file().c_str()); |
| 332 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 333 | load_volume_table(); |
| 334 | has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr; |
| 335 | |
| 336 | std::vector<std::string> args = get_args(argc, argv); |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 337 | auto args_to_parse = StringVectorToNullTerminatedArray(args); |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 338 | |
| 339 | static constexpr struct option OPTIONS[] = { |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 340 | { "fastboot", no_argument, nullptr, 0 }, |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 341 | { "locale", required_argument, nullptr, 0 }, |
| 342 | { "show_text", no_argument, nullptr, 't' }, |
| 343 | { nullptr, 0, nullptr, 0 }, |
| 344 | }; |
| 345 | |
| 346 | bool show_text = false; |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 347 | bool fastboot = false; |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 348 | std::string locale; |
| 349 | |
| 350 | int arg; |
| 351 | int option_index; |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 352 | while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS, |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 353 | &option_index)) != -1) { |
| 354 | switch (arg) { |
| 355 | case 't': |
| 356 | show_text = true; |
| 357 | break; |
| 358 | case 0: { |
| 359 | std::string option = OPTIONS[option_index].name; |
| 360 | if (option == "locale") { |
| 361 | locale = optarg; |
Hridya Valsaraju | 7f41a2c | 2018-09-19 16:29:01 -0700 | [diff] [blame] | 362 | } else if (option == "fastboot" && |
Yifan Hong | d17174c | 2018-11-16 12:49:33 -0800 | [diff] [blame] | 363 | android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 364 | fastboot = true; |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 365 | } |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | } |
Jerry Zhang | 49fd5d2 | 2018-05-17 12:54:41 -0700 | [diff] [blame] | 370 | optind = 1; |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 371 | |
| 372 | if (locale.empty()) { |
| 373 | if (has_cache) { |
| 374 | locale = load_locale_from_cache(); |
| 375 | } |
| 376 | |
| 377 | if (locale.empty()) { |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 378 | locale = DEFAULT_LOCALE; |
| 379 | } |
| 380 | } |
| 381 | |
Tao Bao | 42c45e2 | 2018-07-31 09:37:12 -0700 | [diff] [blame] | 382 | static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so"; |
| 383 | // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have |
| 384 | // handed out pointers to code or static [or thread-local] data and doesn't collect them all back |
| 385 | // in on dlclose). |
| 386 | void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW); |
| 387 | |
| 388 | using MakeDeviceType = decltype(&make_device); |
| 389 | MakeDeviceType make_device_func = nullptr; |
| 390 | if (librecovery_ui_ext == nullptr) { |
| 391 | printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror()); |
| 392 | } else { |
| 393 | reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device"); |
| 394 | if (make_device_func == nullptr) { |
| 395 | printf("Failed to dlsym make_device: %s\n", dlerror()); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | Device* device; |
| 400 | if (make_device_func == nullptr) { |
| 401 | printf("Falling back to the default make_device() instead\n"); |
| 402 | device = make_device(); |
| 403 | } else { |
| 404 | printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt); |
| 405 | device = (*make_device_func)(); |
| 406 | } |
| 407 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 408 | if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { |
| 409 | printf("Quiescent recovery mode.\n"); |
| 410 | device->ResetUI(new StubRecoveryUI()); |
| 411 | } else { |
| 412 | if (!device->GetUI()->Init(locale)) { |
| 413 | printf("Failed to initialize UI; using stub UI instead.\n"); |
| 414 | device->ResetUI(new StubRecoveryUI()); |
| 415 | } |
| 416 | } |
| 417 | ui = device->GetUI(); |
| 418 | |
| 419 | if (!has_cache) { |
| 420 | device->RemoveMenuItemForAction(Device::WIPE_CACHE); |
| 421 | } |
| 422 | |
Yifan Hong | d17174c | 2018-11-16 12:49:33 -0800 | [diff] [blame] | 423 | if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) { |
Hridya Valsaraju | daa301e | 2018-09-18 14:48:01 -0700 | [diff] [blame] | 424 | device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT); |
| 425 | } |
| 426 | |
Tao Bao | c6dc325 | 2019-04-16 14:22:25 -0700 | [diff] [blame] | 427 | if (!is_ro_debuggable()) { |
| 428 | device->RemoveMenuItemForAction(Device::ENTER_RESCUE); |
| 429 | } |
| 430 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 431 | ui->SetBackground(RecoveryUI::NONE); |
| 432 | if (show_text) ui->ShowText(true); |
| 433 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 434 | LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start); |
| 435 | LOG(INFO) << "locale is [" << locale << "]"; |
| 436 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 437 | sehandle = selinux_android_file_context_handle(); |
| 438 | selinux_android_set_sehandle(sehandle); |
| 439 | if (!sehandle) { |
| 440 | ui->Print("Warning: No file_contexts\n"); |
| 441 | } |
| 442 | |
xunchang | 2239b9e | 2019-04-15 15:24:24 -0700 | [diff] [blame] | 443 | SetLoggingSehandle(sehandle); |
xunchang | 316e971 | 2019-04-12 16:22:15 -0700 | [diff] [blame] | 444 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 445 | std::atomic<Device::BuiltinAction> action; |
| 446 | std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action)); |
| 447 | listener_thread.detach(); |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 448 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 449 | while (true) { |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 450 | std::string usb_config = fastboot ? "fastboot" : is_ro_debuggable() ? "adb" : "none"; |
| 451 | std::string usb_state = android::base::GetProperty("sys.usb.state", "none"); |
| 452 | if (usb_config != usb_state) { |
| 453 | if (!SetUsbConfig("none")) { |
| 454 | LOG(ERROR) << "Failed to clear USB config"; |
| 455 | } |
| 456 | if (!SetUsbConfig(usb_config)) { |
| 457 | LOG(ERROR) << "Failed to set USB config to " << usb_config; |
| 458 | } |
| 459 | } |
| 460 | |
David Anderson | 983e2d5 | 2019-01-02 11:35:38 -0800 | [diff] [blame] | 461 | ui->SetEnableFastbootdLogo(fastboot); |
| 462 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 463 | auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args); |
| 464 | |
| 465 | if (ret == Device::KEY_INTERRUPTED) { |
| 466 | ret = action.exchange(ret); |
| 467 | if (ret == Device::NO_ACTION) { |
| 468 | continue; |
| 469 | } |
| 470 | } |
| 471 | switch (ret) { |
| 472 | case Device::SHUTDOWN: |
| 473 | ui->Print("Shutting down...\n"); |
Tao Bao | feefbf2 | 2019-05-22 18:03:01 +0000 | [diff] [blame] | 474 | Shutdown(); |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 475 | break; |
| 476 | |
| 477 | case Device::REBOOT_BOOTLOADER: |
| 478 | ui->Print("Rebooting to bootloader...\n"); |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 479 | Reboot("bootloader"); |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 480 | break; |
| 481 | |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 482 | case Device::REBOOT_FASTBOOT: |
| 483 | ui->Print("Rebooting to recovery/fastboot...\n"); |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 484 | Reboot("fastboot"); |
Tao Bao | 10f441a | 2019-04-19 15:22:15 -0700 | [diff] [blame] | 485 | break; |
| 486 | |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 487 | case Device::REBOOT_RECOVERY: |
| 488 | ui->Print("Rebooting to recovery...\n"); |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 489 | Reboot("recovery"); |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 490 | break; |
| 491 | |
| 492 | case Device::REBOOT_RESCUE: { |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 493 | // Not using `Reboot("rescue")`, as it requires matching support in kernel and/or |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 494 | // bootloader. |
| 495 | bootloader_message boot = {}; |
| 496 | strlcpy(boot.command, "boot-rescue", sizeof(boot.command)); |
| 497 | std::string err; |
| 498 | if (!write_bootloader_message(boot, &err)) { |
| 499 | LOG(ERROR) << "Failed to write bootloader message: " << err; |
| 500 | // Stay under recovery on failure. |
| 501 | continue; |
| 502 | } |
| 503 | ui->Print("Rebooting to recovery/rescue...\n"); |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 504 | Reboot("recovery"); |
Tao Bao | d9cb014 | 2019-04-23 11:46:25 -0700 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 508 | case Device::ENTER_FASTBOOT: |
Tao Bao | f90d9a1 | 2019-05-10 10:40:59 -0700 | [diff] [blame] | 509 | if (android::fs_mgr::LogicalPartitionsMapped()) { |
David Anderson | 2b2f423 | 2018-10-29 18:48:56 -0700 | [diff] [blame] | 510 | ui->Print("Partitions may be mounted - rebooting to enter fastboot."); |
Tao Bao | 782dcc1 | 2019-04-29 11:23:16 -0700 | [diff] [blame] | 511 | Reboot("fastboot"); |
David Anderson | 2b2f423 | 2018-10-29 18:48:56 -0700 | [diff] [blame] | 512 | } else { |
| 513 | LOG(INFO) << "Entering fastboot"; |
| 514 | fastboot = true; |
| 515 | } |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 516 | break; |
| 517 | |
| 518 | case Device::ENTER_RECOVERY: |
| 519 | LOG(INFO) << "Entering recovery"; |
| 520 | fastboot = false; |
| 521 | break; |
| 522 | |
| 523 | default: |
| 524 | ui->Print("Rebooting...\n"); |
Tao Bao | feefbf2 | 2019-05-22 18:03:01 +0000 | [diff] [blame] | 525 | Reboot(""); |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 526 | break; |
| 527 | } |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 528 | } |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 529 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 530 | // Should be unreachable. |
| 531 | return EXIT_SUCCESS; |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 532 | } |