2008-10-12 02:55:55 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2008-10-12 02:55:55 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2008-12-04 05:04:54 +01:00
|
|
|
#include <string.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/efi/efi.h>
|
2010-05-28 05:08:28 +02:00
|
|
|
#include <ipxe/efi/Protocol/LoadedImage.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/uuid.h>
|
2011-03-29 22:35:06 +02:00
|
|
|
#include <ipxe/init.h>
|
2008-10-12 02:55:55 +02:00
|
|
|
|
|
|
|
/** Image handle passed to entry point */
|
|
|
|
EFI_HANDLE efi_image_handle;
|
|
|
|
|
2010-05-28 05:08:28 +02:00
|
|
|
/** Loaded image protocol for this image */
|
|
|
|
EFI_LOADED_IMAGE_PROTOCOL *efi_loaded_image;
|
|
|
|
|
2008-10-12 02:55:55 +02:00
|
|
|
/** System table passed to entry point */
|
|
|
|
EFI_SYSTEM_TABLE *efi_systab;
|
|
|
|
|
2010-05-28 05:08:28 +02:00
|
|
|
/** EFI loaded image protocol GUID */
|
|
|
|
static EFI_GUID efi_loaded_image_protocol_guid
|
|
|
|
= EFI_LOADED_IMAGE_PROTOCOL_GUID;
|
|
|
|
|
2011-03-29 22:35:06 +02:00
|
|
|
/** Event used to signal shutdown */
|
|
|
|
static EFI_EVENT efi_shutdown_event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shut down in preparation for booting an OS.
|
|
|
|
*
|
|
|
|
* This hook gets called at ExitBootServices time in order to make
|
|
|
|
* sure that everything is properly shut down before the OS takes
|
|
|
|
* over.
|
|
|
|
*/
|
|
|
|
static EFIAPI void efi_shutdown_hook ( EFI_EVENT event __unused,
|
|
|
|
void *context __unused ) {
|
|
|
|
shutdown_boot();
|
|
|
|
}
|
|
|
|
|
2008-12-04 05:04:54 +01:00
|
|
|
/**
|
|
|
|
* Look up EFI configuration table
|
|
|
|
*
|
|
|
|
* @v guid Configuration table GUID
|
|
|
|
* @ret table Configuration table, or NULL
|
|
|
|
*/
|
|
|
|
static void * efi_find_table ( EFI_GUID *guid ) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < efi_systab->NumberOfTableEntries ; i++ ) {
|
|
|
|
if ( memcmp ( &efi_systab->ConfigurationTable[i].VendorGuid,
|
|
|
|
guid, sizeof ( *guid ) ) == 0 )
|
|
|
|
return efi_systab->ConfigurationTable[i].VendorTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:55:55 +02:00
|
|
|
/**
|
2009-01-07 03:05:51 +01:00
|
|
|
* Initialise EFI environment
|
2008-10-12 02:55:55 +02:00
|
|
|
*
|
|
|
|
* @v image_handle Image handle
|
|
|
|
* @v systab System table
|
|
|
|
* @ret efirc EFI return status code
|
|
|
|
*/
|
2009-01-07 03:05:51 +01:00
|
|
|
EFI_STATUS efi_init ( EFI_HANDLE image_handle,
|
|
|
|
EFI_SYSTEM_TABLE *systab ) {
|
2008-10-12 02:55:55 +02:00
|
|
|
EFI_BOOT_SERVICES *bs;
|
|
|
|
struct efi_protocol *prot;
|
2008-12-04 05:04:54 +01:00
|
|
|
struct efi_config_table *tab;
|
2008-10-12 02:55:55 +02:00
|
|
|
EFI_STATUS efirc;
|
2010-05-28 05:08:28 +02:00
|
|
|
void *loaded_image;
|
2008-10-12 02:55:55 +02:00
|
|
|
|
|
|
|
/* Store image handle and system table pointer for future use */
|
|
|
|
efi_image_handle = image_handle;
|
|
|
|
efi_systab = systab;
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
if ( ! systab )
|
|
|
|
return EFI_NOT_AVAILABLE_YET;
|
|
|
|
if ( ! systab->ConOut )
|
|
|
|
return EFI_NOT_AVAILABLE_YET;
|
|
|
|
if ( ! systab->BootServices ) {
|
|
|
|
DBGC ( systab, "EFI provided no BootServices entry point\n" );
|
|
|
|
return EFI_NOT_AVAILABLE_YET;
|
|
|
|
}
|
|
|
|
if ( ! systab->RuntimeServices ) {
|
|
|
|
DBGC ( systab, "EFI provided no RuntimeServices entry "
|
|
|
|
"point\n" );
|
|
|
|
return EFI_NOT_AVAILABLE_YET;
|
|
|
|
}
|
|
|
|
DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
|
|
|
|
|
|
|
|
bs = systab->BootServices;
|
2010-05-28 05:08:28 +02:00
|
|
|
efirc = bs->OpenProtocol ( image_handle,
|
|
|
|
&efi_loaded_image_protocol_guid,
|
|
|
|
&loaded_image, image_handle, NULL,
|
|
|
|
EFI_OPEN_PROTOCOL_GET_PROTOCOL );
|
|
|
|
if ( efirc ) {
|
|
|
|
DBGC ( systab, "Could not get loaded image protocol" );
|
|
|
|
return efirc;
|
|
|
|
}
|
|
|
|
|
|
|
|
efi_loaded_image = loaded_image;
|
|
|
|
DBG ( "Image base address = %p\n", efi_loaded_image->ImageBase );
|
|
|
|
|
|
|
|
/* Look up used protocols */
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( prot, EFI_PROTOCOLS ) {
|
2013-03-20 16:25:16 +01:00
|
|
|
if ( ( efirc = bs->LocateProtocol ( &prot->guid, NULL,
|
2008-12-04 05:04:54 +01:00
|
|
|
prot->protocol ) ) == 0 ) {
|
|
|
|
DBGC ( systab, "EFI protocol %s is at %p\n",
|
2013-03-20 16:25:16 +01:00
|
|
|
efi_guid_ntoa ( &prot->guid ),
|
|
|
|
*(prot->protocol) );
|
2008-12-04 05:04:54 +01:00
|
|
|
} else {
|
2008-10-12 02:55:55 +02:00
|
|
|
DBGC ( systab, "EFI does not provide protocol %s\n",
|
2013-03-20 16:25:16 +01:00
|
|
|
efi_guid_ntoa ( &prot->guid ) );
|
2008-12-04 05:04:54 +01:00
|
|
|
/* All protocols are required */
|
2008-10-12 02:55:55 +02:00
|
|
|
return efirc;
|
|
|
|
}
|
2008-12-04 05:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up used configuration tables */
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( tab, EFI_CONFIG_TABLES ) {
|
2013-03-20 16:25:16 +01:00
|
|
|
if ( ( *(tab->table) = efi_find_table ( &tab->guid ) ) ) {
|
2008-12-04 05:04:54 +01:00
|
|
|
DBGC ( systab, "EFI configuration table %s is at %p\n",
|
2013-03-20 16:25:16 +01:00
|
|
|
efi_guid_ntoa ( &tab->guid ), *(tab->table) );
|
2008-12-04 05:04:54 +01:00
|
|
|
} else {
|
|
|
|
DBGC ( systab, "EFI does not provide configuration "
|
2013-03-20 16:25:16 +01:00
|
|
|
"table %s\n", efi_guid_ntoa ( &tab->guid ) );
|
2008-12-04 05:04:54 +01:00
|
|
|
if ( tab->required )
|
|
|
|
return EFI_NOT_AVAILABLE_YET;
|
|
|
|
}
|
2008-10-12 02:55:55 +02:00
|
|
|
}
|
|
|
|
|
2011-03-29 22:35:06 +02:00
|
|
|
/* EFI is perfectly capable of gracefully shutting down any
|
|
|
|
* loaded devices if it decides to fall back to a legacy boot.
|
|
|
|
* For no particularly comprehensible reason, it doesn't
|
|
|
|
* bother doing so when ExitBootServices() is called.
|
|
|
|
*/
|
|
|
|
if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
|
|
|
|
TPL_CALLBACK, efi_shutdown_hook,
|
|
|
|
NULL, &efi_shutdown_event ) ) != 0 ) {
|
|
|
|
DBGC ( systab, "EFI could not create ExitBootServices event: "
|
|
|
|
"%s\n", efi_strerror ( efirc ) );
|
|
|
|
return efirc;
|
|
|
|
}
|
|
|
|
|
2009-01-07 03:05:51 +01:00
|
|
|
return 0;
|
2008-10-12 02:55:55 +02:00
|
|
|
}
|