blob: b999505fb00ecb33a11933587f3ce0a34c687370 [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 Bao6d99d4b2018-04-25 16:47:04 -070045#include <private/android_logger.h> /* private pmsg functions */
Jerry Zhangf5e319a2018-05-04 11:24:10 -070046#include <selinux/android.h>
47#include <selinux/label.h>
48#include <selinux/selinux.h>
Tao Bao6d99d4b2018-04-25 16:47:04 -070049
50#include "common.h"
Hridya Valsaraju20c81b32018-07-27 22:09:12 -070051#include "fastboot/fastboot.h"
xunchang316e9712019-04-12 16:22:15 -070052#include "install/wipe_data.h"
53#include "otautil/logging.h"
Tao Bao6d99d4b2018-04-25 16:47:04 -070054#include "otautil/paths.h"
xunchang24788852019-03-22 16:08:52 -070055#include "otautil/roots.h"
Jerry Zhangf5e319a2018-05-04 11:24:10 -070056#include "otautil/sysutil.h"
57#include "recovery.h"
Tianjie Xu8f397302018-08-20 13:40:47 -070058#include "recovery_ui/device.h"
59#include "recovery_ui/stub_ui.h"
60#include "recovery_ui/ui.h"
Tao Bao6d99d4b2018-04-25 16:47:04 -070061
Jerry Zhangf5e319a2018-05-04 11:24:10 -070062static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
63static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
64
65static constexpr const char* CACHE_ROOT = "/cache";
66
67bool has_cache = false;
68
69RecoveryUI* ui = nullptr;
70struct selabel_handle* sehandle;
71
Tao Bao6d99d4b2018-04-25 16:47:04 -070072static void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity,
73 const char* /* tag */, const char* /* file */, unsigned int /* line */,
74 const char* message) {
75 static constexpr char log_characters[] = "VDIWEF";
76 if (severity >= android::base::ERROR && ui != nullptr) {
77 ui->Print("E:%s\n", message);
78 } else {
79 fprintf(stdout, "%c:%s\n", log_characters[severity], message);
80 }
81}
82
Jerry Zhangf5e319a2018-05-04 11:24:10 -070083// command line args come from, in decreasing precedence:
84// - the actual command line
85// - the bootloader control block (one per line, after "recovery")
86// - the contents of COMMAND_FILE (one per line)
87static std::vector<std::string> get_args(const int argc, char** const argv) {
88 CHECK_GT(argc, 0);
89
90 bootloader_message boot = {};
91 std::string err;
92 if (!read_bootloader_message(&boot, &err)) {
93 LOG(ERROR) << err;
94 // If fails, leave a zeroed bootloader_message.
95 boot = {};
96 }
97 stage = std::string(boot.stage);
98
David Andersoneee4e262018-08-21 13:10:45 -070099 std::string boot_command;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700100 if (boot.command[0] != 0) {
David Andersoneee4e262018-08-21 13:10:45 -0700101 if (memchr(boot.command, '\0', sizeof(boot.command))) {
102 boot_command = std::string(boot.command);
103 } else {
104 boot_command = std::string(boot.command, sizeof(boot.command));
105 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700106 LOG(INFO) << "Boot command: " << boot_command;
107 }
108
109 if (boot.status[0] != 0) {
110 std::string boot_status = std::string(boot.status, sizeof(boot.status));
111 LOG(INFO) << "Boot status: " << boot_status;
112 }
113
114 std::vector<std::string> args(argv, argv + argc);
115
116 // --- if arguments weren't supplied, look in the bootloader control block
117 if (args.size() == 1) {
118 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
119 std::string boot_recovery(boot.recovery);
120 std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n");
121 if (!tokens.empty() && tokens[0] == "recovery") {
122 for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
123 // Skip empty and '\0'-filled tokens.
124 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
125 }
126 LOG(INFO) << "Got " << args.size() << " arguments from boot message";
127 } else if (boot.recovery[0] != 0) {
128 LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\"";
129 }
130 }
131
132 // --- if that doesn't work, try the command file (if we have /cache).
133 if (args.size() == 1 && has_cache) {
134 std::string content;
135 if (ensure_path_mounted(COMMAND_FILE) == 0 &&
136 android::base::ReadFileToString(COMMAND_FILE, &content)) {
137 std::vector<std::string> tokens = android::base::Split(content, "\n");
138 // All the arguments in COMMAND_FILE are needed (unlike the BCB message,
139 // COMMAND_FILE doesn't use filename as the first argument).
140 for (auto it = tokens.begin(); it != tokens.end(); it++) {
141 // Skip empty and '\0'-filled tokens.
142 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
143 }
144 LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
145 }
146 }
147
148 // Write the arguments (excluding the filename in args[0]) back into the
149 // bootloader control block. So the device will always boot into recovery to
150 // finish the pending work, until finish_recovery() is called.
151 std::vector<std::string> options(args.cbegin() + 1, args.cend());
152 if (!update_bootloader_message(options, &err)) {
153 LOG(ERROR) << "Failed to set BCB message: " << err;
154 }
155
David Andersoneee4e262018-08-21 13:10:45 -0700156 // Finally, if no arguments were specified, check whether we should boot
Tao Baod9cb0142019-04-23 11:46:25 -0700157 // into fastboot or rescue mode.
David Andersoneee4e262018-08-21 13:10:45 -0700158 if (args.size() == 1 && boot_command == "boot-fastboot") {
159 args.emplace_back("--fastboot");
Tao Baod9cb0142019-04-23 11:46:25 -0700160 } else if (args.size() == 1 && boot_command == "boot-rescue") {
161 args.emplace_back("--rescue");
David Andersoneee4e262018-08-21 13:10:45 -0700162 }
163
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700164 return args;
165}
166
167static std::string load_locale_from_cache() {
168 if (ensure_path_mounted(LOCALE_FILE) != 0) {
169 LOG(ERROR) << "Can't mount " << LOCALE_FILE;
170 return "";
171 }
172
173 std::string content;
174 if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
175 PLOG(ERROR) << "Can't read " << LOCALE_FILE;
176 return "";
177 }
178
179 return android::base::Trim(content);
180}
181
Tao Baoe0cfab32019-03-29 15:53:23 -0700182// Sets the usb config to 'state'.
183static bool SetUsbConfig(const std::string& state) {
184 android::base::SetProperty("sys.usb.config", state);
185 return android::base::WaitForProperty("sys.usb.state", state);
186}
187
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700188static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) {
189 android::base::unique_fd sock_fd(android_get_control_socket("recovery"));
190 if (sock_fd < 0) {
191 PLOG(ERROR) << "Failed to open recovery socket";
192 return;
193 }
194 listen(sock_fd, 4);
195
196 while (true) {
197 android::base::unique_fd connection_fd;
198 connection_fd.reset(accept(sock_fd, nullptr, nullptr));
199 if (connection_fd < 0) {
200 PLOG(ERROR) << "Failed to accept socket connection";
201 continue;
202 }
203 char msg;
204 constexpr char kSwitchToFastboot = 'f';
205 constexpr char kSwitchToRecovery = 'r';
206 ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg)));
207 if (ret != sizeof(msg)) {
208 PLOG(ERROR) << "Couldn't read from socket";
209 continue;
210 }
211 switch (msg) {
212 case kSwitchToRecovery:
213 action = Device::BuiltinAction::ENTER_RECOVERY;
214 break;
215 case kSwitchToFastboot:
216 action = Device::BuiltinAction::ENTER_FASTBOOT;
217 break;
218 default:
219 LOG(ERROR) << "Unrecognized char from socket " << msg;
220 continue;
221 }
222 ui->InterruptKey();
223 }
224}
225
Tao Bao6d99d4b2018-04-25 16:47:04 -0700226static void redirect_stdio(const char* filename) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800227 android::base::unique_fd pipe_read, pipe_write;
228 // Create a pipe that allows parent process sending logs over.
229 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
230 PLOG(ERROR) << "Failed to create pipe for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700231
232 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
233 // anywhere to complain...
234 freopen(filename, "a", stdout);
235 setbuf(stdout, nullptr);
236 freopen(filename, "a", stderr);
237 setbuf(stderr, nullptr);
238
239 return;
240 }
241
242 pid_t pid = fork();
243 if (pid == -1) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800244 PLOG(ERROR) << "Failed to fork for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700245
246 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
247 // anywhere to complain...
248 freopen(filename, "a", stdout);
249 setbuf(stdout, nullptr);
250 freopen(filename, "a", stderr);
251 setbuf(stderr, nullptr);
252
253 return;
254 }
255
256 if (pid == 0) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800257 // Child process reads the incoming logs and doesn't write to the pipe.
258 pipe_write.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700259
260 auto start = std::chrono::steady_clock::now();
261
262 // Child logger to actually write to the log file.
263 FILE* log_fp = fopen(filename, "ae");
264 if (log_fp == nullptr) {
265 PLOG(ERROR) << "fopen \"" << filename << "\" failed";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700266 _exit(EXIT_FAILURE);
267 }
268
Tao Bao6fcd2082019-01-16 09:29:17 -0800269 FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), "r");
Tao Bao6d99d4b2018-04-25 16:47:04 -0700270 if (pipe_fp == nullptr) {
271 PLOG(ERROR) << "fdopen failed";
272 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700273 _exit(EXIT_FAILURE);
274 }
275
276 char* line = nullptr;
277 size_t len = 0;
278 while (getline(&line, &len, pipe_fp) != -1) {
279 auto now = std::chrono::steady_clock::now();
280 double duration =
281 std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count();
282 if (line[0] == '\n') {
283 fprintf(log_fp, "[%12.6lf]\n", duration);
284 } else {
285 fprintf(log_fp, "[%12.6lf] %s", duration, line);
286 }
287 fflush(log_fp);
288 }
289
290 PLOG(ERROR) << "getline failed";
291
Tao Bao6fcd2082019-01-16 09:29:17 -0800292 fclose(pipe_fp);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700293 free(line);
294 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700295 _exit(EXIT_FAILURE);
296 } else {
297 // Redirect stdout/stderr to the logger process. Close the unused read end.
Tao Bao6fcd2082019-01-16 09:29:17 -0800298 pipe_read.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700299
300 setbuf(stdout, nullptr);
301 setbuf(stderr, nullptr);
302
Tao Bao6fcd2082019-01-16 09:29:17 -0800303 if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700304 PLOG(ERROR) << "dup2 stdout failed";
305 }
Tao Bao6fcd2082019-01-16 09:29:17 -0800306 if (dup2(pipe_write.get(), STDERR_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700307 PLOG(ERROR) << "dup2 stderr failed";
308 }
Tao Bao6d99d4b2018-04-25 16:47:04 -0700309 }
310}
311
312int main(int argc, char** argv) {
313 // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
314 // (which is redirected to recovery.log) as we used to do.
315 android::base::InitLogging(argv, &UiLogger);
316
317 // Take last pmsg contents and rewrite it to the current pmsg session.
318 static constexpr const char filter[] = "recovery/";
319 // Do we need to rotate?
320 bool do_rotate = false;
321
322 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
323 // Take action to refresh pmsg contents
324 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
325
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700326 time_t start = time(nullptr);
327
Tao Bao6d99d4b2018-04-25 16:47:04 -0700328 // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
329 // instances with different timestamps.
330 redirect_stdio(Paths::Get().temporary_log_file().c_str());
331
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700332 load_volume_table();
333 has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
334
335 std::vector<std::string> args = get_args(argc, argv);
Tao Bao1700cc42018-07-16 22:09:59 -0700336 auto args_to_parse = StringVectorToNullTerminatedArray(args);
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700337
338 static constexpr struct option OPTIONS[] = {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700339 { "fastboot", no_argument, nullptr, 0 },
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700340 { "locale", required_argument, nullptr, 0 },
341 { "show_text", no_argument, nullptr, 't' },
342 { nullptr, 0, nullptr, 0 },
343 };
344
345 bool show_text = false;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700346 bool fastboot = false;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700347 std::string locale;
348
349 int arg;
350 int option_index;
Tao Bao1700cc42018-07-16 22:09:59 -0700351 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700352 &option_index)) != -1) {
353 switch (arg) {
354 case 't':
355 show_text = true;
356 break;
357 case 0: {
358 std::string option = OPTIONS[option_index].name;
359 if (option == "locale") {
360 locale = optarg;
Hridya Valsaraju7f41a2c2018-09-19 16:29:01 -0700361 } else if (option == "fastboot" &&
Yifan Hongd17174c2018-11-16 12:49:33 -0800362 android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700363 fastboot = true;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700364 }
365 break;
366 }
367 }
368 }
Jerry Zhang49fd5d22018-05-17 12:54:41 -0700369 optind = 1;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700370
371 if (locale.empty()) {
372 if (has_cache) {
373 locale = load_locale_from_cache();
374 }
375
376 if (locale.empty()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700377 locale = DEFAULT_LOCALE;
378 }
379 }
380
Tao Bao42c45e22018-07-31 09:37:12 -0700381 static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so";
382 // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have
383 // handed out pointers to code or static [or thread-local] data and doesn't collect them all back
384 // in on dlclose).
385 void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW);
386
387 using MakeDeviceType = decltype(&make_device);
388 MakeDeviceType make_device_func = nullptr;
389 if (librecovery_ui_ext == nullptr) {
390 printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror());
391 } else {
392 reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device");
393 if (make_device_func == nullptr) {
394 printf("Failed to dlsym make_device: %s\n", dlerror());
395 }
396 }
397
398 Device* device;
399 if (make_device_func == nullptr) {
400 printf("Falling back to the default make_device() instead\n");
401 device = make_device();
402 } else {
403 printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt);
404 device = (*make_device_func)();
405 }
406
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700407 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
408 printf("Quiescent recovery mode.\n");
409 device->ResetUI(new StubRecoveryUI());
410 } else {
411 if (!device->GetUI()->Init(locale)) {
412 printf("Failed to initialize UI; using stub UI instead.\n");
413 device->ResetUI(new StubRecoveryUI());
414 }
415 }
416 ui = device->GetUI();
417
418 if (!has_cache) {
419 device->RemoveMenuItemForAction(Device::WIPE_CACHE);
420 }
421
Yifan Hongd17174c2018-11-16 12:49:33 -0800422 if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsarajudaa301e2018-09-18 14:48:01 -0700423 device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT);
424 }
425
Tao Baoc6dc3252019-04-16 14:22:25 -0700426 if (!is_ro_debuggable()) {
427 device->RemoveMenuItemForAction(Device::ENTER_RESCUE);
428 }
429
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700430 ui->SetBackground(RecoveryUI::NONE);
431 if (show_text) ui->ShowText(true);
432
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700433 LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start);
434 LOG(INFO) << "locale is [" << locale << "]";
435
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700436 sehandle = selinux_android_file_context_handle();
437 selinux_android_set_sehandle(sehandle);
438 if (!sehandle) {
439 ui->Print("Warning: No file_contexts\n");
440 }
441
xunchang2239b9e2019-04-15 15:24:24 -0700442 SetLoggingSehandle(sehandle);
xunchang316e9712019-04-12 16:22:15 -0700443
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700444 std::atomic<Device::BuiltinAction> action;
445 std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action));
446 listener_thread.detach();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700447
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700448 while (true) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700449 std::string usb_config = fastboot ? "fastboot" : is_ro_debuggable() ? "adb" : "none";
450 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
451 if (usb_config != usb_state) {
452 if (!SetUsbConfig("none")) {
453 LOG(ERROR) << "Failed to clear USB config";
454 }
455 if (!SetUsbConfig(usb_config)) {
456 LOG(ERROR) << "Failed to set USB config to " << usb_config;
457 }
458 }
459
David Anderson983e2d52019-01-02 11:35:38 -0800460 ui->SetEnableFastbootdLogo(fastboot);
461
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700462 auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args);
463
464 if (ret == Device::KEY_INTERRUPTED) {
465 ret = action.exchange(ret);
466 if (ret == Device::NO_ACTION) {
467 continue;
468 }
469 }
470 switch (ret) {
471 case Device::SHUTDOWN:
472 ui->Print("Shutting down...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700473 Shutdown();
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700474 break;
475
476 case Device::REBOOT_BOOTLOADER:
477 ui->Print("Rebooting to bootloader...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700478 Reboot("bootloader");
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700479 break;
480
Tao Baod9cb0142019-04-23 11:46:25 -0700481 case Device::REBOOT_FASTBOOT:
482 ui->Print("Rebooting to recovery/fastboot...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700483 Reboot("fastboot");
Tao Bao10f441a2019-04-19 15:22:15 -0700484 break;
485
Tao Baod9cb0142019-04-23 11:46:25 -0700486 case Device::REBOOT_RECOVERY:
487 ui->Print("Rebooting to recovery...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700488 Reboot("recovery");
Tao Baod9cb0142019-04-23 11:46:25 -0700489 break;
490
491 case Device::REBOOT_RESCUE: {
Tao Bao782dcc12019-04-29 11:23:16 -0700492 // Not using `Reboot("rescue")`, as it requires matching support in kernel and/or
Tao Baod9cb0142019-04-23 11:46:25 -0700493 // bootloader.
494 bootloader_message boot = {};
495 strlcpy(boot.command, "boot-rescue", sizeof(boot.command));
496 std::string err;
497 if (!write_bootloader_message(boot, &err)) {
498 LOG(ERROR) << "Failed to write bootloader message: " << err;
499 // Stay under recovery on failure.
500 continue;
501 }
502 ui->Print("Rebooting to recovery/rescue...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700503 Reboot("recovery");
Tao Baod9cb0142019-04-23 11:46:25 -0700504 break;
505 }
506
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700507 case Device::ENTER_FASTBOOT:
David Anderson2b2f4232018-10-29 18:48:56 -0700508 if (logical_partitions_mapped()) {
509 ui->Print("Partitions may be mounted - rebooting to enter fastboot.");
Tao Bao782dcc12019-04-29 11:23:16 -0700510 Reboot("fastboot");
David Anderson2b2f4232018-10-29 18:48:56 -0700511 } else {
512 LOG(INFO) << "Entering fastboot";
513 fastboot = true;
514 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700515 break;
516
517 case Device::ENTER_RECOVERY:
518 LOG(INFO) << "Entering recovery";
519 fastboot = false;
520 break;
521
522 default:
523 ui->Print("Rebooting...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700524 Reboot("");
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700525 break;
526 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700527 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700528
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700529 // Should be unreachable.
530 return EXIT_SUCCESS;
Tao Bao6d99d4b2018-04-25 16:47:04 -0700531}