1 /*
2  * Copyright (C) 2014 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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * EFI image wrapping
30  *
31  */
32 
33 #include <string.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <ipxe/efi/efi.h>
37 #include <ipxe/efi/Protocol/LoadedImage.h>
38 #include <ipxe/efi/efi_wrap.h>
39 
40 /** EFI system table wrapper */
41 static EFI_SYSTEM_TABLE efi_systab_wrapper;
42 
43 /** EFI boot services table wrapper */
44 static EFI_BOOT_SERVICES efi_bs_wrapper;
45 
46 /** Colour for debug messages */
47 #define colour &efi_systab_wrapper
48 
49 /**
50  * Convert EFI status code to text
51  *
52  * @v efirc		EFI status code
53  * @ret text		EFI status code text
54  */
efi_status(EFI_STATUS efirc)55 static const char * efi_status ( EFI_STATUS efirc ) {
56 	static char buf[ 19 /* "0xXXXXXXXXXXXXXXXX" + NUL */ ];
57 
58 	switch ( efirc ) {
59 	case EFI_SUCCESS :			return "0";
60 	case EFI_LOAD_ERROR :			return "LOAD_ERROR";
61 	case EFI_INVALID_PARAMETER :		return "INVALID_PARAMETER";
62 	case EFI_UNSUPPORTED :			return "UNSUPPORTED";
63 	case EFI_BAD_BUFFER_SIZE :		return "BAD_BUFFER_SIZE";
64 	case EFI_BUFFER_TOO_SMALL :		return "BUFFER_TOO_SMALL";
65 	case EFI_NOT_READY :			return "NOT_READY";
66 	case EFI_DEVICE_ERROR :			return "DEVICE_ERROR";
67 	case EFI_WRITE_PROTECTED :		return "WRITE_PROTECTED";
68 	case EFI_OUT_OF_RESOURCES :		return "OUT_OF_RESOURCES";
69 	case EFI_VOLUME_CORRUPTED :		return "VOLUME_CORRUPTED";
70 	case EFI_VOLUME_FULL :			return "VOLUME_FULL";
71 	case EFI_NO_MEDIA :			return "NO_MEDIA";
72 	case EFI_MEDIA_CHANGED :		return "MEDIA_CHANGED";
73 	case EFI_NOT_FOUND :			return "NOT_FOUND";
74 	case EFI_ACCESS_DENIED :		return "ACCESS_DENIED";
75 	case EFI_NO_RESPONSE :			return "NO_RESPONSE";
76 	case EFI_NO_MAPPING :			return "NO_MAPPING";
77 	case EFI_TIMEOUT :			return "TIMEOUT";
78 	case EFI_NOT_STARTED :			return "NOT_STARTED";
79 	case EFI_ALREADY_STARTED :		return "ALREADY_STARTED";
80 	case EFI_ABORTED :			return "ABORTED";
81 	case EFI_ICMP_ERROR :			return "ICMP_ERROR";
82 	case EFI_TFTP_ERROR :			return "TFTP_ERROR";
83 	case EFI_PROTOCOL_ERROR :		return "PROTOCOL_ERROR";
84 	case EFI_INCOMPATIBLE_VERSION :		return "INCOMPATIBLE_VERSION";
85 	case EFI_SECURITY_VIOLATION :		return "SECURITY_VIOLATION";
86 	case EFI_CRC_ERROR :			return "CRC_ERROR";
87 	case EFI_END_OF_MEDIA :			return "END_OF_MEDIA";
88 	case EFI_END_OF_FILE :			return "END_OF_FILE";
89 	case EFI_INVALID_LANGUAGE :		return "INVALID_LANGUAGE";
90 	case EFI_COMPROMISED_DATA :		return "COMPROMISED_DATA";
91 	case EFI_WARN_UNKNOWN_GLYPH :		return "WARN_UNKNOWN_GLYPH";
92 	case EFI_WARN_DELETE_FAILURE :		return "WARN_DELETE_FAILURE";
93 	case EFI_WARN_WRITE_FAILURE :		return "WARN_WRITE_FAILURE";
94 	case EFI_WARN_BUFFER_TOO_SMALL :	return "WARN_BUFFER_TOO_SMALL";
95 	case EFI_WARN_STALE_DATA :		return "WARN_STALE_DATA";
96 	default:
97 		snprintf ( buf, sizeof ( buf ), "%#lx",
98 			   ( unsigned long ) efirc );
99 		return buf;
100 	}
101 }
102 
103 /**
104  * Convert EFI boolean to text
105  *
106  * @v boolean		Boolean value
107  * @ret text		Boolean value text
108  */
efi_boolean(BOOLEAN boolean)109 static const char * efi_boolean ( BOOLEAN boolean ) {
110 
111 	return ( boolean ? "TRUE" : "FALSE" );
112 }
113 
114 /**
115  * Wrap InstallProtocolInterface()
116  *
117  */
118 static EFI_STATUS EFIAPI
efi_install_protocol_interface_wrapper(EFI_HANDLE * handle,EFI_GUID * protocol,EFI_INTERFACE_TYPE interface_type,VOID * interface)119 efi_install_protocol_interface_wrapper ( EFI_HANDLE *handle, EFI_GUID *protocol,
120 					 EFI_INTERFACE_TYPE interface_type,
121 					 VOID *interface ) {
122 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
123 	void *retaddr = __builtin_return_address ( 0 );
124 	EFI_STATUS efirc;
125 
126 	DBGC ( colour, "InstallProtocolInterface ( %s, %s, %d, %p ) ",
127 	       efi_handle_name ( *handle ), efi_guid_ntoa ( protocol ),
128 	       interface_type, interface );
129 	efirc = bs->InstallProtocolInterface ( handle, protocol, interface_type,
130 					       interface );
131 	DBGC ( colour, "= %s ( %s ) -> %p\n",
132 	       efi_status ( efirc ), efi_handle_name ( *handle ), retaddr );
133 	return efirc;
134 }
135 
136 /**
137  * Wrap ReinstallProtocolInterface()
138  *
139  */
140 static EFI_STATUS EFIAPI
efi_reinstall_protocol_interface_wrapper(EFI_HANDLE handle,EFI_GUID * protocol,VOID * old_interface,VOID * new_interface)141 efi_reinstall_protocol_interface_wrapper ( EFI_HANDLE handle,
142 					   EFI_GUID *protocol,
143 					   VOID *old_interface,
144 					   VOID *new_interface ) {
145 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
146 	void *retaddr = __builtin_return_address ( 0 );
147 	EFI_STATUS efirc;
148 
149 	DBGC ( colour, "ReinstallProtocolInterface ( %s, %s, %p, %p ) ",
150 	       efi_handle_name ( handle ), efi_guid_ntoa ( protocol ),
151 	       old_interface, new_interface );
152 	efirc = bs->ReinstallProtocolInterface ( handle, protocol,
153 						 old_interface, new_interface );
154 	DBGC ( colour, "= %s -> %p\n", efi_status ( efirc ), retaddr );
155 	return efirc;
156 }
157 
158 /**
159  * Wrap UninstallProtocolInterface()
160  *
161  */
162 static EFI_STATUS EFIAPI
efi_uninstall_protocol_interface_wrapper(EFI_HANDLE handle,EFI_GUID * protocol,VOID * interface)163 efi_uninstall_protocol_interface_wrapper ( EFI_HANDLE handle,
164 					   EFI_GUID *protocol,
165 					   VOID *interface ) {
166 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
167 	void *retaddr = __builtin_return_address ( 0 );
168 	EFI_STATUS efirc;
169 
170 	DBGC ( colour, "UninstallProtocolInterface ( %s, %s, %p ) ",
171 	       efi_handle_name ( handle ), efi_guid_ntoa ( protocol ),
172 	       interface );
173 	efirc = bs->UninstallProtocolInterface ( handle, protocol, interface );
174 	DBGC ( colour, "= %s -> %p\n", efi_status ( efirc ), retaddr );
175 	return efirc;
176 }
177 
178 /**
179  * Wrap HandleProtocol()
180  *
181  */
182 static EFI_STATUS EFIAPI
efi_handle_protocol_wrapper(EFI_HANDLE handle,EFI_GUID * protocol,VOID ** interface)183 efi_handle_protocol_wrapper ( EFI_HANDLE handle, EFI_GUID *protocol,
184 			      VOID **interface ) {
185 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
186 	void *retaddr = __builtin_return_address ( 0 );
187 	EFI_STATUS efirc;
188 
189 	DBGC ( colour, "HandleProtocol ( %s, %s ) ",
190 	       efi_handle_name ( handle ), efi_guid_ntoa ( protocol ) );
191 	efirc = bs->HandleProtocol ( handle, protocol, interface );
192 	DBGC ( colour, "= %s ( %p ) -> %p\n",
193 	       efi_status ( efirc ), *interface, retaddr );
194 	return efirc;
195 }
196 
197 /**
198  * Wrap LocateHandle()
199  *
200  */
201 static EFI_STATUS EFIAPI
efi_locate_handle_wrapper(EFI_LOCATE_SEARCH_TYPE search_type,EFI_GUID * protocol,VOID * search_key,UINTN * buffer_size,EFI_HANDLE * buffer)202 efi_locate_handle_wrapper ( EFI_LOCATE_SEARCH_TYPE search_type,
203 			    EFI_GUID *protocol, VOID *search_key,
204 			    UINTN *buffer_size, EFI_HANDLE *buffer ) {
205 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
206 	void *retaddr = __builtin_return_address ( 0 );
207 	unsigned int i;
208 	EFI_STATUS efirc;
209 
210 	DBGC ( colour, "LocateHandle ( %s, %s, %p, %zd ) ",
211 	       efi_locate_search_type_name ( search_type ),
212 	       efi_guid_ntoa ( protocol ), search_key,
213 	       ( ( size_t ) *buffer_size ) );
214 	efirc = bs->LocateHandle ( search_type, protocol, search_key,
215 				   buffer_size, buffer );
216 	DBGC ( colour, "= %s ( %zd", efi_status ( efirc ),
217 	       ( ( size_t ) *buffer_size ) );
218 	if ( efirc == 0 ) {
219 		DBGC ( colour, ", {" );
220 		for ( i = 0; i < ( *buffer_size / sizeof ( buffer[0] ) ); i++ ){
221 			DBGC ( colour, "%s%s", ( i ? ", " : " " ),
222 			       efi_handle_name ( buffer[i] ) );
223 		}
224 		DBGC ( colour, " }" );
225 	}
226 	DBGC ( colour, " ) -> %p\n", retaddr );
227 	return efirc;
228 }
229 
230 /**
231  * Wrap LocateDevicePath()
232  *
233  */
234 static EFI_STATUS EFIAPI
efi_locate_device_path_wrapper(EFI_GUID * protocol,EFI_DEVICE_PATH_PROTOCOL ** device_path,EFI_HANDLE * device)235 efi_locate_device_path_wrapper ( EFI_GUID *protocol,
236 				 EFI_DEVICE_PATH_PROTOCOL **device_path,
237 				 EFI_HANDLE *device ) {
238 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
239 	void *retaddr = __builtin_return_address ( 0 );
240 	EFI_STATUS efirc;
241 
242 	DBGC ( colour, "LocateDevicePath ( %s, %s ) ",
243 	       efi_guid_ntoa ( protocol ), efi_devpath_text ( *device_path ) );
244 	efirc = bs->LocateDevicePath ( protocol, device_path, device );
245 	DBGC ( colour, "= %s ( %s, ",
246 	       efi_status ( efirc ), efi_devpath_text ( *device_path ) );
247 	DBGC ( colour, "%s ) -> %p\n", efi_handle_name ( *device ), retaddr );
248 	return efirc;
249 }
250 
251 /**
252  * Wrap LoadImage()
253  *
254  */
255 static EFI_STATUS EFIAPI
efi_load_image_wrapper(BOOLEAN boot_policy,EFI_HANDLE parent_image_handle,EFI_DEVICE_PATH_PROTOCOL * device_path,VOID * source_buffer,UINTN source_size,EFI_HANDLE * image_handle)256 efi_load_image_wrapper ( BOOLEAN boot_policy, EFI_HANDLE parent_image_handle,
257 			 EFI_DEVICE_PATH_PROTOCOL *device_path,
258 			 VOID *source_buffer, UINTN source_size,
259 			 EFI_HANDLE *image_handle ) {
260 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
261 	void *retaddr = __builtin_return_address ( 0 );
262 	EFI_STATUS efirc;
263 
264 	DBGC ( colour, "LoadImage ( %s, %s, ", efi_boolean ( boot_policy ),
265 	       efi_handle_name ( parent_image_handle ) );
266 	DBGC ( colour, "%s, %p, %#llx ) ",
267 	       efi_devpath_text ( device_path ), source_buffer,
268 	       ( ( unsigned long long ) source_size ) );
269 	efirc = bs->LoadImage ( boot_policy, parent_image_handle, device_path,
270 				source_buffer, source_size, image_handle );
271 	DBGC ( colour, "= %s ( ", efi_status ( efirc ) );
272 	if ( efirc == 0 )
273 		DBGC ( colour, "%s ", efi_handle_name ( *image_handle ) );
274 	DBGC ( colour, ") -> %p\n", retaddr );
275 
276 	/* Wrap the new image */
277 	if ( efirc == 0 )
278 		efi_wrap ( *image_handle );
279 
280 	return efirc;
281 }
282 
283 /**
284  * Wrap StartImage()
285  *
286  */
287 static EFI_STATUS EFIAPI
efi_start_image_wrapper(EFI_HANDLE image_handle,UINTN * exit_data_size,CHAR16 ** exit_data)288 efi_start_image_wrapper ( EFI_HANDLE image_handle, UINTN *exit_data_size,
289 			  CHAR16 **exit_data ) {
290 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
291 	void *retaddr = __builtin_return_address ( 0 );
292 	EFI_STATUS efirc;
293 
294 	DBGC ( colour, "StartImage ( %s ) ", efi_handle_name ( image_handle ) );
295 	efirc = bs->StartImage ( image_handle, exit_data_size, exit_data );
296 	DBGC ( colour, "= %s", efi_status ( efirc ) );
297 	if ( ( efirc != 0 ) && exit_data && *exit_data_size )
298 		DBGC ( colour, " ( \"%ls\" )", *exit_data );
299 	DBGC ( colour, " -> %p\n", retaddr );
300 	if ( ( efirc != 0 ) && exit_data && *exit_data_size )
301 		DBGC_HD ( colour, *exit_data, *exit_data_size );
302 	return efirc;
303 }
304 
305 /**
306  * Wrap Exit()
307  *
308  */
309 static EFI_STATUS EFIAPI
efi_exit_wrapper(EFI_HANDLE image_handle,EFI_STATUS exit_status,UINTN exit_data_size,CHAR16 * exit_data)310 efi_exit_wrapper ( EFI_HANDLE image_handle, EFI_STATUS exit_status,
311 		   UINTN exit_data_size, CHAR16 *exit_data ) {
312 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
313 	void *retaddr = __builtin_return_address ( 0 );
314 	EFI_STATUS efirc;
315 
316 	if ( ( exit_status != 0 ) && exit_data && exit_data_size )
317 		DBGC_HD ( colour, exit_data, exit_data_size );
318 	DBGC ( colour, "Exit ( %s, %s",
319 	       efi_handle_name ( image_handle ), efi_status ( exit_status ) );
320 	if ( ( exit_status != 0 ) && exit_data && exit_data_size )
321 		DBGC ( colour, ", \"%ls\"", exit_data );
322 	DBGC ( colour, " ) " );
323 	efirc = bs->Exit ( image_handle, exit_status, exit_data_size,
324 			   exit_data );
325 	DBGC ( colour, "= %s -> %p\n", efi_status ( efirc ), retaddr );
326 	return efirc;
327 }
328 
329 /**
330  * Wrap UnloadImage()
331  *
332  */
333 static EFI_STATUS EFIAPI
efi_unload_image_wrapper(EFI_HANDLE image_handle)334 efi_unload_image_wrapper ( EFI_HANDLE image_handle ) {
335 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
336 	void *retaddr = __builtin_return_address ( 0 );
337 	EFI_STATUS efirc;
338 
339 	DBGC ( colour, "UnloadImage ( %s ) ",
340 	       efi_handle_name ( image_handle ) );
341 	efirc = bs->UnloadImage ( image_handle );
342 	DBGC ( colour, "= %s -> %p\n", efi_status ( efirc ), retaddr );
343 	return efirc;
344 }
345 
346 /**
347  * Wrap ExitBootServices()
348  *
349  */
350 static EFI_STATUS EFIAPI
efi_exit_boot_services_wrapper(EFI_HANDLE image_handle,UINTN map_key)351 efi_exit_boot_services_wrapper ( EFI_HANDLE image_handle, UINTN map_key ) {
352 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
353 	void *retaddr = __builtin_return_address ( 0 );
354 	EFI_STATUS efirc;
355 
356 	DBGC ( colour, "ExitBootServices ( %s, %#llx ) ",
357 	       efi_handle_name ( image_handle ),
358 	       ( ( unsigned long long ) map_key ) );
359 	efirc = bs->ExitBootServices ( image_handle, map_key );
360 	DBGC ( colour, "= %s -> %p\n", efi_status ( efirc ), retaddr );
361 	return efirc;
362 }
363 
364 /**
365  * Wrap ConnectController()
366  *
367  */
368 static EFI_STATUS EFIAPI
efi_connect_controller_wrapper(EFI_HANDLE controller_handle,EFI_HANDLE * driver_image_handle,EFI_DEVICE_PATH_PROTOCOL * remaining_path,BOOLEAN recursive)369 efi_connect_controller_wrapper ( EFI_HANDLE controller_handle,
370 				 EFI_HANDLE *driver_image_handle,
371 				 EFI_DEVICE_PATH_PROTOCOL *remaining_path,
372 				 BOOLEAN recursive ) {
373 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
374 	void *retaddr = __builtin_return_address ( 0 );
375 	EFI_HANDLE *tmp;
376 	EFI_STATUS efirc;
377 
378 	DBGC ( colour, "ConnectController ( %s, {",
379 	       efi_handle_name ( controller_handle ) );
380 	if ( driver_image_handle ) {
381 		for ( tmp = driver_image_handle ; *tmp ; tmp++ ) {
382 			DBGC ( colour, "%s%s",
383 			       ( ( tmp == driver_image_handle ) ? " " : ", " ),
384 			       efi_handle_name ( *tmp ) );
385 		}
386 	}
387 	DBGC ( colour, " }, %s, %s ) ", efi_devpath_text ( remaining_path ),
388 	       efi_boolean ( recursive ) );
389 	efirc = bs->ConnectController ( controller_handle, driver_image_handle,
390 					remaining_path, recursive );
391 	DBGC ( colour, "= %s -> %p\n", efi_status ( efirc ), retaddr );
392 	return efirc;
393 }
394 
395 /**
396  * Wrap DisconnectController()
397  *
398  */
399 static EFI_STATUS EFIAPI
efi_disconnect_controller_wrapper(EFI_HANDLE controller_handle,EFI_HANDLE driver_image_handle,EFI_HANDLE child_handle)400 efi_disconnect_controller_wrapper ( EFI_HANDLE controller_handle,
401 				    EFI_HANDLE driver_image_handle,
402 				    EFI_HANDLE child_handle ) {
403 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
404 	void *retaddr = __builtin_return_address ( 0 );
405 	EFI_STATUS efirc;
406 
407 	DBGC ( colour, "DisconnectController ( %s",
408 	       efi_handle_name ( controller_handle ) );
409 	DBGC ( colour, ", %s", efi_handle_name ( driver_image_handle ) );
410 	DBGC ( colour, ", %s ) ", efi_handle_name ( child_handle ) );
411 	efirc = bs->DisconnectController ( controller_handle,
412 					   driver_image_handle,
413 					   child_handle );
414 	DBGC ( colour, "= %s -> %p\n", efi_status ( efirc ), retaddr );
415 	return efirc;
416 }
417 
418 /**
419  * Wrap OpenProtocol()
420  *
421  */
422 static EFI_STATUS EFIAPI
efi_open_protocol_wrapper(EFI_HANDLE handle,EFI_GUID * protocol,VOID ** interface,EFI_HANDLE agent_handle,EFI_HANDLE controller_handle,UINT32 attributes)423 efi_open_protocol_wrapper ( EFI_HANDLE handle, EFI_GUID *protocol,
424 			    VOID **interface, EFI_HANDLE agent_handle,
425 			    EFI_HANDLE controller_handle, UINT32 attributes ) {
426 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
427 	void *retaddr = __builtin_return_address ( 0 );
428 	EFI_STATUS efirc;
429 
430 	DBGC ( colour, "OpenProtocol ( %s, %s, ",
431 	       efi_handle_name ( handle ), efi_guid_ntoa ( protocol ) );
432 	DBGC ( colour, "%s, ", efi_handle_name ( agent_handle ) );
433 	DBGC ( colour, "%s, %s ) ", efi_handle_name ( controller_handle ),
434 	       efi_open_attributes_name ( attributes ) );
435 	efirc = bs->OpenProtocol ( handle, protocol, interface, agent_handle,
436 				   controller_handle, attributes );
437 	DBGC ( colour, "= %s ( %p ) -> %p\n",
438 	       efi_status ( efirc ), *interface, retaddr );
439 	return efirc;
440 }
441 
442 /**
443  * Wrap CloseProtocol()
444  *
445  */
446 static EFI_STATUS EFIAPI
efi_close_protocol_wrapper(EFI_HANDLE handle,EFI_GUID * protocol,EFI_HANDLE agent_handle,EFI_HANDLE controller_handle)447 efi_close_protocol_wrapper ( EFI_HANDLE handle, EFI_GUID *protocol,
448 			     EFI_HANDLE agent_handle,
449 			     EFI_HANDLE controller_handle ) {
450 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
451 	void *retaddr = __builtin_return_address ( 0 );
452 	EFI_STATUS efirc;
453 
454 	DBGC ( colour, "CloseProtocol ( %s, %s, ",
455 	       efi_handle_name ( handle ), efi_guid_ntoa ( protocol ) );
456 	DBGC ( colour, "%s, ", efi_handle_name ( agent_handle ) );
457 	DBGC ( colour, "%s ) ", efi_handle_name ( controller_handle ) );
458 	efirc = bs->CloseProtocol ( handle, protocol, agent_handle,
459 				    controller_handle );
460 	DBGC ( colour, "= %s -> %p\n",
461 	       efi_status ( efirc ), retaddr );
462 	return efirc;
463 }
464 
465 /**
466  * Wrap ProtocolsPerHandle()
467  *
468  */
469 static EFI_STATUS EFIAPI
efi_protocols_per_handle_wrapper(EFI_HANDLE handle,EFI_GUID *** protocol_buffer,UINTN * protocol_buffer_count)470 efi_protocols_per_handle_wrapper ( EFI_HANDLE handle,
471 				   EFI_GUID ***protocol_buffer,
472 				   UINTN *protocol_buffer_count ) {
473 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
474 	void *retaddr = __builtin_return_address ( 0 );
475 	unsigned int i;
476 	EFI_STATUS efirc;
477 
478 	DBGC ( colour, "ProtocolsPerHandle ( %s ) ",
479 	       efi_handle_name ( handle ) );
480 	efirc = bs->ProtocolsPerHandle ( handle, protocol_buffer,
481 					 protocol_buffer_count );
482 	DBGC ( colour, "= %s", efi_status ( efirc ) );
483 	if ( efirc == 0 ) {
484 		DBGC ( colour, " ( {" );
485 		for ( i = 0 ; i < *protocol_buffer_count ; i++ ) {
486 			DBGC ( colour, "%s%s", ( i ? ", " : " " ),
487 			       efi_guid_ntoa ( (*protocol_buffer)[i] ) );
488 		}
489 		DBGC ( colour, " } )" );
490 	}
491 	DBGC ( colour, " -> %p\n", retaddr );
492 	return efirc;
493 }
494 
495 /**
496  * Wrap LocateHandleBuffer()
497  *
498  */
499 static EFI_STATUS EFIAPI
efi_locate_handle_buffer_wrapper(EFI_LOCATE_SEARCH_TYPE search_type,EFI_GUID * protocol,VOID * search_key,UINTN * no_handles,EFI_HANDLE ** buffer)500 efi_locate_handle_buffer_wrapper ( EFI_LOCATE_SEARCH_TYPE search_type,
501 				   EFI_GUID *protocol, VOID *search_key,
502 				   UINTN *no_handles, EFI_HANDLE **buffer ) {
503 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
504 	void *retaddr = __builtin_return_address ( 0 );
505 	unsigned int i;
506 	EFI_STATUS efirc;
507 
508 	DBGC ( colour, "LocateHandleBuffer ( %s, %s, %p ) ",
509 	       efi_locate_search_type_name ( search_type ),
510 	       efi_guid_ntoa ( protocol ), search_key );
511 	efirc = bs->LocateHandleBuffer ( search_type, protocol, search_key,
512 					 no_handles, buffer );
513 	DBGC ( colour, "= %s", efi_status ( efirc ) );
514 	if ( efirc == 0 ) {
515 		DBGC ( colour, " ( %d, {", ( ( unsigned int ) *no_handles ) );
516 		for ( i = 0 ; i < *no_handles ; i++ ) {
517 			DBGC ( colour, "%s%s", ( i ? ", " : " " ),
518 			       efi_handle_name ( (*buffer)[i] ) );
519 		}
520 		DBGC ( colour, " } )" );
521 	}
522 	DBGC ( colour, " -> %p\n", retaddr );
523 	return efirc;
524 }
525 
526 /**
527  * Wrap LocateProtocol()
528  *
529  */
530 static EFI_STATUS EFIAPI
efi_locate_protocol_wrapper(EFI_GUID * protocol,VOID * registration,VOID ** interface)531 efi_locate_protocol_wrapper ( EFI_GUID *protocol, VOID *registration,
532 			      VOID **interface ) {
533 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
534 	void *retaddr = __builtin_return_address ( 0 );
535 	EFI_STATUS efirc;
536 
537 	DBGC ( colour, "LocateProtocol ( %s, %p ) ",
538 	       efi_guid_ntoa ( protocol ), registration );
539 	efirc = bs->LocateProtocol ( protocol, registration, interface );
540 	DBGC ( colour, "= %s ( %p ) -> %p\n",
541 	       efi_status ( efirc ), *interface, retaddr );
542 	return efirc;
543 }
544 
545 /**
546  * Wrap the calls made by a loaded image
547  *
548  * @v handle		Image handle
549  */
efi_wrap(EFI_HANDLE handle)550  void efi_wrap ( EFI_HANDLE handle ) {
551 	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
552 	union {
553 		EFI_LOADED_IMAGE_PROTOCOL *image;
554 		void *intf;
555 	} loaded;
556 	EFI_STATUS efirc;
557 	int rc;
558 
559 	/* Do nothing unless debugging is enabled */
560 	if ( ! DBG_LOG )
561 		return;
562 
563 	/* Populate table wrappers */
564 	memcpy ( &efi_systab_wrapper, efi_systab,
565 		 sizeof ( efi_systab_wrapper ) );
566 	memcpy ( &efi_bs_wrapper, bs, sizeof ( efi_bs_wrapper ) );
567 	efi_systab_wrapper.BootServices	= &efi_bs_wrapper;
568 	efi_bs_wrapper.InstallProtocolInterface
569 		= efi_install_protocol_interface_wrapper;
570 	efi_bs_wrapper.ReinstallProtocolInterface
571 		= efi_reinstall_protocol_interface_wrapper;
572 	efi_bs_wrapper.UninstallProtocolInterface
573 		= efi_uninstall_protocol_interface_wrapper;
574 	efi_bs_wrapper.HandleProtocol	= efi_handle_protocol_wrapper;
575 	efi_bs_wrapper.LocateHandle	= efi_locate_handle_wrapper;
576 	efi_bs_wrapper.LocateDevicePath	= efi_locate_device_path_wrapper;
577 	efi_bs_wrapper.LoadImage	= efi_load_image_wrapper;
578 	efi_bs_wrapper.StartImage	= efi_start_image_wrapper;
579 	efi_bs_wrapper.Exit		= efi_exit_wrapper;
580 	efi_bs_wrapper.UnloadImage	= efi_unload_image_wrapper;
581 	efi_bs_wrapper.ExitBootServices	= efi_exit_boot_services_wrapper;
582 	efi_bs_wrapper.ConnectController
583 		= efi_connect_controller_wrapper;
584 	efi_bs_wrapper.DisconnectController
585 		= efi_disconnect_controller_wrapper;
586 	efi_bs_wrapper.OpenProtocol	= efi_open_protocol_wrapper;
587 	efi_bs_wrapper.CloseProtocol	= efi_close_protocol_wrapper;
588 	efi_bs_wrapper.ProtocolsPerHandle
589 		= efi_protocols_per_handle_wrapper;
590 	efi_bs_wrapper.LocateHandleBuffer
591 		= efi_locate_handle_buffer_wrapper;
592 	efi_bs_wrapper.LocateProtocol	= efi_locate_protocol_wrapper;
593 
594 	/* Open loaded image protocol */
595 	if ( ( efirc = bs->OpenProtocol ( handle,
596 					  &efi_loaded_image_protocol_guid,
597 					  &loaded.intf, efi_image_handle, NULL,
598 					  EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
599 		rc = -EEFI ( efirc );
600 		DBGC ( colour, "WRAP %s could not get loaded image protocol: "
601 		       "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
602 		return;
603 	}
604 
605 	/* Provide system table wrapper to image */
606 	loaded.image->SystemTable = &efi_systab_wrapper;
607 	DBGC ( colour, "WRAP %s at base %p has protocols:\n",
608 	       efi_handle_name ( handle ), loaded.image->ImageBase );
609 	DBGC_EFI_PROTOCOLS ( colour, handle );
610 	DBGC ( colour, "WRAP %s parent", efi_handle_name ( handle ) );
611 	DBGC ( colour, " %s\n", efi_handle_name ( loaded.image->ParentHandle ));
612 	DBGC ( colour, "WRAP %s device", efi_handle_name ( handle ) );
613 	DBGC ( colour, " %s\n", efi_handle_name ( loaded.image->DeviceHandle ));
614 	DBGC ( colour, "WRAP %s file", efi_handle_name ( handle ) );
615 	DBGC ( colour, " %s\n", efi_devpath_text ( loaded.image->FilePath ) );
616 
617 	/* Close loaded image protocol */
618 	bs->CloseProtocol ( handle, &efi_loaded_image_protocol_guid,
619 			    efi_image_handle, NULL );
620 }
621