From nobody Fri May 17 05:54:28 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+86189+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+86189+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1643413487; cv=none; d=zohomail.com; s=zohoarc; b=cwXuKByJubtCURPw3KWS3HCs6mDSTPgIowu47mrbTsueCCu4af5sxlEgWZQcebpXlKpxjcQ1htj6DQowtlDAtbF63ZJNrLBrgxcaTmqU+sN7MJmTC/28BMy+7ql0EXODjhjTxuVNGyvBlwpMLX39NlrYucNgW/xLT0biOBnd9lo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1643413487; 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=87csu5RxDUwH6Q4AOPIeel3/kNHhtMuMFDHSjXxE3PU=; b=Q8Hs0hx5anoA67wVsWUIJOEVDFBNpFG9hHPna4A13gb+sL4FsvJffvKtU7EVq2ni0t4jNGbOzNEtftFpjNrg/kMLT7l/dZXbi6Cx6EfiMFKtFVTPfwLwVsaNef58o8+wIfCvKXd+Ikom5WElQLAobKJGFBH/6u2PuoCbQIIUSlw= 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+86189+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1643413487451571.4989734788549; Fri, 28 Jan 2022 15:44:47 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id VVHaYY1788612xRks08U3dld; Fri, 28 Jan 2022 15:44:46 -0800 X-Received: from smarthost01b.sbp.mail.zen.net.uk (smarthost01b.sbp.mail.zen.net.uk [212.23.1.3]) by mx.groups.io with SMTP id smtpd.web09.44.1643405922584382911 for ; Fri, 28 Jan 2022 13:38:43 -0800 X-Received: from [217.155.46.38] (helo=sean-StarBook.lan) by smarthost01b.sbp.mail.zen.net.uk with esmtp (Exim 4.90_1) (envelope-from ) id 1nDYx6-0008RG-0m; Fri, 28 Jan 2022 21:38:40 +0000 From: "Sean Rhodes" To: devel@edk2.groups.io Cc: Benjamin Doron Subject: [edk2-devel] [PATCH] UefiPayloadPkg: Add support for logging to CBMEM console Date: Fri, 28 Jan 2022 21:38:38 +0000 Message-Id: MIME-Version: 1.0 X-Originating-smarthost01b-IP: [217.155.46.38] Feedback-ID: 217.155.46.38 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,sean@starlabs.systems X-Gm-Message-State: 8I24Lp8U9sLXLsxOwPuWk7Tmx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1643413486; bh=JB16o2joVsURQlDCXILBZuyEjUrLN1oPgkIH5JvgY2U=; h=Cc:Date:From:Reply-To:Subject:To; b=GP7xF1fW0EN1mZ+iUPya2f6AkDTEJy5eor4u3T3GlOn9UZS3b+TCh5Eolhqr4DtmCkO PaBA1Fj0eB7tSQ333en1pAom+FJ9IY3GAzBys0Q/bb4zF54e53phkpxZRlTRykDq3oSDc U4Y94B44c9pxdRfUI9h75hcovB8psqgpXvk= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1643413489456100023 Content-Type: text/plain; charset="utf-8" From: Benjamin Doron Tested on QEMU, dumping the appropriate memory region in UEFI shell shows the TianoCore log. `find_cb_subtable` is sourced from STM/SeaBIOS. Signed-off-by: Benjamin Doron --- UefiPayloadPkg/Include/Coreboot.h | 14 + .../Library/CbSerialPortLib/CbSerialPortLib.c | 272 ++++++++++++++++++ .../CbSerialPortLib/CbSerialPortLib.inf | 27 ++ UefiPayloadPkg/UefiPayloadPkg.dsc | 5 + UefiPayloadPkg/UefiPayloadPkg.fdf | 6 +- 5 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c create mode 100644 UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.= inf diff --git a/UefiPayloadPkg/Include/Coreboot.h b/UefiPayloadPkg/Include/Cor= eboot.h index a3e1109fe8..f2f577a02e 100644 --- a/UefiPayloadPkg/Include/Coreboot.h +++ b/UefiPayloadPkg/Include/Coreboot.h @@ -199,7 +199,14 @@ 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; UINT32 size; @@ -230,6 +237,13 @@ struct cb_vdat { #define CB_TAG_TIMESTAMPS 0x0016 #define CB_TAG_CBMEM_CONSOLE 0x0017 #define CB_TAG_MRC_CACHE 0x0018 + +struct cbmem_console { + UINT32 size; + UINT32 cursor; + UINT8 body[0]; +} __attribute__((packed)); + struct cb_cbmem_tab { UINT32 tag; UINT32 size; diff --git a/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c b/Uef= iPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c new file mode 100644 index 0000000000..7b26c08dd7 --- /dev/null +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.c @@ -0,0 +1,272 @@ +/** @file + CBMEM console SerialPortLib instance + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include +#include +#include + +// +// We can't use DebugLib due to a constructor dependency cycle between Deb= ugLib +// and ourselves. +// +#define ASSERT(Expression) \ + do { \ + if (!(Expression)) { \ + CpuDeadLoop (); \ + } \ + } while (FALSE) + +#define CBMC_CURSOR_MASK ((1 << 28) - 1) +#define CBMC_OVERFLOW (1 << 31) + +STATIC struct cbmem_console *gCbConsole =3D NULL; +STATIC UINT32 STM_cursor =3D 0; + +// Try to find the coreboot memory table in the given coreboot table. +static void * +find_cb_subtable(struct cb_header *cbh, UINT32 tag) +{ + char *tbl =3D (char *)cbh + sizeof(*cbh); + UINT32 count =3D cbh->table_entries; + int i; + for (i=3D0; isize; + if (cbm->tag =3D=3D tag) + return cbm; + } + return NULL; +} + +/** + 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 + ) +{ + /* `FindCbTag` doesn't work because we need direct access to the memory = addresses? */ + struct cb_header *cbh =3D GetParameterBase(); + if (!cbh) { + return RETURN_DEVICE_ERROR; + } + + struct cb_cbmem_ref *cbref =3D find_cb_subtable(cbh, CB_TAG_CBMEM_CONSOL= E); + 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; + } + + // set the cursor such that the STM console will not overwrite the + // coreboot console output + STM_cursor =3D gCbConsole->cursor & CBMC_CURSOR_MASK; + + 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 + ) +{ + UINTN Sent; + UINT32 cursor; + UINT32 flags; + + ASSERT (Buffer !=3D NULL); + + if (NumberOfBytes =3D=3D 0) { + return 0; + } + + if (!gCbConsole) { + return 0; + } + + Sent =3D 0; + do { + cursor =3D gCbConsole->cursor & CBMC_CURSOR_MASK; + flags =3D gCbConsole->cursor & ~CBMC_CURSOR_MASK; + + if (cursor >=3D gCbConsole->size) { + return 0; // Old coreboot version with legacy overflow mechanism. + } + + gCbConsole->body[cursor++] =3D Buffer[Sent++]; + + if (cursor >=3D gCbConsole->size) { + cursor =3D STM_cursor; + flags |=3D CBMC_OVERFLOW; + } + + gCbConsole->cursor =3D flags | cursor; + } while (Sent < NumberOfBytes); + + return Sent; +} + +/** + 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 0000000000..37b33c24ad --- /dev/null +++ b/UefiPayloadPkg/Library/CbSerialPortLib/CbSerialPortLib.inf @@ -0,0 +1,27 @@ +## @file +# Component description file for CbSerialPortLib module. +# +# 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 0.5 + LIBRARY_CLASS =3D SerialPortLib + CONSTRUCTOR =3D SerialPortInitialize + +[Sources] + CbSerialPortLib.c + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + UefiPayloadPkg/UefiPayloadPkg.dec + +[LibraryClasses] + BaseLib + BlParseLib diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index 3d08edfe31..b3770b9be6 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -33,6 +33,7 @@ DEFINE UNIVERSAL_PAYLOAD =3D FALSE DEFINE SECURITY_STUB_ENABLE =3D TRUE DEFINE SMM_SUPPORT =3D FALSE + DEFINE USE_CBMEM_FOR_CONSOLE =3D FALSE # # SBL: UEFI payload for Slim Bootloader # COREBOOT: UEFI payload for coreboot @@ -223,7 +224,11 @@ 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 +!else SerialPortLib|MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPort= Lib16550.inf +!endif PlatformHookLib|UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.i= nf PlatformBootManagerLib|UefiPayloadPkg/Library/PlatformBootManagerLib/Pla= tformBootManagerLib.inf IoApicLib|PcAtChipsetPkg/Library/BaseIoApicLib/BaseIoApicLib.inf diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf b/UefiPayloadPkg/UefiPayload= Pkg.fdf index f619a23139..0df8342252 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.fdf +++ b/UefiPayloadPkg/UefiPayloadPkg.fdf @@ -15,8 +15,12 @@ DEFINE FD_BLOCK_SIZE =3D 0x00001000 !if $(TARGET) =3D=3D "NOOPT" DEFINE FD_SIZE =3D 0x00850000 DEFINE NUM_BLOCKS =3D 0x850 -!else =20 +!elseif $(USE_CBMEM_FOR_CONSOLE) =3D=3D TRUE +DEFINE FD_SIZE =3D 0x00600000 +DEFINE NUM_BLOCKS =3D 0x600 + +!else DEFINE FD_SIZE =3D 0x00590000 DEFINE NUM_BLOCKS =3D 0x590 !endif --=20 2.32.0 -=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 (#86189): https://edk2.groups.io/g/devel/message/86189 Mute This Topic: https://groups.io/mt/88756033/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-