From nobody Fri May 3 05:07:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+89943+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+89943+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1653244659; cv=none; d=zohomail.com; s=zohoarc; b=Fa3LMCx59xW466RF6BMM4+Ygi66u49K0DuOrRQMin8NNVCMU29qJtzjrzd9+cXOHBx4obvI67sK7O5gIzGMCQI6CBuiFfFeVQGZ1zg1tSgQ5NZQwC6KF1mEiun7/gmxwy0XaJHROIcQqBvX5PR2EAHb0zVb5H/fu6FQy0164q98= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1653244659; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=aPyW+2PPU02aJ8w8rJcFbPXbgbWhGX7RTdUfMtkuEz4=; b=g1YvIgF59QwZ98L7TJuI/YVixc3XIa9Ayk2m3RIcvJ2tBmzFRIkGiO85/i6kixkXfBB+jgP4gZqCZppSsA2y6vI+0JW9kDI+JAT2WAxR5pA26LeDpNllV9aZPB7+oc7R0ESOeQFCmLmvRDnlh/8sCS+e3L/zlkod/si33+yCiMg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+89943+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1653244659418245.39418062157324; Sun, 22 May 2022 11:37:39 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id taahYY1788612xkHAmtFjPXb; Sun, 22 May 2022 11:37:39 -0700 X-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 X-Received: by mail-qk1-f171.google.com with SMTP id r84so59662qke.10 for ; Sun, 22 May 2022 11:37:37 -0700 (PDT) X-Gm-Message-State: hZ6FeYycaJ0GJWCEe7NZU2C0x1787277AA= 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) X-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: [edk2-devel] [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> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,benjamin.doron00@gmail.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1653244659; bh=G/NkUyDq4lP0GxTmhuGKzo5/JUPtfhXgdpttPCUiHtI=; h=Cc:Date:From:Reply-To:Subject:To; b=BYGJZlw+0siQ0ZD8ze7j+XXwCiBJah9VjWwhRC5dJtGB0dydZZQgAlDJOhqx+fzLNQY RMMN4LCa/f0Fc2YBhcpzS888L7FNRKz3cG1qgXaf6Nflg+PfsLKstGhbK4+A2jVYfjLV1 HoiW5GSq53uJZ/H0gO66IX+hksiZtrxU8gQ= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1653244660126100001 Content-Type: text/plain; charset="utf-8" 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; }; =20 +struct cb_cbmem_ref { + UINT32 tag; + UINT32 size; + UINT64 cbmem_addr; +}; + #define CB_TAG_FRAMEBUFFER 0x0012 struct cb_framebuffer { UINT32 tag; @@ -229,6 +235,12 @@ struct cb_vdat { =20 #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 { UINT32 tag; 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 + 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 *gCbConsole =3D 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 Others The 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_SU= CCESS. + If the serial device could not be initialized, then return RETURN_DEVICE= _ERROR. + + @retval RETURN_SUCCESS The serial device was initialized. + @retval RETURN_DEVICE_ERROR The serial device could not be initialized. + +**/ +RETURN_STATUS +EFIAPI +SerialPortInitialize ( + VOID + ) +{ + struct cb_cbmem_ref *cbref =3D FindCbTag(CB_TAG_CBMEM_CONSOLE); + if (!cbref) { + return RETURN_DEVICE_ERROR; + } + + gCbConsole =3D (VOID *)(UINTN)cbref->cbmem_addr; // Support PEI and DXE + if (gCbConsole =3D=3D 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 NumberOfBytes Number of bytes to written to the serial device. + + @retval 0 NumberOfBytes is 0. + @retval >0 The number of bytes written to the serial devic= e. + If this value is less than NumberOfBytes, then = the write operation failed. + +**/ +UINTN +EFIAPI +SerialPortWrite ( + IN UINT8 *Buffer, + IN UINTN NumberOfBytes + ) +{ + UINT32 cursor; + UINT32 flags; + + if (Buffer =3D=3D NULL || NumberOfBytes =3D=3D 0) { + return 0; + } + + if (!gCbConsole) { + return 0; + } + + cursor =3D gCbConsole->cursor & CBMC_CURSOR_MASK; + flags =3D gCbConsole->cursor & ~CBMC_CURSOR_MASK; + if (cursor >=3D gCbConsole->size) { + // Already overflowed; bail out. TODO: Is this unnecessarily cautious? + // - Supports old coreboot version with legacy overflow mechanism. + return 0; + } + + if (cursor + NumberOfBytes > gCbConsole->size) { + // Will overflow, zero cursor and set flag. + cursor =3D 0; + flags |=3D CBMC_OVERFLOW; + } + + // Longest debug messages supported by a DebugLib seems to be CHAR16[0x1= 00] =3D=3D 512. + // So, no chance that one message could overflow even the smallest buffe= r. + CopyMem (&gCbConsole->body[cursor], Buffer, NumberOfBytes); + cursor +=3D NumberOfBytes; + + if (cursor =3D=3D gCbConsole->size) { + // Next message will overflow, zero cursor. + // - But not overflowed yet. Do not set flag. + cursor =3D 0; + } + + gCbConsole->cursor =3D flags | cursor; + + return NumberOfBytes; +} + +/** + Read data from serial device and save the datas in buffer. + + Reads NumberOfBytes data bytes from a serial device into the buffer + specified by Buffer. The number of bytes actually read is returned. + If Buffer is NULL, then ASSERT(). + If NumberOfBytes is zero, then return 0. + + @param Buffer Pointer to the data buffer to store the data re= ad from the serial device. + @param NumberOfBytes Number of bytes which will be read. + + @retval 0 Read data failed, no data is to be read. + @retval >0 Actual number of bytes read from serial device. + +**/ +UINTN +EFIAPI +SerialPortRead ( + OUT UINT8 *Buffer, + IN UINTN NumberOfBytes +) +{ + return 0; +} + +/** + Polls a serial device to see if there is any data waiting to be read. + + @retval TRUE Data is waiting to be read from the serial devi= ce. + @retval FALSE There is no data waiting to be read from the se= rial device. + +**/ +BOOLEAN +EFIAPI +SerialPortPoll ( + VOID + ) +{ + return FALSE; +} + +/** + Sets the control bits on a serial device. + + @param Control Sets the bits of Control that are settable. + + @retval RETURN_SUCCESS The new control bits were set on the seria= l device. + @retval RETURN_UNSUPPORTED The serial device does not support this op= eration. + @retval RETURN_DEVICE_ERROR The serial device is not functioning corre= ctly. + +**/ +RETURN_STATUS +EFIAPI +SerialPortSetControl ( + IN UINT32 Control + ) +{ + return RETURN_UNSUPPORTED; +} + +/** + Retrieve the status of the control bits on a serial device. + + @param Control A pointer to return the current control si= gnals from the serial device. + + @retval RETURN_SUCCESS The control bits were read from the serial= device. + @retval RETURN_UNSUPPORTED The serial device does not support this op= eration. + @retval RETURN_DEVICE_ERROR The serial device is not functioning corre= ctly. + +**/ +RETURN_STATUS +EFIAPI +SerialPortGetControl ( + OUT UINT32 *Control + ) +{ + return RETURN_UNSUPPORTED; +} + +/** + Sets the baud rate, receive FIFO depth, transmit/receive time out, parit= y, + data bits, and stop bits on a serial device. + + @param BaudRate The requested baud rate. A BaudRate value of 0= will use the + device's default interface speed. + On output, the value actually set. + @param ReceiveFifoDepth The requested depth of the FIFO on the receive= side of the + serial interface. A ReceiveFifoDepth value of = 0 will use + the device's default FIFO depth. + On output, the value actually set. + @param Timeout The requested time out for a single character = in microseconds. + This timeout applies to both the transmit and = receive side of the + interface. A Timeout value of 0 will use the d= evice's default time + out value. + On output, the value actually set. + @param Parity The type of parity to use on this serial devic= e. A Parity value of + DefaultParity will use the device's default pa= rity value. + On output, the value actually set. + @param DataBits The number of data bits to use on the serial d= evice. A DataBits + value of 0 will use the device's default data = bit setting. + On output, the value actually set. + @param StopBits The number of stop bits to use on this serial = device. A StopBits + value of DefaultStopBits will use the device's= default number of + stop bits. + On output, the value actually set. + + @retval RETURN_SUCCESS The new attributes were set on the ser= ial device. + @retval RETURN_UNSUPPORTED The serial device does not support thi= s operation. + @retval RETURN_INVALID_PARAMETER One or more of the attributes has an u= nsupported value. + @retval RETURN_DEVICE_ERROR The serial device is not functioning c= orrectly. + +**/ +RETURN_STATUS +EFIAPI +SerialPortSetAttributes ( + IN OUT UINT64 *BaudRate, + IN OUT UINT32 *ReceiveFifoDepth, + IN OUT UINT32 *Timeout, + IN OUT EFI_PARITY_TYPE *Parity, + IN OUT UINT8 *DataBits, + IN OUT EFI_STOP_BITS_TYPE *StopBits + ) +{ + return RETURN_UNSUPPORTED; +} + 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 +# Component description file for CbSerialPortLib library. +# +# Copyright (c) 2022, Baruch Binyamin Doron +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D CbSerialPortLib + FILE_GUID =3D 0DB3EF12-1426-4086-B012-113184C4CE11 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + # Recall that debug logging can be unsafe to core. Route over RSC. + LIBRARY_CLASS =3D SerialPortLib + CONSTRUCTOR =3D SerialPortInitialize + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiPayloadPkg/UefiPayloadPkg.dec + +[LibraryClasses] + BaseMemoryLib + BlParseLib + +[Sources] + CbSerialPortLib.c + 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 DEFINE BOOT_MANAGER_ESCAPE =3D FALSE DEFINE SD_MMC_TIMEOUT =3D 1000000 + DEFINE USE_CBMEM_FOR_CONSOLE =3D FALSE # # SBL: UEFI payload for Slim Bootloader # COREBOOT: UEFI payload for coreboot @@ -121,10 +122,11 @@ =20 [BuildOptions] *_*_*_CC_FLAGS =3D -D DISABLE_NEW_DEPRECATED_INTERFACES - GCC:*_UNIXGCC_*_CC_FLAGS =3D -DMDEPKG_NDEBUG +!if $(USE_CBMEM_FOR_CONSOLE) =3D=3D FALSE GCC:RELEASE_*_*_CC_FLAGS =3D -DMDEPKG_NDEBUG INTEL:RELEASE_*_*_CC_FLAGS =3D /D MDEPKG_NDEBUG MSFT:RELEASE_*_*_CC_FLAGS =3D /D MDEPKG_NDEBUG +!endif =20 [BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER] GCC:*_*_*_DLINK_FLAGS =3D -z common-page-size=3D0x1000 @@ -231,8 +233,13 @@ TimerLib|UefiPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.inf !endif ResetSystemLib|UefiPayloadPkg/Library/ResetSystemLib/ResetSystemLib.inf +!if $(USE_CBMEM_FOR_CONSOLE) =3D=3D TRUE + SerialPortLib|UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf + PlatformHookLib|MdeModulePkg/Library/BasePlatformHookLibNull/BasePlatfor= mHookLibNull.inf +!else SerialPortLib|MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPort= Lib16550.inf PlatformHookLib|UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.i= nf +!endif PlatformBootManagerLib|UefiPayloadPkg/Library/PlatformBootManagerLib/Pla= tformBootManagerLib.inf IoApicLib|PcAtChipsetPkg/Library/BaseIoApicLib/BaseIoApicLib.inf =20 @@ -422,10 +429,18 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdBootManagerMenuFile|{ 0x21, 0xaa, 0x2c= , 0x46, 0x14, 0x76, 0x03, 0x45, 0x83, 0x6e, 0x8a, 0xb6, 0xf4, 0x66, 0x23, 0= x31 } gEfiMdePkgTokenSpaceGuid.PcdReportStatusCodePropertyMask|0x7 gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x8000004F -!if $(SOURCE_DEBUG_ENABLE) - gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x17 +!if $(USE_CBMEM_FOR_CONSOLE) =3D=3D FALSE + !if $(SOURCE_DEBUG_ENABLE) + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x17 + !else + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x2F + !endif !else - gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x2F + if $(TARGET) =3D=3D DEBUG + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x07 + !else + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x03 + !endif !endif gEfiMdeModulePkgTokenSpaceGuid.PcdMaxSizeNonPopulateCapsule|$(MAX_SIZE_N= ON_POPULATE_CAPSULE) # @@ -471,7 +486,7 @@ gUefiCpuPkgTokenSpaceGuid.PcdCpuApLoopMode gUefiCpuPkgTokenSpaceGuid.PcdCpuMicrocodePatchAddress gUefiCpuPkgTokenSpaceGuid.PcdCpuMicrocodePatchRegionSize -!if $(TARGET) =3D=3D DEBUG +!if ($(TARGET) =3D=3D DEBUG || $(USE_CBMEM_FOR_CONSOLE) =3D=3D TRUE) gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial|TRUE !else gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseSerial|FALSE --=20 2.36.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#89943): https://edk2.groups.io/g/devel/message/89943 Mute This Topic: https://groups.io/mt/91273764/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-