Home | History | Annotate | Line # | Download | only in efiboot
efiboot.c revision 1.21
      1  1.21  jmcneill /* $NetBSD: efiboot.c,v 1.21 2021/09/28 11:37:45 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17   1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20   1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include "efiboot.h"
     30   1.1  jmcneill #include "efifile.h"
     31   1.4  jmcneill #include "efiblock.h"
     32   1.1  jmcneill #include "efifdt.h"
     33   1.9  jmcneill #include "efiacpi.h"
     34  1.18  riastrad #include "efirng.h"
     35   1.1  jmcneill 
     36   1.8  jmcneill #include <sys/reboot.h>
     37   1.8  jmcneill 
     38  1.10  jmcneill #include <libfdt.h>
     39  1.10  jmcneill 
     40   1.5  jmcneill EFI_HANDLE IH;
     41   1.1  jmcneill EFI_DEVICE_PATH *efi_bootdp;
     42   1.1  jmcneill EFI_LOADED_IMAGE *efi_li;
     43   1.1  jmcneill 
     44   1.8  jmcneill int howto = 0;
     45   1.8  jmcneill 
     46  1.14     skrll #ifdef _LP64
     47  1.14     skrll #define PRIxEFIPTR "lX"
     48  1.14     skrll #define PRIxEFISIZE "lX"
     49  1.14     skrll #else
     50  1.14     skrll #define PRIxEFIPTR "X"
     51  1.14     skrll #define PRIxEFISIZE "X"
     52  1.14     skrll #endif
     53  1.14     skrll 
     54   1.1  jmcneill static EFI_PHYSICAL_ADDRESS heap_start;
     55  1.10  jmcneill static UINTN heap_size = 8 * 1024 * 1024;
     56   1.1  jmcneill static EFI_EVENT delay_ev = 0;
     57   1.1  jmcneill 
     58   1.1  jmcneill EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
     59   1.1  jmcneill 
     60   1.1  jmcneill EFI_STATUS EFIAPI
     61   1.1  jmcneill efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
     62   1.1  jmcneill {
     63   1.1  jmcneill 	EFI_STATUS status;
     64   1.1  jmcneill 	u_int sz = EFI_SIZE_TO_PAGES(heap_size);
     65   1.1  jmcneill 
     66   1.5  jmcneill 	IH = imageHandle;
     67   1.1  jmcneill 
     68   1.1  jmcneill 	InitializeLib(imageHandle, systemTable);
     69   1.1  jmcneill 
     70  1.17  jmcneill 	uefi_call_wrapper(ST->ConOut->Reset, 2, ST->ConOut, TRUE);
     71  1.17  jmcneill 	uefi_call_wrapper(ST->ConOut->SetMode, 2, ST->ConOut, 0);
     72  1.17  jmcneill 	uefi_call_wrapper(ST->ConOut->EnableCursor, 2, ST->ConOut, TRUE);
     73   1.1  jmcneill 
     74   1.1  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, sz, &heap_start);
     75   1.1  jmcneill 	if (EFI_ERROR(status))
     76   1.1  jmcneill 		return status;
     77  1.13  jmcneill 	setheap((void *)(uintptr_t)heap_start, (void *)(uintptr_t)(heap_start + heap_size));
     78   1.1  jmcneill 
     79   1.1  jmcneill 	status = uefi_call_wrapper(BS->HandleProtocol, 3, imageHandle, &LoadedImageProtocol, (void **)&efi_li);
     80   1.1  jmcneill 	if (EFI_ERROR(status))
     81   1.1  jmcneill 		return status;
     82   1.1  jmcneill 	status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle, &DevicePathProtocol, (void **)&efi_bootdp);
     83   1.1  jmcneill 	if (EFI_ERROR(status))
     84   1.3  jmcneill 		efi_bootdp = NULL;
     85   1.1  jmcneill 
     86   1.1  jmcneill #ifdef EFIBOOT_DEBUG
     87  1.14     skrll 	Print(L"Loaded image      : 0x%" PRIxEFIPTR "\n", efi_li);
     88  1.14     skrll 	Print(L"FilePath          : 0x%" PRIxEFIPTR "\n", efi_li->FilePath);
     89  1.14     skrll 	Print(L"ImageBase         : 0x%" PRIxEFIPTR "\n", efi_li->ImageBase);
     90  1.14     skrll 	Print(L"ImageSize         : 0x%" PRIxEFISIZE "\n", efi_li->ImageSize);
     91   1.1  jmcneill 	Print(L"Image file        : %s\n", DevicePathToStr(efi_li->FilePath));
     92   1.1  jmcneill #endif
     93   1.1  jmcneill 
     94   1.9  jmcneill 	efi_acpi_probe();
     95   1.1  jmcneill 	efi_fdt_probe();
     96  1.12  jmcneill 	efi_pxe_probe();
     97  1.12  jmcneill 	efi_net_probe();
     98   1.1  jmcneill 	efi_file_system_probe();
     99   1.4  jmcneill 	efi_block_probe();
    100  1.18  riastrad 	efi_rng_probe();
    101  1.21  jmcneill 	efi_gop_probe();
    102   1.1  jmcneill 
    103   1.1  jmcneill 	boot();
    104   1.1  jmcneill 
    105   1.1  jmcneill 	return EFI_SUCCESS;
    106   1.1  jmcneill }
    107   1.1  jmcneill 
    108  1.10  jmcneill #ifdef EFIBOOT_RUNTIME_ADDRESS
    109  1.10  jmcneill static uint64_t
    110  1.10  jmcneill efi_runtime_alloc_va(uint64_t npages)
    111  1.10  jmcneill {
    112  1.10  jmcneill 	static uint64_t va = EFIBOOT_RUNTIME_ADDRESS;
    113  1.10  jmcneill 	static uint64_t sz = EFIBOOT_RUNTIME_SIZE;
    114  1.10  jmcneill 	uint64_t nva;
    115  1.10  jmcneill 
    116  1.10  jmcneill 	if (sz < (npages * EFI_PAGE_SIZE))
    117  1.10  jmcneill 		panic("efi_acpi_alloc_va: couldn't allocate %" PRIu64 " pages", npages);
    118  1.10  jmcneill 
    119  1.10  jmcneill 	nva = va;
    120  1.10  jmcneill 	va += (npages * EFI_PAGE_SIZE);
    121  1.10  jmcneill 	sz -= (npages * EFI_PAGE_SIZE);
    122  1.10  jmcneill 
    123  1.10  jmcneill 	return nva;
    124  1.10  jmcneill }
    125  1.10  jmcneill #endif
    126  1.10  jmcneill 
    127  1.10  jmcneill #ifdef EFIBOOT_RUNTIME_ADDRESS
    128  1.10  jmcneill static void
    129  1.10  jmcneill efi_set_virtual_address_map(EFI_MEMORY_DESCRIPTOR *memmap, UINTN nentries, UINTN mapkey, UINTN descsize, UINT32 descver)
    130  1.10  jmcneill {
    131  1.10  jmcneill 	EFI_MEMORY_DESCRIPTOR *md, *vmd, *vmemmap;
    132  1.10  jmcneill 	EFI_STATUS status;
    133  1.10  jmcneill 	int n, nrt;
    134  1.10  jmcneill 	void *fdt;
    135  1.10  jmcneill 
    136  1.10  jmcneill 	fdt = efi_fdt_data();
    137  1.10  jmcneill 
    138  1.10  jmcneill 	vmemmap = alloc(nentries * descsize);
    139  1.10  jmcneill 	if (vmemmap == NULL)
    140  1.10  jmcneill 		panic("FATAL: couldn't allocate virtual memory map");
    141  1.10  jmcneill 
    142  1.10  jmcneill 	for (n = 0, nrt = 0, vmd = vmemmap, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
    143  1.10  jmcneill 		if ((md->Attribute & EFI_MEMORY_RUNTIME) == 0)
    144  1.10  jmcneill 			continue;
    145  1.10  jmcneill 		md->VirtualStart = efi_runtime_alloc_va(md->NumberOfPages);
    146  1.10  jmcneill 
    147  1.10  jmcneill 		switch (md->Type) {
    148  1.10  jmcneill 		case EfiRuntimeServicesCode:
    149  1.10  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-code", md->PhysicalStart);
    150  1.10  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-code", md->VirtualStart);
    151  1.10  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-code", md->NumberOfPages * EFI_PAGE_SIZE);
    152  1.10  jmcneill 			break;
    153  1.10  jmcneill 		case EfiRuntimeServicesData:
    154  1.10  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-data", md->PhysicalStart);
    155  1.10  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-data", md->VirtualStart);
    156  1.10  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-data", md->NumberOfPages * EFI_PAGE_SIZE);
    157  1.10  jmcneill 			break;
    158  1.11  jmcneill 		case EfiMemoryMappedIO:
    159  1.11  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-mmio", md->PhysicalStart);
    160  1.11  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-mmio", md->VirtualStart);
    161  1.11  jmcneill 			fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,uefi-runtime-mmio", md->NumberOfPages * EFI_PAGE_SIZE);
    162  1.11  jmcneill 			break;
    163  1.10  jmcneill 		default:
    164  1.10  jmcneill 			break;
    165  1.10  jmcneill 		}
    166  1.10  jmcneill 
    167  1.10  jmcneill 		*vmd = *md;
    168  1.10  jmcneill 		vmd = NextMemoryDescriptor(vmd, descsize);
    169  1.10  jmcneill 		++nrt;
    170  1.10  jmcneill 	}
    171  1.10  jmcneill 
    172  1.10  jmcneill 	status = uefi_call_wrapper(RT->SetVirtualAddressMap, 4, nrt * descsize, descsize, descver, vmemmap);
    173  1.10  jmcneill 	if (EFI_ERROR(status)) {
    174  1.10  jmcneill 		printf("WARNING: SetVirtualAddressMap failed\n");
    175  1.10  jmcneill 		return;
    176  1.10  jmcneill 	}
    177  1.10  jmcneill }
    178  1.10  jmcneill #endif
    179  1.10  jmcneill 
    180   1.1  jmcneill void
    181   1.1  jmcneill efi_cleanup(void)
    182   1.1  jmcneill {
    183   1.1  jmcneill 	EFI_STATUS status;
    184  1.10  jmcneill 	EFI_MEMORY_DESCRIPTOR *memmap;
    185   1.1  jmcneill 	UINTN nentries, mapkey, descsize;
    186   1.1  jmcneill 	UINT32 descver;
    187   1.1  jmcneill 
    188  1.10  jmcneill 	memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    189   1.1  jmcneill 
    190   1.5  jmcneill 	status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, mapkey);
    191  1.10  jmcneill 	if (EFI_ERROR(status)) {
    192   1.1  jmcneill 		printf("WARNING: ExitBootServices failed\n");
    193  1.10  jmcneill 		return;
    194  1.10  jmcneill 	}
    195  1.10  jmcneill 
    196  1.10  jmcneill #ifdef EFIBOOT_RUNTIME_ADDRESS
    197  1.10  jmcneill 	efi_set_virtual_address_map(memmap, nentries, mapkey, descsize, descver);
    198  1.10  jmcneill #endif
    199   1.1  jmcneill }
    200   1.1  jmcneill 
    201   1.1  jmcneill void
    202   1.1  jmcneill efi_exit(void)
    203   1.1  jmcneill {
    204   1.1  jmcneill 	EFI_STATUS status;
    205   1.1  jmcneill 
    206   1.5  jmcneill 	status = uefi_call_wrapper(BS->Exit, 4, IH, EFI_ABORTED, 0, NULL);
    207   1.1  jmcneill 	if (EFI_ERROR(status))
    208   1.1  jmcneill 		printf("WARNING: Exit failed\n");
    209   1.1  jmcneill }
    210   1.1  jmcneill 
    211   1.1  jmcneill void
    212   1.6  jmcneill efi_reboot(void)
    213   1.6  jmcneill {
    214   1.6  jmcneill 	uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
    215   1.6  jmcneill 
    216   1.6  jmcneill 	printf("WARNING: Reset failed\n");
    217   1.6  jmcneill }
    218   1.6  jmcneill 
    219   1.6  jmcneill void
    220   1.1  jmcneill efi_delay(int us)
    221   1.1  jmcneill {
    222   1.1  jmcneill 	EFI_STATUS status;
    223   1.1  jmcneill 	UINTN val;
    224   1.1  jmcneill 
    225   1.1  jmcneill 	if (delay_ev == 0) {
    226   1.1  jmcneill 		status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER, TPL_APPLICATION, 0, 0, &delay_ev);
    227   1.1  jmcneill 		if (EFI_ERROR(status))
    228   1.1  jmcneill 			return;
    229   1.1  jmcneill 	}
    230   1.1  jmcneill 
    231   1.1  jmcneill 	uefi_call_wrapper(BS->SetTimer, 3, delay_ev, TimerRelative, us * 10);
    232   1.1  jmcneill 	uefi_call_wrapper(BS->WaitForEvent, 3, 1, &delay_ev, &val);
    233   1.1  jmcneill }
    234   1.8  jmcneill 
    235   1.8  jmcneill void
    236   1.8  jmcneill efi_progress(const char *fmt, ...)
    237   1.8  jmcneill {
    238   1.8  jmcneill 	va_list ap;
    239   1.8  jmcneill 
    240   1.8  jmcneill 	if ((howto & AB_SILENT) != 0)
    241   1.8  jmcneill 		return;
    242   1.8  jmcneill 
    243   1.8  jmcneill 	va_start(ap, fmt);
    244   1.8  jmcneill 	vprintf(fmt, ap);
    245   1.8  jmcneill 	va_end(ap);
    246   1.8  jmcneill }
    247