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