blob: 21dcdad595d468a742721c3e8e00c5c976c1a820 [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
Michael Bestas93cd1272019-09-27 20:29:42 +030076static std::string get_build_type() {
77 return android::base::GetProperty("ro.build.type", "");
78}
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
Tianjie Xub63a2212019-07-29 14:21:49 -070091// Parses the command line argument from various sources; and reads the stage field from BCB.
Jerry Zhangf5e319a2018-05-04 11:24:10 -070092// command line args come from, in decreasing precedence:
93// - the actual command line
94// - the bootloader control block (one per line, after "recovery")
95// - the contents of COMMAND_FILE (one per line)
Tianjie Xub63a2212019-07-29 14:21:49 -070096static std::vector<std::string> get_args(const int argc, char** const argv, std::string* stage) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -070097 CHECK_GT(argc, 0);
98
99 bootloader_message boot = {};
100 std::string err;
101 if (!read_bootloader_message(&boot, &err)) {
102 LOG(ERROR) << err;
103 // If fails, leave a zeroed bootloader_message.
104 boot = {};
105 }
Tianjie Xub63a2212019-07-29 14:21:49 -0700106 if (stage) {
107 *stage = std::string(boot.stage);
108 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700109
David Andersoneee4e262018-08-21 13:10:45 -0700110 std::string boot_command;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700111 if (boot.command[0] != 0) {
David Andersoneee4e262018-08-21 13:10:45 -0700112 if (memchr(boot.command, '\0', sizeof(boot.command))) {
113 boot_command = std::string(boot.command);
114 } else {
115 boot_command = std::string(boot.command, sizeof(boot.command));
116 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700117 LOG(INFO) << "Boot command: " << boot_command;
118 }
119
120 if (boot.status[0] != 0) {
121 std::string boot_status = std::string(boot.status, sizeof(boot.status));
122 LOG(INFO) << "Boot status: " << boot_status;
123 }
124
125 std::vector<std::string> args(argv, argv + argc);
126
127 // --- if arguments weren't supplied, look in the bootloader control block
128 if (args.size() == 1) {
129 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
130 std::string boot_recovery(boot.recovery);
131 std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n");
132 if (!tokens.empty() && tokens[0] == "recovery") {
133 for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
134 // Skip empty and '\0'-filled tokens.
135 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
136 }
137 LOG(INFO) << "Got " << args.size() << " arguments from boot message";
138 } else if (boot.recovery[0] != 0) {
139 LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\"";
140 }
141 }
142
143 // --- if that doesn't work, try the command file (if we have /cache).
Tianjie Xu164c60a2019-05-15 13:59:39 -0700144 if (args.size() == 1 && HasCache()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700145 std::string content;
146 if (ensure_path_mounted(COMMAND_FILE) == 0 &&
147 android::base::ReadFileToString(COMMAND_FILE, &content)) {
148 std::vector<std::string> tokens = android::base::Split(content, "\n");
149 // All the arguments in COMMAND_FILE are needed (unlike the BCB message,
150 // COMMAND_FILE doesn't use filename as the first argument).
151 for (auto it = tokens.begin(); it != tokens.end(); it++) {
152 // Skip empty and '\0'-filled tokens.
153 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
154 }
155 LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
156 }
157 }
158
159 // Write the arguments (excluding the filename in args[0]) back into the
160 // bootloader control block. So the device will always boot into recovery to
Tianjie Xu164c60a2019-05-15 13:59:39 -0700161 // finish the pending work, until FinishRecovery() is called.
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700162 std::vector<std::string> options(args.cbegin() + 1, args.cend());
163 if (!update_bootloader_message(options, &err)) {
164 LOG(ERROR) << "Failed to set BCB message: " << err;
165 }
166
David Andersoneee4e262018-08-21 13:10:45 -0700167 // Finally, if no arguments were specified, check whether we should boot
Tao Baod9cb0142019-04-23 11:46:25 -0700168 // into fastboot or rescue mode.
David Andersoneee4e262018-08-21 13:10:45 -0700169 if (args.size() == 1 && boot_command == "boot-fastboot") {
170 args.emplace_back("--fastboot");
Tao Baod9cb0142019-04-23 11:46:25 -0700171 } else if (args.size() == 1 && boot_command == "boot-rescue") {
172 args.emplace_back("--rescue");
David Andersoneee4e262018-08-21 13:10:45 -0700173 }
174
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700175 return args;
176}
177
178static std::string load_locale_from_cache() {
179 if (ensure_path_mounted(LOCALE_FILE) != 0) {
180 LOG(ERROR) << "Can't mount " << LOCALE_FILE;
181 return "";
182 }
183
184 std::string content;
185 if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
186 PLOG(ERROR) << "Can't read " << LOCALE_FILE;
187 return "";
188 }
189
190 return android::base::Trim(content);
191}
192
Tao Baoe0cfab32019-03-29 15:53:23 -0700193// Sets the usb config to 'state'.
194static bool SetUsbConfig(const std::string& state) {
195 android::base::SetProperty("sys.usb.config", state);
196 return android::base::WaitForProperty("sys.usb.state", state);
197}
198
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700199static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) {
200 android::base::unique_fd sock_fd(android_get_control_socket("recovery"));
201 if (sock_fd < 0) {
202 PLOG(ERROR) << "Failed to open recovery socket";
203 return;
204 }
205 listen(sock_fd, 4);
206
207 while (true) {
208 android::base::unique_fd connection_fd;
209 connection_fd.reset(accept(sock_fd, nullptr, nullptr));
210 if (connection_fd < 0) {
211 PLOG(ERROR) << "Failed to accept socket connection";
212 continue;
213 }
214 char msg;
215 constexpr char kSwitchToFastboot = 'f';
216 constexpr char kSwitchToRecovery = 'r';
217 ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg)));
218 if (ret != sizeof(msg)) {
219 PLOG(ERROR) << "Couldn't read from socket";
220 continue;
221 }
222 switch (msg) {
223 case kSwitchToRecovery:
224 action = Device::BuiltinAction::ENTER_RECOVERY;
225 break;
226 case kSwitchToFastboot:
227 action = Device::BuiltinAction::ENTER_FASTBOOT;
228 break;
229 default:
230 LOG(ERROR) << "Unrecognized char from socket " << msg;
231 continue;
232 }
233 ui->InterruptKey();
234 }
235}
236
Tao Bao6d99d4b2018-04-25 16:47:04 -0700237static void redirect_stdio(const char* filename) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800238 android::base::unique_fd pipe_read, pipe_write;
239 // Create a pipe that allows parent process sending logs over.
240 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
241 PLOG(ERROR) << "Failed to create pipe for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700242
243 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
244 // anywhere to complain...
245 freopen(filename, "a", stdout);
246 setbuf(stdout, nullptr);
247 freopen(filename, "a", stderr);
248 setbuf(stderr, nullptr);
249
250 return;
251 }
252
253 pid_t pid = fork();
254 if (pid == -1) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800255 PLOG(ERROR) << "Failed to fork for redirecting stdio";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700256
257 // Fall back to traditional logging mode without timestamps. If these fail, there's not really
258 // anywhere to complain...
259 freopen(filename, "a", stdout);
260 setbuf(stdout, nullptr);
261 freopen(filename, "a", stderr);
262 setbuf(stderr, nullptr);
263
264 return;
265 }
266
267 if (pid == 0) {
Tao Bao6fcd2082019-01-16 09:29:17 -0800268 // Child process reads the incoming logs and doesn't write to the pipe.
269 pipe_write.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700270
271 auto start = std::chrono::steady_clock::now();
272
273 // Child logger to actually write to the log file.
274 FILE* log_fp = fopen(filename, "ae");
275 if (log_fp == nullptr) {
276 PLOG(ERROR) << "fopen \"" << filename << "\" failed";
Tao Bao6d99d4b2018-04-25 16:47:04 -0700277 _exit(EXIT_FAILURE);
278 }
279
Tao Bao6fcd2082019-01-16 09:29:17 -0800280 FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), "r");
Tao Bao6d99d4b2018-04-25 16:47:04 -0700281 if (pipe_fp == nullptr) {
282 PLOG(ERROR) << "fdopen failed";
283 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700284 _exit(EXIT_FAILURE);
285 }
286
287 char* line = nullptr;
288 size_t len = 0;
289 while (getline(&line, &len, pipe_fp) != -1) {
290 auto now = std::chrono::steady_clock::now();
291 double duration =
292 std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count();
293 if (line[0] == '\n') {
294 fprintf(log_fp, "[%12.6lf]\n", duration);
295 } else {
296 fprintf(log_fp, "[%12.6lf] %s", duration, line);
297 }
298 fflush(log_fp);
299 }
300
301 PLOG(ERROR) << "getline failed";
302
Tao Bao6fcd2082019-01-16 09:29:17 -0800303 fclose(pipe_fp);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700304 free(line);
305 check_and_fclose(log_fp, filename);
Tao Bao6d99d4b2018-04-25 16:47:04 -0700306 _exit(EXIT_FAILURE);
307 } else {
308 // Redirect stdout/stderr to the logger process. Close the unused read end.
Tao Bao6fcd2082019-01-16 09:29:17 -0800309 pipe_read.reset();
Tao Bao6d99d4b2018-04-25 16:47:04 -0700310
311 setbuf(stdout, nullptr);
312 setbuf(stderr, nullptr);
313
Tao Bao6fcd2082019-01-16 09:29:17 -0800314 if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700315 PLOG(ERROR) << "dup2 stdout failed";
316 }
Tao Bao6fcd2082019-01-16 09:29:17 -0800317 if (dup2(pipe_write.get(), STDERR_FILENO) == -1) {
Tao Bao6d99d4b2018-04-25 16:47:04 -0700318 PLOG(ERROR) << "dup2 stderr failed";
319 }
Tao Bao6d99d4b2018-04-25 16:47:04 -0700320 }
321}
322
323int main(int argc, char** argv) {
324 // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
325 // (which is redirected to recovery.log) as we used to do.
326 android::base::InitLogging(argv, &UiLogger);
327
328 // Take last pmsg contents and rewrite it to the current pmsg session.
329 static constexpr const char filter[] = "recovery/";
330 // Do we need to rotate?
331 bool do_rotate = false;
332
333 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
334 // Take action to refresh pmsg contents
335 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
336
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700337 time_t start = time(nullptr);
338
Tao Bao6d99d4b2018-04-25 16:47:04 -0700339 // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
340 // instances with different timestamps.
341 redirect_stdio(Paths::Get().temporary_log_file().c_str());
342
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700343 load_volume_table();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700344
Tianjie Xub63a2212019-07-29 14:21:49 -0700345 std::string stage;
346 std::vector<std::string> args = get_args(argc, argv, &stage);
Tao Bao1700cc42018-07-16 22:09:59 -0700347 auto args_to_parse = StringVectorToNullTerminatedArray(args);
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700348
349 static constexpr struct option OPTIONS[] = {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700350 { "fastboot", no_argument, nullptr, 0 },
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700351 { "locale", required_argument, nullptr, 0 },
Tianjie Xub63a2212019-07-29 14:21:49 -0700352 { "reason", required_argument, nullptr, 0 },
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700353 { "show_text", no_argument, nullptr, 't' },
354 { nullptr, 0, nullptr, 0 },
355 };
356
357 bool show_text = false;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700358 bool fastboot = false;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700359 std::string locale;
Tianjie Xub63a2212019-07-29 14:21:49 -0700360 std::string reason;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700361
Tao Baoff185862019-09-18 13:28:32 -0700362 // The code here is only interested in the options that signal the intent to start fastbootd or
363 // recovery. Unrecognized options are likely meant for recovery, which will be processed later in
364 // start_recovery(). Suppress the warnings for such -- even if some flags were indeed invalid, the
365 // code in start_recovery() will capture and report them.
366 opterr = 0;
367
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700368 int arg;
369 int option_index;
Tao Bao1700cc42018-07-16 22:09:59 -0700370 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700371 &option_index)) != -1) {
372 switch (arg) {
373 case 't':
374 show_text = true;
375 break;
376 case 0: {
377 std::string option = OPTIONS[option_index].name;
378 if (option == "locale") {
379 locale = optarg;
Tianjie Xub63a2212019-07-29 14:21:49 -0700380 } else if (option == "reason") {
381 reason = optarg;
Hridya Valsaraju7f41a2c2018-09-19 16:29:01 -0700382 } else if (option == "fastboot" &&
Yifan Hongd17174c2018-11-16 12:49:33 -0800383 android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700384 fastboot = true;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700385 }
386 break;
387 }
388 }
389 }
Jerry Zhang49fd5d22018-05-17 12:54:41 -0700390 optind = 1;
Tao Baoff185862019-09-18 13:28:32 -0700391 opterr = 1;
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700392
393 if (locale.empty()) {
Tianjie Xu164c60a2019-05-15 13:59:39 -0700394 if (HasCache()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700395 locale = load_locale_from_cache();
396 }
397
398 if (locale.empty()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700399 locale = DEFAULT_LOCALE;
400 }
401 }
402
Tao Bao42c45e22018-07-31 09:37:12 -0700403 static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so";
404 // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have
405 // handed out pointers to code or static [or thread-local] data and doesn't collect them all back
406 // in on dlclose).
407 void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW);
408
409 using MakeDeviceType = decltype(&make_device);
410 MakeDeviceType make_device_func = nullptr;
411 if (librecovery_ui_ext == nullptr) {
412 printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror());
413 } else {
414 reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device");
415 if (make_device_func == nullptr) {
416 printf("Failed to dlsym make_device: %s\n", dlerror());
417 }
418 }
419
420 Device* device;
421 if (make_device_func == nullptr) {
422 printf("Falling back to the default make_device() instead\n");
423 device = make_device();
424 } else {
425 printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt);
426 device = (*make_device_func)();
427 }
428
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700429 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
430 printf("Quiescent recovery mode.\n");
431 device->ResetUI(new StubRecoveryUI());
432 } else {
433 if (!device->GetUI()->Init(locale)) {
434 printf("Failed to initialize UI; using stub UI instead.\n");
435 device->ResetUI(new StubRecoveryUI());
436 }
437 }
Tianjie Xub63a2212019-07-29 14:21:49 -0700438
439 BootState boot_state(reason, stage); // recovery_main owns the state of boot.
440 device->SetBootState(&boot_state);
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700441 ui = device->GetUI();
442
Tianjie Xu164c60a2019-05-15 13:59:39 -0700443 if (!HasCache()) {
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700444 device->RemoveMenuItemForAction(Device::WIPE_CACHE);
445 }
446
Yifan Hongd17174c2018-11-16 12:49:33 -0800447 if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
Hridya Valsarajudaa301e2018-09-18 14:48:01 -0700448 device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT);
449 }
450
Michael Bestas93cd1272019-09-27 20:29:42 +0300451 if (get_build_type() != "eng") {
452 device->RemoveMenuItemForAction(Device::RUN_GRAPHICS_TEST);
453 device->RemoveMenuItemForAction(Device::RUN_LOCALE_TEST);
454 }
455
Tianjie Xu164c60a2019-05-15 13:59:39 -0700456 if (!IsRoDebuggable()) {
Tao Baoc6dc3252019-04-16 14:22:25 -0700457 device->RemoveMenuItemForAction(Device::ENTER_RESCUE);
458 }
459
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700460 ui->SetBackground(RecoveryUI::NONE);
461 if (show_text) ui->ShowText(true);
462
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700463 LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start);
464 LOG(INFO) << "locale is [" << locale << "]";
465
Tianjie Xu164c60a2019-05-15 13:59:39 -0700466 auto sehandle = selinux_android_file_context_handle();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700467 selinux_android_set_sehandle(sehandle);
468 if (!sehandle) {
469 ui->Print("Warning: No file_contexts\n");
470 }
471
xunchang2239b9e2019-04-15 15:24:24 -0700472 SetLoggingSehandle(sehandle);
xunchang316e9712019-04-12 16:22:15 -0700473
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700474 std::atomic<Device::BuiltinAction> action;
475 std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action));
476 listener_thread.detach();
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700477
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700478 while (true) {
Tianjie Xu7d5c3412019-10-29 21:44:39 -0700479 // We start adbd in recovery for the device with userdebug build or a unlocked bootloader.
480 std::string usb_config =
481 fastboot ? "fastboot" : IsRoDebuggable() || IsDeviceUnlocked() ? "adb" : "none";
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700482 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
Hongguang Chen04267272020-04-21 20:58:04 -0700483 if (fastboot) {
484 device->PreFastboot();
485 } else {
486 device->PreRecovery();
487 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700488 if (usb_config != usb_state) {
489 if (!SetUsbConfig("none")) {
490 LOG(ERROR) << "Failed to clear USB config";
491 }
492 if (!SetUsbConfig(usb_config)) {
493 LOG(ERROR) << "Failed to set USB config to " << usb_config;
494 }
495 }
496
David Anderson983e2d52019-01-02 11:35:38 -0800497 ui->SetEnableFastbootdLogo(fastboot);
498
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700499 auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args);
500
501 if (ret == Device::KEY_INTERRUPTED) {
502 ret = action.exchange(ret);
503 if (ret == Device::NO_ACTION) {
504 continue;
505 }
506 }
507 switch (ret) {
508 case Device::SHUTDOWN:
509 ui->Print("Shutting down...\n");
Mark Salyzyn488cc052019-05-20 10:36:16 -0700510 Shutdown("userrequested,recovery");
511 break;
512
513 case Device::SHUTDOWN_FROM_FASTBOOT:
514 ui->Print("Shutting down...\n");
515 Shutdown("userrequested,fastboot");
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700516 break;
517
518 case Device::REBOOT_BOOTLOADER:
519 ui->Print("Rebooting to bootloader...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700520 Reboot("bootloader");
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700521 break;
522
Tao Baod9cb0142019-04-23 11:46:25 -0700523 case Device::REBOOT_FASTBOOT:
524 ui->Print("Rebooting to recovery/fastboot...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700525 Reboot("fastboot");
Tao Bao10f441a2019-04-19 15:22:15 -0700526 break;
527
Tao Baod9cb0142019-04-23 11:46:25 -0700528 case Device::REBOOT_RECOVERY:
529 ui->Print("Rebooting to recovery...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700530 Reboot("recovery");
Tao Baod9cb0142019-04-23 11:46:25 -0700531 break;
532
533 case Device::REBOOT_RESCUE: {
Tao Bao782dcc12019-04-29 11:23:16 -0700534 // Not using `Reboot("rescue")`, as it requires matching support in kernel and/or
Tao Baod9cb0142019-04-23 11:46:25 -0700535 // bootloader.
536 bootloader_message boot = {};
537 strlcpy(boot.command, "boot-rescue", sizeof(boot.command));
538 std::string err;
539 if (!write_bootloader_message(boot, &err)) {
540 LOG(ERROR) << "Failed to write bootloader message: " << err;
541 // Stay under recovery on failure.
542 continue;
543 }
544 ui->Print("Rebooting to recovery/rescue...\n");
Tao Bao782dcc12019-04-29 11:23:16 -0700545 Reboot("recovery");
Tao Baod9cb0142019-04-23 11:46:25 -0700546 break;
547 }
548
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700549 case Device::ENTER_FASTBOOT:
Tao Baof90d9a12019-05-10 10:40:59 -0700550 if (android::fs_mgr::LogicalPartitionsMapped()) {
David Anderson2b2f4232018-10-29 18:48:56 -0700551 ui->Print("Partitions may be mounted - rebooting to enter fastboot.");
Tao Bao782dcc12019-04-29 11:23:16 -0700552 Reboot("fastboot");
David Anderson2b2f4232018-10-29 18:48:56 -0700553 } else {
554 LOG(INFO) << "Entering fastboot";
555 fastboot = true;
556 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700557 break;
558
559 case Device::ENTER_RECOVERY:
560 LOG(INFO) << "Entering recovery";
561 fastboot = false;
562 break;
563
Mark Salyzyn488cc052019-05-20 10:36:16 -0700564 case Device::REBOOT:
565 ui->Print("Rebooting...\n");
566 Reboot("userrequested,recovery");
567 break;
568
569 case Device::REBOOT_FROM_FASTBOOT:
570 ui->Print("Rebooting...\n");
571 Reboot("userrequested,fastboot");
572 break;
573
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700574 default:
575 ui->Print("Rebooting...\n");
Mark Salyzyn488cc052019-05-20 10:36:16 -0700576 Reboot("unknown" + std::to_string(ret));
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700577 break;
578 }
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700579 }
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700580
Jerry Zhangf5e319a2018-05-04 11:24:10 -0700581 // Should be unreachable.
582 return EXIT_SUCCESS;
Tao Bao6d99d4b2018-04-25 16:47:04 -0700583}