From e6aaa1a83c321a1cdad11cfc11d829ff51929ba3 Mon Sep 17 00:00:00 2001 From: belliash Date: Wed, 10 Aug 2022 17:56:41 +0200 Subject: [PATCH] Mark serial port as fully initialized and always check that before printing anything to the serial console --- sdk/xtdk/hltypes.h | 5 +++-- sdk/xtklib/hl/cport.c | 7 ++++++- xtldr/efiutil.c | 24 ++++++++++++++++-------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/sdk/xtdk/hltypes.h b/sdk/xtdk/hltypes.h index a72cebb..cb5ef83 100644 --- a/sdk/xtdk/hltypes.h +++ b/sdk/xtdk/hltypes.h @@ -21,8 +21,9 @@ #define COMPORT_DIV_DLM 0x01 /* Divisor Latch Most */ /* Serial port control flags */ -#define COMPORT_FLAG_DBR 0x01 /* Default Baud Rate */ -#define COMPORT_FLAG_MC 0x02 /* Modem Control */ +#define COMPORT_FLAG_INIT 0x01 /* Port Initialized */ +#define COMPORT_FLAG_DBR 0x02 /* Default Baud Rate */ +#define COMPORT_FLAG_MC 0x04 /* Modem Control */ /* Serial port Fifo Control Register (FCR) access masks */ #define COMPORT_FCR_DISABLE 0x00 /* Disable */ diff --git a/sdk/xtklib/hl/cport.c b/sdk/xtklib/hl/cport.c index 2fe354b..58aa879 100644 --- a/sdk/xtklib/hl/cport.c +++ b/sdk/xtklib/hl/cport.c @@ -212,6 +212,7 @@ HlInitializeComPort(IN OUT PCPPORT Port, { PUCHAR Address; UCHAR Byte = 0; + USHORT Flags = 0; ULONG Mode; /* Check if serial port is set */ @@ -233,7 +234,7 @@ HlInitializeComPort(IN OUT PCPPORT Port, { /* Use default baud (clock) rate if not set */ BaudRate = COMPORT_CLOCK_RATE; - Port->Flags = COMPORT_FLAG_DBR; + Flags |= COMPORT_FLAG_DBR; } /* Store COM pointer */ @@ -288,10 +289,14 @@ HlInitializeComPort(IN OUT PCPPORT Port, return STATUS_IO_DEVICE_ERROR; } + /* Mark port as fully initialized */ + Flags |= COMPORT_FLAG_INIT; + /* Disable loopback mode and use port normally */ HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_NOM); Port->Address = Address; Port->Baud = BaudRate; + Port->Flags = Flags; /* Return success */ return STATUS_SUCCESS; diff --git a/xtldr/efiutil.c b/xtldr/efiutil.c index 755a2c2..cb41c4a 100644 --- a/xtldr/efiutil.c +++ b/xtldr/efiutil.c @@ -28,14 +28,18 @@ BlDbgPrint(IN PUINT16 Format, { VA_LIST Arguments; - /* Initialise the va_list */ - VA_START(Arguments, Format); + /* Check if EFI serial port is fully initialized */ + if(EfiSerialPort.Flags & COMPORT_FLAG_INIT) + { + /* Initialise the va_list */ + VA_START(Arguments, Format); - /* Format and print the string to the serial console */ - BlStringPrint(BlComPortPutChar, Format, Arguments); + /* Format and print the string to the serial console */ + BlStringPrint(BlComPortPutChar, Format, Arguments); - /* Clean up the va_list */ - VA_END(Arguments); + /* Clean up the va_list */ + VA_END(Arguments); + } } /** @@ -65,8 +69,12 @@ BlEfiPrint(IN PUINT16 Format, /* Format and print the string to the stdout */ BlStringPrint(BlConsolePutChar, Format, Arguments); - /* Format and print the string to the serial console */ - BlStringPrint(BlComPortPutChar, Format, Arguments); + /* Check if EFI serial port is fully initialized */ + if(EfiSerialPort.Flags & COMPORT_FLAG_INIT) + { + /* Format and print the string to the serial console */ + BlStringPrint(BlComPortPutChar, Format, Arguments); + } /* Clean up the va_list */ VA_END(Arguments);