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