From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by mx.groups.io with SMTP id smtpd.web11.19210.1653244657923045577 for ; Sun, 22 May 2022 11:37:38 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Vr1Wlnxt; spf=pass (domain: gmail.com, ip: 209.85.222.171, mailfrom: benjamin.doron00@gmail.com) Received: by mail-qk1-f171.google.com with SMTP id r84so59662qke.10 for ; Sun, 22 May 2022 11:37:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=PwH/zvGoOBfzzzblp3s47cBfveKPUAaib+pE2tGq46o=; b=Vr1Wlnxt6WQXBL7kBYBQ9qI4aS7ktvdZcrzwIunCefLMHwAbfVYS1hgq9Ce+hit3/V mYE6bjxt9eqOWs85q0z7B5EQbP2w/VNr8TmuyrpUSFzBAk3jD68qNh7VM8yxx6d/x03H Hs8GAuPM2zlOnGdubqGT6NNdE7/y/Jg4NY1kv/LA4EDAMnTsiyqjwqsw1JDb8e3264rW wNxEsAbHzT7N5imj7PTN/bukAZXI1zsjD8JZZ/UBHv0zyoQxfCzRxHCPuZDegwsoJtbU 4x8UTy2fS26HofZMgvHvkn76B/zJLkjpbBkj3nD6bJvyt3Fu7VEP0VHq4SgijduYMxpL FXFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=PwH/zvGoOBfzzzblp3s47cBfveKPUAaib+pE2tGq46o=; b=UGmvmqin9SKDa8c6uFghifMGocfZ3B4ppdNWZl/N6lb1FoDTH2isQI911VOJo2CgOk jchWLSWAoIk/Kd3JMXIjZQ5HF1hvBZKAtm3BT1gNR4UsxXY8fdxMa2Go9LcRsFQTnnHZ 6CXwnRbv+pGzRexVGE9d7w4a2IOlMxO0Ze7Pew/4OCUhVPnHXQvJKB8xY9NHnH8x211b M+mkkpNSoNRtAA+ZnN/U3rQXU4+6pcaawOWu2ujzH65kJIkfoOS5qt3GwICE6oyPNvD+ +ce9Bz9z4urFkJCmoQBqyHtLtkMDIhyn1XJ5OfJsbrpvAsyp7/2eVliYWHm5FItoIBpg F8XQ== X-Gm-Message-State: AOAM533bTZk8DMXE2enuc03m0IfCXJyynxeFTwNvl1rQJ8Jt4/k7qfbM r36E+fU3HEYqtbIFp9RJd3k33h9qb9I= X-Google-Smtp-Source: ABdhPJzbn2b7yQIAL8ChnEFDdrVXvjUaiCVCoTYoXkyHTkOJtGb4ZSRZQeMghhoyKQFnkyt4zbgv+A== X-Received: by 2002:a05:620a:2991:b0:6a0:5212:96a6 with SMTP id r17-20020a05620a299100b006a0521296a6mr11780539qkp.646.1653244656678; Sun, 22 May 2022 11:37:36 -0700 (PDT) Return-Path: Received: from aturtleortwo-benjamindomain.. (24-52-200-135.cable.teksavvy.com. [24.52.200.135]) by smtp.gmail.com with ESMTPSA id t5-20020a05620a004500b0069fc13ce24asm3077402qkt.123.2022.05.22.11.37.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 11:37:36 -0700 (PDT) From: "Benjamin Doron" To: devel@edk2.groups.io Cc: Guo Dong , Ray Ni , Maurice Ma , Benjamin You , Sean Rhodes Subject: [PATCH v1] UefiPayloadPkg: Add support for logging to CBMEM console Date: Sun, 22 May 2022 14:36:43 -0400 Message-Id: <20220522183643.991945-1-benjamin.doron00@gmail.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 | 12 + UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c | 258 +++++++++= +++++++++++ UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf | 30 +++ UefiPayloadPkg/UefiPayloadPkg.dsc | 25 +- 4 files changed, 320 insertions(+), 5 deletions(-) diff --git a/UefiPayloadPkg/Include/Coreboot.h b/UefiPayloadPkg/Include/Cor= eboot.h index a3e1109fe84e..4c9cf965519b 100644 --- a/UefiPayloadPkg/Include/Coreboot.h +++ b/UefiPayloadPkg/Include/Coreboot.h @@ -199,6 +199,12 @@ struct cb_forward { UINT64 forward;=0D };=0D =0D +struct cb_cbmem_ref {=0D + UINT32 tag;=0D + UINT32 size;=0D + UINT64 cbmem_addr;=0D +};=0D +=0D #define CB_TAG_FRAMEBUFFER 0x0012=0D struct cb_framebuffer {=0D UINT32 tag;=0D @@ -229,6 +235,12 @@ struct cb_vdat { =0D #define CB_TAG_TIMESTAMPS 0x0016=0D #define CB_TAG_CBMEM_CONSOLE 0x0017=0D +struct cbmem_console {=0D + UINT32 size;=0D + UINT32 cursor;=0D + UINT8 body[0];=0D +} __attribute__((packed));=0D +=0D #define CB_TAG_MRC_CACHE 0x0018=0D struct cb_cbmem_tab {=0D UINT32 tag;=0D diff --git a/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c b/Uef= iPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c new file mode 100644 index 000000000000..f7c06b0f41e9 --- /dev/null +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c @@ -0,0 +1,258 @@ +/** @file=0D + CBMEM console SerialPortLib instance=0D +=0D + Copyright (c) 2022, Baruch Binyamin Doron=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +=0D +// Upper nibble contains flags=0D +#define CBMC_CURSOR_MASK ((1 << 28) - 1)=0D +#define CBMC_OVERFLOW (1 << 31)=0D +=0D +STATIC struct cbmem_console *gCbConsole =3D NULL;=0D +=0D +/**=0D + Find coreboot record with given Tag.=0D + NOTE: This coreboot-specific function definition is absent=0D + from the common BlParseLib header.=0D +=0D + @param Tag The tag id to be found=0D +=0D + @retval NULL The Tag is not found.=0D + @retval Others The pointer to the record found.=0D +=0D +**/=0D +VOID *=0D +FindCbTag (=0D + IN UINT32 Tag=0D + );=0D +=0D +/**=0D + Initialize the serial device hardware.=0D +=0D + If no initialization is required, then return RETURN_SUCCESS.=0D + If the serial device was successfully initialized, then return RETURN_SU= CCESS.=0D + If the serial device could not be initialized, then return RETURN_DEVICE= _ERROR.=0D +=0D + @retval RETURN_SUCCESS The serial device was initialized.=0D + @retval RETURN_DEVICE_ERROR The serial device could not be initialized= .=0D +=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +SerialPortInitialize (=0D + VOID=0D + )=0D +{=0D + struct cb_cbmem_ref *cbref =3D FindCbTag(CB_TAG_CBMEM_CONSOLE);=0D + if (!cbref) {=0D + return RETURN_DEVICE_ERROR;=0D + }=0D +=0D + gCbConsole =3D (VOID *)(UINTN)cbref->cbmem_addr; // Support PEI and DXE= =0D + if (gCbConsole =3D=3D NULL) {=0D + return RETURN_DEVICE_ERROR;=0D + }=0D +=0D + return RETURN_SUCCESS;=0D +}=0D +=0D +/**=0D + Write data from buffer to serial device.=0D +=0D + Writes NumberOfBytes data bytes from Buffer to the serial device.=0D + The number of bytes actually written to the serial device is returned.=0D + If the return value is less than NumberOfBytes, then the write operation= failed.=0D + If Buffer is NULL, then ASSERT().=0D + If NumberOfBytes is zero, then return 0.=0D +=0D + @param Buffer Pointer to the data buffer to be written.=0D + @param NumberOfBytes Number of bytes to written to the serial device= .=0D +=0D + @retval 0 NumberOfBytes is 0.=0D + @retval >0 The number of bytes written to the serial devic= e.=0D + If this value is less than NumberOfBytes, then = the write operation failed.=0D +=0D +**/=0D +UINTN=0D +EFIAPI=0D +SerialPortWrite (=0D + IN UINT8 *Buffer,=0D + IN UINTN NumberOfBytes=0D + )=0D +{=0D + UINT32 cursor;=0D + UINT32 flags;=0D +=0D + if (Buffer =3D=3D NULL || NumberOfBytes =3D=3D 0) {=0D + return 0;=0D + }=0D +=0D + if (!gCbConsole) {=0D + return 0;=0D + }=0D +=0D + cursor =3D gCbConsole->cursor & CBMC_CURSOR_MASK;=0D + flags =3D gCbConsole->cursor & ~CBMC_CURSOR_MASK;=0D + if (cursor >=3D gCbConsole->size) {=0D + // Already overflowed; bail out. TODO: Is this unnecessarily cautious?= =0D + // - Supports old coreboot version with legacy overflow mechanism.=0D + return 0;=0D + }=0D +=0D + if (cursor + NumberOfBytes > gCbConsole->size) {=0D + // Will overflow, zero cursor and set flag.=0D + cursor =3D 0;=0D + flags |=3D CBMC_OVERFLOW;=0D + }=0D +=0D + // Longest debug messages supported by a DebugLib seems to be CHAR16[0x1= 00] =3D=3D 512.=0D + // So, no chance that one message could overflow even the smallest buffe= r.=0D + CopyMem (&gCbConsole->body[cursor], Buffer, NumberOfBytes);=0D + cursor +=3D NumberOfBytes;=0D +=0D + if (cursor =3D=3D gCbConsole->size) {=0D + // Next message will overflow, zero cursor.=0D + // - But not overflowed yet. Do not set flag.=0D + cursor =3D 0;=0D + }=0D +=0D + gCbConsole->cursor =3D flags | cursor;=0D +=0D + return NumberOfBytes;=0D +}=0D +=0D +/**=0D + Read data from serial device and save the datas in buffer.=0D +=0D + Reads NumberOfBytes data bytes from a serial device into the buffer=0D + specified by Buffer. The number of bytes actually read is returned.=0D + If Buffer is NULL, then ASSERT().=0D + If NumberOfBytes is zero, then return 0.=0D +=0D + @param Buffer Pointer to the data buffer to store the data re= ad from the serial device.=0D + @param NumberOfBytes Number of bytes which will be read.=0D +=0D + @retval 0 Read data failed, no data is to be read.=0D + @retval >0 Actual number of bytes read from serial device.= =0D +=0D +**/=0D +UINTN=0D +EFIAPI=0D +SerialPortRead (=0D + OUT UINT8 *Buffer,=0D + IN UINTN NumberOfBytes=0D +)=0D +{=0D + return 0;=0D +}=0D +=0D +/**=0D + Polls a serial device to see if there is any data waiting to be read.=0D +=0D + @retval TRUE Data is waiting to be read from the serial devi= ce.=0D + @retval FALSE There is no data waiting to be read from the se= rial device.=0D +=0D +**/=0D +BOOLEAN=0D +EFIAPI=0D +SerialPortPoll (=0D + VOID=0D + )=0D +{=0D + return FALSE;=0D +}=0D +=0D +/**=0D + Sets the control bits on a serial device.=0D +=0D + @param Control Sets the bits of Control that are settable= .=0D +=0D + @retval RETURN_SUCCESS The new control bits were set on the seria= l device.=0D + @retval RETURN_UNSUPPORTED The serial device does not support this op= eration.=0D + @retval RETURN_DEVICE_ERROR The serial device is not functioning corre= ctly.=0D +=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +SerialPortSetControl (=0D + IN UINT32 Control=0D + )=0D +{=0D + return RETURN_UNSUPPORTED;=0D +}=0D +=0D +/**=0D + Retrieve the status of the control bits on a serial device.=0D +=0D + @param Control A pointer to return the current control si= gnals from the serial device.=0D +=0D + @retval RETURN_SUCCESS The control bits were read from the serial= device.=0D + @retval RETURN_UNSUPPORTED The serial device does not support this op= eration.=0D + @retval RETURN_DEVICE_ERROR The serial device is not functioning corre= ctly.=0D +=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +SerialPortGetControl (=0D + OUT UINT32 *Control=0D + )=0D +{=0D + return RETURN_UNSUPPORTED;=0D +}=0D +=0D +/**=0D + Sets the baud rate, receive FIFO depth, transmit/receive time out, parit= y,=0D + data bits, and stop bits on a serial device.=0D +=0D + @param BaudRate The requested baud rate. A BaudRate value of 0= will use the=0D + device's default interface speed.=0D + On output, the value actually set.=0D + @param ReceiveFifoDepth The requested depth of the FIFO on the receive= side of the=0D + serial interface. A ReceiveFifoDepth value of = 0 will use=0D + the device's default FIFO depth.=0D + On output, the value actually set.=0D + @param Timeout The requested time out for a single character = in microseconds.=0D + This timeout applies to both the transmit and = receive side of the=0D + interface. A Timeout value of 0 will use the d= evice's default time=0D + out value.=0D + On output, the value actually set.=0D + @param Parity The type of parity to use on this serial devic= e. A Parity value of=0D + DefaultParity will use the device's default pa= rity value.=0D + On output, the value actually set.=0D + @param DataBits The number of data bits to use on the serial d= evice. A DataBits=0D + value of 0 will use the device's default data = bit setting.=0D + On output, the value actually set.=0D + @param StopBits The number of stop bits to use on this serial = device. A StopBits=0D + value of DefaultStopBits will use the device's= default number of=0D + stop bits.=0D + On output, the value actually set.=0D +=0D + @retval RETURN_SUCCESS The new attributes were set on the ser= ial device.=0D + @retval RETURN_UNSUPPORTED The serial device does not support thi= s operation.=0D + @retval RETURN_INVALID_PARAMETER One or more of the attributes has an u= nsupported value.=0D + @retval RETURN_DEVICE_ERROR The serial device is not functioning c= orrectly.=0D +=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +SerialPortSetAttributes (=0D + IN OUT UINT64 *BaudRate,=0D + IN OUT UINT32 *ReceiveFifoDepth,=0D + IN OUT UINT32 *Timeout,=0D + IN OUT EFI_PARITY_TYPE *Parity,=0D + IN OUT UINT8 *DataBits,=0D + IN OUT EFI_STOP_BITS_TYPE *StopBits=0D + )=0D +{=0D + return RETURN_UNSUPPORTED;=0D +}=0D +=0D diff --git a/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf b/U= efiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf new file mode 100644 index 000000000000..3df6a0c736a5 --- /dev/null +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf @@ -0,0 +1,30 @@ +## @file=0D +# Component description file for CbSerialPortLib library.=0D +#=0D +# Copyright (c) 2022, Baruch Binyamin Doron=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D CbSerialPortLib=0D + FILE_GUID =3D 0DB3EF12-1426-4086-B012-113184C4CE11= =0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 1.0=0D + # Recall that debug logging can be unsafe to core. Route over RSC.=0D + LIBRARY_CLASS =3D SerialPortLib=0D + CONSTRUCTOR =3D SerialPortInitialize=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + UefiPayloadPkg/UefiPayloadPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseMemoryLib=0D + BlParseLib=0D +=0D +[Sources]=0D + CbSerialPortLib.c=0D +=0D diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index 4d9bbc80c866..feaf8dfbefab 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -37,6 +37,7 @@ DEFINE ABOVE_4G_MEMORY =3D TRUE=0D DEFINE BOOT_MANAGER_ESCAPE =3D FALSE=0D DEFINE SD_MMC_TIMEOUT =3D 1000000=0D + DEFINE USE_CBMEM_FOR_CONSOLE =3D FALSE=0D #=0D # SBL: UEFI payload for Slim Bootloader=0D # COREBOOT: UEFI payload for coreboot=0D @@ -121,10 +122,11 @@ =0D [BuildOptions]=0D *_*_*_CC_FLAGS =3D -D DISABLE_NEW_DEPRECATED_INTERFACES= =0D - GCC:*_UNIXGCC_*_CC_FLAGS =3D -DMDEPKG_NDEBUG=0D +!if $(USE_CBMEM_FOR_CONSOLE) =3D=3D FALSE=0D GCC:RELEASE_*_*_CC_FLAGS =3D -DMDEPKG_NDEBUG=0D INTEL:RELEASE_*_*_CC_FLAGS =3D /D MDEPKG_NDEBUG=0D MSFT:RELEASE_*_*_CC_FLAGS =3D /D MDEPKG_NDEBUG=0D +!endif=0D =0D [BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]=0D GCC:*_*_*_DLINK_FLAGS =3D -z common-page-size=3D0x1000=0D @@ -231,8 +233,13 @@ TimerLib|UefiPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.inf=0D !endif=0D ResetSystemLib|UefiPayloadPkg/Library/ResetSystemLib/ResetSystemLib.inf= =0D +!if $(USE_CBMEM_FOR_CONSOLE) =3D=3D TRUE=0D + SerialPortLib|UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf= =0D + PlatformHookLib|MdeModulePkg/Library/BasePlatformHookLibNull/BasePlatfor= mHookLibNull.inf=0D +!else=0D SerialPortLib|MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPort= Lib16550.inf=0D PlatformHookLib|UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.i= nf=0D +!endif=0D PlatformBootManagerLib|UefiPayloadPkg/Library/PlatformBootManagerLib/Pla= tformBootManagerLib.inf=0D IoApicLib|PcAtChipsetPkg/Library/BaseIoApicLib/BaseIoApicLib.inf=0D =0D @@ -422,10 +429,18 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdBootManagerMenuFile|{ 0x21, 0xaa, 0x2c= , 0x46, 0x14, 0x76, 0x03, 0x45, 0x83, 0x6e, 0x8a, 0xb6, 0xf4, 0x66, 0x23, 0= x31 }=0D gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask|0x7=0D gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x8000004F=0D -!if $(SOURCE_DEBUG_ENABLE)=0D - gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x17=0D +!if $(USE_CBMEM_FOR_CONSOLE) =3D=3D FALSE=0D + !if $(SOURCE_DEBUG_ENABLE)=0D + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x17=0D + !else=0D + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x2F=0D + !endif=0D !else=0D - gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x2F=0D + if $(TARGET) =3D=3D DEBUG=0D + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x07=0D + !else=0D + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x03=0D + !endif=0D !endif=0D gEfiMdeModulePkgTokenSpaceGuid.PcdMaxSizeNonPopulateCapsule|$(MAX_SIZE_N= ON_POPULATE_CAPSULE)=0D #=0D @@ -471,7 +486,7 @@ gUefiCpuPkgTokenSpaceGuid.PcdCpuApLoopMode=0D gUefiCpuPkgTokenSpaceGuid.PcdCpuMicrocodePatchAddress=0D gUefiCpuPkgTokenSpaceGuid.PcdCpuMicrocodePatchRegionSize=0D -!if $(TARGET) =3D=3D DEBUG=0D +!if ($(TARGET) =3D=3D DEBUG || $(USE_CBMEM_FOR_CONSOLE) =3D=3D TRUE)=0D gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial|TRUE=0D !else=0D gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial|FALSE=0D --=20 2.36.1