From nobody Wed May 1 01:14:03 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+90732+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+90732+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1655998141; cv=none; d=zohomail.com; s=zohoarc; b=h2Zy0QxdFk/e/EXLaM2wLYcSUsiXDC8A9n9pZiYocnKpAgGrVUY8DE+FNaVLiep3dyXMPmCYQ/a/SPA2Puk58EO+wo5jpggdoIctzENvLZuprXDGi+KnNfHtF49CRs75jZIxvRrUKXDJd7gr54uLNIDaVnNRyqu+qy4ajIHvhAI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1655998141; 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=UNfeutfWd1nZHnTZaSTFQ59g89n7ekxXRaThhSa6oqE=; b=gt8I63wGEkZllQfQl5FipC6sRAXrqXVaWcdgcHwhXmzpmP/+eOzYJWdi8m7S+YfrT+xye21jfvG0v6Qj42uGzZjcS1wyGWehKrXUEDW42W+LndSBCU08ObZhhA/86dDI5+zFIIP/pEBI48xodq3PH2nJU5VolFLq3+m6nTP7z8k= 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+90732+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 1655998141968508.24477056445596; Thu, 23 Jun 2022 08:29:01 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id BixKYY1788612xy1AVmxFmKl; Thu, 23 Jun 2022 08:29:01 -0700 X-Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) by mx.groups.io with SMTP id smtpd.web09.9031.1655998140836583524 for ; Thu, 23 Jun 2022 08:29:01 -0700 X-Received: by mail-qv1-f45.google.com with SMTP id i17so23776261qvo.13 for ; Thu, 23 Jun 2022 08:29:00 -0700 (PDT) X-Gm-Message-State: TyWqn7NGn0mgZFx6y4tdCLjFx1787277AA= X-Google-Smtp-Source: AGRyM1tvYvVFVq8wXGFDC2Lu3RrhF9b88B/0D0+HLICLpJWxcaB+WxBrBl/xDc3HaGBfiZy0FPT71Q== X-Received: by 2002:a05:622a:4c6:b0:304:ea95:127e with SMTP id q6-20020a05622a04c600b00304ea95127emr8602705qtx.65.1655998139646; Thu, 23 Jun 2022 08:28:59 -0700 (PDT) X-Received: from aturtleortwo-benjamindomain.. ([2607:f2c0:e98c:e:6962:fbd4:5d01:33fa]) by smtp.gmail.com with ESMTPSA id w13-20020a05620a444d00b006a9901130b2sm21058564qkp.86.2022.06.23.08.28.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 08:28:58 -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 v4] UefiPayloadPkg: Add support for logging to CBMEM console Date: Thu, 23 Jun 2022 11:28:15 -0400 Message-Id: <20220623152815.537835-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=1655998141; bh=YAf5Xogc2nVHV4BBTLqCb8HkFH1Zdbgbsx2SxEDgUIA=; h=Cc:Date:From:Reply-To:Subject:To; b=KB8h6OsQIRiR31d05BnG5AAMYK1y3eo2ooQKdUEp8pFmjjIxaEVboWvhIO1KRgouyK4 /MsCQwecQD6l65WQ/MQxBsr/7lssKMpCXXexNNdXepjJthe/ENtX74KTY1InSIp/+5VyR 3dfIQKiD+gbs0WS8HROHeX6Re4FqRoZXeBI= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1655998142677100001 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 Reviewed-by: Lean Sheng Tan --- 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/Cor= eboot.h index a3e1109fe84e..2898c07baf3c 100644 --- a/UefiPayloadPkg/Include/Coreboot.h +++ b/UefiPayloadPkg/Include/Coreboot.h @@ -199,6 +199,13 @@ struct cb_forward { UINT64 forward; }; =20 +struct cb_cbmem_ref { + UINT32 tag; + // Field contains size of this struct =3D=3D 0x0010 + UINT32 size; + UINT64 cbmem_addr; +}; + #define CB_TAG_FRAMEBUFFER 0x0012 struct cb_framebuffer { UINT32 tag; @@ -229,6 +236,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..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 =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 *CbMemRef; + + // The coreboot table contains large structures as references + CbMemRef =3D FindCbTag(CB_TAG_CBMEM_CONSOLE); + if (CbMemRef =3D=3D NULL) { + return RETURN_DEVICE_ERROR; + } + + mCbConsole =3D (VOID *)(UINTN)CbMemRef->cbmem_addr; // Support PEI and = DXE + if (mCbConsole =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 (mCbConsole =3D=3D NULL) { + return 0; + } + + Cursor =3D mCbConsole->cursor & CBMC_CURSOR_MASK; + Flags =3D mCbConsole->cursor & ~CBMC_CURSOR_MASK; + if (Cursor >=3D mCbConsole->size) { + // Already overflowed; bail out. TODO: Is this unnecessarily cautious? + // - Supports old coreboot version with legacy overflow mechanism. + return 0; + } + + if (Cursor + NumberOfBytes > mCbConsole->size) { + // Will overflow, zero cursor and set flag. + Cursor =3D 0; + Flags |=3D CBMC_OVERFLOW; + } + + if (NumberOfBytes > mCbConsole->size) { + // This one debug message is longer than the entire buffer. Truncate i= t. + // - TODO: Is this unnecessarily cautious? + NumberOfBytes =3D mCbConsole->size; + } + + CopyMem (&mCbConsole->body[Cursor], Buffer, NumberOfBytes); + Cursor +=3D NumberOfBytes; + + if (Cursor =3D=3D mCbConsole->size) { + // Next message will overflow, zero cursor. + // - Set flag preemptively. This could not be determined later. + Cursor =3D 0; + Flags |=3D CBMC_OVERFLOW; + } + + mCbConsole->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..ec5739beac52 --- /dev/null +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf @@ -0,0 +1,28 @@ +## @file +# CBMEM console SerialPortLib instance +# +# 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 + 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..0e4248767756 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 (#90732): https://edk2.groups.io/g/devel/message/90732 Mute This Topic: https://groups.io/mt/91945631/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-