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