1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <string.h>
23 #include <errno.h>
24 #include <ipxe/init.h>
25 #include <ipxe/efi/efi.h>
26 #include <ipxe/efi/efi_driver.h>
27 #include <ipxe/efi/Protocol/LoadedImage.h>
28 
29 /** Image handle passed to entry point */
30 EFI_HANDLE efi_image_handle;
31 
32 /** Loaded image protocol for this image */
33 EFI_LOADED_IMAGE_PROTOCOL *efi_loaded_image;
34 
35 /** System table passed to entry point
36  *
37  * We construct the symbol name efi_systab via the PLATFORM macro.
38  * This ensures that the symbol is defined only in EFI builds, and so
39  * prevents EFI code from being incorrectly linked in to a non-EFI
40  * build.
41  */
42 EFI_SYSTEM_TABLE * _C2 ( PLATFORM, _systab );
43 
44 /** EFI shutdown is in progress */
45 int efi_shutdown_in_progress;
46 
47 /** Event used to signal shutdown */
48 static EFI_EVENT efi_shutdown_event;
49 
50 /* Forward declarations */
51 static EFI_STATUS EFIAPI efi_unload ( EFI_HANDLE image_handle );
52 
53 /**
54  * Shut down in preparation for booting an OS.
55  *
56  * This hook gets called at ExitBootServices time in order to make
57  * sure that everything is properly shut down before the OS takes
58  * over.
59  */
efi_shutdown_hook(EFI_EVENT event __unused,void * context __unused)60 static EFIAPI void efi_shutdown_hook ( EFI_EVENT event __unused,
61 				       void *context __unused ) {
62 
63 	/* Mark shutdown as being in progress, to indicate that large
64 	 * parts of the system (e.g. timers) are no longer functional.
65 	 */
66 	efi_shutdown_in_progress = 1;
67 
68 	/* Shut down iPXE */
69 	shutdown_boot();
70 }
71 
72 /**
73  * Look up EFI configuration table
74  *
75  * @v guid		Configuration table GUID
76  * @ret table		Configuration table, or NULL
77  */
efi_find_table(EFI_GUID * guid)78 static void * efi_find_table ( EFI_GUID *guid ) {
79 	unsigned int i;
80 
81 	for ( i = 0 ; i < efi_systab->NumberOfTableEntries ; i++ ) {
82 		if ( memcmp ( &efi_systab->ConfigurationTable[i].VendorGuid,
83 			      guid, sizeof ( *guid ) ) == 0 )
84 			return efi_systab->ConfigurationTable[i].VendorTable;
85 	}
86 
87 	return NULL;
88 }
89 
90 /**
91  * Initialise EFI environment
92  *
93  * @v image_handle	Image handle
94  * @v systab		System table
95  * @ret efirc		EFI return status code
96  */
efi_init(EFI_HANDLE image_handle,EFI_SYSTEM_TABLE * systab)97 EFI_STATUS efi_init ( EFI_HANDLE image_handle,
98 		      EFI_SYSTEM_TABLE *systab ) {
99 	EFI_BOOT_SERVICES *bs;
100 	struct efi_protocol *prot;
101 	struct efi_config_table *tab;
102 	void *loaded_image;
103 	EFI_STATUS efirc;
104 	int rc;
105 
106 	/* Store image handle and system table pointer for future use */
107 	efi_image_handle = image_handle;
108 	efi_systab = systab;
109 
110 	/* Sanity checks */
111 	if ( ! systab ) {
112 		efirc = EFI_NOT_AVAILABLE_YET;
113 		goto err_sanity;
114 	}
115 	if ( ! systab->ConOut ) {
116 		efirc = EFI_NOT_AVAILABLE_YET;
117 		goto err_sanity;
118 	}
119 	if ( ! systab->BootServices ) {
120 		DBGC ( systab, "EFI provided no BootServices entry point\n" );
121 		efirc = EFI_NOT_AVAILABLE_YET;
122 		goto err_sanity;
123 	}
124 	if ( ! systab->RuntimeServices ) {
125 		DBGC ( systab, "EFI provided no RuntimeServices entry "
126 		       "point\n" );
127 		efirc = EFI_NOT_AVAILABLE_YET;
128 		goto err_sanity;
129 	}
130 	DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
131 	bs = systab->BootServices;
132 
133 	/* Look up used protocols */
134 	for_each_table_entry ( prot, EFI_PROTOCOLS ) {
135 		if ( ( efirc = bs->LocateProtocol ( &prot->guid, NULL,
136 						    prot->protocol ) ) == 0 ) {
137 			DBGC ( systab, "EFI protocol %s is at %p\n",
138 			       efi_guid_ntoa ( &prot->guid ),
139 			       *(prot->protocol) );
140 		} else {
141 			DBGC ( systab, "EFI does not provide protocol %s\n",
142 			       efi_guid_ntoa ( &prot->guid ) );
143 			/* Fail if protocol is required */
144 			if ( prot->required )
145 				goto err_missing_protocol;
146 		}
147 	}
148 
149 	/* Look up used configuration tables */
150 	for_each_table_entry ( tab, EFI_CONFIG_TABLES ) {
151 		if ( ( *(tab->table) = efi_find_table ( &tab->guid ) ) ) {
152 			DBGC ( systab, "EFI configuration table %s is at %p\n",
153 			       efi_guid_ntoa ( &tab->guid ), *(tab->table) );
154 		} else {
155 			DBGC ( systab, "EFI does not provide configuration "
156 			       "table %s\n", efi_guid_ntoa ( &tab->guid ) );
157 			if ( tab->required ) {
158 				efirc = EFI_NOT_AVAILABLE_YET;
159 				goto err_missing_table;
160 			}
161 		}
162 	}
163 
164 	/* Get loaded image protocol */
165 	if ( ( efirc = bs->OpenProtocol ( image_handle,
166 				&efi_loaded_image_protocol_guid,
167 				&loaded_image, image_handle, NULL,
168 				EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
169 		rc = -EEFI ( efirc );
170 		DBGC ( systab, "EFI could not get loaded image protocol: %s",
171 		       strerror ( rc ) );
172 		goto err_no_loaded_image;
173 	}
174 	efi_loaded_image = loaded_image;
175 	DBGC ( systab, "EFI image base address %p\n",
176 	       efi_loaded_image->ImageBase );
177 
178 	/* EFI is perfectly capable of gracefully shutting down any
179 	 * loaded devices if it decides to fall back to a legacy boot.
180 	 * For no particularly comprehensible reason, it doesn't
181 	 * bother doing so when ExitBootServices() is called.
182 	 */
183 	if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
184 					 TPL_CALLBACK, efi_shutdown_hook,
185 					 NULL, &efi_shutdown_event ) ) != 0 ) {
186 		rc = -EEFI ( efirc );
187 		DBGC ( systab, "EFI could not create ExitBootServices event: "
188 		       "%s\n", strerror ( rc ) );
189 		goto err_create_event;
190 	}
191 
192 	/* Install driver binding protocol */
193 	if ( ( rc = efi_driver_install() ) != 0 ) {
194 		DBGC ( systab, "EFI could not install driver: %s\n",
195 		       strerror ( rc ) );
196 		efirc = EFIRC ( rc );
197 		goto err_driver_install;
198 	}
199 
200 	/* Install image unload method */
201 	efi_loaded_image->Unload = efi_unload;
202 
203 	return 0;
204 
205 	efi_driver_uninstall();
206  err_driver_install:
207 	bs->CloseEvent ( efi_shutdown_event );
208  err_create_event:
209  err_no_loaded_image:
210  err_missing_table:
211  err_missing_protocol:
212  err_sanity:
213 	return efirc;
214 }
215 
216 /**
217  * Shut down EFI environment
218  *
219  * @v image_handle	Image handle
220  */
efi_unload(EFI_HANDLE image_handle __unused)221 static EFI_STATUS EFIAPI efi_unload ( EFI_HANDLE image_handle __unused ) {
222 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
223 	EFI_SYSTEM_TABLE *systab = efi_systab;
224 
225 	DBGC ( systab, "EFI image unloading\n" );
226 
227 	/* Shut down */
228 	shutdown_exit();
229 
230 	/* Disconnect any remaining devices */
231 	efi_driver_disconnect_all();
232 
233 	/* Uninstall driver binding protocol */
234 	efi_driver_uninstall();
235 
236 	/* Uninstall exit boot services event */
237 	bs->CloseEvent ( efi_shutdown_event );
238 
239 	DBGC ( systab, "EFI image unloaded\n" );
240 
241 	return 0;
242 }
243