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> |
| 44 | #include <cutils/android_reboot.h> |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 45 | #include <cutils/sockets.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" |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 52 | #include "device.h" |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 53 | #include "fastboot/fastboot.h" |
Jerry Zhang | 152933a | 2018-05-02 16:56:00 -0700 | [diff] [blame] | 54 | #include "logging.h" |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 55 | #include "minadbd/minadbd.h" |
| 56 | #include "otautil/paths.h" |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 57 | #include "otautil/sysutil.h" |
| 58 | #include "recovery.h" |
| 59 | #include "roots.h" |
| 60 | #include "stub_ui.h" |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 61 | #include "ui.h" |
| 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 | |
| 100 | if (boot.command[0] != 0) { |
| 101 | std::string boot_command = std::string(boot.command, sizeof(boot.command)); |
| 102 | LOG(INFO) << "Boot command: " << boot_command; |
| 103 | } |
| 104 | |
| 105 | if (boot.status[0] != 0) { |
| 106 | std::string boot_status = std::string(boot.status, sizeof(boot.status)); |
| 107 | LOG(INFO) << "Boot status: " << boot_status; |
| 108 | } |
| 109 | |
| 110 | std::vector<std::string> args(argv, argv + argc); |
| 111 | |
| 112 | // --- if arguments weren't supplied, look in the bootloader control block |
| 113 | if (args.size() == 1) { |
| 114 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 115 | std::string boot_recovery(boot.recovery); |
| 116 | std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n"); |
| 117 | if (!tokens.empty() && tokens[0] == "recovery") { |
| 118 | for (auto it = tokens.begin() + 1; it != tokens.end(); it++) { |
| 119 | // Skip empty and '\0'-filled tokens. |
| 120 | if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it)); |
| 121 | } |
| 122 | LOG(INFO) << "Got " << args.size() << " arguments from boot message"; |
| 123 | } else if (boot.recovery[0] != 0) { |
| 124 | LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\""; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // --- if that doesn't work, try the command file (if we have /cache). |
| 129 | if (args.size() == 1 && has_cache) { |
| 130 | std::string content; |
| 131 | if (ensure_path_mounted(COMMAND_FILE) == 0 && |
| 132 | android::base::ReadFileToString(COMMAND_FILE, &content)) { |
| 133 | std::vector<std::string> tokens = android::base::Split(content, "\n"); |
| 134 | // All the arguments in COMMAND_FILE are needed (unlike the BCB message, |
| 135 | // COMMAND_FILE doesn't use filename as the first argument). |
| 136 | for (auto it = tokens.begin(); it != tokens.end(); it++) { |
| 137 | // Skip empty and '\0'-filled tokens. |
| 138 | if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it)); |
| 139 | } |
| 140 | LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Write the arguments (excluding the filename in args[0]) back into the |
| 145 | // bootloader control block. So the device will always boot into recovery to |
| 146 | // finish the pending work, until finish_recovery() is called. |
| 147 | std::vector<std::string> options(args.cbegin() + 1, args.cend()); |
| 148 | if (!update_bootloader_message(options, &err)) { |
| 149 | LOG(ERROR) << "Failed to set BCB message: " << err; |
| 150 | } |
| 151 | |
| 152 | return args; |
| 153 | } |
| 154 | |
| 155 | static std::string load_locale_from_cache() { |
| 156 | if (ensure_path_mounted(LOCALE_FILE) != 0) { |
| 157 | LOG(ERROR) << "Can't mount " << LOCALE_FILE; |
| 158 | return ""; |
| 159 | } |
| 160 | |
| 161 | std::string content; |
| 162 | if (!android::base::ReadFileToString(LOCALE_FILE, &content)) { |
| 163 | PLOG(ERROR) << "Can't read " << LOCALE_FILE; |
| 164 | return ""; |
| 165 | } |
| 166 | |
| 167 | return android::base::Trim(content); |
| 168 | } |
| 169 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 170 | static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) { |
| 171 | android::base::unique_fd sock_fd(android_get_control_socket("recovery")); |
| 172 | if (sock_fd < 0) { |
| 173 | PLOG(ERROR) << "Failed to open recovery socket"; |
| 174 | return; |
| 175 | } |
| 176 | listen(sock_fd, 4); |
| 177 | |
| 178 | while (true) { |
| 179 | android::base::unique_fd connection_fd; |
| 180 | connection_fd.reset(accept(sock_fd, nullptr, nullptr)); |
| 181 | if (connection_fd < 0) { |
| 182 | PLOG(ERROR) << "Failed to accept socket connection"; |
| 183 | continue; |
| 184 | } |
| 185 | char msg; |
| 186 | constexpr char kSwitchToFastboot = 'f'; |
| 187 | constexpr char kSwitchToRecovery = 'r'; |
| 188 | ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg))); |
| 189 | if (ret != sizeof(msg)) { |
| 190 | PLOG(ERROR) << "Couldn't read from socket"; |
| 191 | continue; |
| 192 | } |
| 193 | switch (msg) { |
| 194 | case kSwitchToRecovery: |
| 195 | action = Device::BuiltinAction::ENTER_RECOVERY; |
| 196 | break; |
| 197 | case kSwitchToFastboot: |
| 198 | action = Device::BuiltinAction::ENTER_FASTBOOT; |
| 199 | break; |
| 200 | default: |
| 201 | LOG(ERROR) << "Unrecognized char from socket " << msg; |
| 202 | continue; |
| 203 | } |
| 204 | ui->InterruptKey(); |
| 205 | } |
| 206 | } |
| 207 | |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 208 | static void redirect_stdio(const char* filename) { |
| 209 | int pipefd[2]; |
| 210 | if (pipe(pipefd) == -1) { |
| 211 | PLOG(ERROR) << "pipe failed"; |
| 212 | |
| 213 | // Fall back to traditional logging mode without timestamps. If these fail, there's not really |
| 214 | // anywhere to complain... |
| 215 | freopen(filename, "a", stdout); |
| 216 | setbuf(stdout, nullptr); |
| 217 | freopen(filename, "a", stderr); |
| 218 | setbuf(stderr, nullptr); |
| 219 | |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | pid_t pid = fork(); |
| 224 | if (pid == -1) { |
| 225 | PLOG(ERROR) << "fork failed"; |
| 226 | |
| 227 | // Fall back to traditional logging mode without timestamps. If these fail, there's not really |
| 228 | // anywhere to complain... |
| 229 | freopen(filename, "a", stdout); |
| 230 | setbuf(stdout, nullptr); |
| 231 | freopen(filename, "a", stderr); |
| 232 | setbuf(stderr, nullptr); |
| 233 | |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | if (pid == 0) { |
| 238 | /// Close the unused write end. |
| 239 | close(pipefd[1]); |
| 240 | |
| 241 | auto start = std::chrono::steady_clock::now(); |
| 242 | |
| 243 | // Child logger to actually write to the log file. |
| 244 | FILE* log_fp = fopen(filename, "ae"); |
| 245 | if (log_fp == nullptr) { |
| 246 | PLOG(ERROR) << "fopen \"" << filename << "\" failed"; |
| 247 | close(pipefd[0]); |
| 248 | _exit(EXIT_FAILURE); |
| 249 | } |
| 250 | |
| 251 | FILE* pipe_fp = fdopen(pipefd[0], "r"); |
| 252 | if (pipe_fp == nullptr) { |
| 253 | PLOG(ERROR) << "fdopen failed"; |
| 254 | check_and_fclose(log_fp, filename); |
| 255 | close(pipefd[0]); |
| 256 | _exit(EXIT_FAILURE); |
| 257 | } |
| 258 | |
| 259 | char* line = nullptr; |
| 260 | size_t len = 0; |
| 261 | while (getline(&line, &len, pipe_fp) != -1) { |
| 262 | auto now = std::chrono::steady_clock::now(); |
| 263 | double duration = |
| 264 | std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count(); |
| 265 | if (line[0] == '\n') { |
| 266 | fprintf(log_fp, "[%12.6lf]\n", duration); |
| 267 | } else { |
| 268 | fprintf(log_fp, "[%12.6lf] %s", duration, line); |
| 269 | } |
| 270 | fflush(log_fp); |
| 271 | } |
| 272 | |
| 273 | PLOG(ERROR) << "getline failed"; |
| 274 | |
| 275 | free(line); |
| 276 | check_and_fclose(log_fp, filename); |
| 277 | close(pipefd[0]); |
| 278 | _exit(EXIT_FAILURE); |
| 279 | } else { |
| 280 | // Redirect stdout/stderr to the logger process. Close the unused read end. |
| 281 | close(pipefd[0]); |
| 282 | |
| 283 | setbuf(stdout, nullptr); |
| 284 | setbuf(stderr, nullptr); |
| 285 | |
| 286 | if (dup2(pipefd[1], STDOUT_FILENO) == -1) { |
| 287 | PLOG(ERROR) << "dup2 stdout failed"; |
| 288 | } |
| 289 | if (dup2(pipefd[1], STDERR_FILENO) == -1) { |
| 290 | PLOG(ERROR) << "dup2 stderr failed"; |
| 291 | } |
| 292 | |
| 293 | close(pipefd[1]); |
| 294 | } |
| 295 | } |
| 296 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 297 | static bool SetUsbConfig(const std::string& state) { |
| 298 | android::base::SetProperty("sys.usb.config", state); |
| 299 | return android::base::WaitForProperty("sys.usb.state", state); |
| 300 | } |
| 301 | |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 302 | int main(int argc, char** argv) { |
| 303 | // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout |
| 304 | // (which is redirected to recovery.log) as we used to do. |
| 305 | android::base::InitLogging(argv, &UiLogger); |
| 306 | |
| 307 | // Take last pmsg contents and rewrite it to the current pmsg session. |
| 308 | static constexpr const char filter[] = "recovery/"; |
| 309 | // Do we need to rotate? |
| 310 | bool do_rotate = false; |
| 311 | |
| 312 | __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate); |
| 313 | // Take action to refresh pmsg contents |
| 314 | __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate); |
| 315 | |
| 316 | // If this binary is started with the single argument "--adbd", instead of being the normal |
| 317 | // recovery binary, it turns into kind of a stripped-down version of adbd that only supports the |
| 318 | // 'sideload' command. Note this must be a real argument, not anything in the command file or |
| 319 | // bootloader control block; the only way recovery should be run with this argument is when it |
| 320 | // starts a copy of itself from the apply_from_adb() function. |
| 321 | if (argc == 2 && strcmp(argv[1], "--adbd") == 0) { |
| 322 | minadbd_main(); |
| 323 | return 0; |
| 324 | } |
| 325 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 326 | time_t start = time(nullptr); |
| 327 | |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 328 | // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger |
| 329 | // instances with different timestamps. |
| 330 | redirect_stdio(Paths::Get().temporary_log_file().c_str()); |
| 331 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 332 | load_volume_table(); |
| 333 | has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr; |
| 334 | |
| 335 | std::vector<std::string> args = get_args(argc, argv); |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 336 | auto args_to_parse = StringVectorToNullTerminatedArray(args); |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 337 | |
| 338 | static constexpr struct option OPTIONS[] = { |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 339 | { "fastboot", no_argument, nullptr, 0 }, |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 340 | { "locale", required_argument, nullptr, 0 }, |
| 341 | { "show_text", no_argument, nullptr, 't' }, |
| 342 | { nullptr, 0, nullptr, 0 }, |
| 343 | }; |
| 344 | |
| 345 | bool show_text = false; |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 346 | bool fastboot = false; |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 347 | std::string locale; |
| 348 | |
| 349 | int arg; |
| 350 | int option_index; |
Tao Bao | 1700cc4 | 2018-07-16 22:09:59 -0700 | [diff] [blame] | 351 | 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] | 352 | &option_index)) != -1) { |
| 353 | switch (arg) { |
| 354 | case 't': |
| 355 | show_text = true; |
| 356 | break; |
| 357 | case 0: { |
| 358 | std::string option = OPTIONS[option_index].name; |
| 359 | if (option == "locale") { |
| 360 | locale = optarg; |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 361 | } else if (option == "fastboot") { |
| 362 | fastboot = true; |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 363 | } |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | } |
Jerry Zhang | 49fd5d2 | 2018-05-17 12:54:41 -0700 | [diff] [blame] | 368 | optind = 1; |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 369 | |
| 370 | if (locale.empty()) { |
| 371 | if (has_cache) { |
| 372 | locale = load_locale_from_cache(); |
| 373 | } |
| 374 | |
| 375 | if (locale.empty()) { |
| 376 | static constexpr const char* DEFAULT_LOCALE = "en-US"; |
| 377 | locale = DEFAULT_LOCALE; |
| 378 | } |
| 379 | } |
| 380 | |
Tao Bao | 42c45e2 | 2018-07-31 09:37:12 -0700 | [diff] [blame] | 381 | static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so"; |
| 382 | // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have |
| 383 | // handed out pointers to code or static [or thread-local] data and doesn't collect them all back |
| 384 | // in on dlclose). |
| 385 | void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW); |
| 386 | |
| 387 | using MakeDeviceType = decltype(&make_device); |
| 388 | MakeDeviceType make_device_func = nullptr; |
| 389 | if (librecovery_ui_ext == nullptr) { |
| 390 | printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror()); |
| 391 | } else { |
| 392 | reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device"); |
| 393 | if (make_device_func == nullptr) { |
| 394 | printf("Failed to dlsym make_device: %s\n", dlerror()); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | Device* device; |
| 399 | if (make_device_func == nullptr) { |
| 400 | printf("Falling back to the default make_device() instead\n"); |
| 401 | device = make_device(); |
| 402 | } else { |
| 403 | printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt); |
| 404 | device = (*make_device_func)(); |
| 405 | } |
| 406 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 407 | if (android::base::GetBoolProperty("ro.boot.quiescent", false)) { |
| 408 | printf("Quiescent recovery mode.\n"); |
| 409 | device->ResetUI(new StubRecoveryUI()); |
| 410 | } else { |
| 411 | if (!device->GetUI()->Init(locale)) { |
| 412 | printf("Failed to initialize UI; using stub UI instead.\n"); |
| 413 | device->ResetUI(new StubRecoveryUI()); |
| 414 | } |
| 415 | } |
| 416 | ui = device->GetUI(); |
| 417 | |
| 418 | if (!has_cache) { |
| 419 | device->RemoveMenuItemForAction(Device::WIPE_CACHE); |
| 420 | } |
| 421 | |
| 422 | ui->SetBackground(RecoveryUI::NONE); |
| 423 | if (show_text) ui->ShowText(true); |
| 424 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 425 | LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start); |
| 426 | LOG(INFO) << "locale is [" << locale << "]"; |
| 427 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 428 | sehandle = selinux_android_file_context_handle(); |
| 429 | selinux_android_set_sehandle(sehandle); |
| 430 | if (!sehandle) { |
| 431 | ui->Print("Warning: No file_contexts\n"); |
| 432 | } |
| 433 | |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 434 | std::atomic<Device::BuiltinAction> action; |
| 435 | std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action)); |
| 436 | listener_thread.detach(); |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 437 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 438 | while (true) { |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 439 | std::string usb_config = fastboot ? "fastboot" : is_ro_debuggable() ? "adb" : "none"; |
| 440 | std::string usb_state = android::base::GetProperty("sys.usb.state", "none"); |
| 441 | if (usb_config != usb_state) { |
| 442 | if (!SetUsbConfig("none")) { |
| 443 | LOG(ERROR) << "Failed to clear USB config"; |
| 444 | } |
| 445 | if (!SetUsbConfig(usb_config)) { |
| 446 | LOG(ERROR) << "Failed to set USB config to " << usb_config; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args); |
| 451 | |
| 452 | if (ret == Device::KEY_INTERRUPTED) { |
| 453 | ret = action.exchange(ret); |
| 454 | if (ret == Device::NO_ACTION) { |
| 455 | continue; |
| 456 | } |
| 457 | } |
| 458 | switch (ret) { |
| 459 | case Device::SHUTDOWN: |
| 460 | ui->Print("Shutting down...\n"); |
| 461 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,"); |
| 462 | break; |
| 463 | |
| 464 | case Device::REBOOT_BOOTLOADER: |
| 465 | ui->Print("Rebooting to bootloader...\n"); |
| 466 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
| 467 | break; |
| 468 | |
| 469 | case Device::ENTER_FASTBOOT: |
| 470 | LOG(INFO) << "Entering fastboot"; |
| 471 | fastboot = true; |
| 472 | break; |
| 473 | |
| 474 | case Device::ENTER_RECOVERY: |
| 475 | LOG(INFO) << "Entering recovery"; |
| 476 | fastboot = false; |
| 477 | break; |
| 478 | |
| 479 | default: |
| 480 | ui->Print("Rebooting...\n"); |
| 481 | reboot("reboot,"); |
| 482 | break; |
| 483 | } |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 484 | } |
Hridya Valsaraju | 20c81b3 | 2018-07-27 22:09:12 -0700 | [diff] [blame] | 485 | |
Jerry Zhang | f5e319a | 2018-05-04 11:24:10 -0700 | [diff] [blame] | 486 | // Should be unreachable. |
| 487 | return EXIT_SUCCESS; |
Tao Bao | 6d99d4b | 2018-04-25 16:47:04 -0700 | [diff] [blame] | 488 | } |