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