소스 검색

[efi] Provide efi_handle_name() for debugging

Provide a function efi_handle_name() (as a generalisation of
efi_handle_devpath_text()) which tries various methods to produce a
human-readable name for an EFI handle.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 년 전
부모
커밋
550f212d15
2개의 변경된 파일236개의 추가작업 그리고 0개의 파일을 삭제
  1. 1
    0
      src/include/ipxe/efi/efi.h
  2. 235
    0
      src/interface/efi/efi_debug.c

+ 1
- 0
src/include/ipxe/efi/efi.h 파일 보기

@@ -151,6 +151,7 @@ extern EFI_SYSTEM_TABLE *efi_systab;
151 151
 extern const char * efi_guid_ntoa ( EFI_GUID *guid );
152 152
 extern const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path );
153 153
 extern const char * efi_handle_devpath_text ( EFI_HANDLE handle );
154
+extern const char * efi_handle_name ( EFI_HANDLE handle );
154 155
 
155 156
 extern void dbg_efi_protocols ( EFI_HANDLE handle );
156 157
 

+ 235
- 0
src/interface/efi/efi_debug.c 파일 보기

@@ -47,6 +47,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
47 47
 #include <ipxe/efi/Protocol/PciRootBridgeIo.h>
48 48
 #include <ipxe/efi/Protocol/SimpleFileSystem.h>
49 49
 #include <ipxe/efi/Protocol/SimpleNetwork.h>
50
+#include <ipxe/efi/IndustryStandard/PeImage.h>
50 51
 
51 52
 /** Block I/O protocol GUID */
52 53
 static EFI_GUID efi_block_io_protocol_guid
@@ -239,6 +240,240 @@ const char * efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
239 240
 	return text;
240 241
 }
241 242
 
