blob: 2749c35a9a389e8bd25170d84470b50e7d6246d9 [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() {
Alessandro83b95512020-04-06 23:55:07 +0200191 android::base::SetLogger(android::base::StdioLogger);
Steve Kondikb50e01b2013-10-19 19:49:20 -0700192 if (ensure_path_mounted("/data") == 0) {
193 if (access(adb_keys_root, F_OK) != 0) {
194 if (access(adb_keys_data, R_OK) == 0) {
195 std::error_code ec; // to invoke the overloaded copy_file() that won't throw.
196 if (!fs::copy_file(adb_keys_data, adb_keys_root, ec)) {
197 PLOG(ERROR) << "Failed to copy adb keys";
198 }
199 }
200 }
201 ensure_path_unmounted("/data");
202 }
Alessandro83b95512020-04-06 23:55:07 +0200203 android::base::SetLogger(UiLogger);
Steve Kondikb50e01b2013-10-19 19:49:20 -0700204}
205
Tao Baoe0cfab32019-03-29 15:53:23 -0700206// Sets the usb config to 'state'.
207static bool SetUsbConfig(const std::string& state) {
208 android::base::SetProperty("sys.usb.config", state);
209 return android::base::WaitForProperty("sys.usb.state", state);
210}
211
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700212static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) {
213 android::base::unique_fd sock_fd(android_get_control_socket("recovery"));
214 if (sock_fd < 0) {
215 PLOG(ERROR) << "Failed to open recovery socket";
216 return;
217 }
218 listen(sock_fd, 4);
219
220 while (true) {
221 android::base::unique_fd connection_fd;
222 connection_fd.reset(accept(sock_fd, nullptr, nullptr));
223 if (connection_fd < 0) {
224 PLOG(ERROR) << "Failed to accept socket connection";
225 continue;
226 }
227 char msg;
228 constexpr char kSwitchToFastboot = 'f';
229 constexpr char kSwitchToRecovery = 'r';
230 ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg)));
231 if (ret != sizeof(msg)) {
232 PLOG(ERROR) << "Couldn't read from socket";
233 continue;
234 }
235 switch (msg) {
236 case kSwitchToRecovery:
237 action = Device::BuiltinAction::ENTER_RECOVERY;
238 break;
239 case kSwitchToFastboot:
240 action = Device::BuiltinAction::ENTER_FASTBOOT;
241 break;
242 default:
243 LOG(ERROR) << "Unrecognized char from socket " << msg;
244 continue;
245 }
246 ui->InterruptKey();
247 }
248}
249
Tao Bao6d99d4b2018-04-25 16:47:04 -0700250static void redirect_stdio(const char* filename) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800251 android::base::unique_fd pipe_read, pipe_write;
252 // Create a pipe that allows parent process sending logs over.
253 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
254 PLOG(ERROR) << "Failed to create pipe for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700255
256 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
257 // anywhere to complain...
258 freopen(filename, "a", stdout);
259 setbuf(stdout, nullptr);
260 freopen(filename, "a", stderr);
261 setbuf(stderr, nullptr);
262
263 return;
264 }
265
266 pid_t pid = fork();
267 if (pid == -1) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800268 PLOG(ERROR) << "Failed to fork for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700269
270 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
271 // anywhere to complain...
272 freopen(filename, "a", stdout);
273 setbuf(stdout, nullptr);
274 freopen(filename, "a", stderr);
275 setbuf(stderr, nullptr);
276
277 return;
278 }
279
280 if (pid == 0) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800281 // Child process reads the incoming logs and doesn't write to the pipe.
282 pipe_write.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700283
284 auto start = std::chrono::steady_clock::now();
285
286 // Child logger to actually write to the log file.
287 FILE* log_fp = fopen(filename, "ae");
288 if (log_fp == nullptr) {
289 PLOG(ERROR) << "fopen \"" << filename << "\" failed";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700290 _exit(EXIT_FAILURE);
291 }
292
Tao Bao6fcd2082019-01-16 09:29:17 -0800293 FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), "r");
Tao Bao6d99d4b2018-04-25 16:47:04 -0700294 if (pipe_fp == nullptr) {
295 PLOG(ERROR) << "fdopen failed";
296 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700297 _exit(EXIT_FAILURE);
298 }
299
300 char* line = nullptr;
301 size_t len = 0;
302 while (getline(&line, &len, pipe_fp) != -1) {
303 auto now = std::chrono::steady_clock::now();
304 double duration =
305 std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count();
306 if (line[0] == '\n') {
307 fprintf(log_fp, "[%12.6lf]\n", duration);
308 } else {
309 fprintf(log_fp, "[%12.6lf] %s", duration, line);
310 }
311 fflush(log_fp);
312 }
313
314 PLOG(ERROR) << "getline failed";
315
Tao Bao6fcd2082019-01-16 09:29:17 -0800316 fclose(pipe_fp);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700317 free(line);
318 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700319 _exit(EXIT_FAILURE);
320 } else {
321 // Redirect stdout/stderr to the logger process. Close the unused read end.
Tao Bao6fcd2082019-01-16 09:29:17 -0800322 pipe_read.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700323
324 setbuf(stdout, nullptr);
325 setbuf(stderr, nullptr);
326
Tao Bao6fcd2082019-01-16 09:29:17 -0800327 if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700328 PLOG(ERROR) << "dup2 stdout failed";
329 }
Tao Bao6fcd2082019-01-16 09:29:17 -0800330 if (dup2(pipe_write.get(), STDERR_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700331 PLOG(ERROR) << "dup2 stderr failed";
332 }
Tao Bao6d99d4b2018-04-25 16:47:04 -0700333 }
334}
335
336int main(int argc, char** argv) {
337 // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
338 // (which is redirected to recovery.log) as we used to do.
339 android::base::InitLogging(argv, &UiLogger);
340
341 // Take last pmsg contents and rewrite it to the current pmsg session.
342 static constexpr const char filter[] = "recovery/";
343 // Do we need to rotate?
344 bool do_rotate = false;
345
346 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
347 // Take action to refresh pmsg contents
348 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
349
Steve Kondikb50e01b2013-10-19 19:49:20 -0700350 // Clear umask for packages that copy files out to /tmp and then over
351 // to /system without properly setting all permissions (eg. gapps).
352 umask(0);
353
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700354 time_t start = time(nullptr);
355
Tao Bao6d99d4b2018-04-25 16:47:04 -0700356 // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
357 // instances with different timestamps.
358 redirect_stdio(Paths::Get().temporary_log_file().c_str());
359
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700360 load_volume_table();
361 has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
362
363 std::vector<std::string> args = get_args(argc, argv);
Tao Bao1700cc42018-07-16 22:09:59 -0700364 auto args_to_parse = StringVectorToNullTerminatedArray(args);
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700365
366 static constexpr struct option OPTIONS[] = {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700367 { "fastboot", no_argument, nullptr, 0 },
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700368 { "locale", required_argument, nullptr, 0 },
369 { "show_text", no_argument, nullptr, 't' },
370 { nullptr, 0, nullptr, 0 },
371 };
372
373 bool show_text = false;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700374 bool fastboot = false;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700375 std::string locale;
376
377 int arg;
378 int option_index;
Tao Bao1700cc42018-07-16 22:09:59 -0700379 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700380 &option_index)) != -1) {
381 switch (arg) {
382 case 't':
383 show_text = true;
384 break;
385 case 0: {
386 std::string option = OPTIONS[option_index].name;
387 if (option == "locale") {
388 locale = optarg;
Hridya Valsaraju7f41a2c2018-09-19 16:29:01 -0700389 } else if (option == "fastboot" &&
Alessandro Astone2effe772020-02-26 17:25:54 +0100390 (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) ||
391 android::base::GetBoolProperty("ro.fastbootd.available", false))) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700392 fastboot = true;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700393 }
394 break;
395 }
396 }
397 }
Jerry Zhang49fd5d22018-05-17 12:54:41 -0700398 optind = 1;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700399
400 if (locale.empty()) {
401 if (has_cache) {
402 locale = load_locale_from_cache();
403 }
404
405 if (locale.empty()) {
406 static constexpr const char* DEFAULT_LOCALE = "en-US";
407 locale = DEFAULT_LOCALE;
408 }
409 }
410
Tao Bao42c45e22018-07-31 09:37:12 -0700411 static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so";
412 // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have
413 // handed out pointers to code or static [or thread-local] data and doesn't collect them all back
414 // in on dlclose).
415 void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW);
416
417 using MakeDeviceType = decltype(&make_device);
418 MakeDeviceType make_device_func = nullptr;
419 if (librecovery_ui_ext == nullptr) {
420 printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror());
421 } else {
422 reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device");
423 if (make_device_func == nullptr) {
424 printf("Failed to dlsym make_device: %s\n", dlerror());
425 }
426 }
427
428 Device* device;
429 if (make_device_func == nullptr) {
430 printf("Falling back to the default make_device() instead\n");
431 device = make_device();
432 } else {
433 printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt);
434 device = (*make_device_func)();
435 }
436
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700437 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
438 printf("Quiescent recovery mode.\n");
439 device->ResetUI(new StubRecoveryUI());
440 } else {
441 if (!device->GetUI()->Init(locale)) {
442 printf("Failed to initialize UI; using stub UI instead.\n");
443 device->ResetUI(new StubRecoveryUI());
444 }
445 }
446 ui = device->GetUI();
447
448 if (!has_cache) {
449 device->RemoveMenuItemForAction(Device::WIPE_CACHE);
450 }
451
Alessandro Astone2effe772020-02-26 17:25:54 +0100452 if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) &&
453 !android::base::GetBoolProperty("ro.fastbootd.available", false)) {
Hridya Valsarajudaa301e2018-09-18 14:48:01 -0700454 device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT);
455 }
456
Michael Bestas6108e702019-09-27 20:29:42 +0300457 if (get_build_type() != "eng") {
458 device->RemoveMenuItemForAction(Device::RUN_GRAPHICS_TEST);
459 device->RemoveMenuItemForAction(Device::RUN_LOCALE_TEST);
Tao Bao378bfbf2019-04-16 14:22:25 -0700460 device->RemoveMenuItemForAction(Device::ENTER_RESCUE);
461 }
462
LuK133759e04592020-04-12 19:04:59 +0200463 if (get_build_type() != "userdebug") {
464 device->RemoveMenuItemForAction(Device::ENABLE_ADB);
465 }
466
Luca Stefani69531d22020-03-14 00:12:18 +0100467 if (get_build_type() == "user") {
468 device->RemoveMenuItemForAction(Device::WIPE_SYSTEM);
469 device->RemoveMenuItemForAction(Device::MOUNT_SYSTEM);
470 }
471
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700472 ui->SetBackground(RecoveryUI::NONE);
473 if (show_text) ui->ShowText(true);
474
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700475 LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start);
476 LOG(INFO) << "locale is [" << locale << "]";
477
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700478 sehandle = selinux_android_file_context_handle();
479 selinux_android_set_sehandle(sehandle);
480 if (!sehandle) {
481 ui->Print("Warning: No file_contexts\n");
482 }
483
xunchangcd780b42019-04-15 15:24:24 -0700484 SetLoggingSehandle(sehandle);
xunchang388d2532019-04-12 16:22:15 -0700485
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700486 std::atomic<Device::BuiltinAction> action;
487 std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action));
488 listener_thread.detach();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700489
Steve Kondikb50e01b2013-10-19 19:49:20 -0700490 // Set up adb_keys and enable root before starting ADB.
491 if (is_ro_debuggable() && !fastboot) {
492 copy_userdata_files();
Alessandro Astoneb441fc72020-03-20 20:27:23 +0100493 android::base::SetProperty("service.adb.root", "1");
Steve Kondikb50e01b2013-10-19 19:49:20 -0700494 }
495
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700496 while (true) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700497 std::string usb_config = fastboot ? "fastboot" : is_ro_debuggable() ? "adb" : "none";
498 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
499 if (usb_config != usb_state) {
500 if (!SetUsbConfig("none")) {
501 LOG(ERROR) << "Failed to clear USB config";
502 }
503 if (!SetUsbConfig(usb_config)) {
504 LOG(ERROR) << "Failed to set USB config to " << usb_config;
505 }
506 }
507
David Anderson983e2d52019-01-02 11:35:38 -0800508 ui->SetEnableFastbootdLogo(fastboot);
509
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700510 auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args);
511
512 if (ret == Device::KEY_INTERRUPTED) {
513 ret = action.exchange(ret);
514 if (ret == Device::NO_ACTION) {
515 continue;
516 }
517 }
518 switch (ret) {
519 case Device::SHUTDOWN:
520 ui->Print("Shutting down...\n");
Tao Bao75321ad2019-04-23 11:46:25 -0700521 // TODO: Move all the reboots to reboot(), which should conditionally set quiescent flag.
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700522 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,");
523 break;
524
525 case Device::REBOOT_BOOTLOADER:
526 ui->Print("Rebooting to bootloader...\n");
527 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
528 break;
529
Tao Bao75321ad2019-04-23 11:46:25 -0700530 case Device::REBOOT_FASTBOOT:
531 ui->Print("Rebooting to recovery/fastboot...\n");
532 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
Tao Bao7b9b7db2019-04-19 15:22:15 -0700533 break;
534
Tao Bao75321ad2019-04-23 11:46:25 -0700535 case Device::REBOOT_RECOVERY:
536 ui->Print("Rebooting to recovery...\n");
537 reboot("reboot,recovery");
538 break;
539
540 case Device::REBOOT_RESCUE: {
541 // Not using `reboot("reboot,rescue")`, as it requires matching support in kernel and/or
542 // bootloader.
543 bootloader_message boot = {};
544 strlcpy(boot.command, "boot-rescue", sizeof(boot.command));
545 std::string err;
546 if (!write_bootloader_message(boot, &err)) {
547 LOG(ERROR) << "Failed to write bootloader message: " << err;
548 // Stay under recovery on failure.
549 continue;
550 }
551 ui->Print("Rebooting to recovery/rescue...\n");
552 reboot("reboot,recovery");
553 break;
554 }
555
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700556 case Device::ENTER_FASTBOOT:
David Anderson2b2f4232018-10-29 18:48:56 -0700557 if (logical_partitions_mapped()) {
558 ui->Print("Partitions may be mounted - rebooting to enter fastboot.");
559 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
560 } else {
561 LOG(INFO) << "Entering fastboot";
562 fastboot = true;
563 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700564 break;
565
566 case Device::ENTER_RECOVERY:
567 LOG(INFO) << "Entering recovery";
568 fastboot = false;
Tom Marshall645801f2020-03-29 14:36:57 +0200569 device->GoHome();
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700570 break;
571
572 default:
573 ui->Print("Rebooting...\n");
574 reboot("reboot,");
575 break;
576 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700577 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700578
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700579 // Should be unreachable.
580 return EXIT_SUCCESS;
Tao Bao6d99d4b2018-04-25 16:47:04 -0700581}