blob: 3b5694766257cf2457397c964c8b5f2196721f05 [file] [log] [blame]
Tao Bao6d99d4b2018-04-25 16:47:04 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
Steve Kondikb50e01b2013-10-19 19:49:20 -07003 * Copyright (C) 2019 The LineageOS Project
Tao Bao6d99d4b2018-04-25 16:47:04 -07004 *
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 Bao42c45e22018-07-31 09:37:12 -070018#include <dlfcn.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070019#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 Bao6d99d4b2018-04-25 16:47:04 -070026#include <stdio.h>
27#include <stdlib.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070028#include <string.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <time.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070032#include <unistd.h>
33
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070034#include <atomic>
Steve Kondikb50e01b2013-10-19 19:49:20 -070035#include <filesystem>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070036#include <string>
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070037#include <thread>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070038#include <vector>
Tao Bao6d99d4b2018-04-25 16:47:04 -070039
Jerry Zhangf5e319a2018-05-04 11:24:10 -070040#include <android-base/file.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070041#include <android-base/logging.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070042#include <android-base/properties.h>
43#include <android-base/strings.h>
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070044#include <android-base/unique_fd.h>
Jerry Zhangf5e319a2018-05-04 11:24:10 -070045#include <bootloader_message/bootloader_message.h>
46#include <cutils/android_reboot.h>
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070047#include <cutils/sockets.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070048#include <private/android_logger.h> /* private pmsg functions */
Jerry Zhangf5e319a2018-05-04 11:24:10 -070049#include <selinux/android.h>
50#include <selinux/label.h>
51#include <selinux/selinux.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070052
53#include "common.h"
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070054#include "fastboot/fastboot.h"
xunchang388d2532019-04-12 16:22:15 -070055#include "install/wipe_data.h"
56#include "otautil/logging.h"
Tao Bao6d99d4b2018-04-25 16:47:04 -070057#include "otautil/paths.h"
xunchang24788852019-03-22 16:08:52 -070058#include "otautil/roots.h"
Jerry Zhangf5e319a2018-05-04 11:24:10 -070059#include "otautil/sysutil.h"
60#include "recovery.h"
Tianjie Xu8f397302018-08-20 13:40:47 -070061#include "recovery_ui/device.h"
62#include "recovery_ui/stub_ui.h"
63#include "recovery_ui/ui.h"
Tao Bao6d99d4b2018-04-25 16:47:04 -070064
Steve Kondikb50e01b2013-10-19 19:49:20 -070065namespace fs = std::filesystem;
66
Jerry Zhangf5e319a2018-05-04 11:24:10 -070067static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
68static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
69
70static constexpr const char* CACHE_ROOT = "/cache";
71
72bool has_cache = false;
73
74RecoveryUI* ui = nullptr;
75struct selabel_handle* sehandle;
76
Steve Kondikb50e01b2013-10-19 19:49:20 -070077static constexpr const char* adb_keys_data = "/data/misc/adb/adb_keys";
78static constexpr const char* adb_keys_root = "/adb_keys";
79
Tao Bao6d99d4b2018-04-25 16:47:04 -070080static 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 Zhangf5e319a2018-05-04 11:24:10 -070091// 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)
95static 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 Andersoneee4e262018-08-21 13:10:45 -0700107 std::string boot_command;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700108 if (boot.command[0] != 0) {
David Andersoneee4e262018-08-21 13:10:45 -0700109 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 Zhangf5e319a2018-05-04 11:24:10 -0700114 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 Andersoneee4e262018-08-21 13:10:45 -0700164 // Finally, if no arguments were specified, check whether we should boot
Tao Bao75321ad2019-04-23 11:46:25 -0700165 // into fastboot or rescue mode.
David Andersoneee4e262018-08-21 13:10:45 -0700166 if (args.size() == 1 && boot_command == "boot-fastboot") {
167 args.emplace_back("--fastboot");
Tao Bao75321ad2019-04-23 11:46:25 -0700168 } else if (args.size() == 1 && boot_command == "boot-rescue") {
169 args.emplace_back("--rescue");
David Andersoneee4e262018-08-21 13:10:45 -0700170 }
171
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700172 return args;
173}
174
175static 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 Kondikb50e01b2013-10-19 19:49:20 -0700190static void copy_userdata_files() {
191 if (ensure_path_mounted("/data") == 0) {
192 if (access(adb_keys_root, F_OK) != 0) {
193 if (access(adb_keys_data, R_OK) == 0) {
194 std::error_code ec; // to invoke the overloaded copy_file() that won't throw.
195 if (!fs::copy_file(adb_keys_data, adb_keys_root, ec)) {
196 PLOG(ERROR) << "Failed to copy adb keys";
197 }
198 }
199 }
200 ensure_path_unmounted("/data");
201 }
202}
203
Tao Baoe0cfab32019-03-29 15:53:23 -0700204// Sets the usb config to 'state'.
205static bool SetUsbConfig(const std::string& state) {
206 android::base::SetProperty("sys.usb.config", state);
207 return android::base::WaitForProperty("sys.usb.state", state);
208}
209
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700210static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) {
211 android::base::unique_fd sock_fd(android_get_control_socket("recovery"));
212 if (sock_fd < 0) {
213 PLOG(ERROR) << "Failed to open recovery socket";
214 return;
215 }
216 listen(sock_fd, 4);
217
218 while (true) {
219 android::base::unique_fd connection_fd;
220 connection_fd.reset(accept(sock_fd, nullptr, nullptr));
221 if (connection_fd < 0) {
222 PLOG(ERROR) << "Failed to accept socket connection";
223 continue;
224 }
225 char msg;
226 constexpr char kSwitchToFastboot = 'f';
227 constexpr char kSwitchToRecovery = 'r';
228 ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg)));
229 if (ret != sizeof(msg)) {
230 PLOG(ERROR) << "Couldn't read from socket";
231 continue;
232 }
233 switch (msg) {
234 case kSwitchToRecovery:
235 action = Device::BuiltinAction::ENTER_RECOVERY;
236 break;
237 case kSwitchToFastboot:
238 action = Device::BuiltinAction::ENTER_FASTBOOT;
239 break;
240 default:
241 LOG(ERROR) << "Unrecognized char from socket " << msg;
242 continue;
243 }
244 ui->InterruptKey();
245 }
246}
247
Tao Bao6d99d4b2018-04-25 16:47:04 -0700248static void redirect_stdio(const char* filename) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800249 android::base::unique_fd pipe_read, pipe_write;
250 // Create a pipe that allows parent process sending logs over.
251 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
252 PLOG(ERROR) << "Failed to create pipe for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700253
254 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
255 // anywhere to complain...
256 freopen(filename, "a", stdout);
257 setbuf(stdout, nullptr);
258 freopen(filename, "a", stderr);
259 setbuf(stderr, nullptr);
260
261 return;
262 }
263
264 pid_t pid = fork();
265 if (pid == -1) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800266 PLOG(ERROR) << "Failed to fork for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700267
268 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
269 // anywhere to complain...
270 freopen(filename, "a", stdout);
271 setbuf(stdout, nullptr);
272 freopen(filename, "a", stderr);
273 setbuf(stderr, nullptr);
274
275 return;
276 }
277
278 if (pid == 0) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800279 // Child process reads the incoming logs and doesn't write to the pipe.
280 pipe_write.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700281
282 auto start = std::chrono::steady_clock::now();
283
284 // Child logger to actually write to the log file.
285 FILE* log_fp = fopen(filename, "ae");
286 if (log_fp == nullptr) {
287 PLOG(ERROR) << "fopen \"" << filename << "\" failed";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700288 _exit(EXIT_FAILURE);
289 }
290
Tao Bao6fcd2082019-01-16 09:29:17 -0800291 FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), "r");
Tao Bao6d99d4b2018-04-25 16:47:04 -0700292 if (pipe_fp == nullptr) {
293 PLOG(ERROR) << "fdopen failed";
294 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700295 _exit(EXIT_FAILURE);
296 }
297
298 char* line = nullptr;
299 size_t len = 0;
300 while (getline(&line, &len, pipe_fp) != -1) {
301 auto now = std::chrono::steady_clock::now();
302 double duration =
303 std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count();
304 if (line[0] == '\n') {
305 fprintf(log_fp, "[%12.6lf]\n", duration);
306 } else {
307 fprintf(log_fp, "[%12.6lf] %s", duration, line);
308 }
309 fflush(log_fp);
310 }
311
312 PLOG(ERROR) << "getline failed";
313
Tao Bao6fcd2082019-01-16 09:29:17 -0800314 fclose(pipe_fp);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700315 free(line);
316 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700317 _exit(EXIT_FAILURE);
318 } else {
319 // Redirect stdout/stderr to the logger process. Close the unused read end.
Tao Bao6fcd2082019-01-16 09:29:17 -0800320 pipe_read.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700321
322 setbuf(stdout, nullptr);
323 setbuf(stderr, nullptr);
324
Tao Bao6fcd2082019-01-16 09:29:17 -0800325 if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700326 PLOG(ERROR) << "dup2 stdout failed";
327 }
Tao Bao6fcd2082019-01-16 09:29:17 -0800328 if (dup2(pipe_write.get(), STDERR_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700329 PLOG(ERROR) << "dup2 stderr failed";
330 }
Tao Bao6d99d4b2018-04-25 16:47:04 -0700331 }
332}
333
334int main(int argc, char** argv) {
335 // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
336 // (which is redirected to recovery.log) as we used to do.
337 android::base::InitLogging(argv, &UiLogger);
338
339 // Take last pmsg contents and rewrite it to the current pmsg session.
340 static constexpr const char filter[] = "recovery/";
341 // Do we need to rotate?
342 bool do_rotate = false;
343
344 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
345 // Take action to refresh pmsg contents
346 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
347
Steve Kondikb50e01b2013-10-19 19:49:20 -0700348 // Clear umask for packages that copy files out to /tmp and then over
349 // to /system without properly setting all permissions (eg. gapps).
350 umask(0);
351
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700352 time_t start = time(nullptr);
353
Tao Bao6d99d4b2018-04-25 16:47:04 -0700354 // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
355 // instances with different timestamps.
356 redirect_stdio(Paths::Get().temporary_log_file().c_str());
357
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700358 load_volume_table();
359 has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
360
361 std::vector<std::string> args = get_args(argc, argv);
Tao Bao1700cc42018-07-16 22:09:59 -0700362 auto args_to_parse = StringVectorToNullTerminatedArray(args);
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700363
364 static constexpr struct option OPTIONS[] = {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700365 { "fastboot", no_argument, nullptr, 0 },
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700366 { "locale", required_argument, nullptr, 0 },
367 { "show_text", no_argument, nullptr, 't' },
368 { nullptr, 0, nullptr, 0 },
369 };
370
371 bool show_text = false;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700372 bool fastboot = false;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700373 std::string locale;
374
375 int arg;
376 int option_index;
Tao Bao1700cc42018-07-16 22:09:59 -0700377 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700378 &option_index)) != -1) {
379 switch (arg) {
380 case 't':
381 show_text = true;
382 break;
383 case 0: {
384 std::string option = OPTIONS[option_index].name;
385 if (option == "locale") {
386 locale = optarg;
Hridya Valsaraju7f41a2c2018-09-19 16:29:01 -0700387 } else if (option == "fastboot" &&
Alessandro Astone2effe772020-02-26 17:25:54 +0100388 (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) ||
389 android::base::GetBoolProperty("ro.fastbootd.available", false))) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700390 fastboot = true;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700391 }
392 break;
393 }
394 }
395 }
Jerry Zhang49fd5d22018-05-17 12:54:41 -0700396 optind = 1;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700397
398 if (locale.empty()) {
399 if (has_cache) {
400 locale = load_locale_from_cache();
401 }
402
403 if (locale.empty()) {
404 static constexpr const char* DEFAULT_LOCALE = "en-US";
405 locale = DEFAULT_LOCALE;
406 }
407 }
408
Tao Bao42c45e22018-07-31 09:37:12 -0700409 static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so";
410 // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have
411 // handed out pointers to code or static [or thread-local] data and doesn't collect them all back
412 // in on dlclose).
413 void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW);
414
415 using MakeDeviceType = decltype(&make_device);
416 MakeDeviceType make_device_func = nullptr;
417 if (librecovery_ui_ext == nullptr) {
418 printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror());
419 } else {
420 reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device");
421 if (make_device_func == nullptr) {
422 printf("Failed to dlsym make_device: %s\n", dlerror());
423 }
424 }
425
426 Device* device;
427 if (make_device_func == nullptr) {
428 printf("Falling back to the default make_device() instead\n");
429 device = make_device();
430 } else {
431 printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt);
432 device = (*make_device_func)();
433 }
434
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700435 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
436 printf("Quiescent recovery mode.\n");
437 device->ResetUI(new StubRecoveryUI());
438 } else {
439 if (!device->GetUI()->Init(locale)) {
440 printf("Failed to initialize UI; using stub UI instead.\n");
441 device->ResetUI(new StubRecoveryUI());
442 }
443 }
444 ui = device->GetUI();
445
446 if (!has_cache) {
447 device->RemoveMenuItemForAction(Device::WIPE_CACHE);
448 }
449
Alessandro Astone2effe772020-02-26 17:25:54 +0100450 if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) &&
451 !android::base::GetBoolProperty("ro.fastbootd.available", false)) {
Hridya Valsarajudaa301e2018-09-18 14:48:01 -0700452 device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT);
453 }
454
Michael Bestas6108e702019-09-27 20:29:42 +0300455 if (get_build_type() != "eng") {
456 device->RemoveMenuItemForAction(Device::RUN_GRAPHICS_TEST);
457 device->RemoveMenuItemForAction(Device::RUN_LOCALE_TEST);
Tao Bao378bfbf2019-04-16 14:22:25 -0700458 device->RemoveMenuItemForAction(Device::ENTER_RESCUE);
459 }
460
Luca Stefani69531d22020-03-14 00:12:18 +0100461 if (get_build_type() == "user") {
462 device->RemoveMenuItemForAction(Device::WIPE_SYSTEM);
463 device->RemoveMenuItemForAction(Device::MOUNT_SYSTEM);
464 }
465
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700466 ui->SetBackground(RecoveryUI::NONE);
467 if (show_text) ui->ShowText(true);
468
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700469 LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start);
470 LOG(INFO) << "locale is [" << locale << "]";
471
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700472 sehandle = selinux_android_file_context_handle();
473 selinux_android_set_sehandle(sehandle);
474 if (!sehandle) {
475 ui->Print("Warning: No file_contexts\n");
476 }
477
xunchangcd780b42019-04-15 15:24:24 -0700478 SetLoggingSehandle(sehandle);
xunchang388d2532019-04-12 16:22:15 -0700479
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700480 std::atomic<Device::BuiltinAction> action;
481 std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action));
482 listener_thread.detach();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700483
Steve Kondikb50e01b2013-10-19 19:49:20 -0700484 // Set up adb_keys and enable root before starting ADB.
485 if (is_ro_debuggable() && !fastboot) {
486 copy_userdata_files();
487 android::base::SetProperty("lineage.service.adb.root", "1");
488 }
489
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700490 while (true) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700491 std::string usb_config = fastboot ? "fastboot" : is_ro_debuggable() ? "adb" : "none";
492 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
493 if (usb_config != usb_state) {
494 if (!SetUsbConfig("none")) {
495 LOG(ERROR) << "Failed to clear USB config";
496 }
497 if (!SetUsbConfig(usb_config)) {
498 LOG(ERROR) << "Failed to set USB config to " << usb_config;
499 }
500 }
501
David Anderson983e2d52019-01-02 11:35:38 -0800502 ui->SetEnableFastbootdLogo(fastboot);
503
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700504 auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args);
505
506 if (ret == Device::KEY_INTERRUPTED) {
507 ret = action.exchange(ret);
508 if (ret == Device::NO_ACTION) {
509 continue;
510 }
511 }
512 switch (ret) {
513 case Device::SHUTDOWN:
514 ui->Print("Shutting down...\n");
Tao Bao75321ad2019-04-23 11:46:25 -0700515 // TODO: Move all the reboots to reboot(), which should conditionally set quiescent flag.
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700516 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,");
517 break;
518
519 case Device::REBOOT_BOOTLOADER:
520 ui->Print("Rebooting to bootloader...\n");
521 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
522 break;
523
Tao Bao75321ad2019-04-23 11:46:25 -0700524 case Device::REBOOT_FASTBOOT:
525 ui->Print("Rebooting to recovery/fastboot...\n");
526 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
Tao Bao7b9b7db2019-04-19 15:22:15 -0700527 break;
528
Tao Bao75321ad2019-04-23 11:46:25 -0700529 case Device::REBOOT_RECOVERY:
530 ui->Print("Rebooting to recovery...\n");
531 reboot("reboot,recovery");
532 break;
533
534 case Device::REBOOT_RESCUE: {
535 // Not using `reboot("reboot,rescue")`, as it requires matching support in kernel and/or
536 // bootloader.
537 bootloader_message boot = {};
538 strlcpy(boot.command, "boot-rescue", sizeof(boot.command));
539 std::string err;
540 if (!write_bootloader_message(boot, &err)) {
541 LOG(ERROR) << "Failed to write bootloader message: " << err;
542 // Stay under recovery on failure.
543 continue;
544 }
545 ui->Print("Rebooting to recovery/rescue...\n");
546 reboot("reboot,recovery");
547 break;
548 }
549
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700550 case Device::ENTER_FASTBOOT:
David Anderson2b2f4232018-10-29 18:48:56 -0700551 if (logical_partitions_mapped()) {
552 ui->Print("Partitions may be mounted - rebooting to enter fastboot.");
553 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
554 } else {
555 LOG(INFO) << "Entering fastboot";
556 fastboot = true;
557 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700558 break;
559
560 case Device::ENTER_RECOVERY:
561 LOG(INFO) << "Entering recovery";
562 fastboot = false;
Tom Marshall645801f2020-03-29 14:36:57 +0200563 device->GoHome();
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700564 break;
565
566 default:
567 ui->Print("Rebooting...\n");
568 reboot("reboot,");
569 break;
570 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700571 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700572
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700573 // Should be unreachable.
574 return EXIT_SUCCESS;
Tao Bao6d99d4b2018-04-25 16:47:04 -0700575}