| 1 | #include <stddef.h> |
| 2 | #include <stdbool.h> |
| 3 | #include <stdnoreturn.h> |
| 4 | #include <lib/print.h> |
| 5 | #include <lib/real.h> |
| 6 | #include <lib/trace.h> |
| 7 | #if defined (UEFI) |
| 8 | # include <efi.h> |
| 9 | #endif |
| 10 | #include <lib/misc.h> |
| 11 | #include <lib/getchar.h> |
| 12 | #include <lib/gterm.h> |
| 13 | #include <lib/term.h> |
| 14 | #include <mm/pmm.h> |
| 15 | #include <menu.h> |
| 16 | |
| 17 | noreturn void panic(bool allow_menu, const char *fmt, ...) { |
| 18 | va_list args; |
| 19 | |
| 20 | va_start(args, fmt); |
| 21 | |
| 22 | #if defined (UEFI) |
| 23 | // Don't return to the menu under measured boot: a partial boot may |
| 24 | // have already dirtied the TPM PCRs. |
| 25 | if (measured_boot) { |
| 26 | allow_menu = false; |
| 27 | } |
| 28 | #endif |
| 29 | |
| 30 | quiet = false; |
| 31 | |
| 32 | FOR_TERM(TERM->autoflush = true); |
| 33 | |
| 34 | if ( |
| 35 | #if defined (BIOS) |
| 36 | stage3_loaded == true && |
| 37 | #endif |
| 38 | term_backend == _NOT_READY) { |
| 39 | term_fallback(); |
| 40 | } |
| 41 | |
| 42 | if ( |
| 43 | #if defined (BIOS) |
| 44 | stage3_loaded == true && |
| 45 | #endif |
| 46 | term_backend != FALLBACK) { |
| 47 | print("\033[31mPANIC\033[37;1m\033[0m: "); |
| 48 | } else { |
| 49 | print("PANIC: "); |
| 50 | } |
| 51 | vprint(fmt, args); |
| 52 | |
| 53 | va_end(args); |
| 54 | |
| 55 | print("\n"); |
| 56 | print_stacktrace(NULL); |
| 57 | |
| 58 | if ( |
| 59 | #if defined (BIOS) |
| 60 | stage3_loaded == true && |
| 61 | #elif defined (UEFI) |
| 62 | efi_boot_services_exited == false && |
| 63 | #endif |
| 64 | allow_menu == true) { |
| 65 | print("Press a key to return to %s.", booting_from_editor ? "editor" : "menu"); |
| 66 | |
| 67 | getchar(); |
| 68 | |
| 69 | // This fixes a crash |
| 70 | term_notready(); |
| 71 | |
| 72 | menu(false); |
| 73 | /* |
| 74 | fb_clear(&fbinfo); |
| 75 |
|
| 76 | // release all uefi memory and return to firmware |
| 77 | pmm_release_uefi_mem(); |
| 78 | gBS->Exit(efi_image_handle, EFI_ABORTED, 0, NULL); |
| 79 | */ |
| 80 | } else { |
| 81 | #if defined (BIOS) |
| 82 | print("Press CTRL+ALT+DEL to reboot."); |
| 83 | rm_hcf(); |
| 84 | #elif defined (UEFI) |
| 85 | print("System halted."); |
| 86 | for (;;) { |
| 87 | #if defined (__x86_64__) || defined (__i386__) |
| 88 | asm ("hlt"); |
| 89 | #elif defined (__aarch64__) || defined (__riscv) |
| 90 | asm ("wfi"); |
| 91 | #elif defined (__loongarch64) |
| 92 | asm ("idle 0"); |
| 93 | #else |
| 94 | #error Unknown architecture |
| 95 | #endif |
| 96 | } |
| 97 | #endif |
| 98 | } |
| 99 | } |