// Clear the recovery command and prepare to boot a (hopefully working) system, // copy our log file to cache as well (for the system to read). This function is // idempotent: call it as many times as you like. staticvoid FinishRecovery(RecoveryUI* ui) {
std::string locale = ui->GetLocale(); // Save the locale to cache, so if recovery is next started up without a '--locale' argument // (e.g., directly from the bootloader) it will use the last-known locale. if (!locale.empty() && HasCache()) {
LOG(INFO) << "Saving locale \"" << locale << "\""; if (ensure_path_mounted(LOCALE_FILE) != 0) {
LOG(ERROR) << "Failed to mount " << LOCALE_FILE;
} elseif (!android::base::WriteStringToFile(locale, LOCALE_FILE)) {
PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
}
}
copy_logs(save_current_log);
// Reset to normal system boot so recovery won't cycle indefinitely.
std::string err; if (!clear_bootloader_message(&err)) {
LOG(ERROR) << "Failed to clear BCB message: " << err;
}
// Remove the command file, so recovery won't repeat indefinitely. if (HasCache()) { if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) {
LOG(WARNING) << "Can't unlink " << COMMAND_FILE;
}
ensure_path_unmounted(CACHE_ROOT);
}
staticbool ask_to_wipe_data(Device* device) {
std::vector<std::string> headers{ "Wipe all user data?", " THIS CAN NOT BE UNDONE!" };
std::vector<std::string> items{ " Cancel", " Factory data reset" };
staticbool ask_to_cancel_ota(Device* device) { // clang-format off
std::vector<std::string> headers{ "Overwrite in-progress update?", "An update may already be in progress. If you proceed, " "the existing OS may not longer boot, and completing " "an update via ADB will be required."
};
std::vector<std::string> items{ "Cancel", "Continue",
}; // clang-format on
size_t chosen_item = device->GetUI()->ShowMenu(
headers, items, 0, true,
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); return (chosen_item == 1);
}
static InstallResult prompt_and_wipe_data(Device* device) { // Reset to normal system boot so recovery won't cycle indefinitely.
std::string err; if (!clear_bootloader_message(&err)) {
LOG(ERROR) << "Failed to clear BCB message: " << err;
} // Use a single string and let ScreenRecoveryUI handles the wrapping.
std::vector<std::string> wipe_data_menu_headers{ "Can't load Android system. Your data may be corrupt. " "If you continue to get this message, you may need to " "perform a factory data reset and erase all user data " "stored on this device.", "Reason: " + device->GetReason().value_or(""),
}; // clang-format off
std::vector<std::string> wipe_data_menu_items { "Try again", "Factory data reset",
}; // clang-format on for (;;) {
size_t chosen_item = device->GetUI()->ShowPromptWipeDataMenu(
wipe_data_menu_headers, wipe_data_menu_items,
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); // If ShowMenu() returned RecoveryUI::KeyError::INTERRUPTED, WaitKey() was interrupted. if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) { return INSTALL_KEY_INTERRUPTED;
} if (chosen_item != 1) { return INSTALL_SUCCESS; // Just reboot, no wipe; not a failure, user asked for it
}
if (ask_to_wipe_data(device)) {
CHECK(device->GetReason().has_value()); if (WipeData(device)) { return INSTALL_SUCCESS;
} else { return INSTALL_ERROR;
}
}
}
}
staticvoid choose_recovery_file(Device* device) {
std::vector<std::string> entries; if (HasCache()) { for (int i = 0; i < KEEP_LOG_COUNT; i++) { auto add_to_entries = [&](constchar* filename) {
std::string log_file(filename); if (i > 0) {
log_file += "." + std::to_string(i);
}
// Shows the recovery UI and waits for user input. Returns one of the device builtin actions, such // as REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default, which // is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery. static Device::BuiltinAction PromptAndWait(Device* device, InstallResult status) { auto ui = device->GetUI(); bool update_in_progress = (device->GetReason().value_or("") == "update_in_progress"); for (;;) {
FinishRecovery(ui); switch (status) { case INSTALL_SUCCESS: case INSTALL_NONE: case INSTALL_SKIPPED: case INSTALL_RETRY: case INSTALL_KEY_INTERRUPTED:
ui->SetBackground(RecoveryUI::NO_COMMAND); break;
case INSTALL_ERROR: case INSTALL_CORRUPT:
ui->SetBackground(RecoveryUI::ERROR); break;
case INSTALL_REBOOT: // All the reboots should have been handled prior to entering PromptAndWait() or immediately // after installing a package.
LOG(FATAL) << "Invalid status code of INSTALL_REBOOT"; break;
}
ui->SetProgressType(RecoveryUI::EMPTY);
std::vector<std::string> headers; if (update_in_progress) {
headers = { "WARNING: Previous installation has failed.", " Your device may fail to boot if you reboot or power off now." };
}
size_t chosen_item = ui->ShowMenu(
headers, device->GetMenuItems(), 0, false,
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2)); // Handle Interrupt key if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) { return Device::KEY_INTERRUPTED;
} // Device-specific code may take some action here. It may return one of the core actions // handled in the switch statement below.
Device::BuiltinAction chosen_action =
(chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT))
? Device::REBOOT
: device->InvokeMenuItem(chosen_item);
switch (chosen_action) { case Device::REBOOT_FROM_FASTBOOT: // Can not happen case Device::SHUTDOWN_FROM_FASTBOOT: // Can not happen case Device::NO_ACTION: break;
case Device::ENTER_FASTBOOT: case Device::ENTER_RECOVERY: case Device::REBOOT_BOOTLOADER: case Device::REBOOT_FASTBOOT: case Device::REBOOT_RECOVERY: case Device::REBOOT_RESCUE: return chosen_action;
case Device::REBOOT: case Device::SHUTDOWN: if (!ui->IsTextVisible()) { return chosen_action;
} // okay to reboot; no need to ask. if (!update_in_progress) { return chosen_action;
} // An update might have been failed. Ask if user really wants to reboot. if (AskToReboot(device, chosen_action)) { return chosen_action;
} break;
case Device::WIPE_DATA:
save_current_log = true; if (ui->IsTextVisible()) { if (ask_to_wipe_data(device)) {
WipeData(device);
}
} else {
WipeData(device); return Device::NO_ACTION;
} break;
case Device::WIPE_CACHE: {
save_current_log = true;
std::function<bool()> confirm_func = [&device]() { return yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!");
};
WipeCache(ui, ui->IsTextVisible() ? confirm_func : nullptr); if (!ui->IsTextVisible()) return Device::NO_ACTION; break;
}
case Device::APPLY_ADB_SIDELOAD: case Device::APPLY_SDCARD: case Device::ENTER_RESCUE: {
save_current_log = true;
if (!IsCancelUpdateSafe(device)) { if (!ask_to_cancel_ota(device)) { break;
}
}
bool adb = true;
Device::BuiltinAction reboot_action{}; if (chosen_action == Device::ENTER_RESCUE) { // Switch to graphics screen.
ui->ShowText(false);
status = ApplyFromAdb(device, true/* rescue_mode */, &reboot_action);
} elseif (chosen_action == Device::APPLY_ADB_SIDELOAD) {
status = ApplyFromAdb(device, false/* rescue_mode */, &reboot_action);
} else {
adb = false;
status = ApplyFromSdcard(device);
}
ui->Print("\nInstall from %s completed with status %d.\n", adb ? "ADB" : "SD card", status); if (status == INSTALL_REBOOT) { return reboot_action;
}
if (status == INSTALL_SUCCESS) {
update_in_progress = false; if (!ui->IsTextVisible()) { return Device::NO_ACTION; // reboot if logs aren't visible
}
} else {
ui->SetBackground(RecoveryUI::ERROR);
ui->Print("Installation aborted.\n");
copy_logs(save_current_log);
} break;
}
case Device::VIEW_RECOVERY_LOGS:
choose_recovery_file(device); break;
case Device::RUN_GRAPHICS_TEST:
run_graphics_test(ui); break;
case Device::RUN_LOCALE_TEST: {
ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui);
screen_ui->CheckBackgroundTextImages(); break;
}
case Device::MOUNT_SYSTEM: // For Virtual A/B, set up the snapshot devices (if exist). if (!CreateSnapshotPartitions()) {
ui->Print("Virtual A/B: snapshot partitions creation failed.\n"); break;
} if (ensure_path_mounted_at(android::fs_mgr::GetSystemRoot(), "/mnt/system") != -1) {
ui->Print("Mounted /system.\n");
} break;
case Device::KEY_INTERRUPTED: return Device::KEY_INTERRUPTED;
}
}
}
staticbool IsBatteryOk(int* required_battery_level) { // GmsCore enters recovery mode to install package when having enough battery percentage. // Normally, the threshold is 40% without charger and 20% with charger. So we check the battery // level against a slightly lower limit.
constexpr int BATTERY_OK_PERCENTAGE = 20;
constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
// Set the retry count to |retry_count| in BCB. staticvoid set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) {
std::vector<std::string> options; for (constauto& arg : args) { if (!android::base::StartsWith(arg, "--retry_count")) {
options.push_back(arg);
}
}
// Update the retry counter in BCB.
options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count));
std::string err; if (!update_bootloader_message(options, &err)) {
LOG(ERROR) << err;
}
}
staticbool bootreason_in_blocklist() {
std::string bootreason = android::base::GetProperty("ro.boot.bootreason", ""); if (!bootreason.empty()) { // More bootreasons can be found in "system/core/bootstat/bootstat.cpp". staticconst std::vector<std::string> kBootreasonBlocklist{ "kernel_panic", "Panic",
}; for (constauto& str : kBootreasonBlocklist) { if (android::base::EqualsIgnoreCase(str, bootreason)) returntrue;
}
} returnfalse;
}
printf("stage is [%s]\n", device->GetStage().value_or("").c_str());
printf("reason is [%s]\n", device->GetReason().value_or("").c_str());
auto ui = device->GetUI();
// Set background string to "installing security update" for security update, // otherwise set it to "installing system update".
ui->SetSystemUpdateText(security_update);
int st_cur = 0, st_max = 0; if (!device->GetStage().has_value() &&
sscanf(device->GetStage().value().c_str(), "%d/%d", &st_cur, &st_max) == 2) {
ui->SetStage(st_cur, st_max);
}
InstallResult status = INSTALL_SUCCESS; // next_action indicates the next target to reboot into upon finishing the install. It could be // overridden to a different reboot target per user request.
Device::BuiltinAction next_action = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
if (update_package != nullptr) { // It's not entirely true that we will modify the flash. But we want // to log the update attempt since update_package is non-NULL.
save_current_log = true;
if (int required_battery_level = 0; retry_count == 0 && !IsBatteryOk(&required_battery_level)) {
ui->Print("battery capacity is not enough for installing package: %d%% needed\n",
required_battery_level); // Log the error code to last_install when installation skips due to low battery.
log_failure_code(kLowBattery, update_package);
status = INSTALL_SKIPPED;
} elseif (retry_count == 0 && bootreason_in_blocklist()) { // Skip update-on-reboot when bootreason is kernel_panic or similar
ui->Print("bootreason is in the blocklist; skip OTA installation\n");
log_failure_code(kBootreasonInBlocklist, update_package);
status = INSTALL_SKIPPED;
} else { // It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later // identify the interrupted update due to unexpected reboots. if (retry_count == 0) {
set_retry_bootloader_message(retry_count + 1, args);
}
bool should_use_fuse = false; if (!SetupPackageMount(update_package, &should_use_fuse)) {
LOG(INFO) << "Failed to set up the package access, skipping installation";
status = INSTALL_ERROR;
} elseif (install_with_fuse || should_use_fuse) {
LOG(INFO) << "Installing package " << update_package << " with fuse";
status = InstallWithFuseFromPath(update_package, device);
} elseif (auto memory_package = Package::CreateMemoryPackage(
update_package,
std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
memory_package != nullptr) {
status = InstallPackage(memory_package.get(), update_package, should_wipe_cache,
retry_count, device);
} else { // We may fail to memory map the package on 32 bit builds for packages with 2GiB+ size. // In such cases, we will try to install the package with fuse. This is not the default // installation method because it introduces a layer of indirection from the kernel space.
LOG(WARNING) << "Failed to memory map package " << update_package
<< "; falling back to install with fuse";
status = InstallWithFuseFromPath(update_package, device);
} if (status != INSTALL_SUCCESS) {
ui->Print("Installation aborted.\n");
// When I/O error or bspatch/imgpatch error happens, reboot and retry installation // RETRY_LIMIT times before we abandon this OTA update. static constexpr int RETRY_LIMIT = 4; if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) {
copy_logs(save_current_log);
retry_count += 1;
set_retry_bootloader_message(retry_count, args); // Print retry count on screen.
ui->Print("Retry attempt %d\n", retry_count);
// Reboot back into recovery to retry the update.
Reboot("recovery");
} // If this is an eng or userdebug build, then automatically // turn the text display on if the script fails so the error // message is visible. if (IsRoDebuggable()) {
ui->ShowText(true);
}
}
}
} elseif (should_wipe_data) {
save_current_log = true;
CHECK(device->GetReason().has_value()); if (!WipeData(device, should_keep_memtag_mode, data_fstype)) {
status = INSTALL_ERROR;
}
} elseif (should_prompt_and_wipe_data) { // Trigger the logging to capture the cause, even if user chooses to not wipe data.
save_current_log = true;
ui->ShowText(true);
ui->SetBackground(RecoveryUI::ERROR);
status = prompt_and_wipe_data(device); if (status != INSTALL_KEY_INTERRUPTED) {
ui->ShowText(false);
}
} elseif (should_wipe_cache) {
save_current_log = true; if (!WipeCache(ui, nullptr, data_fstype)) {
status = INSTALL_ERROR;
}
} elseif (should_wipe_ab) { if (!WipeAbDevice(device, wipe_package_size)) {
status = INSTALL_ERROR;
}
} elseif (sideload) { // 'adb reboot sideload' acts the same as user presses key combinations to enter the sideload // mode. When 'sideload-auto-reboot' is used, text display will NOT be turned on by default. And // it will reboot after sideload finishes even if there are errors. This is to enable automated // testing.
save_current_log = true; if (!sideload_auto_reboot) {
ui->ShowText(true);
}
status = ApplyFromAdb(device, false/* rescue_mode */, &next_action);
ui->Print("\nInstall from ADB complete (status: %d).\n", status); if (sideload_auto_reboot) {
status = INSTALL_REBOOT;
ui->Print("Rebooting automatically.\n");
}
} elseif (rescue) {
save_current_log = true;
status = ApplyFromAdb(device, true/* rescue_mode */, &next_action);
ui->Print("\nInstall from ADB complete (status: %d).\n", status);
} elseif (!just_exit) { // If this is an eng or userdebug build, automatically turn on the text display if no command // is specified. Note that this should be called before setting the background to avoid // flickering the background image. if (IsRoDebuggable()) {
ui->ShowText(true);
}
status = INSTALL_NONE; // No command specified
ui->SetBackground(RecoveryUI::NO_COMMAND);
}
if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
ui->SetBackground(RecoveryUI::ERROR); if (!ui->IsTextVisible()) {
sleep(5);
}
}
// Determine the next action. // - If the state is INSTALL_REBOOT, device will reboot into the target as specified in // `next_action`. // - If the recovery menu is visible, prompt and wait for commands. // - If the state is INSTALL_NONE, wait for commands (e.g. in user build, one manually boots // into recovery to sideload a package or to wipe the device). // - In all other cases, reboot the device. Therefore, normal users will observe the device // rebooting a) immediately upon successful finish (INSTALL_SUCCESS); or b) an "error" screen // for 5s followed by an automatic reboot. if (status != INSTALL_REBOOT) { if (status == INSTALL_NONE || ui->IsTextVisible()) { auto temp = PromptAndWait(device, status); if (temp != Device::NO_ACTION) {
next_action = temp;
}
}
}
// Save logs and clean up before rebooting or shutting down.
FinishRecovery(ui);
return next_action;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.22 Sekunden
(vorverarbeitet am 2026-06-30)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.