Home | History | Annotate | Line # | Download | only in efiboot
      1  1.13    manu /*	$NetBSD: efiboot.c,v 1.13 2023/04/20 00:42:24 manu Exp $	*/
      2   1.1  nonaka 
      3   1.1  nonaka /*-
      4   1.1  nonaka  * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
      5   1.1  nonaka  * All rights reserved.
      6   1.1  nonaka  *
      7   1.1  nonaka  * Redistribution and use in source and binary forms, with or without
      8   1.1  nonaka  * modification, are permitted provided that the following conditions
      9   1.1  nonaka  * are met:
     10   1.1  nonaka  * 1. Redistributions of source code must retain the above copyright
     11   1.1  nonaka  *    notice, this list of conditions and the following disclaimer.
     12   1.1  nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  nonaka  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  nonaka  *    documentation and/or other materials provided with the distribution.
     15   1.1  nonaka  *
     16   1.1  nonaka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17   1.1  nonaka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1  nonaka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1  nonaka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20   1.1  nonaka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1  nonaka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1  nonaka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1  nonaka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1  nonaka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  nonaka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  nonaka  * SUCH DAMAGE.
     27   1.1  nonaka  */
     28   1.1  nonaka 
     29   1.1  nonaka #include "efiboot.h"
     30   1.1  nonaka 
     31   1.1  nonaka #include "bootinfo.h"
     32   1.1  nonaka #include "devopen.h"
     33   1.1  nonaka 
     34   1.1  nonaka EFI_HANDLE IH;
     35   1.1  nonaka EFI_DEVICE_PATH *efi_bootdp;
     36   1.7  nonaka enum efi_boot_device_type efi_bootdp_type = BOOT_DEVICE_TYPE_HD;
     37   1.1  nonaka EFI_LOADED_IMAGE *efi_li;
     38   1.1  nonaka uintptr_t efi_main_sp;
     39  1.13    manu physaddr_t efi_loadaddr, efi_kernel_start, efi_load_start;
     40  1.13    manu physaddr_t efi_kernel_reloc = 0;
     41  1.13    manu enum efi_reloc_type efi_reloc_type = RELOC_DEFAULT;
     42   1.4  nonaka u_long efi_kernel_size;
     43   1.3  nonaka bool efi_cleanuped;
     44  1.11    manu struct btinfo_efimemmap *btinfo_efimemmap = NULL;
     45   1.1  nonaka 
     46   1.1  nonaka static EFI_PHYSICAL_ADDRESS heap_start = EFI_ALLOCATE_MAX_ADDRESS;
     47   1.1  nonaka static UINTN heap_size = 1 * 1024 * 1024;			/* 1MB */
     48   1.1  nonaka static struct btinfo_efi btinfo_efi;
     49   1.1  nonaka 
     50   1.1  nonaka static void efi_heap_init(void);
     51   1.1  nonaka 
     52   1.1  nonaka EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
     53   1.1  nonaka EFI_STATUS EFIAPI
     54   1.1  nonaka efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
     55   1.1  nonaka {
     56   1.1  nonaka 	EFI_STATUS status;
     57   1.1  nonaka 	EFI_DEVICE_PATH *dp0, *dp;
     58   1.1  nonaka 	extern char twiddle_toggle;
     59   1.1  nonaka 
     60   1.1  nonaka 	IH = imageHandle;
     61   1.1  nonaka 	InitializeLib(IH, systemTable);
     62   1.1  nonaka 
     63   1.1  nonaka 	efi_main_sp = (uintptr_t)&status;
     64   1.1  nonaka 	twiddle_toggle = 1;	/* no twiddling until we're ready */
     65   1.1  nonaka 
     66   1.1  nonaka 	cninit();
     67   1.1  nonaka 	efi_heap_init();
     68   1.1  nonaka 	efi_md_init();
     69   1.1  nonaka 
     70   1.1  nonaka 	status = uefi_call_wrapper(BS->HandleProtocol, 3, IH,
     71   1.1  nonaka 	    &LoadedImageProtocol, (void **)&efi_li);
     72   1.1  nonaka 	if (EFI_ERROR(status))
     73   1.6  nonaka 		panic("HandleProtocol(LoadedImageProtocol): %" PRIxMAX,
     74   1.6  nonaka 		    (uintmax_t)status);
     75   1.1  nonaka 	status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle,
     76   1.1  nonaka 	    &DevicePathProtocol, (void **)&dp0);
     77   1.1  nonaka 	if (EFI_ERROR(status))
     78   1.6  nonaka 		panic("HandleProtocol(DevicePathProtocol): %" PRIxMAX,
     79   1.6  nonaka 		    (uintmax_t)status);
     80   1.5  nonaka 	efi_bootdp = dp0;
     81   1.1  nonaka 	for (dp = dp0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
     82   1.5  nonaka 		if (DevicePathType(dp) == MEDIA_DEVICE_PATH &&
     83   1.5  nonaka 		    DevicePathSubType(dp) == MEDIA_CDROM_DP) {
     84   1.7  nonaka 			efi_bootdp_type = BOOT_DEVICE_TYPE_CD;
     85   1.7  nonaka 			break;
     86   1.7  nonaka 		}
     87   1.7  nonaka 		if (DevicePathType(dp) == MESSAGING_DEVICE_PATH &&
     88   1.7  nonaka 		    DevicePathSubType(dp) == MSG_MAC_ADDR_DP) {
     89   1.7  nonaka 			efi_bootdp_type = BOOT_DEVICE_TYPE_NET;
     90   1.1  nonaka 			break;
     91   1.1  nonaka 		}
     92   1.1  nonaka 	}
     93   1.1  nonaka 
     94  1.10  nonaka 	efi_memory_probe();
     95   1.1  nonaka 	efi_disk_probe();
     96   1.7  nonaka 	efi_pxe_probe();
     97   1.7  nonaka 	efi_net_probe();
     98   1.1  nonaka 
     99   1.1  nonaka 	status = uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0, 0, NULL);
    100   1.1  nonaka 	if (EFI_ERROR(status))
    101   1.6  nonaka 		panic("SetWatchdogTimer: %" PRIxMAX, (uintmax_t)status);
    102   1.1  nonaka 
    103   1.1  nonaka 	boot();
    104   1.1  nonaka 
    105   1.1  nonaka 	return EFI_SUCCESS;
    106   1.1  nonaka }
    107   1.1  nonaka 
    108   1.1  nonaka void
    109   1.1  nonaka efi_cleanup(void)
    110   1.1  nonaka {
    111   1.1  nonaka 	EFI_STATUS status;
    112   1.1  nonaka 	EFI_MEMORY_DESCRIPTOR *desc;
    113   1.1  nonaka 	UINTN NoEntries, MapKey, DescriptorSize;
    114   1.1  nonaka 	UINT32 DescriptorVersion;
    115   1.2  nonaka 	size_t allocsz;
    116   1.1  nonaka 
    117   1.1  nonaka 	memset(&btinfo_efi, 0, sizeof(btinfo_efi));
    118   1.1  nonaka 	btinfo_efi.systblpa = (intptr_t)ST;
    119   1.1  nonaka #ifdef	__i386__	/* bootia32.efi */
    120   1.1  nonaka 	btinfo_efi.flags |= BI_EFI_32BIT;
    121   1.1  nonaka #endif
    122   1.1  nonaka 	BI_ADD(&btinfo_efi, BTINFO_EFI, sizeof(btinfo_efi));
    123   1.1  nonaka 
    124   1.1  nonaka 	NoEntries = 0;
    125   1.2  nonaka 	desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
    126   1.2  nonaka 	    &DescriptorVersion, true);
    127   1.1  nonaka 	status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
    128   1.1  nonaka 	if (EFI_ERROR(status)) {
    129   1.1  nonaka 		FreePool(desc);
    130   1.2  nonaka 		desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
    131   1.2  nonaka 		    &DescriptorVersion, true);
    132   1.1  nonaka 		status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
    133   1.1  nonaka 		if (EFI_ERROR(status))
    134   1.6  nonaka 			panic("ExitBootServices failed");
    135   1.1  nonaka 	}
    136   1.3  nonaka 	efi_cleanuped = true;
    137   1.2  nonaka 
    138   1.9  nonaka 	efi_memory_compact_map(desc, &NoEntries, DescriptorSize);
    139   1.2  nonaka 	allocsz = sizeof(struct btinfo_efimemmap) - 1
    140   1.2  nonaka 	    + NoEntries * DescriptorSize;
    141  1.11    manu 	btinfo_efimemmap = alloc(allocsz);
    142  1.11    manu 	btinfo_efimemmap->num = NoEntries;
    143  1.11    manu 	btinfo_efimemmap->version = DescriptorVersion;
    144  1.11    manu 	btinfo_efimemmap->size = DescriptorSize;
    145  1.11    manu 	memcpy(btinfo_efimemmap->memmap, desc, NoEntries * DescriptorSize);
    146  1.11    manu 	BI_ADD(btinfo_efimemmap, BTINFO_EFIMEMMAP, allocsz);
    147   1.1  nonaka }
    148   1.1  nonaka 
    149   1.1  nonaka static void
    150   1.1  nonaka efi_heap_init(void)
    151   1.1  nonaka {
    152   1.1  nonaka 	EFI_STATUS status;
    153   1.1  nonaka 	u_int sz = EFI_SIZE_TO_PAGES(heap_size);
    154   1.1  nonaka 
    155   1.1  nonaka 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
    156   1.1  nonaka 	    EfiLoaderData, sz, &heap_start);
    157   1.1  nonaka 	if (EFI_ERROR(status))
    158   1.1  nonaka 		Panic(L"%a: AllocatePages() failed: %d page(s): %r",
    159   1.1  nonaka 		    __func__, sz, status);
    160   1.1  nonaka 	setheap((void *)(UINTN)heap_start,
    161   1.1  nonaka 	    (void *)(UINTN)(heap_start + heap_size));
    162   1.1  nonaka }
    163