From 906e09fd9f0b02b40a64796bf6f2cc0925a9da38 Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Wed, 12 Jun 2024 18:02:29 +0200 Subject: [PATCH] Refactor COM port support, to get rid of global variables in library --- sdk/xtdk/amd64/hltypes.h | 5 ++-- sdk/xtdk/hlfuncs.h | 1 - sdk/xtdk/i686/hltypes.h | 5 ++-- xtldr/debug.c | 29 ++++++++++++++---- xtldr/globals.c | 3 ++ xtldr/includes/globals.h | 3 ++ xtoskrnl/hl/cport.c | 63 ++++++++++++---------------------------- xtoskrnl/includes/hli.h | 1 - 8 files changed, 53 insertions(+), 57 deletions(-) diff --git a/sdk/xtdk/amd64/hltypes.h b/sdk/xtdk/amd64/hltypes.h index e317f7c..6c6a034 100644 --- a/sdk/xtdk/amd64/hltypes.h +++ b/sdk/xtdk/amd64/hltypes.h @@ -68,8 +68,9 @@ /* PIC vector definitions */ #define PIC1_VECTOR_SPURIOUS 0x37 -/* Serial port I/O addresses */ -#define COMPORT_ADDRESSES {0x000, 0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8} +/* Serial ports information */ +#define COMPORT_ADDRESS {0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8} +#define COMPORT_COUNT 8 /* Initial stall factor */ #define INITIAL_STALL_FACTOR 100 diff --git a/sdk/xtdk/hlfuncs.h b/sdk/xtdk/hlfuncs.h index c7286d8..43de6a4 100644 --- a/sdk/xtdk/hlfuncs.h +++ b/sdk/xtdk/hlfuncs.h @@ -23,7 +23,6 @@ HlComPortPutByte(IN PCPPORT Port, XTCDECL XTSTATUS HlInitializeComPort(IN OUT PCPPORT Port, - IN ULONG PortNumber, IN PUCHAR PortAddress, IN ULONG BaudRate); diff --git a/sdk/xtdk/i686/hltypes.h b/sdk/xtdk/i686/hltypes.h index ab2bf7b..f13e826 100644 --- a/sdk/xtdk/i686/hltypes.h +++ b/sdk/xtdk/i686/hltypes.h @@ -75,8 +75,9 @@ /* PIC vector definitions */ #define PIC1_VECTOR_SPURIOUS 0x37 -/* Serial port I/O addresses */ -#define COMPORT_ADDRESSES {0x000, 0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8} +/* Serial ports information */ +#define COMPORT_ADDRESS {0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8} +#define COMPORT_COUNT 8 /* Initial stall factor */ #define INITIAL_STALL_FACTOR 100 diff --git a/xtldr/debug.c b/xtldr/debug.c index 75c2fd0..b607f58 100644 --- a/xtldr/debug.c +++ b/xtldr/debug.c @@ -232,18 +232,35 @@ BlpInitializeSerialPort(IN ULONG PortNumber, EFI_STATUS EfiStatus; XTSTATUS Status; - /* Print debug message depending on port settings */ - if(PortAddress) + /* Check if custom COM port address supplied */ + if(!PortAddress) { - BlConsolePrint(L"Initializing serial console at COM port address: 0x%lX\n", PortAddress); + /* We support only a pre-defined number of ports */ + if(PortNumber > COMPORT_COUNT) + { + /* Fail if wrong/unsupported port used */ + return STATUS_INVALID_PARAMETER; + } + + /* Check if serial port is set */ + if(PortNumber == 0) + { + /* Use COM1 by default */ + PortNumber = 1; + } + + /* Set custom port address based on the port number and print debug message */ + PortAddress = BlComPortList[PortNumber - 1]; + BlConsolePrint(L"Initializing serial console at port COM%d\n", PortNumber); } else { - BlConsolePrint(L"Initializing serial console at port COM%d\n", PortNumber); + /* Custom port address supplied, print debug message */ + BlConsolePrint(L"Initializing serial console at COM port address: 0x%lX\n", PortAddress); } /* Initialize COM port */ - Status = HlInitializeComPort(&BlpStatus.SerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); + Status = HlInitializeComPort(&BlpStatus.SerialPort, UlongToPtr(PortAddress), BaudRate); /* Port not found under supplied address */ if(Status == STATUS_NOT_FOUND && PortAddress) @@ -254,7 +271,7 @@ BlpInitializeSerialPort(IN ULONG PortNumber, { /* Try to reinitialize COM port */ BlConsolePrint(L"Enabled I/O space access for all PCI(E) serial controllers found\n"); - Status = HlInitializeComPort(&BlpStatus.SerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate); + Status = HlInitializeComPort(&BlpStatus.SerialPort, UlongToPtr(PortAddress), BaudRate); } } diff --git a/xtldr/globals.c b/xtldr/globals.c index 584b4d8..7c8db0e 100644 --- a/xtldr/globals.c +++ b/xtldr/globals.c @@ -12,6 +12,9 @@ /* XT Boot Loader registered boot protocol list */ LIST_ENTRY BlpBootProtocols; +/* XT Boot Loader serial ports list */ +ULONG BlComPortList[COMPORT_COUNT] = COMPORT_ADDRESS; + /* XT Boot Loader configuration list */ LIST_ENTRY BlpConfig; diff --git a/xtldr/includes/globals.h b/xtldr/includes/globals.h index 56d02ce..f86dd83 100644 --- a/xtldr/includes/globals.h +++ b/xtldr/includes/globals.h @@ -15,6 +15,9 @@ /* XT Boot Loader registered boot protocol list */ EXTERN LIST_ENTRY BlpBootProtocols; +/* XT Boot Loader serial ports list */ +EXTERN ULONG BlComPortList[COMPORT_COUNT]; + /* XT Boot Loader configuration list */ EXTERN LIST_ENTRY BlpConfig; diff --git a/xtoskrnl/hl/cport.c b/xtoskrnl/hl/cport.c index 592e2cd..09f3129 100644 --- a/xtoskrnl/hl/cport.c +++ b/xtoskrnl/hl/cport.c @@ -9,9 +9,6 @@ #include -/* COM port I/O addresses */ -ULONG ComPortAddress[] = COMPORT_ADDRESSES; - /** * This routine gets a byte from serial port. * @@ -212,37 +209,16 @@ HlComPortReadLsr(IN PCPPORT Port, XTCDECL XTSTATUS HlInitializeComPort(IN OUT PCPPORT Port, - IN ULONG PortNumber, IN PUCHAR PortAddress, IN ULONG BaudRate) { - USHORT Flags = 0; - UCHAR Byte = 0; - PUCHAR Address; + USHORT Flags; + UCHAR Byte; ULONG Mode; - /* We support only a pre-defined number of ports */ - if(PortNumber > ARRAY_SIZE(ComPortAddress)) - { - /* Fail if wrong/unsupported port used */ - return STATUS_INVALID_PARAMETER; - } - - /* Check if serial port is set */ - if(PortNumber == 0) - { - /* Check if port address supplied instead */ - if(PortAddress) - { - /* Set custom port address */ - ComPortAddress[PortNumber] = PtrToUlong(PortAddress); - } - else - { - /* Use COM1 by default */ - PortNumber = 1; - } - } + /* Initialize variables */ + Byte = 0; + Flags = 0; /* Check if baud rate is set */ if(BaudRate == 0) @@ -252,11 +228,8 @@ HlInitializeComPort(IN OUT PCPPORT Port, Flags |= COMPORT_FLAG_DBR; } - /* Store COM pointer */ - Address = UlongToPtr(ComPortAddress[PortNumber]); - /* Check whether this port is not already initialized */ - if((Port->Address == Address) && (Port->Baud == BaudRate)) + if((Port->Address == PortAddress) && (Port->Baud == BaudRate)) { return STATUS_SUCCESS; } @@ -265,8 +238,8 @@ HlInitializeComPort(IN OUT PCPPORT Port, do { /* Check whether the 16450/16550 scratch register exists */ - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_SR), Byte); - if(HlIoPortInByte(PtrToUshort(Address + (ULONG)COMPORT_REG_SR)) != Byte) + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_SR), Byte); + if(HlIoPortInByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_SR)) != Byte) { return STATUS_NOT_FOUND; } @@ -274,40 +247,40 @@ HlInitializeComPort(IN OUT PCPPORT Port, while(++Byte != 0); /* Disable interrupts */ - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR), COMPORT_LSR_DIS); - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_IER), COMPORT_LSR_DIS); + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_LCR), COMPORT_LSR_DIS); + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_IER), COMPORT_LSR_DIS); /* Enable Divisor Latch Access Bit (DLAB) */ - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR), COMPORT_LCR_DLAB); + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_LCR), COMPORT_LCR_DLAB); /* Set baud rate */ Mode = COMPORT_CLOCK_RATE / BaudRate; - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_DIV_DLL), (UCHAR)(Mode & 0xFF)); - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_DIV_DLM), (UCHAR)((Mode >> 8) & 0xFF)); + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_DIV_DLL), (UCHAR)(Mode & 0xFF)); + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_DIV_DLM), (UCHAR)((Mode >> 8) & 0xFF)); /* Set 8 data bits, 1 stop bits, no parity (8n1) */ - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR), + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_LCR), COMPORT_LCR_8DATA | COMPORT_LCR_1STOP | COMPORT_LCR_PARN); /* Enable DTR, RTS and OUT2 */ - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR), + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_DTR | COMPORT_MCR_RTS | COMPORT_MCR_OUT2); /* Enable FIFO */ - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_FCR), + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_FCR), COMPORT_FCR_ENABLE | COMPORT_FCR_RCVR_RESET | COMPORT_FCR_TXMT_RESET); /* Mark port as fully initialized */ Flags |= COMPORT_FLAG_INIT; /* Make sure port works in Normal Operation Mode (NOM) */ - HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_NOM); + HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_NOM); /* Read junk data out of the Receive Buffer Register (RBR) */ HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_RBR)); /* Store port details */ - Port->Address = Address; + Port->Address = PortAddress; Port->Baud = BaudRate; Port->Flags = Flags; diff --git a/xtoskrnl/includes/hli.h b/xtoskrnl/includes/hli.h index 0b869e7..9f06cb2 100644 --- a/xtoskrnl/includes/hli.h +++ b/xtoskrnl/includes/hli.h @@ -56,7 +56,6 @@ HlGetRunLevel(VOID); XTCDECL XTSTATUS HlInitializeComPort(IN OUT PCPPORT Port, - IN ULONG PortNumber, IN PUCHAR PortAddress, IN ULONG BaudRate);