From cb7f4deb3730a983c8b438afd14df49b45fcc356 Mon Sep 17 00:00:00 2001 From: Jozef Nagy Date: Sun, 3 Dec 2023 18:23:50 +0100 Subject: [PATCH] INI configuration parser for XTLDR --- ExectOS.code-workspace | 3 ++ sdk/xtdk/bmtypes.h | 13 +++++ xtldr2/config.c | 105 ++++++++++++++++++++++++++++++++++++++ xtldr2/includes/bootman.h | 5 ++ xtldr2/xtldr.c | 7 +++ 5 files changed, 133 insertions(+) diff --git a/ExectOS.code-workspace b/ExectOS.code-workspace index 6c383fe..1647a29 100644 --- a/ExectOS.code-workspace +++ b/ExectOS.code-workspace @@ -20,5 +20,8 @@ } }, "terminal.integrated.defaultProfile.linux": "xtchain", + "files.associations": { + "globals.h": "c" + }, } } \ No newline at end of file diff --git a/sdk/xtdk/bmtypes.h b/sdk/xtdk/bmtypes.h index 8f75f5a..c5cda46 100644 --- a/sdk/xtdk/bmtypes.h +++ b/sdk/xtdk/bmtypes.h @@ -11,6 +11,19 @@ #include +typedef struct _XTBL_INI_SECTION +{ + LIST_ENTRY Flink; + LIST_ENTRY Options; + PWCHAR SectionName; +} XTBL_INI_SECTION, *PXTBL_INI_SECTION; + +typedef struct _XTBL_INI_OPTION +{ + LIST_ENTRY Flink; + PWCHAR Name; + PWCHAR Value; +} XTBL_INI_OPTION, *PXTBL_INI_OPTION; /* XTLDR configuration data */ typedef struct _XTBM_CONFIGURATION diff --git a/xtldr2/config.c b/xtldr2/config.c index 2c8ac23..cf5ffb0 100644 --- a/xtldr2/config.c +++ b/xtldr2/config.c @@ -4,10 +4,115 @@ * FILE: xtldr/config.c * DESCRIPTION: XT Boot Loader Configuration * DEVELOPERS: Rafal Kupiec + * Jozef Nagy */ #include +/** + * Parses configuration INI file contents. + * + * @param FileContents + * Supplies a pointer to a wide string containing contents of the configuration file. + * + * @param Sections + * Supplies a pointer to a linked list which will be written by this routine. + * + * @return This routine returns a status code. + * + * @since XT 1.0 + */ +XTCDECL +XTSTATUS +BlConfigParseIniFile(IN PWCHAR FileContents, + OUT PLIST_ENTRY Sections) +{ + PWCHAR Argument, LastArg, CurrentSectionName; + SIZE_T Length; + + /* Set a default section name */ + CurrentSectionName = L"Unknown"; + + /* Tokenize provided options */ + Argument = RtlWideStringTokenize(FileContents, L"\r\n", &LastArg); + + /* Iterate over all arguments passed to boot loader */ + while(Argument != NULL) + { + /* Check every line */ + if(RtlWideStringCompare(Argument, L"[", 1) == 0) + { + /* Skip to the section name */ + Argument += 1; + Length = RtlWideStringLength(Argument, 0); + + /* Everything until ']' belongs to the section's name */ + // CurrentSectionName = RtlWideStringTokenize(Argument, L"]", &LastArg); + BlConsolePrint(L"New section: %S\n", CurrentSectionName); + } + else if(RtlWideStringCompare(Argument, L"Default=", 8) == 0) + { + /* Skip to the argument value and calculate argument length */ + Argument += 8; + Length = RtlWideStringLength(Argument, 0); + + /* Set a default section */ + BlConsolePrint(L"Setting DEFAULT value to %S\n", Argument); + } + else if(RtlWideStringCompare(Argument, L"Debug=", 6) == 0) + { + /* Skip to the argument value */ + Argument += 6; + Length = RtlWideStringLength(Argument, 0); + + /* Set a debug port and baud rate */ + BlConsolePrint(L"Setting DEBUG value to %S\n", Argument); + } + else if(RtlWideStringCompare(Argument, L"Theme=", 6) == 0) + { + /* Skip to the argument value */ + Argument += 6; + Length = RtlWideStringLength(Argument, 0); + + /* Set a theme name */ + BlConsolePrint(L"Setting THEME value to %S\n", Argument); + } + else if(RtlWideStringCompare(Argument, L"Timeout=", 8) == 0) + { + /* Skip to the argument value */ + Argument += 8; + Length = RtlWideStringLength(Argument, 0); + + /* Set a timeout */ + BlConsolePrint(L"Setting TIMEOUT value to %S\n", Argument); + } + else if(RtlWideStringCompare(Argument, L"Tune=", 5) == 0) + { + /* Skip to the argument value */ + Argument += 5; + Length = RtlWideStringLength(Argument, 0); + + /* Set a tune */ + BlConsolePrint(L"Setting TUNE value to %S\n", Argument); + } + else if(RtlWideStringCompare(Argument, L"#", 1) == 0) + { + /* We can safely ignore a comment */ + Argument += 1; + } + else + { + /* Configuration file might be corrupt */ + BlConsolePrint(L"Unknown argument: %S\n", Argument); + return STATUS_INVALID_PARAMETER; + } + + /* Take next argument */ + Argument = RtlWideStringTokenize(NULL, L"\n", &LastArg); + } + + return STATUS_SUCCESS; +} /** * Parses command line arguments and updates global configuration. diff --git a/xtldr2/includes/bootman.h b/xtldr2/includes/bootman.h index a4665fa..e2a05b1 100644 --- a/xtldr2/includes/bootman.h +++ b/xtldr2/includes/bootman.h @@ -79,6 +79,11 @@ BlpStringFormat(IN BMPRINTCHAR PrintCharRoutine, IN PUINT16 Format, IN ...); +XTCDECL +XTSTATUS +BlConfigParseIniFile(IN PWCHAR FileContents, + OUT PLIST_ENTRY Sections); + XTCDECL VOID BlpConfigParseCommandLine(VOID); diff --git a/xtldr2/xtldr.c b/xtldr2/xtldr.c index 8a6bb3f..76940fc 100644 --- a/xtldr2/xtldr.c +++ b/xtldr2/xtldr.c @@ -28,15 +28,22 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle, IN PEFI_SYSTEM_TABLE SystemTable) { EFI_STATUS Status; + LIST_ENTRY ConfigSections; /* Set the system table and image handle */ EfiImageHandle = ImageHandle; EfiSystemTable = SystemTable; + + RtlInitializeListHead(&ConfigSections); + /* Initialize UEFI console and early print XTLDR version */ BlConsoleInitialize(); BlConsolePrint(L"XTLDR boot loader v%s\n", XTOS_VERSION); + /* Parse INI Configuration file */ + BlConfigParseIniFile(L"# This is the XT Boot Loader (XTLDR) configuration file. It follows an INI format and is divided into sections, which\r\n# contain a properties. Each property has a name and value delimited by an equal (=) character. Comments must start\r\n# with a semicolon (;) or a hash character (#) and run to the end of the line.\r\n#\r\n# Basic section is [XTLDR] which contains a bootloader specific options:\r\n# Debug - enables the debugging port and consists of two comma-separated parameters: com port and baud rate;\r\n# it is also possible to specify custom port address with: COM0:[address],[baud_rate]\r\n# Default - specifies which operating system listen in config file will be started if no choice is made\r\n# Theme - allows to set a custom theme to personalize XTLDR's look'n'feel\r\n# Timeout - sets the countdown timer (in seconds) before the default OS get started automatically\r\n# Tune - plays a tune on the pcspeaker right before the XTLDR loads\r\n#\r\n# Another type of section is [OS-Section] which adds a new position (operating system) to the boot menu. Each type\r\n# of the operating system provides a set of available parameters. If unsupported option is added, it is being ignored\r\n# by the XT Boot Loader. The available options are:\r\n# SystemName - sets a long operating system name that will be shown on the boot menu\r\n# SystemType - specifies an OS type from a predefined list of supported boot protocols\r\n# SystemPath - the ARC path, eg. multi(0)disk(0)rdisk(0)partition(1)\r\n# KernelFile - sets kernel filename with optional path relative to SystemPath\r\n# InitrdFile - sets initramfs image filename with optional path relative to SystemPath\r\n# HalFile - sets HAL filename with optional path relative to SystemPath\r\n# Parameters - specifies extra boot options for the kernel\r\n[XTLDR]Tune=400 880 2 988 2 783 2 392 2 587 3Debug=COM1,115200Timeout=30Theme=FancyDefault=ExectOS[ExectOS]SystemName=\"ExectOS Operating System\"SystemType=XTOSSystemPath=multi(0)disk(0)rdisk(0)partition(1)/ExectOSKernelFile=xtoskrnl.exeParameters=DEBUG DEBUGPORT=COM1,115200[Windows]SystemName=\"Microsoft Windows 2000\"SystemType=NT50SystemPath=multi(0)disk(0)rdisk(0)partition(2)/WindowsKernelFile=ntoskrnl.exeHalFile=hal.dllParameters=/NOGUIBOOT /MININT[Linux]SystemName=\"GNU/Linux\"SystemType=LINUXSystemPath=multi(0)disk(0)rdisk(0)partition(3)/bootKernelFile=vmlinuzInitrdFile=initramfs.cpio.gzParameters=root=/dev/xvda3 rootfstype=ext4", &ConfigSections); + /* Temporary infinite loop */ for(;;);