243
+/**
244
+ * Get driver name
245
+ *
246
+ * @v wtf		Component name protocol
247
+ * @ret name		Driver name, or NULL
248
+ */
249
+static const char * efi_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf ) {
250
+	static char name[64];
251
+	CHAR16 *driver_name;
252
+	EFI_STATUS efirc;
253
+
254
+	/* Try "en" first; if that fails then try the first language */
255
+	if ( ( ( efirc = wtf->GetDriverName ( wtf, "en",
256
+					      &driver_name ) ) != 0 ) &&
257
+	     ( ( efirc = wtf->GetDriverName ( wtf, wtf->SupportedLanguages,
258
+					      &driver_name ) ) != 0 ) ) {
259
+		return NULL;
260
+	}
261
+
262
+	/* Convert name from CHAR16 to char */
263
+	snprintf ( name, sizeof ( name ), "%ls", driver_name );
264
+	return name;
265
+}
266
+
267
+/**
268
+ * Get PE/COFF debug filename
269
+ *
270
+ * @v loaded		Loaded image
271
+ * @ret name		PE/COFF debug filename, or NULL
272
+ */
273
+static const char *
274
+efi_pecoff_debug_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
275
+	static char buf[32];
276
+	EFI_IMAGE_DOS_HEADER *dos = loaded->ImageBase;
277
+	EFI_IMAGE_OPTIONAL_HEADER_UNION *pe;
278
+	EFI_IMAGE_OPTIONAL_HEADER32 *opt32;
279
+	EFI_IMAGE_OPTIONAL_HEADER64 *opt64;
280
+	EFI_IMAGE_DATA_DIRECTORY *datadir;
281
+	EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *debug;
282
+	EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *codeview;
283
+	size_t max_len;
284
+	char *name;
285
+	char *tmp;
286
+
287
+	/* Parse DOS header */
288
+	if ( ! dos ) {
289
+		DBGC ( loaded, "Missing DOS header\n" );
290
+		return NULL;
291
+	}
292
+	if ( dos->e_magic != EFI_IMAGE_DOS_SIGNATURE ) {
293
+		DBGC ( loaded, "Bad DOS signature\n" );
294
+		return NULL;
295
+	}
296
+	pe = ( loaded->ImageBase + dos->e_lfanew );
297
+
298
+	/* Parse PE header */
299
+	if ( pe->Pe32.Signature != EFI_IMAGE_NT_SIGNATURE ) {
300
+		DBGC ( loaded, "Bad PE signature\n" );
301
+		return NULL;
302
+	}
303
+	opt32 = &pe->Pe32.OptionalHeader;
304
+	opt64 = &pe->Pe32Plus.OptionalHeader;
305
+	if ( opt32->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
306
+		datadir = opt32->DataDirectory;
307
+	} else if ( opt64->Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC ) {
308
+		datadir = opt64->DataDirectory;
309
+	} else {
310
+		DBGC ( loaded, "Bad optional header signature\n" );
311
+		return NULL;
312
+	}
313
+
314
+	/* Parse data directory entry */
315
+	if ( ! datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress ) {
316
+		DBGC ( loaded, "Empty debug directory entry\n" );
317
+		return NULL;
318
+	}
319
+	debug = ( loaded->ImageBase +
320
+		  datadir[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress );
321
+
322
+	/* Parse debug directory entry */
323
+	if ( debug->Type != EFI_IMAGE_DEBUG_TYPE_CODEVIEW ) {
324
+		DBGC ( loaded, "Not a CodeView debug directory entry\n" );
325
+		return NULL;
326
+	}
327
+	codeview = ( loaded->ImageBase + debug->RVA );
328
+
329
+	/* Parse CodeView entry */
330
+	if ( codeview->Signature != CODEVIEW_SIGNATURE_NB10 ) {
331
+		DBGC ( loaded, "Bad CodeView signature\n" );
332
+		return NULL;
333
+	}
334
+	name = ( ( ( void * ) codeview ) + sizeof ( *codeview ) );
335
+
336
+	/* Sanity check - avoid scanning endlessly through memory */
337
+	max_len = EFI_PAGE_SIZE; /* Reasonably sane */
338
+	if ( strnlen ( name, max_len ) == max_len ) {
339
+		DBGC ( loaded, "Excessively long or invalid CodeView name\n" );
340
+		return NULL;
341
+	}
342
+
343
+	/* Skip any directory components.  We cannot modify this data
344
+	 * or create a temporary buffer, so do not use basename().
345
+	 */
346
+	while ( ( ( tmp = strchr ( name, '/' ) ) != NULL ) ||
347
+		( ( tmp = strchr ( name, '\\' ) ) != NULL ) ) {
348
+		name = ( tmp + 1 );
349
+	}
350
+
351
+	/* Copy base name to buffer */
352
+	snprintf ( buf, sizeof ( buf ), "%s", name );
353
+
354
+	/* Strip file suffix, if present */
355
+	if ( ( tmp = strrchr ( name, '.' ) ) != NULL )
356
+		*tmp = '\0';
357
+
358
+	return name;
359
+}
360
+
361
+/**
362
+ * Get initial loaded image name
363
+ *
364
+ * @v loaded		Loaded image
365
+ * @ret name		Initial loaded image name, or NULL
366
+ */
367
+static const char *
368
+efi_first_loaded_image_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
369
+
370
+	return ( ( loaded->ParentHandle == NULL ) ? "DxeCore(?)" : NULL );
371
+}
372
+
373
+/**
374
+ * Get loaded image name from file path
375
+ *
376
+ * @v loaded		Loaded image
377
+ * @ret name		Loaded image name, or NULL
378
+ */
379
+static const char *
380
+efi_loaded_image_filepath_name ( EFI_LOADED_IMAGE_PROTOCOL *loaded ) {
381
+
382
+	return efi_devpath_text ( loaded->FilePath );
383
+}
384
+
385
+/** An EFI handle name type */
386
+struct efi_handle_name_type {
387
+	/** Protocol */
388
+	EFI_GUID *protocol;
389
+	/**
390
+	 * Get name
391
+	 *
392
+	 * @v interface		Protocol interface
393
+	 * @ret name		Name of handle, or NULL on failure
394
+	 */
395
+	const char * ( * name ) ( void *interface );
396
+};
397
+
398
+/**
399
+ * Define an EFI handle name type
400
+ *
401
+ * @v protocol		Protocol interface
402
+ * @v name		Method to get name
403
+ * @ret type		EFI handle name type
404
+ */
405
+#define EFI_HANDLE_NAME_TYPE( protocol, name ) {	\
406
+	(protocol),					\
407
+	( const char * ( * ) ( void * ) ) (name),	\
408
+	}
409
+
410
+/** EFI handle name types */
411
+static struct efi_handle_name_type efi_handle_name_types[] = {
412
+	/* Device path */
413
+	EFI_HANDLE_NAME_TYPE ( &efi_device_path_protocol_guid,
414
+			       efi_devpath_text ),
415
+	/* Driver name (for driver image handles) */
416
+	EFI_HANDLE_NAME_TYPE ( &efi_component_name2_protocol_guid,
417
+			       efi_driver_name ),
418
+	/* PE/COFF debug filename (for image handles) */
419
+	EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
420
+			       efi_pecoff_debug_name ),
421
+	/* Loaded image device path (for image handles) */
422
+	EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_device_path_protocol_guid,
423
+			       efi_devpath_text ),
424
+	/* First loaded image name (for the DxeCore image) */
425
+	EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
426
+			       efi_first_loaded_image_name ),
427
+	/* Handle's loaded image file path (for image handles) */
428
+	EFI_HANDLE_NAME_TYPE ( &efi_loaded_image_protocol_guid,
429
+			       efi_loaded_image_filepath_name ),
430
+};
431
+
432
+/**
433
+ * Get name of an EFI handle
434
+ *
435
+ * @v handle		EFI handle
436
+ * @ret text		Name of handle, or NULL
437
+ */
438
+const char * efi_handle_name ( EFI_HANDLE handle ) {
439
+	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
440
+	struct efi_handle_name_type *type;
441
+	unsigned int i;
442
+	void *interface;
443
+	const char *name;
444
+	EFI_STATUS efirc;
445
+
446
+	/* Fail immediately for NULL handles */
447
+	if ( ! handle )
448
+		return NULL;
449
+
450
+	/* Try each name type in turn */
451
+	for ( i = 0 ; i < ( sizeof ( efi_handle_name_types ) /
452
+			    sizeof ( efi_handle_name_types[0] ) ) ; i++ ) {
453
+		type = &efi_handle_name_types[i];
454
+
455
+		/* Try to open the applicable protocol */
456
+		efirc = bs->OpenProtocol ( handle, type->protocol, &interface,
457
+					   efi_image_handle, handle,
458
+					   EFI_OPEN_PROTOCOL_GET_PROTOCOL );
459
+		if ( efirc != 0 )
460
+			continue;
461
+
462
+		/* Try to get name from this protocol */
463
+		name = type->name ( interface );
464
+
465
+		/* Close protocol */
466
+		bs->CloseProtocol ( handle, type->protocol,
467
+				    efi_image_handle, handle );
468
+
469
+		/* Use this name, if possible */
470
+		if ( name && name[0] )
471
+			return name;
472
+	}
473
+
474
+	return "UNKNOWN";
475
+}
476
+
242 477
 /**
243 478
  * Get textual representation of device path for a handle
244 479
  *

Loading…
취소
저장