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