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