| 1 | #include <stddef.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stdnoreturn.h> |
| 4 | #include <lib/term.h> |
| 5 | #include <lib/real.h> |
| 6 | #include <lib/misc.h> |
| 7 | #include <lib/libc.h> |
| 8 | #include <lib/part.h> |
| 9 | #include <lib/config.h> |
| 10 | #include <lib/trace.h> |
| 11 | #include <lib/bli.h> |
| 12 | #include <lib/tpm.h> |
| 13 | #include <sys/e820.h> |
| 14 | #include <sys/a20.h> |
| 15 | #include <sys/idt.h> |
| 16 | #include <sys/gdt.h> |
| 17 | #include <lib/print.h> |
| 18 | #include <fs/file.h> |
| 19 | #include <lib/elf.h> |
| 20 | #include <mm/pmm.h> |
| 21 | #include <menu.h> |
| 22 | #include <pxe/pxe.h> |
| 23 | #include <pxe/tftp.h> |
| 24 | #include <drivers/disk.h> |
| 25 | #include <sys/lapic.h> |
| 26 | #include <lib/getchar.h> |
| 27 | #include <sys/cpu.h> |
| 28 | |
| 29 | void stage3_common(void); |
| 30 | |
| 31 | #if defined (UEFI) |
| 32 | extern symbol __slide, __image_base, __image_end; |
| 33 | extern symbol _start; |
| 34 | |
| 35 | noreturn void uefi_entry(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { |
| 36 | gST = SystemTable; |
| 37 | gBS = SystemTable->BootServices; |
| 38 | gRT = SystemTable->RuntimeServices; |
| 39 | efi_image_handle = ImageHandle; |
| 40 | |
| 41 | calibrate_tsc(); |
| 42 | usec_at_bootloader_entry = rdtsc_usec(); |
| 43 | |
| 44 | EFI_STATUS status; |
| 45 | |
| 46 | const char *deferred_error = NULL; |
| 47 | |
| 48 | #if defined (__x86_64__) |
| 49 | if ((uintptr_t)__slide >= 0x100000000) { |
| 50 | size_t image_size = ALIGN_UP((uintptr_t)__image_end - (uintptr_t)__image_base, 4096, panic(false, "Alignment overflow")); |
| 51 | size_t image_size_pages = ALIGN_UP((size_t)image_size, 4096, panic(false, "Alignment overflow")) / 4096; |
| 52 | size_t new_base; |
| 53 | for (new_base = 0x1000; new_base + (size_t)image_size < 0x100000000; new_base += 0x1000) { |
| 54 | EFI_PHYSICAL_ADDRESS _new_base = (EFI_PHYSICAL_ADDRESS)new_base; |
| 55 | status = gBS->AllocatePages(AllocateAddress, EfiLoaderCode, image_size_pages, &_new_base); |
| 56 | if (status == 0) { |
| 57 | goto new_base_gotten; |
| 58 | } |
| 59 | } |
| 60 | deferred_error = "Limine does not support being loaded above 4GiB and no alternative loading spot found"; |
| 61 | goto defer_error; |
| 62 | new_base_gotten: |
| 63 | memcpy((void *)new_base, __slide, (size_t)image_size); |
| 64 | __attribute__((ms_abi)) |
| 65 | void (*new_entry_point)(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable); |
| 66 | new_entry_point = (void *)(new_base + ((uintptr_t)_start - (uintptr_t)__slide)); |
| 67 | new_entry_point(ImageHandle, SystemTable); |
| 68 | __builtin_unreachable(); |
| 69 | } |
| 70 | |
| 71 | defer_error: |
| 72 | #endif |
| 73 | |
| 74 | gST->ConOut->EnableCursor(gST->ConOut, false); |
| 75 | |
| 76 | init_memmap(); |
| 77 | |
| 78 | term_fallback(); |
| 79 | |
| 80 | status = gBS->SetWatchdogTimer(0, 0x10000, 0, NULL); |
| 81 | if (status) { |
| 82 | print("WARNING: Failed to disable watchdog timer!\n"); |
| 83 | } |
| 84 | |
| 85 | if (deferred_error != NULL) { |
| 86 | panic(false, "%s", deferred_error); |
| 87 | } |
| 88 | |
| 89 | #if defined (__x86_64__) || defined (__i386__) |
| 90 | init_gdt(); |
| 91 | #endif |
| 92 | |
| 93 | disk_create_index(); |
| 94 | |
| 95 | // Detect UEFI Secure Boot |
| 96 | { |
| 97 | EFI_GUID global_variable = EFI_GLOBAL_VARIABLE; |
| 98 | UINT8 secure_boot = 0; |
| 99 | UINTN sb_size = sizeof(secure_boot); |
| 100 | EFI_STATUS sb_status = gRT->GetVariable(L"SecureBoot", &global_variable, NULL, &sb_size, &secure_boot); |
| 101 | if (sb_status == EFI_SUCCESS && secure_boot == 1) { |
| 102 | UINT8 setup_mode = 0; |
| 103 | UINTN sm_size = sizeof(setup_mode); |
| 104 | EFI_STATUS sm_status = gRT->GetVariable(L"SetupMode", &global_variable, NULL, &sm_size, &setup_mode); |
| 105 | if (sm_status != EFI_SUCCESS || setup_mode == 0) { |
| 106 | secure_boot_active = true; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | tpm_init(); |
| 112 | |
| 113 | boot_volume = NULL; |
| 114 | |
| 115 | EFI_HANDLE current_handle = ImageHandle; |
| 116 | for (size_t j = 0; j < 25; j++) { |
| 117 | if (current_handle == NULL) { |
| 118 | could_not_match: |
| 119 | print("WARNING: Could not meaningfully match the boot device handle with a volume.\n"); |
| 120 | print(" Using the first volume containing a Limine configuration!\n"); |
| 121 | print("\n"); |
| 122 | print("THIS IS A BUG! Please report this issue upstream.\n"); |
| 123 | print("Press any key to continue...\n"); |
| 124 | for (;;) { |
| 125 | int ret = pit_sleep_and_quit_on_keypress(65535); |
| 126 | if (ret != 0) { |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | for (size_t i = 0; i < volume_index_i; i++) { |
| 132 | struct file_handle *f; |
| 133 | |
| 134 | bool old_cif = case_insensitive_fopen; |
| 135 | case_insensitive_fopen = true; |
| 136 | if ( |
| 137 | false |
| 138 | #if defined (UEFI) |
| 139 | || (f = fopen(volume_index[i], "/EFI/limine/limine.conf")) != NULL |
| 140 | || (f = fopen(volume_index[i], "/EFI/BOOT/limine.conf")) != NULL |
| 141 | #endif |
| 142 | || (f = fopen(volume_index[i], "/boot/limine/limine.conf")) != NULL |
| 143 | || (f = fopen(volume_index[i], "/boot/limine.conf")) != NULL |
| 144 | || (f = fopen(volume_index[i], "/limine/limine.conf")) != NULL |
| 145 | || (f = fopen(volume_index[i], "/limine.conf")) != NULL |
| 146 | ) { |
| 147 | goto opened; |
| 148 | } |
| 149 | |
| 150 | case_insensitive_fopen = old_cif; |
| 151 | continue; |
| 152 | |
| 153 | opened: |
| 154 | case_insensitive_fopen = old_cif; |
| 155 | |
| 156 | fclose(f); |
| 157 | |
| 158 | if (volume_index[i]->backing_dev != NULL) { |
| 159 | boot_volume = volume_index[i]->backing_dev; |
| 160 | } else { |
| 161 | boot_volume = volume_index[i]; |
| 162 | } |
| 163 | |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | if (boot_volume != NULL) { |
| 168 | stage3_common(); |
| 169 | } |
| 170 | |
| 171 | panic(false, "No volume contained a Limine configuration file"); |
| 172 | } |
| 173 | |
| 174 | EFI_GUID loaded_img_prot_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID; |
| 175 | EFI_LOADED_IMAGE_PROTOCOL *loaded_image = NULL; |
| 176 | |
| 177 | status = gBS->HandleProtocol(current_handle, &loaded_img_prot_guid, |
| 178 | (void **)&loaded_image); |
| 179 | |
| 180 | if (status) { |
| 181 | goto could_not_match; |
| 182 | } |
| 183 | |
| 184 | boot_volume = disk_volume_from_efi_handle(loaded_image->DeviceHandle); |
| 185 | |
| 186 | if (boot_volume != NULL) { |
| 187 | stage3_common(); |
| 188 | } |
| 189 | |
| 190 | current_handle = loaded_image->ParentHandle; |
| 191 | } |
| 192 | |
| 193 | goto could_not_match; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | noreturn void stage3_common(void) { |
| 198 | #if defined (__x86_64__) || defined (__i386__) |
| 199 | init_flush_irqs(); |
| 200 | init_io_apics(); |
| 201 | #endif |
| 202 | |
| 203 | #if defined (__riscv) |
| 204 | #if defined (UEFI) |
| 205 | RISCV_EFI_BOOT_PROTOCOL *rv_proto = get_riscv_boot_protocol(); |
| 206 | if (rv_proto == NULL || rv_proto->GetBootHartId(rv_proto, &bsp_hartid) != EFI_SUCCESS) { |
| 207 | panic(false, "failed to get BSP's hartid"); |
| 208 | } |
| 209 | #else |
| 210 | #error riscv: only UEFI is supported |
| 211 | #endif |
| 212 | #endif |
| 213 | |
| 214 | term_notready(); |
| 215 | |
| 216 | #if defined (UEFI) |
| 217 | init_bli(); |
| 218 | #endif |
| 219 | |
| 220 | menu(true); |
| 221 | } |