Re: [edk2-devel] [PATCH v4] UefiPayloadPkg: Add support for logging to CBMEM console

2022-07-07 Thread Sheng Lean Tan
Reviewed-by: Lean Sheng Tan


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#91144): https://edk2.groups.io/g/devel/message/91144
Mute This Topic: https://groups.io/mt/91945631/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH v4] UefiPayloadPkg: Add support for logging to CBMEM console

2022-06-23 Thread Benjamin Doron
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   | 266 

 UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf |  28 +++
 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..2898c07baf3c 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 ..1b2756166537
--- /dev/null
+++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c
@@ -0,0 +1,266 @@
+/** @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
+  )
+{
+  struct cb_cbmem_ref  *CbMemRef;
+
+  // The coreboot table contains large structures as references
+  CbMemRef = FindCbTag(CB_TAG_CBMEM_CONSOLE);
+  if (CbMemRef == NULL) {
+return RETURN_DEVICE_ERROR;
+  }
+
+  mCbConsole = (VOID *)(UINTN)CbMemRef->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