[edk2-devel] [PATCH v3] UefiPayloadPkg: Add support for logging to CBMEM console
Writes TianoCore debug logs into the CBMEM console ringbuffer, from where the user can retrieve them with the `cbmem` userspace utility. The intention is to aid in debugging non-fatal issues even in release builds, or simply make TianoCore's logs available to those interested. Consequently, MDEPKG_NDEBUG must be masked. As an in-memory debug logging library, ASSERTs must be non-fatal to be seen, so they neither dead-loop nor create a breakpoint. It is assumed that ASSERT() neither enforces fatal conditions nor security integrity, as release builds do not call DebugAssert() from the ASSERT macro. More detailed debug logs are produced with the DEBUG_CODE macro, but this guards other debug-related code throughout the codebase. To avoid changing behaviour on release builds, this is only set for debug builds. Tested on QEMU, dumping the appropriate memory region in the UEFI shell shows the TianoCore log. An improved revision of the debug library used in several coreboot-related EDK2 forks, including MrChromebox's. Previous revisions also tested on an Acer Aspire VN7-572G laptop. Cc: Guo Dong Cc: Ray Ni Cc: Maurice Ma Cc: Benjamin You Cc: Sean Rhodes Signed-off-by: Benjamin Doron --- UefiPayloadPkg/Include/Coreboot.h | 13 + UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c | 264 UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf | 30 +++ UefiPayloadPkg/UefiPayloadPkg.dsc | 25 +- 4 files changed, 327 insertions(+), 5 deletions(-) diff --git a/UefiPayloadPkg/Include/Coreboot.h b/UefiPayloadPkg/Include/Coreboot.h index a3e1109fe84e..5892fae7682b 100644 --- a/UefiPayloadPkg/Include/Coreboot.h +++ b/UefiPayloadPkg/Include/Coreboot.h @@ -199,6 +199,13 @@ struct cb_forward { UINT64forward; }; +struct cb_cbmem_ref { + UINT32tag; + // Field contains size of this struct = 0x0010 + UINT32size; + UINT64cbmem_addr; +}; + #define CB_TAG_FRAMEBUFFER 0x0012 struct cb_framebuffer { UINT32tag; @@ -229,6 +236,12 @@ struct cb_vdat { #define CB_TAG_TIMESTAMPS 0x0016 #define CB_TAG_CBMEM_CONSOLE 0x0017 +struct cbmem_console { + UINT32 size; + UINT32 cursor; + UINT8 body[0]; +} __attribute__((packed)); + #define CB_TAG_MRC_CACHE 0x0018 struct cb_cbmem_tab { UINT32tag; diff --git a/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c new file mode 100644 index ..863fbcfef2f0 --- /dev/null +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c @@ -0,0 +1,264 @@ +/** @file + CBMEM console SerialPortLib instance + + Copyright (c) 2022, Baruch Binyamin Doron + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include +#include +#include + +// Upper nibble contains flags +#define CBMC_CURSOR_MASK ((1 << 28) - 1) +#define CBMC_OVERFLOW (1 << 31) + +STATIC struct cbmem_console *mCbConsole = NULL; + +/** + Find coreboot record with given Tag. + NOTE: This coreboot-specific function definition is absent + from the common BlParseLib header. + + @param TagThe tag id to be found + + @retval NULL The Tag is not found. + @retval OthersThe pointer to the record found. + +**/ +VOID * +FindCbTag ( + IN UINT32 Tag + ); + +/** + Initialize the serial device hardware. + + If no initialization is required, then return RETURN_SUCCESS. + If the serial device was successfully initialized, then return RETURN_SUCCESS. + If the serial device could not be initialized, then return RETURN_DEVICE_ERROR. + + @retval RETURN_SUCCESSThe serial device was initialized. + @retval RETURN_DEVICE_ERROR The serial device could not be initialized. + +**/ +RETURN_STATUS +EFIAPI +SerialPortInitialize ( + VOID + ) +{ + // The coreboot table contains large structures as references + struct cb_cbmem_ref *cbref = FindCbTag(CB_TAG_CBMEM_CONSOLE); + if (!cbref) { +return RETURN_DEVICE_ERROR; + } + + mCbConsole = (VOID *)(UINTN)cbref->cbmem_addr; // Support PEI and DXE + if (mCbConsole == NULL) { +return RETURN_DEVICE_ERROR; + } + + return RETURN_SUCCESS; +} + +/** + Write data from buffer to serial device. + + Writes NumberOfBytes data bytes from Buffer to the serial device. + The number of bytes actually written to the serial device is returned. + If the return value is less than NumberOfBytes, then the write operation failed. + If Buffer is NULL, then ASSERT(). + If NumberOfBytes is zero, then return 0. + + @param Buffer Pointer to the data buffer to be written. + @param NumberOfBytesNumber of bytes to written to the serial device. + + @retval 0NumberOfBytes is 0. + @retval >0 The number of bytes written to the serial device. + If this value is less than NumberOfBytes, then the write operation failed. + +**/ +UIN
Re: [edk2-devel] [PATCH v3] UefiPayloadPkg: Add support for logging to CBMEM console
v3: - Rename gCbConsole to mCbConsole - Add comments about the cb_cbmem_ref structure in coreboot table - Ensure that no single debug message could overflow a very small buffer - Set overflow flag preemptively in the event that the cursor must be zeroed after a message reaches the tail of the buffer -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#90215): https://edk2.groups.io/g/devel/message/90215 Mute This Topic: https://groups.io/mt/91568694/21656 Group Owner: devel+ow...@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com] -=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH v3] UefiPayloadPkg: Add support for logging to CBMEM console
Reviewed-by: Lean Sheng Tan On Mon, 6 Jun 2022 at 02:56, Benjamin Doron wrote: > Writes TianoCore debug logs into the CBMEM console ringbuffer, from > where the user can retrieve them with the `cbmem` userspace utility. > > The intention is to aid in debugging non-fatal issues even in release > builds, or simply make TianoCore's logs available to those interested. > Consequently, MDEPKG_NDEBUG must be masked. As an in-memory debug > logging library, ASSERTs must be non-fatal to be seen, so they neither > dead-loop nor create a breakpoint. It is assumed that ASSERT() neither > enforces fatal conditions nor security integrity, as release builds do > not call DebugAssert() from the ASSERT macro. > > More detailed debug logs are produced with the DEBUG_CODE macro, but > this guards other debug-related code throughout the codebase. To avoid > changing behaviour on release builds, this is only set for debug builds. > > Tested on QEMU, dumping the appropriate memory region in the UEFI shell > shows the TianoCore log. An improved revision of the debug library used > in several coreboot-related EDK2 forks, including MrChromebox's. > Previous revisions also tested on an Acer Aspire VN7-572G laptop. > > Cc: Guo Dong > Cc: Ray Ni > Cc: Maurice Ma > Cc: Benjamin You > Cc: Sean Rhodes > Signed-off-by: Benjamin Doron > --- > UefiPayloadPkg/Include/Coreboot.h | 13 + > UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c | 264 > > UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf | 30 +++ > UefiPayloadPkg/UefiPayloadPkg.dsc | 25 +- > 4 files changed, 327 insertions(+), 5 deletions(-) > > diff --git a/UefiPayloadPkg/Include/Coreboot.h > b/UefiPayloadPkg/Include/Coreboot.h > index a3e1109fe84e..5892fae7682b 100644 > --- a/UefiPayloadPkg/Include/Coreboot.h > +++ b/UefiPayloadPkg/Include/Coreboot.h > @@ -199,6 +199,13 @@ struct cb_forward { >UINT64forward; > }; > > +struct cb_cbmem_ref { > + UINT32tag; > + // Field contains size of this struct = 0x0010 > + UINT32size; > + UINT64cbmem_addr; > +}; > + > #define CB_TAG_FRAMEBUFFER 0x0012 > struct cb_framebuffer { >UINT32tag; > @@ -229,6 +236,12 @@ struct cb_vdat { > > #define CB_TAG_TIMESTAMPS 0x0016 > #define CB_TAG_CBMEM_CONSOLE 0x0017 > +struct cbmem_console { > + UINT32 size; > + UINT32 cursor; > + UINT8 body[0]; > +} __attribute__((packed)); > + > #define CB_TAG_MRC_CACHE 0x0018 > struct cb_cbmem_tab { >UINT32tag; > diff --git a/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c > b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c > new file mode 100644 > index ..863fbcfef2f0 > --- /dev/null > +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c > @@ -0,0 +1,264 @@ > +/** @file > + CBMEM console SerialPortLib instance > + > + Copyright (c) 2022, Baruch Binyamin Doron > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > +#include > + > +#include > +#include > +#include > + > +// Upper nibble contains flags > +#define CBMC_CURSOR_MASK ((1 << 28) - 1) > +#define CBMC_OVERFLOW (1 << 31) > + > +STATIC struct cbmem_console *mCbConsole = NULL; > + > +/** > + Find coreboot record with given Tag. > + NOTE: This coreboot-specific function definition is absent > + from the common BlParseLib header. > + > + @param TagThe tag id to be found > + > + @retval NULL The Tag is not found. > + @retval OthersThe pointer to the record found. > + > +**/ > +VOID * > +FindCbTag ( > + IN UINT32 Tag > + ); > + > +/** > + Initialize the serial device hardware. > + > + If no initialization is required, then return RETURN_SUCCESS. > + If the serial device was successfully initialized, then return > RETURN_SUCCESS. > + If the serial device could not be initialized, then return > RETURN_DEVICE_ERROR. > + > + @retval RETURN_SUCCESSThe serial device was initialized. > + @retval RETURN_DEVICE_ERROR The serial device could not be > initialized. > + > +**/ > +RETURN_STATUS > +EFIAPI > +SerialPortInitialize ( > + VOID > + ) > +{ > + // The coreboot table contains large structures as references > + struct cb_cbmem_ref *cbref = FindCbTag(CB_TAG_CBMEM_CONSOLE); > + if (!cbref) { > +return RETURN_DEVICE_ERROR; > + } > + > + mCbConsole = (VOID *)(UINTN)cbref->cbmem_addr; // Support PEI and DXE > + if (mCbConsole == NULL) { > +return RETURN_DEVICE_ERROR; > + } > + > + return RETURN_SUCCESS; > +} > + > +/** > + Write data from buffer to serial device. > + > + Writes NumberOfBytes data bytes from Buffer to the serial device. > + The number of bytes actually written to the serial device is returned. > + If the return value is less than NumberOfBytes, then the write > operation failed. > + If Buffer is NULL, then ASSERT(). > + If NumberOfBytes is zero, then return 0. > + > + @param
Re: [edk2-devel] [PATCH v3] UefiPayloadPkg: Add support for logging to CBMEM console
Reviewed-by: Sean Rhodes On Wed, 8 Jun 2022 at 16:05, Lean Sheng Tan wrote: > Reviewed-by: Lean Sheng Tan > > > > On Mon, 6 Jun 2022 at 02:56, Benjamin Doron > wrote: > >> Writes TianoCore debug logs into the CBMEM console ringbuffer, from >> where the user can retrieve them with the `cbmem` userspace utility. >> >> The intention is to aid in debugging non-fatal issues even in release >> builds, or simply make TianoCore's logs available to those interested. >> Consequently, MDEPKG_NDEBUG must be masked. As an in-memory debug >> logging library, ASSERTs must be non-fatal to be seen, so they neither >> dead-loop nor create a breakpoint. It is assumed that ASSERT() neither >> enforces fatal conditions nor security integrity, as release builds do >> not call DebugAssert() from the ASSERT macro. >> >> More detailed debug logs are produced with the DEBUG_CODE macro, but >> this guards other debug-related code throughout the codebase. To avoid >> changing behaviour on release builds, this is only set for debug builds. >> >> Tested on QEMU, dumping the appropriate memory region in the UEFI shell >> shows the TianoCore log. An improved revision of the debug library used >> in several coreboot-related EDK2 forks, including MrChromebox's. >> Previous revisions also tested on an Acer Aspire VN7-572G laptop. >> >> Cc: Guo Dong >> Cc: Ray Ni >> Cc: Maurice Ma >> Cc: Benjamin You >> Cc: Sean Rhodes >> Signed-off-by: Benjamin Doron >> --- >> UefiPayloadPkg/Include/Coreboot.h | 13 + >> UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c | 264 >> >> UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf | 30 +++ >> UefiPayloadPkg/UefiPayloadPkg.dsc | 25 +- >> 4 files changed, 327 insertions(+), 5 deletions(-) >> >> diff --git a/UefiPayloadPkg/Include/Coreboot.h >> b/UefiPayloadPkg/Include/Coreboot.h >> index a3e1109fe84e..5892fae7682b 100644 >> --- a/UefiPayloadPkg/Include/Coreboot.h >> +++ b/UefiPayloadPkg/Include/Coreboot.h >> @@ -199,6 +199,13 @@ struct cb_forward { >>UINT64forward; >> }; >> >> +struct cb_cbmem_ref { >> + UINT32tag; >> + // Field contains size of this struct = 0x0010 >> + UINT32size; >> + UINT64cbmem_addr; >> +}; >> + >> #define CB_TAG_FRAMEBUFFER 0x0012 >> struct cb_framebuffer { >>UINT32tag; >> @@ -229,6 +236,12 @@ struct cb_vdat { >> >> #define CB_TAG_TIMESTAMPS 0x0016 >> #define CB_TAG_CBMEM_CONSOLE 0x0017 >> +struct cbmem_console { >> + UINT32 size; >> + UINT32 cursor; >> + UINT8 body[0]; >> +} __attribute__((packed)); >> + >> #define CB_TAG_MRC_CACHE 0x0018 >> struct cb_cbmem_tab { >>UINT32tag; >> diff --git a/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c >> b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c >> new file mode 100644 >> index ..863fbcfef2f0 >> --- /dev/null >> +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c >> @@ -0,0 +1,264 @@ >> +/** @file >> + CBMEM console SerialPortLib instance >> + >> + Copyright (c) 2022, Baruch Binyamin Doron >> + SPDX-License-Identifier: BSD-2-Clause-Patent >> + >> +**/ >> + >> +#include >> +#include >> + >> +#include >> +#include >> +#include >> + >> +// Upper nibble contains flags >> +#define CBMC_CURSOR_MASK ((1 << 28) - 1) >> +#define CBMC_OVERFLOW (1 << 31) >> + >> +STATIC struct cbmem_console *mCbConsole = NULL; >> + >> +/** >> + Find coreboot record with given Tag. >> + NOTE: This coreboot-specific function definition is absent >> + from the common BlParseLib header. >> + >> + @param TagThe tag id to be found >> + >> + @retval NULL The Tag is not found. >> + @retval OthersThe pointer to the record found. >> + >> +**/ >> +VOID * >> +FindCbTag ( >> + IN UINT32 Tag >> + ); >> + >> +/** >> + Initialize the serial device hardware. >> + >> + If no initialization is required, then return RETURN_SUCCESS. >> + If the serial device was successfully initialized, then return >> RETURN_SUCCESS. >> + If the serial device could not be initialized, then return >> RETURN_DEVICE_ERROR. >> + >> + @retval RETURN_SUCCESSThe serial device was initialized. >> + @retval RETURN_DEVICE_ERROR The serial device could not be >> initialized. >> + >> +**/ >> +RETURN_STATUS >> +EFIAPI >> +SerialPortInitialize ( >> + VOID >> + ) >> +{ >> + // The coreboot table contains large structures as references >> + struct cb_cbmem_ref *cbref = FindCbTag(CB_TAG_CBMEM_CONSOLE); >> + if (!cbref) { >> +return RETURN_DEVICE_ERROR; >> + } >> + >> + mCbConsole = (VOID *)(UINTN)cbref->cbmem_addr; // Support PEI and DXE >> + if (mCbConsole == NULL) { >> +return RETURN_DEVICE_ERROR; >> + } >> + >> + return RETURN_SUCCESS; >> +} >> + >> +/** >> + Write data from buffer to serial device. >> + >> + Writes NumberOfBytes data bytes from Buffer to the serial device. >> + The number of bytes a
Re: [edk2-devel] [PATCH v3] UefiPayloadPkg: Add support for logging to CBMEM console
Reviewed-by: Guo Dong -Original Message- From: Benjamin Doron Sent: Sunday, June 5, 2022 5:55 PM To: devel@edk2.groups.io Cc: Dong, Guo ; Ni, Ray ; Maurice Ma ; You, Benjamin ; Rhodes, Sean Subject: [PATCH v3] UefiPayloadPkg: Add support for logging to CBMEM console Writes TianoCore debug logs into the CBMEM console ringbuffer, from where the user can retrieve them with the `cbmem` userspace utility. The intention is to aid in debugging non-fatal issues even in release builds, or simply make TianoCore's logs available to those interested. Consequently, MDEPKG_NDEBUG must be masked. As an in-memory debug logging library, ASSERTs must be non-fatal to be seen, so they neither dead-loop nor create a breakpoint. It is assumed that ASSERT() neither enforces fatal conditions nor security integrity, as release builds do not call DebugAssert() from the ASSERT macro. More detailed debug logs are produced with the DEBUG_CODE macro, but this guards other debug-related code throughout the codebase. To avoid changing behaviour on release builds, this is only set for debug builds. Tested on QEMU, dumping the appropriate memory region in the UEFI shell shows the TianoCore log. An improved revision of the debug library used in several coreboot-related EDK2 forks, including MrChromebox's. Previous revisions also tested on an Acer Aspire VN7-572G laptop. Cc: Guo Dong Cc: Ray Ni Cc: Maurice Ma Cc: Benjamin You Cc: Sean Rhodes Signed-off-by: Benjamin Doron --- UefiPayloadPkg/Include/Coreboot.h | 13 + UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c | 264 UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf | 30 +++ UefiPayloadPkg/UefiPayloadPkg.dsc | 25 +- 4 files changed, 327 insertions(+), 5 deletions(-) diff --git a/UefiPayloadPkg/Include/Coreboot.h b/UefiPayloadPkg/Include/Coreboot.h index a3e1109fe84e..5892fae7682b 100644 --- a/UefiPayloadPkg/Include/Coreboot.h +++ b/UefiPayloadPkg/Include/Coreboot.h @@ -199,6 +199,13 @@ struct cb_forward { UINT64forward; }; +struct cb_cbmem_ref {+ UINT32tag;+ // Field contains size of this struct = 0x0010+ UINT32size;+ UINT64 cbmem_addr;+};+ #define CB_TAG_FRAMEBUFFER 0x0012 struct cb_framebuffer { UINT32tag;@@ -229,6 +236,12 @@ struct cb_vdat { #define CB_TAG_TIMESTAMPS 0x0016 #define CB_TAG_CBMEM_CONSOLE 0x0017+struct cbmem_console {+ UINT32 size;+ UINT32 cursor;+ UINT8 body[0];+} __attribute__((packed));+ #define CB_TAG_MRC_CACHE 0x0018 struct cb_cbmem_tab { UINT32tag;diff --git a/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c new file mode 100644 index ..863fbcfef2f0 --- /dev/null +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c @@ -0,0 +1,264 @@ +/** @file+ CBMEM console SerialPortLib instance++ Copyright (c) 2022, Baruch Binyamin Doron+ SPDX-License-Identifier: BSD-2-Clause-Patent++**/++#include +#include ++#include +#include +#include ++// Upper nibble contains flags+#define CBMC_CURSOR_MASK ((1 << 28) - 1)+#define CBMC_OVERFLOW (1 << 31)++STATIC struct cbmem_console *mCbConsole = NULL;++/**+ Find coreboot record with given Tag.+ NOTE: This coreboot-specific function definition is absent+ from the common BlParseLib header.++ @param Tag The tag id to be found++ @retval NULL The Tag is not found.+ @retval OthersThe pointer to the record found.++**/+VOID *+FindCbTag (+ IN UINT32 Tag+ );++/**+ Initialize the serial device hardware.++ If no initialization is required, then return RETURN_SUCCESS.+ If the serial device was successfully initialized, then return RETURN_SUCCESS.+ If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.++ @retval RETURN_SUCCESSThe serial device was initialized.+ @retval RETURN_DEVICE_ERROR The serial device could not be initialized.++**/+RETURN_STATUS+EFIAPI+SerialPortInitialize (+ VOID+ )+{+ // The coreboot table contains large structures as references+ struct cb_cbmem_ref *cbref = FindCbTag(CB_TAG_CBMEM_CONSOLE);+ if (!cbref) {+ return RETURN_DEVICE_ERROR;+ }++ mCbConsole = (VOID *)(UINTN)cbref->cbmem_addr; // Support PEI and DXE+ if (mCbConsole == NULL) {+return RETURN_DEVICE_ERROR;+ }++ return RETURN_SUCCESS;+}++/**+ Write data from buffer to serial device.++ Writes NumberOfBytes data bytes from Buffer to the serial device.+ The number of bytes actually written to the serial device is returned.+ If the return value is less than NumberOfBytes, then the write operation failed.+ If Buffer is NULL, then ASSERT().+ If NumberOfBytes is zero, then return 0.++ @param Buffer Pointer to the data buffer to be written.+ @param NumberOfBytesNumber of bytes to written to the serial device.++ @retval 0