diff --git a/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c b/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c index 57e3cd0331..9cb50dd80d 100644 --- a/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c +++ b/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c @@ -17,7 +17,6 @@ #include #include #include -#include // // PCI Defintions. @@ -602,8 +601,6 @@ SerialPortWrite ( UINTN Result; UINTN Index; UINTN FifoSize; - UINT8 *BufPtr = Buffer; - UINTN Bytes = NumberOfBytes; if (Buffer == NULL) { return 0; @@ -666,9 +663,6 @@ SerialPortWrite ( SerialPortWriteRegister (SerialRegisterBase, R_UART_TXBUF, *Buffer); } } - - System76EcWrite(BufPtr, Bytes); - return Result; } diff --git a/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.inf b/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.inf index 5677a579d2..8b4ae3f1d4 100644 --- a/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.inf +++ b/MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.inf @@ -18,14 +18,12 @@ [Packages] MdePkg/MdePkg.dec MdeModulePkg/MdeModulePkg.dec - UefiPayloadPkg/UefiPayloadPkg.dec [LibraryClasses] PcdLib IoLib PlatformHookLib PciLib - System76EcLib [Sources] BaseSerialPortLib16550.c diff --git a/UefiPayloadPkg/Include/Library/System76EcLib.h b/UefiPayloadPkg/Include/Library/System76EcLib.h deleted file mode 100644 index 5840ccf11f..0000000000 --- a/UefiPayloadPkg/Include/Library/System76EcLib.h +++ /dev/null @@ -1,26 +0,0 @@ -/** @file - System76 EC logging - - Copyright (c) 2020 System76, Inc. - SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#ifndef __SYSTEM76_EC_LIB__ -#define __SYSTEM76_EC_LIB__ - -/** - Write data to the embedded controller using SMFI command interface. - - @param Buffer Pointer to the data buffer to be written. - @param NumberOfBytes Number of bytes to write. - - @return -1 if the command failed, else the number of data bytes written. -**/ -INTN -System76EcWrite ( - IN UINT8 *Buffer, - IN UINTN NumberOfBytes -); - -#endif /* __SYSTEM76_EC_LIB__ */ diff --git a/UefiPayloadPkg/Library/System76EcLib/System76EcLib.c b/UefiPayloadPkg/Library/System76EcLib/System76EcLib.c index 57567e1e44..9bb7a90606 100644 --- a/UefiPayloadPkg/Library/System76EcLib/System76EcLib.c +++ b/UefiPayloadPkg/Library/System76EcLib/System76EcLib.c @@ -6,98 +6,139 @@ **/ -#include #include -#include -#define SYSTEM76_EC_BASE 0xE00 -#define CMD_PRINT 0x4 +// From coreboot/src/drivers/system76_ec/system76_ec.c { +#define SYSTEM76_EC_BASE 0x0E00 -/** - @param Offset The offset into the SFMI RAM window to read from. - - @return The value read. -**/ -STATIC -UINT8 -System76EcReadByte ( - UINT8 Offset - ) -{ - return IoRead8 (SYSTEM76_EC_BASE + Offset); +static inline UINT8 system76_ec_read(UINT8 addr) { + return IoRead8(SYSTEM76_EC_BASE + (UINT16)addr); } -/** - @param Offset The offset into the SFMI RAM window to write to. - @param Value The value to write. - - @return The value written back to the I/O port. -**/ -STATIC -UINT8 -System76EcWriteByte ( - UINT8 Offset, - UINT8 Value - ) -{ - return IoWrite8 (SYSTEM76_EC_BASE + Offset, Value); +static inline void system76_ec_write(UINT8 addr, UINT8 data) { + IoWrite8(SYSTEM76_EC_BASE + (UINT16)addr, data); } -/** - Write data to the embedded controller using SMFI command interface. - - @param Buffer Pointer to the data buffer to be written. - @param NumberOfBytes Number of bytes to write. - - @return -1 if the command failed, else the number of data bytes written. -**/ -INTN -System76EcWrite ( - IN UINT8 *Buffer, - IN UINTN NumberOfBytes - ) -{ - UINTN Index; - UINTN Index2; - UINTN Timeout; - - if (Buffer == NULL) { - return 0; - } - - for (Index = 0; Index < NumberOfBytes;) { - if (System76EcReadByte (0) == 0) { - - // Can write 256 bytes of data at a time - for (Index2 = 0; (Index2 < NumberOfBytes) && ((Index2 + 4) < 256); Index++, Index2++) { - System76EcWriteByte ((Index + 4), Buffer[Index]); - } - // Flags - System76EcWriteByte (2, 0); - // Length - System76EcWriteByte (3, Index2); - // Command - System76EcWriteByte (0, CMD_PRINT); - - // Wait for command completion, for up to 1 second - for (Timeout = 1000000; Timeout > 0; Timeout--) { - if (System76EcReadByte (0) == 0) - break; - MicroSecondDelay (1); - } - if (Timeout == 0) { - // Error: Timeout occured - return -1; - } - if (System76EcReadByte (1) != 0) { - // Error: Command failed - return -1; - } - } else { - // Error: Command already running - return -1; +void system76_ec_init(void) { + // Clear entire command region + for (int i = 0; i < 256; i++) { + system76_ec_write((UINT8)i, 0); } - } - - return Index; } + +void system76_ec_flush(void) { + // Send command + system76_ec_write(0, 4); + + // Wait for command completion + while (system76_ec_read(0) != 0) {} + + // Clear length + system76_ec_write(3, 0); +} + +void system76_ec_print(UINT8 byte) { + // Read length + UINT8 len = system76_ec_read(3); + // Write data at offset + system76_ec_write(len + 4, byte); + // Update length + system76_ec_write(3, len + 1); + + // If we hit the end of the buffer, or were given a newline, flush + if (byte == '\n' || len >= 128) { + system76_ec_flush(); + } +} +// } From coreboot/src/drivers/system76_ec/system76_ec.c + +// Implement SerialPortLib { +#include + +RETURN_STATUS +EFIAPI +SerialPortInitialize ( + VOID + ) +{ + system76_ec_init(); + return RETURN_SUCCESS; +} + +UINTN +EFIAPI +SerialPortWrite ( + IN UINT8 *Buffer, + IN UINTN NumberOfBytes + ) +{ + if (Buffer == NULL) { + return 0; + } + + if (NumberOfBytes == 0) { + system76_ec_flush(); + return 0; + } + + for(UINTN i = 0; i < NumberOfBytes; i++) { + system76_ec_print(Buffer[i]); + } + + return NumberOfBytes; +} + +BOOLEAN +EFIAPI +SerialPortPoll ( + VOID + ) +{ + return FALSE; +} + +RETURN_STATUS +EFIAPI +SerialPortGetControl ( + OUT UINT32 *Control + ) +{ + return RETURN_UNSUPPORTED; +} + +RETURN_STATUS +EFIAPI +SerialPortSetControl ( + IN UINT32 Control + ) +{ + return RETURN_UNSUPPORTED; +} + +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; +} +// } Implement SerialPortLib + +// Implement PlatformHookLib { +#include + +RETURN_STATUS +EFIAPI +PlatformHookSerialPortInitialize ( + VOID + ) +{ + return RETURN_SUCCESS; +} +// } Implement PlatformHookLib diff --git a/UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf b/UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf index caa751baa9..e82206f63b 100644 --- a/UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf +++ b/UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf @@ -21,7 +21,6 @@ [LibraryClasses] IoLib - TimerLib [Sources] System76EcLib.c diff --git a/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.c b/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.c deleted file mode 100644 index d7afcd4b90..0000000000 --- a/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.c +++ /dev/null @@ -1,18 +0,0 @@ -/** @file - System76 EC logging - - Copyright (c) 2020 System76, Inc. - SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#include - -INTN -System76EcWrite ( - IN UINT8 *Buffer, - IN UINTN NumberOfBytes - ) -{ - return 0; -} diff --git a/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.inf b/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.inf deleted file mode 100644 index f99ca46c18..0000000000 --- a/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.inf +++ /dev/null @@ -1,27 +0,0 @@ -## @file -# System76 EC logging. -# -# Copyright (c) 2020, System76, Inc. -# SPDX-License-Identifier: BSD-2-Clause-Patent -## - -[Defines] - INF_VERSION = 0x00010005 - BASE_NAME = System76EcLibNull - MODULE_UNI_FILE = System76EcLibNull.uni - FILE_GUID = 76ECF0DD-148B-4E48-8589-FC998823F8C2 - MODULE_TYPE = BASE - VERSION_STRING = 0.1 - LIBRARY_CLASS = System76EcLibNull - -[Packages] - MdePkg/MdePkg.dec - MdeModulePkg/MdeModulePkg.dec - UefiPayloadPkg/UefiPayloadPkg.dec - -[LibraryClasses] - -[Sources] - System76EcLibNull.c - -[Pcd] diff --git a/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.uni b/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.uni deleted file mode 100644 index f0db62f232..0000000000 --- a/UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.uni +++ /dev/null @@ -1,13 +0,0 @@ -// /** @file -// System76 EC logging. -// -// Copyright (c) 2020, System76, Inc. -// SPDX-License-Identifier: BSD-2-Clause-Patent -// -// **/ - - -#string STR_MODULE_ABSTRACT #language en-US "System76 EC logging" - -#string STR_MODULE_DESCRIPTION #language en-US "System76 EC logging." - diff --git a/UefiPayloadPkg/UefiPayloadPkgIa32.dsc b/UefiPayloadPkg/UefiPayloadPkgIa32.dsc index e9e72ec48c..44ad1ef1f3 100644 --- a/UefiPayloadPkg/UefiPayloadPkgIa32.dsc +++ b/UefiPayloadPkg/UefiPayloadPkgIa32.dsc @@ -27,6 +27,11 @@ DEFINE SOURCE_DEBUG_ENABLE = FALSE DEFINE PS2_KEYBOARD_ENABLE = TRUE + # + # Send logs to System76 EC + # + DEFINE SYSTEM76_EC_LOGGING = FALSE + # # SBL: UEFI payload for Slim Bootloader # COREBOOT: UEFI payload for coreboot @@ -176,8 +181,13 @@ # TimerLib|UefiPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.inf ResetSystemLib|UefiPayloadPkg/Library/ResetSystemLib/ResetSystemLib.inf +!if $(SYSTEM76_EC_LOGGING) == TRUE + SerialPortLib|UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf + PlatformHookLib|UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf +!else SerialPortLib|MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.inf PlatformHookLib|UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.inf +!endif PlatformBootManagerLib|UefiPayloadPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf IoApicLib|PcAtChipsetPkg/Library/BaseIoApicLib/BaseIoApicLib.inf diff --git a/UefiPayloadPkg/UefiPayloadPkgIa32X64.dsc b/UefiPayloadPkg/UefiPayloadPkgIa32X64.dsc index 1774188c0c..b5b3af0078 100644 --- a/UefiPayloadPkg/UefiPayloadPkgIa32X64.dsc +++ b/UefiPayloadPkg/UefiPayloadPkgIa32X64.dsc @@ -181,8 +181,13 @@ # TimerLib|UefiPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.inf ResetSystemLib|UefiPayloadPkg/Library/ResetSystemLib/ResetSystemLib.inf +!if $(SYSTEM76_EC_LOGGING) == TRUE + SerialPortLib|UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf + PlatformHookLib|UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf +!else SerialPortLib|MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.inf PlatformHookLib|UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib.inf +!endif PlatformBootManagerLib|UefiPayloadPkg/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf IoApicLib|PcAtChipsetPkg/Library/BaseIoApicLib/BaseIoApicLib.inf @@ -212,12 +217,6 @@ TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf VarCheckLib|MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf -!if $(SYSTEM76_EC_LOGGING) == TRUE - System76EcLib|UefiPayloadPkg/Library/System76EcLib/System76EcLib.inf -!else - System76EcLib|UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.inf -!endif - [LibraryClasses.IA32.SEC] DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf @@ -236,8 +235,6 @@ DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgentLib.inf !endif - System76EcLib|UefiPayloadPkg/Library/System76EcLibNull/System76EcLibNull.inf - [LibraryClasses.common.DXE_CORE] PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf