blob: 80cba61d341bd7096e7d37264ede0cee349b7a65 [file] [log] [blame]
Tao Bao6d99d4b2018-04-25 16:47:04 -07001/*
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 Bao42c45e22018-07-31 09:37:12 -070017#include <dlfcn.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070018#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 Bao6d99d4b2018-04-25 16:47:04 -070025#include <stdio.h>
26#include <stdlib.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070027#include <string.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <time.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070031#include <unistd.h>
32
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070033#include <atomic>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070034#include <string>
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070035#include <thread>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070036#include <vector>
Tao Bao6d99d4b2018-04-25 16:47:04 -070037
Jerry Zhangf5e319a2018-05-04 11:24:10 -070038#include <android-base/file.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070039#include <android-base/logging.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070040#include <android-base/properties.h>
41#include <android-base/strings.h>
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070042#include <android-base/unique_fd.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070043#include <bootloader_message/bootloader_message.h>
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070044#include <cutils/sockets.h>
Tao Baof90d9a12019-05-10 10:40:59 -070045#include <fs_mgr/roots.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070046#include <private/android_logger.h> /* private pmsg functions */
Jerry Zhangf5e319a2018-05-04 11:24:10 -070047#include <selinux/android.h>
48#include <selinux/label.h>
49#include <selinux/selinux.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070050
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070051#include "fastboot/fastboot.h"
xunchang316e9712019-04-12 16:22:15 -070052#include "install/wipe_data.h"
Tianjie Xub63a2212019-07-29 14:21:49 -070053#include "otautil/boot_state.h"
Tao Bao6d99d4b2018-04-25 16:47:04 -070054#include "otautil/paths.h"
Jerry Zhangf5e319a2018-05-04 11:24:10 -070055#include "otautil/sysutil.h"
56#include "recovery.h"
Tianjie Xu8f397302018-08-20 13:40:47 -070057#include "recovery_ui/device.h"
58#include "recovery_ui/stub_ui.h"
59#include "recovery_ui/ui.h"
Tao Baoe3f09a72019-10-01 11:55:36 -070060#include "recovery_utils/logging.h"
61#include "recovery_utils/roots.h"
Tao Bao6d99d4b2018-04-25 16:47:04 -070062
Jerry Zhangf5e319a2018-05-04 11:24:10 -070063static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
64static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
65
Tianjie Xu164c60a2019-05-15 13:59:39 -070066static RecoveryUI* ui = nullptr;
Jerry Zhangf5e319a2018-05-04 11:24:10 -070067
Tianjie Xu164c60a2019-05-15 13:59:39 -070068static bool IsRoDebuggable() {
69 return android::base::GetBoolProperty("ro.debuggable", false);
70}
Jerry Zhangf5e319a2018-05-04 11:24:10 -070071
Tianjie Xu7d5c3412019-10-29 21:44:39 -070072static bool IsDeviceUnlocked() {
73 return "orange" == android::base::GetProperty("ro.boot.verifiedbootstate", "");
74}
75
Tao Bao6d99d4b2018-04-25 16:47:04 -070076static void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity,
77 const char* /* tag */, const char* /* file */, unsigned int /* line */,
78 const char* message) {
79 static constexpr char log_characters[] = "VDIWEF";
80 if (severity >= android::base::ERROR && ui != nullptr) {
81 ui->Print("E:%s\n", message);
82 } else {
83 fprintf(stdout, "%c:%s\n", log_characters[severity], message);
84 }
85}
86
Tianjie Xub63a2212019-07-29 14:21:49 -070087// Parses the command line argument from various sources; and reads the stage field from BCB.
Jerry Zhangf5e319a2018-05-04 11:24:10 -070088// command line args come from, in decreasing precedence:
89// - the actual command line
90// - the bootloader control block (one per line, after "recovery")
91// - the contents of COMMAND_FILE (one per line)
Tianjie Xub63a2212019-07-29 14:21:49 -070092static std::vector<std::string> get_args(const int argc, char** const argv, std::string* stage) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -070093 CHECK_GT(argc, 0);
94
95 bootloader_message boot = {};
96 std::string err;
97 if (!read_bootloader_message(&boot, &err)) {
98 LOG(ERROR) << err;
99 // If fails, leave a zeroed bootloader_message.
100 boot = {};
101 }
Tianjie Xub63a2212019-07-29 14:21:49 -0700102 if (stage) {
103 *stage = std::string(boot.stage);
104 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700105
David Andersoneee4e262018-08-21 13:10:45 -0700106 std::string boot_command;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700107 if (boot.command[0] != 0) {
David Andersoneee4e262018-08-21 13:10:45 -0700108 if (memchr(boot.command, '\0', sizeof(boot.command))) {
109 boot_command = std::string(boot.command);
110 } else {
111 boot_command = std::string(boot.command, sizeof(boot.command));
112 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700113 LOG(INFO) << "Boot command: " << boot_command;
114 }
115
116 if (boot.status[0] != 0) {
117 std::string boot_status = std::string(boot.status, sizeof(boot.status));
118 LOG(INFO) << "Boot status: " << boot_status;
119 }
120
121 std::vector<std::string> args(argv, argv + argc);
122
123 // --- if arguments weren't supplied, look in the bootloader control block
124 if (args.size() == 1) {
125 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
126 std::string boot_recovery(boot.recovery);
127 std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n");
128 if (!tokens.empty() && tokens[0] == "recovery") {
129 for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
130 // Skip empty and '\0'-filled tokens.
131 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
132 }
133 LOG(INFO) << "Got " << args.size() << " arguments from boot message";
134 } else if (boot.recovery[0] != 0) {
135 LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\"";
136 }
137 }
138
139 // --- if that doesn't work, try the command file (if we have /cache).
Tianjie Xu164c60a2019-05-15 13:59:39 -0700140 if (args.size() == 1 && HasCache()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700141 std::string content;
142 if (ensure_path_mounted(COMMAND_FILE) == 0 &&
143 android::base::ReadFileToString(COMMAND_FILE, &content)) {
144 std::vector<std::string> tokens = android::base::Split(content, "\n");
145 // All the arguments in COMMAND_FILE are needed (unlike the BCB message,
146 // COMMAND_FILE doesn't use filename as the first argument).
147 for (auto it = tokens.begin(); it != tokens.end(); it++) {
148 // Skip empty and '\0'-filled tokens.
149 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
150 }
151 LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
152 }
153 }
154
155 // Write the arguments (excluding the filename in args[0]) back into the
156 // bootloader control block. So the device will always boot into recovery to
Tianjie Xu164c60a2019-05-15 13:59:39 -0700157 // finish the pending work, until FinishRecovery() is called.
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700158 std::vector<std::string> options(args.cbegin() + 1, args.cend());
159 if (!update_bootloader_message(options, &err)) {
160 LOG(ERROR) << "Failed to set BCB message: " << err;
161 }
162
David Andersoneee4e262018-08-21 13:10:45 -0700163 // Finally, if no arguments were specified, check whether we should boot
Tao Baod9cb0142019-04-23 11:46:25 -0700164 // into fastboot or rescue mode.
David Andersoneee4e262018-08-21 13:10:45 -0700165 if (args.size() == 1 && boot_command == "boot-fastboot") {
166 args.emplace_back("--fastboot");
Tao Baod9cb0142019-04-23 11:46:25 -0700167 } else if (args.size() == 1 && boot_command == "boot-rescue") {
168 args.emplace_back("--rescue");
David Andersoneee4e262018-08-21 13:10:45 -0700169 }
170
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700171 return args;
172}
173
174static std::string load_locale_from_cache() {
175 if (ensure_path_mounted(LOCALE_FILE) != 0) {
176 LOG(ERROR) << "Can't mount " << LOCALE_FILE;
177 return "";
178 }
179
180 std::string content;
181 if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
182 PLOG(ERROR) << "Can't read " << LOCALE_FILE;
183 return "";
184 }
185
186 return android::base::Trim(content);
187}
188
Tao Baoe0cfab32019-03-29 15:53:23 -0700189// Sets the usb config to 'state'.
190static bool SetUsbConfig(const std::string& state) {
191 android::base::SetProperty("sys.usb.config", state);
192 return android::base::WaitForProperty("sys.usb.state", state);
193}
194
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700195static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) {
196 android::base::unique_fd sock_fd(android_get_control_socket("recovery"));
197 if (sock_fd < 0) {
198 PLOG(ERROR) << "Failed to open recovery socket";
199 return;
200 }
201 listen(sock_fd, 4);
202
203 while (true) {
204 android::base::unique_fd connection_fd;
205 connection_fd.reset(accept(sock_fd, nullptr, nullptr));
206 if (connection_fd < 0) {
207 PLOG(ERROR) << "Failed to accept socket connection";
208 continue;
209 }
210 char msg;
211 constexpr char kSwitchToFastboot = 'f';
212 constexpr char kSwitchToRecovery = 'r';
213 ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg)));
214 if (ret != sizeof(msg)) {
215 PLOG(ERROR) << "Couldn't read from socket";
216 continue;
217 }
218 switch (msg) {
219 case kSwitchToRecovery:
220 action = Device::BuiltinAction::ENTER_RECOVERY;
221 break;
222 case kSwitchToFastboot:
223 action = Device::BuiltinAction::ENTER_FASTBOOT;
224 break;
225 default:
226 LOG(ERROR) << "Unrecognized char from socket " << msg;
227 continue;
228 }
229 ui->InterruptKey();
230 }
231}
232
Tao Bao6d99d4b2018-04-25 16:47:04 -0700233static void redirect_stdio(const char* filename) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800234 android::base::unique_fd pipe_read, pipe_write;
235 // Create a pipe that allows parent process sending logs over.
236 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
237 PLOG(ERROR) << "Failed to create pipe for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700238
239 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
240 // anywhere to complain...
241 freopen(filename, "a", stdout);
242 setbuf(stdout, nullptr);
243 freopen(filename, "a", stderr);
244 setbuf(stderr, nullptr);
245
246 return;
247 }
248
249 pid_t pid = fork();
250 if (pid == -1) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800251 PLOG(ERROR) << "Failed to fork for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700252
253 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
254 // anywhere to complain...
255 freopen(filename, "a", stdout);
256 setbuf(stdout, nullptr);
257 freopen(filename, "a", stderr);
258 setbuf(stderr, nullptr);
259
260 return;
261 }
262
263 if (pid == 0) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800264 // Child process reads the incoming logs and doesn't write to the pipe.
265 pipe_write.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700266
267 auto start = std::chrono::steady_clock::now();
268
269 // Child logger to actually write to the log file.
270 FILE* log_fp = fopen(filename, "ae");
271 if (log_fp == nullptr) {
272 PLOG(ERROR) << "fopen \"" << filename << "\" failed";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700273 _exit(EXIT_FAILURE);
274 }
275
Tao Bao6fcd2082019-01-16 09:29:17 -0800276 FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), "r");
Tao Bao6d99d4b2018-04-25 16:47:04 -0700277 if (pipe_fp == nullptr) {
278 PLOG(ERROR) << "fdopen failed";
279 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700280 _exit(EXIT_FAILURE);
281 }
282
283 char* line = nullptr;
284 size_t len = 0;
285 while (getline(&line, &len, pipe_fp) != -1) {
286 auto now = std::chrono::steady_clock::now();
287 double duration =
288 std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count();
289 if (line[0] == '\n') {
290 fprintf(log_fp, "[%12.6lf]\n", duration);
291 } else {
292 fprintf(log_fp, "[%12.6lf] %s", duration, line);
293 }
294 fflush(log_fp);
295 }
296
297 PLOG(ERROR) << "getline failed";
298
Tao Bao6fcd2082019-01-16 09:29:17 -0800299 fclose(pipe_fp);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700300 free(line);
301 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700302 _exit(EXIT_FAILURE);
303 } else {
304 // Redirect stdout/stderr to the logger process. Close the unused read end.
Tao Bao6fcd2082019-01-16 09:29:17 -0800305 pipe_read.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700306
307 setbuf(stdout, nullptr);
308 setbuf(stderr, nullptr);
309
Tao Bao6fcd2082019-01-16 09:29:17 -0800310 if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700311 PLOG(ERROR) << "dup2 stdout failed";
312 }
Tao Bao6fcd2082019-01-16 09:29:17 -0800313 if (dup2(pipe_write.get(), STDERR_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700314 PLOG(ERROR) << "dup2 stderr failed";
315 }
Tao Bao6d99d4b2018-04-25 16:47:04 -0700316 }
317}
318
319int main(int argc, char** argv) {
320 // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
321 // (which is redirected to recovery.log) as we used to do.
322 android::base::InitLogging(argv, &UiLogger);
323
324 // Take last pmsg contents and rewrite it to the current pmsg session.
325 static constexpr const char filter[] = "recovery/";
326 // Do we need to rotate?
327 bool do_rotate = false;
328
329 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
330 // Take action to refresh pmsg contents
331 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
332
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700333 time_t start = time(nullptr);
334
Tao Bao6d99d4b2018-04-25 16:47:04 -0700335 // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
336 // instances with different timestamps.
337 redirect_stdio(Paths::Get().temporary_log_file().c_str());
338
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700339 load_volume_table();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700340
Tianjie Xub63a2212019-07-29 14:21:49 -0700341 std::string stage;
342 std::vector<std::string> args = get_args(argc, argv, &stage);
Tao Bao1700cc42018-07-16 22:09:59 -0700343 auto args_to_parse = StringVectorToNullTerminatedArray(args);
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700344
345 static constexpr struct option OPTIONS[] = {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700346 { "fastboot", no_argument, nullptr, 0 },
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700347 { "locale", required_argument, nullptr, 0 },
Tianjie Xub63a2212019-07-29 14:21:49 -0700348 { "reason", required_argument, nullptr, 0 },
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700349 { "show_text", no_argument, nullptr, 't' },
350 { nullptr, 0, nullptr, 0 },
351 };
352
353 bool show_text = false;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700354 bool fastboot = false;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700355 std::string locale;
Tianjie Xub63a2212019-07-29 14:21:49 -0700356 std::string reason;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700357
Tao Baoff185862019-09-18 13:28:32 -0700358 // The code here is only interested in the options that signal the intent to start fastbootd or
359 // recovery. Unrecognized options are likely meant for recovery, which will be processed later in
360 // start_recovery(). Suppress the warnings for such -- even if some flags were indeed invalid, the
361 // code in start_recovery() will capture and report them.
362 opterr = 0;
363
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700364 int arg;
365 int option_index;
Tao Bao1700cc42018-07-16 22:09:59 -0700366 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700367 &option_index)) != -1) {
368 switch (arg) {
369 case 't':
370 show_text = true;
371 break;
372 case 0: {
373 std::string option = OPTIONS[option_index].name;
374 if (option == "locale") {
375 locale = optarg;
Tianjie Xub63a2212019-07-29 14:21:49 -0700376 } else if (option == "reason") {
377 reason = optarg;
Hridya Valsaraju7f41a2c2018-09-19 16:29:01 -0700378 } else if (option == "fastboot" &&
Yifan Hongd17174c2018-11-16 12:49:33 -0800379 android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700380 fastboot = true;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700381 }
382 break;
383 }
384 }
385 }
Jerry Zhang49fd5d22018-05-17 12:54:41 -0700386 optind = 1;
Tao Baoff185862019-09-18 13:28:32 -0700387 opterr = 1;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700388
389 if (locale.empty()) {
Tianjie Xu164c60a2019-05-15 13:59:39 -0700390 if (HasCache()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700391 locale = load_locale_from_cache();
392 }
393
394 if (locale.empty()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700395 locale = DEFAULT_LOCALE;
396 }
397 }
398
Tao Bao42c45e22018-07-31 09:37:12 -0700399 static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so";
400 // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have
401 // handed out pointers to code or static [or thread-local] data and doesn't collect them all back
402 // in on dlclose).
403 void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW);
404
405 using MakeDeviceType = decltype(&make_device);
406 MakeDeviceType make_device_func = nullptr;
407 if (librecovery_ui_ext == nullptr) {
408 printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror());
409 } else {
410 reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device");
411 if (make_device_func == nullptr) {
412 printf("Failed to dlsym make_device: %s\n", dlerror());
413 }
414 }
415
416 Device* device;
417 if (make_device_func == nullptr) {
418 printf("Falling back to the default make_device() instead\n");
419 device = make_device();
420 } else {
421 printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt);
422 device = (*make_device_func)();
423 }
424
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700425 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
426 printf("Quiescent recovery mode.\n");
427 device->ResetUI(new StubRecoveryUI());
428 } else {
429 if (!device->GetUI()->Init(locale)) {
430 printf("Failed to initialize UI; using stub UI instead.\n");
431 device->ResetUI(new StubRecoveryUI());
432 }
433 }
Tianjie Xub63a2212019-07-29 14:21:49 -0700434
435 BootState boot_state(reason, stage); // recovery_main owns the state of boot.
436 device->SetBootState(&boot_state);
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700437 ui = device->GetUI();
438
Tianjie Xu164c60a2019-05-15 13:59:39 -0700439 if (!HasCache()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700440 device->RemoveMenuItemForAction(Device::WIPE_CACHE);
441 }
442
Yifan Hongd17174c2018-11-16 12:49:33 -0800443 if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsarajudaa301e2018-09-18 14:48:01 -0700444 device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT);
445 }
446
Tianjie Xu164c60a2019-05-15 13:59:39 -0700447 if (!IsRoDebuggable()) {
Tao Baoc6dc3252019-04-16 14:22:25 -0700448 device->RemoveMenuItemForAction(Device::ENTER_RESCUE);
449 }
450
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700451 ui->SetBackground(RecoveryUI::NONE);
452 if (show_text) ui->ShowText(true);
453
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700454 LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start);
455 LOG(INFO) << "locale is [" << locale << "]";
456
Tianjie Xu164c60a2019-05-15 13:59:39 -0700457 auto sehandle = selinux_android_file_context_handle();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700458 selinux_android_set_sehandle(sehandle);
459 if (!sehandle) {
460 ui->Print("Warning: No file_contexts\n");
461 }
462
xunchang2239b9e2019-04-15 15:24:24 -0700463 SetLoggingSehandle(sehandle);
xunchang316e9712019-04-12 16:22:15 -0700464
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700465 std::atomic<Device::BuiltinAction> action;
466 std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action));
467 listener_thread.detach();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700468
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700469 while (true) {
Tianjie Xu7d5c3412019-10-29 21:44:39 -0700470 // We start adbd in recovery for the device with userdebug build or a unlocked bootloader.
471 std::string usb_config =
472 fastboot ? "fastboot" : IsRoDebuggable() || IsDeviceUnlocked() ? "adb" : "none";
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700473 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
Hongguang Chen4d0df882020-04-21 20:58:04 -0700474 if (fastboot) {
475 device->PreFastboot();
476 } else {
477 device->PreRecovery();
478 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700479 if (usb_config != usb_state) {
480 if (!SetUsbConfig("none")) {
481 LOG(ERROR) << "Failed to clear USB config";
482 }
483 if (!SetUsbConfig(usb_config)) {
484 LOG(ERROR) << "Failed to set USB config to " << usb_config;
485 }
486 }
487
David Anderson983e2d52019-01-02 11:35:38 -0800488 ui->SetEnableFastbootdLogo(fastboot);
489
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700490 auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args);
491
492 if (ret == Device::KEY_INTERRUPTED) {
493 ret = action.exchange(ret);
494 if (ret == Device::NO_ACTION) {
495 continue;
496 }
497 }
498 switch (ret) {
499 case Device::SHUTDOWN:
500 ui->Print("Shutting down...\n");
Mark Salyzyn488cc052019-05-20 10:36:16 -0700501 Shutdown("userrequested,recovery");
502 break;
503
504 case Device::SHUTDOWN_FROM_FASTBOOT:
505 ui->Print("Shutting down...\n");
506 Shutdown("userrequested,fastboot");
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700507 break;
508
509 case Device::REBOOT_BOOTLOADER:
510 ui->Print("Rebooting to bootloader...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700511 Reboot("bootloader");
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700512 break;
513
Tao Baod9cb0142019-04-23 11:46:25 -0700514 case Device::REBOOT_FASTBOOT:
515 ui->Print("Rebooting to recovery/fastboot...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700516 Reboot("fastboot");
Tao Bao10f441a2019-04-19 15:22:15 -0700517 break;
518
Tao Baod9cb0142019-04-23 11:46:25 -0700519 case Device::REBOOT_RECOVERY:
520 ui->Print("Rebooting to recovery...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700521 Reboot("recovery");
Tao Baod9cb0142019-04-23 11:46:25 -0700522 break;
523
524 case Device::REBOOT_RESCUE: {
Tao Bao782dcc12019-04-29 11:23:16 -0700525 // Not using `Reboot("rescue")`, as it requires matching support in kernel and/or
Tao Baod9cb0142019-04-23 11:46:25 -0700526 // bootloader.
527 bootloader_message boot = {};
528 strlcpy(boot.command, "boot-rescue", sizeof(boot.command));
529 std::string err;
530 if (!write_bootloader_message(boot, &err)) {
531 LOG(ERROR) << "Failed to write bootloader message: " << err;
532 // Stay under recovery on failure.
533 continue;
534 }
535 ui->Print("Rebooting to recovery/rescue...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700536 Reboot("recovery");
Tao Baod9cb0142019-04-23 11:46:25 -0700537 break;
538 }
539
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700540 case Device::ENTER_FASTBOOT:
Tao Baof90d9a12019-05-10 10:40:59 -0700541 if (android::fs_mgr::LogicalPartitionsMapped()) {
David Anderson2b2f4232018-10-29 18:48:56 -0700542 ui->Print("Partitions may be mounted - rebooting to enter fastboot.");
Tao Bao782dcc12019-04-29 11:23:16 -0700543 Reboot("fastboot");
David Anderson2b2f4232018-10-29 18:48:56 -0700544 } else {
545 LOG(INFO) << "Entering fastboot";
546 fastboot = true;
547 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700548 break;
549
550 case Device::ENTER_RECOVERY:
551 LOG(INFO) << "Entering recovery";
552 fastboot = false;
553 break;
554
Mark Salyzyn488cc052019-05-20 10:36:16 -0700555 case Device::REBOOT:
556 ui->Print("Rebooting...\n");
557 Reboot("userrequested,recovery");
558 break;
559
560 case Device::REBOOT_FROM_FASTBOOT:
561 ui->Print("Rebooting...\n");
562 Reboot("userrequested,fastboot");
563 break;
564
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700565 default:
566 ui->Print("Rebooting...\n");
Mark Salyzyn488cc052019-05-20 10:36:16 -0700567 Reboot("unknown" + std::to_string(ret));
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700568 break;
569 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700570 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700571
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700572 // Should be unreachable.
573 return EXIT_SUCCESS;
Tao Bao6d99d4b2018-04-25 16:47:04 -0700574}