2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
22 #include <gpxe/efi/efi.h>
23 #include <gpxe/efi/Protocol/LoadedImage.h>
24 #include <gpxe/uuid.h>
26 /** Image handle passed to entry point */
27 EFI_HANDLE efi_image_handle
;
29 /** Loaded image protocol for this image */
30 EFI_LOADED_IMAGE_PROTOCOL
*efi_loaded_image
;
32 /** System table passed to entry point */
33 EFI_SYSTEM_TABLE
*efi_systab
;
35 /** EFI loaded image protocol GUID */
36 static EFI_GUID efi_loaded_image_protocol_guid
37 = EFI_LOADED_IMAGE_PROTOCOL_GUID
;
40 * Look up EFI configuration table
42 * @v guid Configuration table GUID
43 * @ret table Configuration table, or NULL
45 static void * efi_find_table ( EFI_GUID
*guid
) {
48 for ( i
= 0 ; i
< efi_systab
->NumberOfTableEntries
; i
++ ) {
49 if ( memcmp ( &efi_systab
->ConfigurationTable
[i
].VendorGuid
,
50 guid
, sizeof ( *guid
) ) == 0 )
51 return efi_systab
->ConfigurationTable
[i
].VendorTable
;
58 * Initialise EFI environment
60 * @v image_handle Image handle
61 * @v systab System table
62 * @ret efirc EFI return status code
64 EFI_STATUS
efi_init ( EFI_HANDLE image_handle
,
65 EFI_SYSTEM_TABLE
*systab
) {
66 EFI_BOOT_SERVICES
*bs
;
67 struct efi_protocol
*prot
;
68 struct efi_config_table
*tab
;
72 /* Store image handle and system table pointer for future use */
73 efi_image_handle
= image_handle
;
78 return EFI_NOT_AVAILABLE_YET
;
79 if ( ! systab
->ConOut
)
80 return EFI_NOT_AVAILABLE_YET
;
81 if ( ! systab
->BootServices
) {
82 DBGC ( systab
, "EFI provided no BootServices entry point\n" );
83 return EFI_NOT_AVAILABLE_YET
;
85 if ( ! systab
->RuntimeServices
) {
86 DBGC ( systab
, "EFI provided no RuntimeServices entry "
88 return EFI_NOT_AVAILABLE_YET
;
90 DBGC ( systab
, "EFI handle %p systab %p\n", image_handle
, systab
);
92 bs
= systab
->BootServices
;
93 efirc
= bs
->OpenProtocol ( image_handle
,
94 &efi_loaded_image_protocol_guid
,
95 &loaded_image
, image_handle
, NULL
,
96 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
98 DBGC ( systab
, "Could not get loaded image protocol" );
102 efi_loaded_image
= loaded_image
;
103 DBG ( "Image base address = %p\n", efi_loaded_image
->ImageBase
);
105 /* Look up used protocols */
106 for_each_table_entry ( prot
, EFI_PROTOCOLS
) {
107 if ( ( efirc
= bs
->LocateProtocol ( &prot
->u
.guid
, NULL
,
108 prot
->protocol
) ) == 0 ) {
109 DBGC ( systab
, "EFI protocol %s is at %p\n",
110 uuid_ntoa ( &prot
->u
.uuid
), *(prot
->protocol
));
112 DBGC ( systab
, "EFI does not provide protocol %s\n",
113 uuid_ntoa ( &prot
->u
.uuid
) );
114 /* All protocols are required */
119 /* Look up used configuration tables */
120 for_each_table_entry ( tab
, EFI_CONFIG_TABLES
) {
121 if ( ( *(tab
->table
) = efi_find_table ( &tab
->u
.guid
) ) ) {
122 DBGC ( systab
, "EFI configuration table %s is at %p\n",
123 uuid_ntoa ( &tab
->u
.uuid
), *(tab
->table
) );
125 DBGC ( systab
, "EFI does not provide configuration "
126 "table %s\n", uuid_ntoa ( &tab
->u
.uuid
) );
128 return EFI_NOT_AVAILABLE_YET
;