Browse Source

[efi] Add excessive sanity checks into efi_debug functions

Try very hard to avoid ever doing something invalid while attempting
to generate a debug message.

Debugged-by: Curtis Larsen <larsen@dixie.edu>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 years ago
parent
commit
f207176987
1 changed files with 53 additions and 3 deletions
  1. 53
    3
      src/interface/efi/efi_debug.c

+ 53
- 3
src/interface/efi/efi_debug.c View File

@@ -168,6 +168,10 @@ const char * efi_guid_ntoa ( EFI_GUID *guid ) {
168 168
 	} u;
169 169
 	unsigned int i;
170 170
 
171
+	/* Sanity check */
172
+	if ( ! guid )
173
+		return NULL;
174
+
171 175
 	/* Check for a match against well-known GUIDs */
172 176
 	for ( i = 0 ; i < ( sizeof ( efi_well_known_guids ) /
173 177
 			    sizeof ( efi_well_known_guids[0] ) ) ; i++ ) {
@@ -224,6 +228,13 @@ void dbg_efi_openers ( EFI_HANDLE handle, EFI_GUID *protocol ) {
224 228
 	EFI_STATUS efirc;
225 229
 	int rc;
226 230
 
231
+	/* Sanity check */
232
+	if ( ( ! handle ) || ( ! protocol ) ) {
233
+		printf ( "EFI could not retrieve openers for %s on %p\n",
234
+			 efi_guid_ntoa ( protocol ), handle );
235
+		return;
236
+	}
237
+
227 238
 	/* Retrieve list of openers */
228 239
 	if ( ( efirc = bs->OpenProtocolInformation ( handle, protocol, &openers,
229 240
 						     &count ) ) != 0 ) {
@@ -268,6 +279,12 @@ void dbg_efi_protocols ( EFI_HANDLE handle ) {
268 279
 	EFI_STATUS efirc;
269 280
 	int rc;
270 281
 
282
+	/* Sanity check */
283
+	if ( ! handle ) {
284
+		printf ( "EFI could not retrieve protocols for %p\n", handle );
285
+		return;
286
+	}
287
+
271 288
 	/* Retrieve list of protocols */
272 289
 	if ( ( efirc = bs->ProtocolsPerHandle ( handle, &protocols,
273 290
 						&count ) ) != 0 ) {
@@ -301,9 +318,17 @@ const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
301 318
 	static char text[256];
302 319
 	CHAR16 *wtext;
303 320
 
304
-	/* Convert path to a textual representation */
305
-	if ( ! efidpt )
321
+	/* Sanity checks */
322
+	if ( ! efidpt ) {
323
+		DBG ( "[No DevicePathToText]" );
324
+		return NULL;
325
+	}
326
+	if ( ! path ) {
327
+		DBG ( "[NULL DevicePath]" );
306 328
 		return NULL;
329
+	}
330
+
331
+	/* Convert path to a textual representation */
307 332
 	wtext = efidpt->ConvertDevicePathToText ( path, TRUE, FALSE );
308 333
 	if ( ! wtext )
309 334
 		return NULL;
@@ -328,6 +353,12 @@ static const char * efi_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
328 353
 	CHAR16 *driver_name;
329 354
 	EFI_STATUS efirc;
330 355
 
356
+	/* Sanity check */
357
+	if ( ! wtf ) {
358
+		DBG ( "[NULL ComponentName2]" );
359
+		return NULL;
360
+	}
361
+
331 362
 	/* Try "en" first; if that fails then try the first language */
332 363
 	if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
333 364
 					      &driver_name ) ) != 0 ) &&
@@ -350,7 +381,7 @@ static const char * efi_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
350 381
 static const char *
351 382
 efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
352 383
 	static char buf[32];
353
-	EFI_IMAGE_DOS_HEADER *dos = loaded->ImageBase;
384
+	EFI_IMAGE_DOS_HEADER *dos;
354 385
 	EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
355 386
 	EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
356 387
 	EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
@@ -367,7 +398,14 @@ efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
367 398
 	char *name;
368 399
 	char *tmp;
369 400
 
401
+	/* Sanity check */
402
+	if ( ! loaded ) {
403
+		DBG ( "[NULL LoadedImage]" );
404
+		return NULL;
405
+	}
406
+
370 407
 	/* Parse DOS header */
408
+	dos = loaded->ImageBase;
371 409
 	if ( ! dos ) {
372 410
 		DBG ( "[Missing DOS header]" );
373 411
 		return NULL;
@@ -462,6 +500,12 @@ efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
462 500
 static const char *
463 501
 efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
464 502
 
503
+	/* Sanity check */
504
+	if ( ! loaded ) {
505
+		DBG ( "[NULL LoadedImage]" );
506
+		return NULL;
507
+	}
508
+
465 509
 	return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
466 510
 }
467 511
 
@@ -474,6 +518,12 @@ efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
474 518
 static const char *
475 519
 efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
476 520
 
521
+	/* Sanity check */
522
+	if ( ! loaded ) {
523
+		DBG ( "[NULL LoadedImage]" );
524
+		return NULL;
525
+	}
526
+
477 527
 	return efi_devpath_text ( loaded->FilePath );
478 528
 }
479 529
 

Loading…
Cancel
Save