Home | History | Annotate | Line # | Download | only in efiboot
efiboot.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: efiboot.c,v 1.1 2018/08/24 02:01:06 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.1  jmcneill #include "efifdt.h"
     32  1.1  jmcneill 
     33  1.1  jmcneill EFI_HANDLE efi_ih;
     34  1.1  jmcneill EFI_DEVICE_PATH *efi_bootdp;
     35  1.1  jmcneill EFI_LOADED_IMAGE *efi_li;
     36  1.1  jmcneill 
     37  1.1  jmcneill static EFI_PHYSICAL_ADDRESS heap_start;
     38  1.1  jmcneill static UINTN heap_size = 1 * 1024 * 1024;
     39  1.1  jmcneill static EFI_EVENT delay_ev = 0;
     40  1.1  jmcneill 
     41  1.1  jmcneill EFI_STATUS EFIAPI efi_main(EFI_HANDLE, EFI_SYSTEM_TABLE *);
     42  1.1  jmcneill 
     43  1.1  jmcneill EFI_STATUS EFIAPI
     44  1.1  jmcneill efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE *systemTable)
     45  1.1  jmcneill {
     46  1.1  jmcneill 	EFI_STATUS status;
     47  1.1  jmcneill 	u_int sz = EFI_SIZE_TO_PAGES(heap_size);
     48  1.1  jmcneill 
     49  1.1  jmcneill 	efi_ih = imageHandle;
     50  1.1  jmcneill 
     51  1.1  jmcneill 	InitializeLib(imageHandle, systemTable);
     52  1.1  jmcneill 
     53  1.1  jmcneill 	status = uefi_call_wrapper(ST->ConOut->Reset, 2, ST->ConOut, FALSE);
     54  1.1  jmcneill 	if (EFI_ERROR(status))
     55  1.1  jmcneill 		return status;
     56  1.1  jmcneill 
     57  1.1  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData, sz, &heap_start);
     58  1.1  jmcneill 	if (EFI_ERROR(status))
     59  1.1  jmcneill 		return status;
     60  1.1  jmcneill 	setheap((void *)heap_start, (void *)(heap_start + heap_size));
     61  1.1  jmcneill 
     62  1.1  jmcneill 	status = uefi_call_wrapper(BS->HandleProtocol, 3, imageHandle, &LoadedImageProtocol, (void **)&efi_li);
     63  1.1  jmcneill 	if (EFI_ERROR(status))
     64  1.1  jmcneill 		return status;
     65  1.1  jmcneill 	status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_li->DeviceHandle, &DevicePathProtocol, (void **)&efi_bootdp);
     66  1.1  jmcneill 	if (EFI_ERROR(status))
     67  1.1  jmcneill 		return status;
     68  1.1  jmcneill 
     69  1.1  jmcneill #ifdef EFIBOOT_DEBUG
     70  1.1  jmcneill 	Print(L"Loaded image      : 0x%lX\n", efi_li);
     71  1.1  jmcneill 	Print(L"FilePath          : 0x%lX\n", efi_li->FilePath);
     72  1.1  jmcneill 	Print(L"ImageBase         : 0x%lX\n", efi_li->ImageBase);
     73  1.1  jmcneill 	Print(L"ImageSize         : 0x%lX\n", efi_li->ImageSize);
     74  1.1  jmcneill 	Print(L"Image file        : %s\n", DevicePathToStr(efi_li->FilePath));
     75  1.1  jmcneill #endif
     76  1.1  jmcneill 
     77  1.1  jmcneill 	efi_fdt_probe();
     78  1.1  jmcneill 	efi_file_system_probe();
     79  1.1  jmcneill 
     80  1.1  jmcneill 	boot();
     81  1.1  jmcneill 
     82  1.1  jmcneill 	return EFI_SUCCESS;
     83  1.1  jmcneill }
     84  1.1  jmcneill 
     85  1.1  jmcneill void
     86  1.1  jmcneill efi_cleanup(void)
     87  1.1  jmcneill {
     88  1.1  jmcneill 	EFI_STATUS status;
     89  1.1  jmcneill 	UINTN nentries, mapkey, descsize;
     90  1.1  jmcneill 	UINT32 descver;
     91  1.1  jmcneill 
     92  1.1  jmcneill 	LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
     93  1.1  jmcneill 
     94  1.1  jmcneill 	status = uefi_call_wrapper(BS->ExitBootServices, 2, efi_ih, mapkey);
     95  1.1  jmcneill 	if (EFI_ERROR(status))
     96  1.1  jmcneill 		printf("WARNING: ExitBootServices failed\n");
     97  1.1  jmcneill }
     98  1.1  jmcneill 
     99  1.1  jmcneill void
    100  1.1  jmcneill efi_exit(void)
    101  1.1  jmcneill {
    102  1.1  jmcneill 	EFI_STATUS status;
    103  1.1  jmcneill 
    104  1.1  jmcneill 	status = uefi_call_wrapper(BS->Exit, 4, efi_ih, EFI_ABORTED, 0, NULL);
    105  1.1  jmcneill 	if (EFI_ERROR(status))
    106  1.1  jmcneill 		printf("WARNING: Exit failed\n");
    107  1.1  jmcneill }
    108  1.1  jmcneill 
    109  1.1  jmcneill void
    110  1.1  jmcneill efi_delay(int us)
    111  1.1  jmcneill {
    112  1.1  jmcneill 	EFI_STATUS status;
    113  1.1  jmcneill 	UINTN val;
    114  1.1  jmcneill 
    115  1.1  jmcneill 	if (delay_ev == 0) {
    116  1.1  jmcneill 		status = uefi_call_wrapper(BS->CreateEvent, 5, EVT_TIMER, TPL_APPLICATION, 0, 0, &delay_ev);
    117  1.1  jmcneill 		if (EFI_ERROR(status))
    118  1.1  jmcneill 			return;
    119  1.1  jmcneill 	}
    120  1.1  jmcneill 
    121  1.1  jmcneill 	uefi_call_wrapper(BS->SetTimer, 3, delay_ev, TimerRelative, us * 10);
    122  1.1  jmcneill 	uefi_call_wrapper(BS->WaitForEvent, 3, 1, &delay_ev, &val);
    123  1.1  jmcneill }
    124