From d8403d01f539d2776a4907f37b1436d884af8608 Mon Sep 17 00:00:00 2001 From: belliash Date: Wed, 22 Nov 2023 17:22:57 +0100 Subject: [PATCH] Verify kernel and boot loader compatibility --- sdk/xtdk/xtfw.h | 8 ++++-- xtldr/modules/xtos/xtos.c | 5 ++-- xtoskrnl/ke/amd64/krnlinit.c | 3 --- xtoskrnl/ke/i686/krnlinit.c | 3 --- xtoskrnl/ke/krnlinit.c | 47 +++++++++++++++++++++--------------- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/sdk/xtdk/xtfw.h b/sdk/xtdk/xtfw.h index e06de36..e7b4578 100644 --- a/sdk/xtdk/xtfw.h +++ b/sdk/xtdk/xtfw.h @@ -16,6 +16,9 @@ /* Version number of the current kernel initialization block */ #define INITIALIZATION_BLOCK_VERSION 1 +/* Version number of the current XTOS loader protocol */ +#define BOOT_PROTOCOL_VERSION 1 + /* Memory allocation structures */ typedef enum _LOADER_MEMORY_TYPE { @@ -115,11 +118,12 @@ typedef struct _LOADER_MEMORY_MAPPING /* Loader provided information needed by the kernel to initialize */ typedef struct _KERNEL_INITIALIZATION_BLOCK { + ULONG BlockSize; + ULONG BlockVersion; + ULONG ProtocolVersion; LIST_ENTRY LoadOrderListHead; LIST_ENTRY MemoryDescriptorListHead; LIST_ENTRY BootDriverListHead; - ULONG Size; - ULONG Version; LOADER_INFORMATION_BLOCK LoaderInformation; FIRMWARE_INFORMATION_BLOCK FirmwareInformation; } KERNEL_INITIALIZATION_BLOCK, *PKERNEL_INITIALIZATION_BLOCK; diff --git a/xtldr/modules/xtos/xtos.c b/xtldr/modules/xtos/xtos.c index c45b169..07b963f 100644 --- a/xtldr/modules/xtos/xtos.c +++ b/xtldr/modules/xtos/xtos.c @@ -352,8 +352,9 @@ XtpInitializeLoaderBlock(IN PLIST_ENTRY MemoryMappings, RtlZeroMemory(LoaderBlock, sizeof(KERNEL_INITIALIZATION_BLOCK)); /* Set basic loader block properties */ - LoaderBlock->Size = sizeof(KERNEL_INITIALIZATION_BLOCK); - LoaderBlock->Version = INITIALIZATION_BLOCK_VERSION; + LoaderBlock->BlockSize = sizeof(KERNEL_INITIALIZATION_BLOCK); + LoaderBlock->BlockVersion = INITIALIZATION_BLOCK_VERSION; + LoaderBlock->ProtocolVersion = BOOT_PROTOCOL_VERSION; /* Set LoaderInformation block properties */ LoaderBlock->LoaderInformation.DbgPrint = XtLdrProtocol->DbgPrint; diff --git a/xtoskrnl/ke/amd64/krnlinit.c b/xtoskrnl/ke/amd64/krnlinit.c index 1232255..429fe26 100644 --- a/xtoskrnl/ke/amd64/krnlinit.c +++ b/xtoskrnl/ke/amd64/krnlinit.c @@ -82,9 +82,6 @@ XTAPI VOID KepStartKernel(VOID) { - /* Print debug message */ - DebugPrint(L"Starting ExectOS ...\n"); - /* Initialize XTOS kernel */ KepInitializeKernel(); diff --git a/xtoskrnl/ke/i686/krnlinit.c b/xtoskrnl/ke/i686/krnlinit.c index 0356add..604079c 100644 --- a/xtoskrnl/ke/i686/krnlinit.c +++ b/xtoskrnl/ke/i686/krnlinit.c @@ -82,9 +82,6 @@ XTAPI VOID KepStartKernel(VOID) { - /* Print debug message */ - DebugPrint(L"Starting ExectOS ...\n"); - /* Initialize XTOS kernel */ KepInitializeKernel(); diff --git a/xtoskrnl/ke/krnlinit.c b/xtoskrnl/ke/krnlinit.c index 958f465..bb29c22 100644 --- a/xtoskrnl/ke/krnlinit.c +++ b/xtoskrnl/ke/krnlinit.c @@ -23,29 +23,36 @@ XTAPI VOID KeStartXtSystem(IN PKERNEL_INITIALIZATION_BLOCK Parameters) { - /* Check if debugging enabled and if boot loader provided routine for debug printing */ - if(DEBUG && Parameters->LoaderInformation.DbgPrint) - { - /* Use loader's provided DbgPrint() routine for early printing to serial console */ - KeDbgPrint = Parameters->LoaderInformation.DbgPrint; - } - - - /* Print some message to serial console and test kernel parameters */ - DebugPrint(L"Hello world from ExectOS kernel!\n"); - DebugPrint(L"\n\n------ Kernel parameters block ------\n" - L"Loader block size: %lu\n" - L"Loader block version: %lu\n" - L"EFI Revision: %lu\n", - Parameters->Size, - Parameters->Version, - Parameters->FirmwareInformation.EfiFirmware.EfiVersion - ); - - /* Save the kernel initialization block */ KeInitializationBlock = Parameters; + /* Verify kernel and boot loader compatibility */ + if(KeInitializationBlock->BlockSize != sizeof(KERNEL_INITIALIZATION_BLOCK) || + KeInitializationBlock->BlockVersion != INITIALIZATION_BLOCK_VERSION || + KeInitializationBlock->ProtocolVersion != BOOT_PROTOCOL_VERSION) + { + /* Kernel and boot loader version mismatch */ + for(;;) + { + /* Halt system */ + ArClearInterruptFlag(); + ArHalt(); + } + } + + /* Check if debugging enabled and if boot loader provided routine for debug printing */ + if(DEBUG && KeInitializationBlock->LoaderInformation.DbgPrint) + { + /* Use loader's provided DbgPrint() routine for early printing to serial console */ + KeDbgPrint = KeInitializationBlock->LoaderInformation.DbgPrint; + } + + /* Announce kernel startup */ + DebugPrint(L"Starting ExectOS v%d.%d (%s-%s-%s-%s / %s %s)\n", + XTOS_VERSION_MAJOR, XTOS_VERSION_MINOR, XTOS_VERSION_DATE, + XTOS_VERSION_BUILD, XTOS_VERSION_ARCH, XTOS_VERSION_HASH, + XTOS_COMPILER_NAME, XTOS_COMPILER_VERSION); + /* Initialize boot CPU */ ArInitializeProcessor();