Home | History | Annotate | Line # | Download | only in efiboot
efiboot.c revision 1.9
      1 /* $NetBSD: efiboot.c,v 1.9 2018/10/12 22:08:04 jmcneill 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 EFI_HANDLE IH;
     38 EFI_DEVICE_PATH *efi_bootdp;
     39 EFI_LOADED_IMAGE *efi_li;
     40 
     41 int howto = 0;
     42 
     43 static EFI_PHYSICAL_ADDRESS heap_start;
     44 static UINTN heap_size = 1 * 1024 * 1024;
     45 static EFI_EVENT delay_ev = 0;
     46 
     47 EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
     48 
     49 EFI_STATUS EFIAPI
     50 efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
     51 {
     52 	EFI_STATUS status;
     53 	u_int sz = EFI_SIZE_TO_PAGES(heap_size);
     54 
     55 	IH = imageHandle;
     56 
     57 	InitializeLib(imageHandle, systemTable);
     58 
     59 	(void)uefi_call_wrapper(ST->ConOut->Reset, 2, ST->ConOut, FALSE);
     60 	if (ST->ConOut->ClearScreen)
     61 		(void)uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
     62 
     63 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, sz, &heap_start);
     64 	if (EFI_ERROR(status))
     65 		return status;
     66 	setheap((void *)heap_start, (void *)(heap_start + heap_size));
     67 
     68 	status = uefi_call_wrapper(BS->HandleProtocol, 3, imageHandle, &LoadedImageProtocol, (void **)&efi_li);
     69 	if (EFI_ERROR(status))
     70 		return status;
     71 	status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle, &DevicePathProtocol, (void **)&efi_bootdp);
     72 	if (EFI_ERROR(status))
     73 		efi_bootdp = NULL;
     74 
     75 #ifdef EFIBOOT_DEBUG
     76 	Print(L"Loaded image      : 0x%lX\n", efi_li);
     77 	Print(L"FilePath          : 0x%lX\n", efi_li->FilePath);
     78 	Print(L"ImageBase         : 0x%lX\n", efi_li->ImageBase);
     79 	Print(L"ImageSize         : 0x%lX\n", efi_li->ImageSize);
     80 	Print(L"Image file        : %s\n", DevicePathToStr(efi_li->FilePath));
     81 #endif
     82 
     83 	efi_acpi_probe();
     84 	efi_fdt_probe();
     85 	efi_file_system_probe();
     86 	efi_block_probe();
     87 	efi_pxe_probe();
     88 	efi_net_probe();
     89 
     90 	boot();
     91 
     92 	return EFI_SUCCESS;
     93 }
     94 
     95 void
     96 efi_cleanup(void)
     97 {
     98 	EFI_STATUS status;
     99 	UINTN nentries, mapkey, descsize;
    100 	UINT32 descver;
    101 
    102 	LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
    103 
    104 	status = uefi_call_wrapper(BS->ExitBootServices, 2, IH, mapkey);
    105 	if (EFI_ERROR(status))
    106 		printf("WARNING: ExitBootServices failed\n");
    107 }
    108 
    109 void
    110 efi_exit(void)
    111 {
    112 	EFI_STATUS status;
    113 
    114 	status = uefi_call_wrapper(BS->Exit, 4, IH, EFI_ABORTED, 0, NULL);
    115 	if (EFI_ERROR(status))
    116 		printf("WARNING: Exit failed\n");
    117 }
    118 
    119 void
    120 efi_reboot(void)
    121 {
    122 	uefi_call_wrapper(RT->ResetSystem, 4, EfiResetCold, EFI_SUCCESS, 0, NULL);
    123 
    124 	printf("WARNING: Reset failed\n");
    125 }
    126 
    127 void
    128 efi_delay(int us)
    129 {
    130 	EFI_STATUS status;
    131 	UINTN val;
    132 
    133 	if (delay_ev == 0) {
    134 		status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER, TPL_APPLICATION, 0, 0, &delay_ev);
    135 		if (EFI_ERROR(status))
    136 			return;
    137 	}
    138 
    139 	uefi_call_wrapper(BS->SetTimer, 3, delay_ev, TimerRelative, us * 10);
    140 	uefi_call_wrapper(BS->WaitForEvent, 3, 1, &delay_ev, &val);
    141 }
    142 
    143 void
    144 efi_progress(const char *fmt, ...)
    145 {
    146 	va_list ap;
    147 
    148 	if ((howto & AB_SILENT) != 0)
    149 		return;
    150 
    151 	va_start(ap, fmt);
    152 	vprintf(fmt, ap);
    153 	va_end(ap);
    154 }
    155