efi: add ovmf debug log driver
PATCH 1/1 efi: add ovmf debug log driver
Recent OVMF versions (edk2-stable202508 + newer) can write their debug log to a memory buffer. This driver exposes the log content via sysfs (/sys/firmware/efi/ovmf_debug_log).
--- include/linux/efi.h | 2 + drivers/firmware/efi/efi.c | 11 +++ drivers/firmware/efi/ovmf-log.c | 136 ++++++++++++++++++++++++++++++++ drivers/firmware/efi/Kconfig | 9 +++ drivers/firmware/efi/Makefile | 1 + 5 files changed, 159 insertions(+) create mode 100644 drivers/firmware/efi/ovmf-log.c
include/linux/efi.h +2 -0
@@ -439,6 +439,7 @@ void efi_native_runtime_setup(void);/* OVMF protocol GUIDs */ #define OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID EFI_GUID(0xc5a010fe, 0x38a7, 0x4531, 0x8a, 0x4a, 0x05, 0x00, 0xd2, 0xfd, 0x16, 0x49) +#define OVMF_MEMORY_LOG_TABLE_GUID EFI_GUID(0x95305139, 0xb20f, 0x4723, 0x84, 0x25, 0x62, 0x7c, 0x88, 0x8f, 0xf1, 0x21) typedef struct { efi_guid_t guid;
@@ -642,6 +643,7 @@ extern struct efi {unsigned long esrt;/* ESRT table */ unsigned long tpm_log;/* TPM2 Event Log table */ unsigned long tpm_final_log;/* TPM2 Final Events Log table */ +unsigned long ovmf_debug_log;unsigned long mokvar_table;/* MOK variable config table */ unsigned long coco_secret;/* Confidential computing secret table */ unsigned long unaccepted;/* Unaccepted memory table */
drivers/firmware/efi/efi.c +11 -0
@@ -45,6 +45,9 @@ struct efi __read_mostly efi = { .esrt = EFI_INVALID_TABLE_ADDR, .tpm_log = EFI_INVALID_TABLE_ADDR, .tpm_final_log = EFI_INVALID_TABLE_ADDR, +#ifdef CONFIG_OVMF_DEBUG_LOG + .ovmf_debug_log = EFI_INVALID_TABLE_ADDR, +#endif #ifdef CONFIG_LOAD_UEFI_KEYS .mokvar_table = EFI_INVALID_TABLE_ADDR,#endif
@@ -473,6 +476,11 @@ static int __init efisubsys_init(void)platform_device_register_simple ("efi_secret" ,0 ,NULL ,0 );#endif +#ifdef CONFIG_OVMF_DEBUG_LOG +if (efi.ovmf_debug_log !=EFI_INVALID_TABLE_ADDR ) +platform_device_register_simple ("ovmf_log" ,0 ,NULL ,0 ); +#endif +return 0 ; err_remove_group:
@@ -617,6 +625,9 @@ static const efi_config_table_type_t common_tables[] __initconst = { {LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" }, {LINUX_EFI_INITRD_MEDIA_GUID, &initrd, "INITRD" }, {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" }, +#ifdef CONFIG_OVMF_DEBUG_LOG + {OVMF_MEMORY_LOG_TABLE_GUID, &efi.ovmf_debug_log, "OVMF-LOG" }, +#endif #ifdef CONFIG_EFI_RCI2_TABLE {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys }, #endif
drivers/firmware/efi/ovmf-log.c new file +136 -0
136 lines not on this page · raw message
drivers/firmware/efi/Kconfig +9 -0
@@ -263,6 +263,15 @@ config EFI_COCO_SECRET virt/coco/efi_secret module to access the secrets, which in turn allows userspace programs to access the injected secrets. +config OVMF_DEBUG_LOG + tristate "Expose OVMF firmware debug log via sysfs" + depends on EFI + default y + help + Recent OVMF versions (edk2-stable202508 + newer) can write + their debug log to a memory buffer. This driver exposes the + log content via sysfs (/sys/firmware/efi/ovmf_debug_log). + config UNACCEPTED_MEMORY bool depends on EFI_STUB
drivers/firmware/efi/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-$(CONFIG_APPLE_PROPERTIES) += apple-properties.o obj-$(CONFIG_EFI_RCI2_TABLE) += rci2-table.o obj-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += embedded-firmware.o obj-$(CONFIG_LOAD_UEFI_KEYS) += mokvar-table.o +obj-$(CONFIG_OVMF_DEBUG_LOG) += ovmf-log.o obj-$(CONFIG_SYSFB) += sysfb_efi.o
Signature · 1 line
2.50.0
Ard Biesheuvel
maintainerThanks for providing implementations for both sides of this interface
Quoted 20 lines · Gerd Hoffmann
Recent OVMF versions (edk2-stable202508 + newer) can write their debug log to a memory buffer. This driver exposes the log content via sysfs (
/sys/firmware/efi/ovmf_debug_log).Signed-off-by Gerd Hoffmann <[email protected]>--- include/linux/efi.h | 2 + drivers/firmware/efi/efi.c | 11 +++ drivers/firmware/efi/ovmf-log.c | 136 ++++++++++++++++++++++++++++++++ drivers/firmware/efi/Kconfig | 9 +++ drivers/firmware/efi/Makefile | 1 + 5 files changed, 159 insertions(+) create mode 100644 drivers/firmware/efi/ovmf-log.cinclude/linux/efi.h +0
-0
include/linux/efi.h void efi_native_runtime_setup(void); · line 443
Quoted 5 lines
include/linux/efi.h +1
-0@@ -439,6 +439,7 @@ void efi_native_runtime_setup(void);/* OVMF protocol GUIDs */ #define OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID EFI_GUID(0xc5a010fe, 0x38a7, 0x4531, 0x8a, 0x4a, 0x05, 0x00, 0xd2, 0xfd, 0x16, 0x49) +#define OVMF_MEMORY_LOG_TABLE_GUID EFI_GUID(0x95305139, 0xb20f, 0x4723, 0x84, 0x25, 0x62, 0x7c, 0x88, 0x8f, 0xf1, 0x21)
Please make the indentation consistent with the preceding entries
Quoted 15 lines
include/linux/efi.h +1
-0typedef struct { efi_guid_t guid;@@ -642,6 +643,7 @@ extern struct efi {unsigned long esrt;/* ESRT table */ unsigned long tpm_log;/* TPM2 Event Log table */ unsigned long tpm_final_log;/* TPM2 Final Events Log table */ +unsigned long ovmf_debug_log;unsigned long mokvar_table;/* MOK variable config table */ unsigned long coco_secret;/* Confidential computing secret table */ unsigned long unaccepted;/* Unaccepted memory table */ drivers/firmware/efi/efi.c +0
-0
drivers/firmware/efi/efi.c struct efi __read_mostly efi = { · line 50
Quoted 6 lines
drivers/firmware/efi/efi.c +3
-0@@ -45,6 +45,9 @@ struct efi __read_mostly efi = { .esrt = EFI_INVALID_TABLE_ADDR, .tpm_log = EFI_INVALID_TABLE_ADDR, .tpm_final_log = EFI_INVALID_TABLE_ADDR, +#ifdef CONFIG_OVMF_DEBUG_LOG + .ovmf_debug_log = EFI_INVALID_TABLE_ADDR, +#endif
You can drop the #ifdef here, given that the struct member is always declared.
drivers/firmware/efi/efi.c static int __init efisubsys_init(void) · line 479
Quoted 8 lines
drivers/firmware/efi/efi.c +1
-0#ifdef CONFIG_LOAD_UEFI_KEYS .mokvar_table = EFI_INVALID_TABLE_ADDR,#endif @@ -473,6 +476,11 @@ static int __init efisubsys_init(void)platform_device_register_simple ("efi_secret" ,0 ,NULL ,0 );#endif +#ifdef CONFIG_OVMF_DEBUG_LOG
... and so you can use if (IS_ENABLED(CONFIG_OVMF_DEBUG_LOG) && ... here
drivers/firmware/efi/efi.c static int __init efisubsys_init(void) · line 481
Quoted 2 lines
drivers/firmware/efi/efi.c +2
-0+if (efi.ovmf_debug_log !=EFI_INVALID_TABLE_ADDR ) +platform_device_register_simple ("ovmf_log" ,0 ,NULL ,0 );
Use 'ovmf_debug_log' for consistency
drivers/firmware/efi/ovmf-log.c
Quoted 20 lines
drivers/firmware/efi/efi.c +5
-0+#endif +return 0 ; err_remove_group:@@ -617,6 +625,9 @@ static const efi_config_table_type_t common_tables[] __initconst = { {LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" }, {LINUX_EFI_INITRD_MEDIA_GUID, &initrd, "INITRD" }, {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" }, +#ifdef CONFIG_OVMF_DEBUG_LOG + {OVMF_MEMORY_LOG_TABLE_GUID, &efi.ovmf_debug_log, "OVMF-LOG" }, +#endif #ifdef CONFIG_EFI_RCI2_TABLE {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys }, #endifdrivers/firmware/efi/ovmf-log.c new file +0
-0
ovmf-debug-log.c
Quoted 5 lines
drivers/firmware/efi/ovmf-log.c new file +4
-0@@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/efi.h> +#include <linux/init.h>
drivers/firmware/efi/ovmf-log.c line 14
Quoted 10 lines
drivers/firmware/efi/ovmf-log.c new file +10
-0+#include <linux/io.h> +#include <linux/kernel.h> +#include <linux/kobject.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/sysfs.h> + +#define MEM_DEBUG_LOG_MAGIC1 0x3167646d666d766f // "ovmfmdg1" +#define MEM_DEBUG_LOG_MAGIC2 0x3267646d666d766f // "ovmfmdg2" +
OVMF_DEBUG_LOG_MAGICn
drivers/firmware/efi/ovmf-log.c line 15
Quoted 1 line
drivers/firmware/efi/ovmf-log.c new file +1
-0+struct mem_debug_log_header {
ovmf_debug_log_header
Quoted 68 lines
drivers/firmware/efi/ovmf-log.c new file +68
-0+u64 magic1; +u64 magic2; +u64 hdr_size; +u64 log_size; +u64 lock;// edk2 spinlock +u64 head_off; +u64 tail_off; +u64 truncated; +u8 fw_version[128 ]; +}; + +static struct mem_debug_log_header *hdr; +static u8 *logbuf; +static u64 logbufsize; + +static ssize_t ovmf_log_read (struct file *filp,struct kobject *kobj, +const struct bin_attribute *attr,char *buf, +loff_t offset,size_t count) +{ +u64 start, end; + + start = hdr->head_off + offset; +if (hdr->head_off > hdr->tail_off && start >= hdr->log_size) + start -= hdr->log_size; + + end = start + count; +if (start > hdr->tail_off) { +if (end > hdr->log_size) + end = hdr->log_size; + }else { +if (end > hdr->tail_off) + end = hdr->tail_off; + } + +if (start > logbufsize || end > logbufsize) +return 0 ; +if (start >= end) +return 0 ; + +memcpy (buf, logbuf + start, end - start); +return end - start; +} + +static struct bin_attribute ovmf_log_bin_attr = { + .attr = { + .name ="ovmf_debug_log" , + .mode =0444 , + }, + .read = ovmf_log_read, +}; + +static int ovmf_log_probe (struct platform_device *dev) +{ +u64 size; +int ret = -EINVAL ; + +if (efi.ovmf_debug_log ==EFI_INVALID_TABLE_ADDR ) { +dev_err (&dev->dev,"OVMF debug log: not available\n" ); +return -EINVAL ; + } + +/* map + verify header */ + hdr =memremap (efi.ovmf_debug_log,sizeof (*hdr),MEMREMAP_WB ); +if (!hdr) { +dev_err (&dev->dev,"OVMF debug log: header map failed\n" ); +return -EINVAL ; + } +
drivers/firmware/efi/ovmf-log.c line 93
Quoted 10 lines
drivers/firmware/efi/ovmf-log.c new file +10
-0+if (hdr->magic1 !=MEM_DEBUG_LOG_MAGIC1 || + hdr->magic2 !=MEM_DEBUG_LOG_MAGIC2 ) { +dev_err (&dev->dev,"OVMF debug log: magic mismatch\n" ); + goto err_unmap; + } + + size = hdr->hdr_size + hdr->log_size; +dev_info (&dev->dev,"firmware version: \"%s\"\n" , hdr->fw_version); +dev_info (&dev->dev,"log buffer size: %lldk\n" , size /1024 ); +
llu
drivers/firmware/efi/ovmf-log.c line 95
Quoted 2 lines
drivers/firmware/efi/ovmf-log.c new file +2
-0+/* map complete log buffer */ +iounmap (hdr);
memunmap()
drivers/firmware/efi/ovmf-log.c line 101
Quoted 6 lines
drivers/firmware/efi/ovmf-log.c new file +6
-0+ hdr =memremap (efi.ovmf_debug_log, size,MEMREMAP_WB ); +if (!hdr) { +dev_err (&dev->dev,"OVMF debug log: buffer map failed\n" ); +return -EINVAL ; + } + logbuf = (void *)hdr + hdr->hdr_size;
space between void and *
drivers/firmware/efi/ovmf-log.c line 114
Quoted 13 lines
drivers/firmware/efi/ovmf-log.c new file +13
-0+ logbufsize = hdr->log_size; + + ovmf_log_bin_attr.size = size; + ret =sysfs_create_bin_file (efi_kobj, &ovmf_log_bin_attr); +if (ret !=0 ) { +dev_err (&dev->dev,"OVMF debug log: sysfs register failed\n" ); + goto err_unmap; + } + +return 0 ; + +err_unmap: +iounmap (hdr);
memunmap
drivers/firmware/efi/ovmf-log.c line 120
Quoted 6 lines
drivers/firmware/efi/ovmf-log.c new file +6
-0+return ret; +} + +static void ovmf_log_remove (struct platform_device *dev) +{ +iounmap (hdr);
memunmap
Quoted 21 lines
drivers/firmware/efi/ovmf-log.c new file +16
-0+} + +static struct platform_driver ovmf_log_driver = { + .probe = ovmf_log_probe, + .remove = ovmf_log_remove, + .driver = { + .name ="ovmf_log" , + }, +}; + +module_platform_driver (ovmf_log_driver); + +MODULE_DESCRIPTION ("OVMF debug log" ); +MODULE_AUTHOR ("Gerd Hoffmann <[email protected]>" ); +MODULE_LICENSE ("GPL" ); +MODULE_ALIAS ("platform:ovmf_log" );drivers/firmware/efi/Kconfig +0
-0
drivers/firmware/efi/Kconfig config EFI_COCO_SECRET · line 269
Quoted 7 lines
drivers/firmware/efi/Kconfig +4
-0@@ -263,6 +263,15 @@ config EFI_COCO_SECRET virt/coco/efi_secret module to access the secrets, which in turn allows userspace programs to access the injected secrets. +config OVMF_DEBUG_LOG + tristate "Expose OVMF firmware debug log via sysfs" + depends on EFI + default y
Please drop this - new features are never on by default.
Quoted 24 lines
drivers/firmware/efi/Kconfig +5
-0+ help + Recent OVMF versions (edk2-stable202508 + newer) can write + their debug log to a memory buffer. This driver exposes the + log content via sysfs (/sys/firmware/efi/ovmf_debug_log). + config UNACCEPTED_MEMORY bool depends on EFI_STUBdrivers/firmware/efi/Makefile +1
-1@@ -29,6 +29,7 @@ obj-$(CONFIG_APPLE_PROPERTIES) += apple-properties.o obj-$(CONFIG_EFI_RCI2_TABLE) += rci2-table.o obj-$(CONFIG_EFI_EMBEDDED_FIRMWARE) += embedded-firmware.o obj-$(CONFIG_LOAD_UEFI_KEYS) += mokvar-table.o +obj-$(CONFIG_OVMF_DEBUG_LOG) += ovmf-log.o obj-$(CONFIG_SYSFB) += sysfb_efi.o --2.50.0