2018-04-26 01:47:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-07-31 18:37:12 +02:00
|
|
|
#include <dlfcn.h>
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <stdarg.h>
|
2018-04-26 01:47:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <time.h>
|
2018-04-26 01:47:04 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
#include <atomic>
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <string>
|
2018-07-28 07:09:12 +02:00
|
|
|
#include <thread>
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <vector>
|
2018-04-26 01:47:04 +02:00
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <android-base/file.h>
|
2018-04-26 01:47:04 +02:00
|
|
|
#include <android-base/logging.h>
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/strings.h>
|
2018-07-28 07:09:12 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <bootloader_message/bootloader_message.h>
|
2018-07-28 07:09:12 +02:00
|
|
|
#include <cutils/sockets.h>
|
2019-05-10 19:40:59 +02:00
|
|
|
#include <fs_mgr/roots.h>
|
2018-04-26 01:47:04 +02:00
|
|
|
#include <private/android_logger.h> /* private pmsg functions */
|
2018-05-04 20:24:10 +02:00
|
|
|
#include <selinux/android.h>
|
|
|
|
#include <selinux/label.h>
|
|
|
|
#include <selinux/selinux.h>
|
2018-04-26 01:47:04 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
2018-07-28 07:09:12 +02:00
|
|
|
#include "fastboot/fastboot.h"
|
2019-04-13 01:22:15 +02:00
|
|
|
#include "install/wipe_data.h"
|
|
|
|
#include "otautil/logging.h"
|
2018-04-26 01:47:04 +02:00
|
|
|
#include "otautil/paths.h"
|
2019-03-23 00:08:52 +01:00
|
|
|
#include "otautil/roots.h"
|
2018-05-04 20:24:10 +02:00
|
|
|
#include "otautil/sysutil.h"
|
|
|
|
#include "recovery.h"
|
2018-08-20 22:40:47 +02:00
|
|
|
#include "recovery_ui/device.h"
|
|
|
|
#include "recovery_ui/stub_ui.h"
|
|
|
|
#include "recovery_ui/ui.h"
|
2018-04-26 01:47:04 +02:00
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
|
|
|
|
static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
|
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
static RecoveryUI* ui = nullptr;
|
2018-05-04 20:24:10 +02:00
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
static bool IsRoDebuggable() {
|
|
|
|
return android::base::GetBoolProperty("ro.debuggable", false);
|
|
|
|
}
|
2018-05-04 20:24:10 +02:00
|
|
|
|
2018-04-26 01:47:04 +02:00
|
|
|
static void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity,
|
|
|
|
const char* /* tag */, const char* /* file */, unsigned int /* line */,
|
|
|
|
const char* message) {
|
|
|
|
static constexpr char log_characters[] = "VDIWEF";
|
|
|
|
if (severity >= android::base::ERROR && ui != nullptr) {
|
|
|
|
ui->Print("E:%s\n", message);
|
|
|
|
} else {
|
|
|
|
fprintf(stdout, "%c:%s\n", log_characters[severity], message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
// command line args come from, in decreasing precedence:
|
|
|
|
// - the actual command line
|
|
|
|
// - the bootloader control block (one per line, after "recovery")
|
|
|
|
// - the contents of COMMAND_FILE (one per line)
|
|
|
|
static std::vector<std::string> get_args(const int argc, char** const argv) {
|
|
|
|
CHECK_GT(argc, 0);
|
|
|
|
|
|
|
|
bootloader_message boot = {};
|
|
|
|
std::string err;
|
|
|
|
if (!read_bootloader_message(&boot, &err)) {
|
|
|
|
LOG(ERROR) << err;
|
|
|
|
// If fails, leave a zeroed bootloader_message.
|
|
|
|
boot = {};
|
|
|
|
}
|
|
|
|
stage = std::string(boot.stage);
|
|
|
|
|
2018-08-21 22:10:45 +02:00
|
|
|
std::string boot_command;
|
2018-05-04 20:24:10 +02:00
|
|
|
if (boot.command[0] != 0) {
|
2018-08-21 22:10:45 +02:00
|
|
|
if (memchr(boot.command, '\0', sizeof(boot.command))) {
|
|
|
|
boot_command = std::string(boot.command);
|
|
|
|
} else {
|
|
|
|
boot_command = std::string(boot.command, sizeof(boot.command));
|
|
|
|
}
|
2018-05-04 20:24:10 +02:00
|
|
|
LOG(INFO) << "Boot command: " << boot_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boot.status[0] != 0) {
|
|
|
|
std::string boot_status = std::string(boot.status, sizeof(boot.status));
|
|
|
|
LOG(INFO) << "Boot status: " << boot_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
|
|
|
|
|
|
// --- if arguments weren't supplied, look in the bootloader control block
|
|
|
|
if (args.size() == 1) {
|
|
|
|
boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
|
|
|
|
std::string boot_recovery(boot.recovery);
|
|
|
|
std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n");
|
|
|
|
if (!tokens.empty() && tokens[0] == "recovery") {
|
|
|
|
for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
|
|
|
|
// Skip empty and '\0'-filled tokens.
|
|
|
|
if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Got " << args.size() << " arguments from boot message";
|
|
|
|
} else if (boot.recovery[0] != 0) {
|
|
|
|
LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- if that doesn't work, try the command file (if we have /cache).
|
2019-05-15 22:59:39 +02:00
|
|
|
if (args.size() == 1 && HasCache()) {
|
2018-05-04 20:24:10 +02:00
|
|
|
std::string content;
|
|
|
|
if (ensure_path_mounted(COMMAND_FILE) == 0 &&
|
|
|
|
android::base::ReadFileToString(COMMAND_FILE, &content)) {
|
|
|
|
std::vector<std::string> tokens = android::base::Split(content, "\n");
|
|
|
|
// All the arguments in COMMAND_FILE are needed (unlike the BCB message,
|
|
|
|
// COMMAND_FILE doesn't use filename as the first argument).
|
|
|
|
for (auto it = tokens.begin(); it != tokens.end(); it++) {
|
|
|
|
// Skip empty and '\0'-filled tokens.
|
|
|
|
if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the arguments (excluding the filename in args[0]) back into the
|
|
|
|
// bootloader control block. So the device will always boot into recovery to
|
2019-05-15 22:59:39 +02:00
|
|
|
// finish the pending work, until FinishRecovery() is called.
|
2018-05-04 20:24:10 +02:00
|
|
|
std::vector<std::string> options(args.cbegin() + 1, args.cend());
|
|
|
|
if (!update_bootloader_message(options, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to set BCB message: " << err;
|
|
|
|
}
|
|
|
|
|
2018-08-21 22:10:45 +02:00
|
|
|
// Finally, if no arguments were specified, check whether we should boot
|
2019-04-23 20:46:25 +02:00
|
|
|
// into fastboot or rescue mode.
|
2018-08-21 22:10:45 +02:00
|
|
|
if (args.size() == 1 && boot_command == "boot-fastboot") {
|
|
|
|
args.emplace_back("--fastboot");
|
2019-04-23 20:46:25 +02:00
|
|
|
} else if (args.size() == 1 && boot_command == "boot-rescue") {
|
|
|
|
args.emplace_back("--rescue");
|
2018-08-21 22:10:45 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string load_locale_from_cache() {
|
|
|
|
if (ensure_path_mounted(LOCALE_FILE) != 0) {
|
|
|
|
LOG(ERROR) << "Can't mount " << LOCALE_FILE;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string content;
|
|
|
|
if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
|
|
|
|
PLOG(ERROR) << "Can't read " << LOCALE_FILE;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return android::base::Trim(content);
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:53:23 +01:00
|
|
|
// Sets the usb config to 'state'.
|
|
|
|
static bool SetUsbConfig(const std::string& state) {
|
|
|
|
android::base::SetProperty("sys.usb.config", state);
|
|
|
|
return android::base::WaitForProperty("sys.usb.state", state);
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
static void ListenRecoverySocket(RecoveryUI* ui, std::atomic<Device::BuiltinAction>& action) {
|
|
|
|
android::base::unique_fd sock_fd(android_get_control_socket("recovery"));
|
|
|
|
if (sock_fd < 0) {
|
|
|
|
PLOG(ERROR) << "Failed to open recovery socket";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listen(sock_fd, 4);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
android::base::unique_fd connection_fd;
|
|
|
|
connection_fd.reset(accept(sock_fd, nullptr, nullptr));
|
|
|
|
if (connection_fd < 0) {
|
|
|
|
PLOG(ERROR) << "Failed to accept socket connection";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char msg;
|
|
|
|
constexpr char kSwitchToFastboot = 'f';
|
|
|
|
constexpr char kSwitchToRecovery = 'r';
|
|
|
|
ssize_t ret = TEMP_FAILURE_RETRY(read(connection_fd, &msg, sizeof(msg)));
|
|
|
|
if (ret != sizeof(msg)) {
|
|
|
|
PLOG(ERROR) << "Couldn't read from socket";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (msg) {
|
|
|
|
case kSwitchToRecovery:
|
|
|
|
action = Device::BuiltinAction::ENTER_RECOVERY;
|
|
|
|
break;
|
|
|
|
case kSwitchToFastboot:
|
|
|
|
action = Device::BuiltinAction::ENTER_FASTBOOT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Unrecognized char from socket " << msg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ui->InterruptKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 01:47:04 +02:00
|
|
|
static void redirect_stdio(const char* filename) {
|
2019-01-16 18:29:17 +01:00
|
|
|
android::base::unique_fd pipe_read, pipe_write;
|
|
|
|
// Create a pipe that allows parent process sending logs over.
|
|
|
|
if (!android::base::Pipe(&pipe_read, &pipe_write)) {
|
|
|
|
PLOG(ERROR) << "Failed to create pipe for redirecting stdio";
|
2018-04-26 01:47:04 +02:00
|
|
|
|
|
|
|
// Fall back to traditional logging mode without timestamps. If these fail, there's not really
|
|
|
|
// anywhere to complain...
|
|
|
|
freopen(filename, "a", stdout);
|
|
|
|
setbuf(stdout, nullptr);
|
|
|
|
freopen(filename, "a", stderr);
|
|
|
|
setbuf(stderr, nullptr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == -1) {
|
2019-01-16 18:29:17 +01:00
|
|
|
PLOG(ERROR) << "Failed to fork for redirecting stdio";
|
2018-04-26 01:47:04 +02:00
|
|
|
|
|
|
|
// Fall back to traditional logging mode without timestamps. If these fail, there's not really
|
|
|
|
// anywhere to complain...
|
|
|
|
freopen(filename, "a", stdout);
|
|
|
|
setbuf(stdout, nullptr);
|
|
|
|
freopen(filename, "a", stderr);
|
|
|
|
setbuf(stderr, nullptr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
2019-01-16 18:29:17 +01:00
|
|
|
// Child process reads the incoming logs and doesn't write to the pipe.
|
|
|
|
pipe_write.reset();
|
2018-04-26 01:47:04 +02:00
|
|
|
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
// Child logger to actually write to the log file.
|
|
|
|
FILE* log_fp = fopen(filename, "ae");
|
|
|
|
if (log_fp == nullptr) {
|
|
|
|
PLOG(ERROR) << "fopen \"" << filename << "\" failed";
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-01-16 18:29:17 +01:00
|
|
|
FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), "r");
|
2018-04-26 01:47:04 +02:00
|
|
|
if (pipe_fp == nullptr) {
|
|
|
|
PLOG(ERROR) << "fdopen failed";
|
|
|
|
check_and_fclose(log_fp, filename);
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* line = nullptr;
|
|
|
|
size_t len = 0;
|
|
|
|
while (getline(&line, &len, pipe_fp) != -1) {
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
double duration =
|
|
|
|
std::chrono::duration_cast<std::chrono::duration<double>>(now - start).count();
|
|
|
|
if (line[0] == '\n') {
|
|
|
|
fprintf(log_fp, "[%12.6lf]\n", duration);
|
|
|
|
} else {
|
|
|
|
fprintf(log_fp, "[%12.6lf] %s", duration, line);
|
|
|
|
}
|
|
|
|
fflush(log_fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
PLOG(ERROR) << "getline failed";
|
|
|
|
|
2019-01-16 18:29:17 +01:00
|
|
|
fclose(pipe_fp);
|
2018-04-26 01:47:04 +02:00
|
|
|
free(line);
|
|
|
|
check_and_fclose(log_fp, filename);
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
// Redirect stdout/stderr to the logger process. Close the unused read end.
|
2019-01-16 18:29:17 +01:00
|
|
|
pipe_read.reset();
|
2018-04-26 01:47:04 +02:00
|
|
|
|
|
|
|
setbuf(stdout, nullptr);
|
|
|
|
setbuf(stderr, nullptr);
|
|
|
|
|
2019-01-16 18:29:17 +01:00
|
|
|
if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
|
2018-04-26 01:47:04 +02:00
|
|
|
PLOG(ERROR) << "dup2 stdout failed";
|
|
|
|
}
|
2019-01-16 18:29:17 +01:00
|
|
|
if (dup2(pipe_write.get(), STDERR_FILENO) == -1) {
|
2018-04-26 01:47:04 +02:00
|
|
|
PLOG(ERROR) << "dup2 stderr failed";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
// We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
|
|
|
|
// (which is redirected to recovery.log) as we used to do.
|
|
|
|
android::base::InitLogging(argv, &UiLogger);
|
|
|
|
|
|
|
|
// Take last pmsg contents and rewrite it to the current pmsg session.
|
|
|
|
static constexpr const char filter[] = "recovery/";
|
|
|
|
// Do we need to rotate?
|
|
|
|
bool do_rotate = false;
|
|
|
|
|
|
|
|
__android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
|
|
|
|
// Take action to refresh pmsg contents
|
|
|
|
__android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
time_t start = time(nullptr);
|
|
|
|
|
2018-04-26 01:47:04 +02:00
|
|
|
// redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
|
|
|
|
// instances with different timestamps.
|
|
|
|
redirect_stdio(Paths::Get().temporary_log_file().c_str());
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
load_volume_table();
|
|
|
|
|
|
|
|
std::vector<std::string> args = get_args(argc, argv);
|
2018-07-17 07:09:59 +02:00
|
|
|
auto args_to_parse = StringVectorToNullTerminatedArray(args);
|
2018-05-04 20:24:10 +02:00
|
|
|
|
|
|
|
static constexpr struct option OPTIONS[] = {
|
2018-07-28 07:09:12 +02:00
|
|
|
{ "fastboot", no_argument, nullptr, 0 },
|
2018-05-04 20:24:10 +02:00
|
|
|
{ "locale", required_argument, nullptr, 0 },
|
|
|
|
{ "show_text", no_argument, nullptr, 't' },
|
|
|
|
{ nullptr, 0, nullptr, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
bool show_text = false;
|
2018-07-28 07:09:12 +02:00
|
|
|
bool fastboot = false;
|
2018-05-04 20:24:10 +02:00
|
|
|
std::string locale;
|
|
|
|
|
|
|
|
int arg;
|
|
|
|
int option_index;
|
2018-07-17 07:09:59 +02:00
|
|
|
while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
|
2018-05-04 20:24:10 +02:00
|
|
|
&option_index)) != -1) {
|
|
|
|
switch (arg) {
|
|
|
|
case 't':
|
|
|
|
show_text = true;
|
|
|
|
break;
|
|
|
|
case 0: {
|
|
|
|
std::string option = OPTIONS[option_index].name;
|
|
|
|
if (option == "locale") {
|
|
|
|
locale = optarg;
|
2018-09-20 01:29:01 +02:00
|
|
|
} else if (option == "fastboot" &&
|
2018-11-16 21:49:33 +01:00
|
|
|
android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
|
2018-07-28 07:09:12 +02:00
|
|
|
fastboot = true;
|
2018-05-04 20:24:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 21:54:41 +02:00
|
|
|
optind = 1;
|
2018-05-04 20:24:10 +02:00
|
|
|
|
|
|
|
if (locale.empty()) {
|
2019-05-15 22:59:39 +02:00
|
|
|
if (HasCache()) {
|
2018-05-04 20:24:10 +02:00
|
|
|
locale = load_locale_from_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (locale.empty()) {
|
|
|
|
locale = DEFAULT_LOCALE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 18:37:12 +02:00
|
|
|
static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so";
|
|
|
|
// Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have
|
|
|
|
// handed out pointers to code or static [or thread-local] data and doesn't collect them all back
|
|
|
|
// in on dlclose).
|
|
|
|
void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW);
|
|
|
|
|
|
|
|
using MakeDeviceType = decltype(&make_device);
|
|
|
|
MakeDeviceType make_device_func = nullptr;
|
|
|
|
if (librecovery_ui_ext == nullptr) {
|
|
|
|
printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror());
|
|
|
|
} else {
|
|
|
|
reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device");
|
|
|
|
if (make_device_func == nullptr) {
|
|
|
|
printf("Failed to dlsym make_device: %s\n", dlerror());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Device* device;
|
|
|
|
if (make_device_func == nullptr) {
|
|
|
|
printf("Falling back to the default make_device() instead\n");
|
|
|
|
device = make_device();
|
|
|
|
} else {
|
|
|
|
printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt);
|
|
|
|
device = (*make_device_func)();
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
|
|
|
|
printf("Quiescent recovery mode.\n");
|
|
|
|
device->ResetUI(new StubRecoveryUI());
|
|
|
|
} else {
|
|
|
|
if (!device->GetUI()->Init(locale)) {
|
|
|
|
printf("Failed to initialize UI; using stub UI instead.\n");
|
|
|
|
device->ResetUI(new StubRecoveryUI());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ui = device->GetUI();
|
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
if (!HasCache()) {
|
2018-05-04 20:24:10 +02:00
|
|
|
device->RemoveMenuItemForAction(Device::WIPE_CACHE);
|
|
|
|
}
|
|
|
|
|
2018-11-16 21:49:33 +01:00
|
|
|
if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
|
2018-09-18 23:48:01 +02:00
|
|
|
device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT);
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
if (!IsRoDebuggable()) {
|
2019-04-16 23:22:25 +02:00
|
|
|
device->RemoveMenuItemForAction(Device::ENTER_RESCUE);
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
ui->SetBackground(RecoveryUI::NONE);
|
|
|
|
if (show_text) ui->ShowText(true);
|
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start);
|
|
|
|
LOG(INFO) << "locale is [" << locale << "]";
|
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
auto sehandle = selinux_android_file_context_handle();
|
2018-05-04 20:24:10 +02:00
|
|
|
selinux_android_set_sehandle(sehandle);
|
|
|
|
if (!sehandle) {
|
|
|
|
ui->Print("Warning: No file_contexts\n");
|
|
|
|
}
|
|
|
|
|
2019-04-16 00:24:24 +02:00
|
|
|
SetLoggingSehandle(sehandle);
|
2019-04-13 01:22:15 +02:00
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
std::atomic<Device::BuiltinAction> action;
|
|
|
|
std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action));
|
|
|
|
listener_thread.detach();
|
|
|
|
|
|
|
|
while (true) {
|
2019-05-15 22:59:39 +02:00
|
|
|
std::string usb_config = fastboot ? "fastboot" : IsRoDebuggable() ? "adb" : "none";
|
2018-07-28 07:09:12 +02:00
|
|
|
std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
|
|
|
|
if (usb_config != usb_state) {
|
|
|
|
if (!SetUsbConfig("none")) {
|
|
|
|
LOG(ERROR) << "Failed to clear USB config";
|
|
|
|
}
|
|
|
|
if (!SetUsbConfig(usb_config)) {
|
|
|
|
LOG(ERROR) << "Failed to set USB config to " << usb_config;
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 20:24:10 +02:00
|
|
|
|
2019-01-02 20:35:38 +01:00
|
|
|
ui->SetEnableFastbootdLogo(fastboot);
|
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args);
|
2018-05-04 20:24:10 +02:00
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
if (ret == Device::KEY_INTERRUPTED) {
|
|
|
|
ret = action.exchange(ret);
|
|
|
|
if (ret == Device::NO_ACTION) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (ret) {
|
|
|
|
case Device::SHUTDOWN:
|
|
|
|
ui->Print("Shutting down...\n");
|
recovery: report compliant reboot reason (Part Deux)
shutdown and reboot should have a corresponding sub-reason.
Adding:
"reboot,userrequested,fastboot"
"reboot,userrequested,recovery"
"reboot,userrequested,recovery,ui"
"shutdown,userrequested,fastboot"
"shutdown,userrequested,recovery"
"reboot,unknown#" (Can't happen, debug)
Test: manual, multiple targets, enter recovery, be able to exit recovery
Bug: 133326470
Change-Id: Ibfcb2a23158e8e99922e8053edd815fb592150f2
2019-05-20 19:36:16 +02:00
|
|
|
Shutdown("userrequested,recovery");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Device::SHUTDOWN_FROM_FASTBOOT:
|
|
|
|
ui->Print("Shutting down...\n");
|
|
|
|
Shutdown("userrequested,fastboot");
|
2018-07-28 07:09:12 +02:00
|
|
|
break;
|
2018-05-04 20:24:10 +02:00
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
case Device::REBOOT_BOOTLOADER:
|
|
|
|
ui->Print("Rebooting to bootloader...\n");
|
2019-04-29 20:23:16 +02:00
|
|
|
Reboot("bootloader");
|
2018-07-28 07:09:12 +02:00
|
|
|
break;
|
|
|
|
|
2019-04-23 20:46:25 +02:00
|
|
|
case Device::REBOOT_FASTBOOT:
|
|
|
|
ui->Print("Rebooting to recovery/fastboot...\n");
|
2019-04-29 20:23:16 +02:00
|
|
|
Reboot("fastboot");
|
2019-04-20 00:22:15 +02:00
|
|
|
break;
|
|
|
|
|
2019-04-23 20:46:25 +02:00
|
|
|
case Device::REBOOT_RECOVERY:
|
|
|
|
ui->Print("Rebooting to recovery...\n");
|
2019-04-29 20:23:16 +02:00
|
|
|
Reboot("recovery");
|
2019-04-23 20:46:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Device::REBOOT_RESCUE: {
|
2019-04-29 20:23:16 +02:00
|
|
|
// Not using `Reboot("rescue")`, as it requires matching support in kernel and/or
|
2019-04-23 20:46:25 +02:00
|
|
|
// bootloader.
|
|
|
|
bootloader_message boot = {};
|
|
|
|
strlcpy(boot.command, "boot-rescue", sizeof(boot.command));
|
|
|
|
std::string err;
|
|
|
|
if (!write_bootloader_message(boot, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to write bootloader message: " << err;
|
|
|
|
// Stay under recovery on failure.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ui->Print("Rebooting to recovery/rescue...\n");
|
2019-04-29 20:23:16 +02:00
|
|
|
Reboot("recovery");
|
2019-04-23 20:46:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
case Device::ENTER_FASTBOOT:
|
2019-05-10 19:40:59 +02:00
|
|
|
if (android::fs_mgr::LogicalPartitionsMapped()) {
|
2018-10-30 02:48:56 +01:00
|
|
|
ui->Print("Partitions may be mounted - rebooting to enter fastboot.");
|
2019-04-29 20:23:16 +02:00
|
|
|
Reboot("fastboot");
|
2018-10-30 02:48:56 +01:00
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Entering fastboot";
|
|
|
|
fastboot = true;
|
|
|
|
}
|
2018-07-28 07:09:12 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Device::ENTER_RECOVERY:
|
|
|
|
LOG(INFO) << "Entering recovery";
|
|
|
|
fastboot = false;
|
|
|
|
break;
|
|
|
|
|
recovery: report compliant reboot reason (Part Deux)
shutdown and reboot should have a corresponding sub-reason.
Adding:
"reboot,userrequested,fastboot"
"reboot,userrequested,recovery"
"reboot,userrequested,recovery,ui"
"shutdown,userrequested,fastboot"
"shutdown,userrequested,recovery"
"reboot,unknown#" (Can't happen, debug)
Test: manual, multiple targets, enter recovery, be able to exit recovery
Bug: 133326470
Change-Id: Ibfcb2a23158e8e99922e8053edd815fb592150f2
2019-05-20 19:36:16 +02:00
|
|
|
case Device::REBOOT:
|
|
|
|
ui->Print("Rebooting...\n");
|
|
|
|
Reboot("userrequested,recovery");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Device::REBOOT_FROM_FASTBOOT:
|
|
|
|
ui->Print("Rebooting...\n");
|
|
|
|
Reboot("userrequested,fastboot");
|
|
|
|
break;
|
|
|
|
|
2018-07-28 07:09:12 +02:00
|
|
|
default:
|
|
|
|
ui->Print("Rebooting...\n");
|
recovery: report compliant reboot reason (Part Deux)
shutdown and reboot should have a corresponding sub-reason.
Adding:
"reboot,userrequested,fastboot"
"reboot,userrequested,recovery"
"reboot,userrequested,recovery,ui"
"shutdown,userrequested,fastboot"
"shutdown,userrequested,recovery"
"reboot,unknown#" (Can't happen, debug)
Test: manual, multiple targets, enter recovery, be able to exit recovery
Bug: 133326470
Change-Id: Ibfcb2a23158e8e99922e8053edd815fb592150f2
2019-05-20 19:36:16 +02:00
|
|
|
Reboot("unknown" + std::to_string(ret));
|
2018-07-28 07:09:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-05-04 20:24:10 +02:00
|
|
|
}
|
2018-07-28 07:09:12 +02:00
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
// Should be unreachable.
|
|
|
|
return EXIT_SUCCESS;
|
2018-04-26 01:47:04 +02:00
|
|
|
}
|