Home | History | Annotate | Line # | Download | only in efiboot
efiboot.c revision 1.2
      1 /*	$NetBSD: efiboot.c,v 1.2 2017/02/11 10:13:46 nonaka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
      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 
     31 #include "bootinfo.h"
     32 #include "devopen.h"
     33 
     34 EFI_HANDLE IH;
     35 EFI_DEVICE_PATH *efi_bootdp;
     36 EFI_LOADED_IMAGE *efi_li;
     37 uintptr_t efi_main_sp;
     38 
     39 static EFI_PHYSICAL_ADDRESS heap_start = EFI_ALLOCATE_MAX_ADDRESS;
     40 static UINTN heap_size = 1 * 1024 * 1024;			/* 1MB */
     41 static struct btinfo_efi btinfo_efi;
     42 
     43 static void efi_heap_init(void);
     44 
     45 EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
     46 EFI_STATUS EFIAPI
     47 efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
     48 {
     49 	EFI_STATUS status;
     50 	EFI_DEVICE_PATH *dp0, *dp;
     51 	extern char twiddle_toggle;
     52 
     53 	IH = imageHandle;
     54 	InitializeLib(IH, systemTable);
     55 
     56 	efi_main_sp = (uintptr_t)&status;
     57 	twiddle_toggle = 1;	/* no twiddling until we're ready */
     58 
     59 	cninit();
     60 	efi_heap_init();
     61 	efi_md_init();
     62 
     63 	status = uefi_call_wrapper(BS->HandleProtocol, 3, IH,
     64 	    &LoadedImageProtocol, (void **)&efi_li);
     65 	if (EFI_ERROR(status))
     66 		Panic(L"HandleProtocol(LoadedImageProtocol): %r", status);
     67 	status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle,
     68 	    &DevicePathProtocol, (void **)&dp0);
     69 	if (EFI_ERROR(status))
     70 		Panic(L"HandleProtocol(DevicePathProtocol): %r", status);
     71 	for (dp = dp0; !IsDevicePathEnd(dp); dp = NextDevicePathNode(dp)) {
     72 		if (DevicePathType(dp) == MEDIA_DEVICE_PATH)
     73 			continue;
     74 		if (DevicePathSubType(dp) == MEDIA_HARDDRIVE_DP) {
     75 			boot_biosdev = 0x80;
     76 			efi_bootdp = dp;
     77 			break;
     78 		}
     79 		break;
     80 	}
     81 
     82 	efi_disk_probe();
     83 
     84 	status = uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0, 0, NULL);
     85 	if (EFI_ERROR(status))
     86 		Panic(L"SetWatchdogTimer: %r", status);
     87 
     88 	boot();
     89 
     90 	return EFI_SUCCESS;
     91 }
     92 
     93 void
     94 efi_cleanup(void)
     95 {
     96 	EFI_STATUS status;
     97 	EFI_MEMORY_DESCRIPTOR *desc;
     98 	UINTN NoEntries, MapKey, DescriptorSize;
     99 	UINT32 DescriptorVersion;
    100 	struct btinfo_efimemmap *bim;
    101 	size_t allocsz;
    102 
    103 	clearit();
    104 
    105 	memset(&btinfo_efi, 0, sizeof(btinfo_efi));
    106 	btinfo_efi.systblpa = (intptr_t)ST;
    107 #ifdef	__i386__	/* bootia32.efi */
    108 	btinfo_efi.flags |= BI_EFI_32BIT;
    109 #endif
    110 	BI_ADD(&btinfo_efi, BTINFO_EFI, sizeof(btinfo_efi));
    111 
    112 	NoEntries = 0;
    113 	desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
    114 	    &DescriptorVersion, true);
    115 	status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
    116 	if (EFI_ERROR(status)) {
    117 		FreePool(desc);
    118 		desc = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
    119 		    &DescriptorVersion, true);
    120 		status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, MapKey);
    121 		if (EFI_ERROR(status))
    122 			Panic(L"ExitBootServices failed");
    123 	}
    124 
    125 	allocsz = sizeof(struct btinfo_efimemmap) - 1
    126 	    + NoEntries * DescriptorSize;
    127 	bim = alloc(allocsz);
    128 	bim->num = NoEntries;
    129 	bim->version = DescriptorVersion;
    130 	bim->size = DescriptorSize;
    131 	memcpy(bim->memmap, desc, NoEntries * DescriptorSize);
    132 	BI_ADD(bim, BTINFO_EFIMEMMAP, allocsz);
    133 }
    134 
    135 static void
    136 efi_heap_init(void)
    137 {
    138 	EFI_STATUS status;
    139 	u_int sz = EFI_SIZE_TO_PAGES(heap_size);
    140 
    141 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
    142 	    EfiLoaderData, sz, &heap_start);
    143 	if (EFI_ERROR(status))
    144 		Panic(L"%a: AllocatePages() failed: %d page(s): %r",
    145 		    __func__, sz, status);
    146 	setheap((void *)(UINTN)heap_start,
    147 	    (void *)(UINTN)(heap_start + heap_size));
    148 }
    149