Rewrite INI parser

This commit is contained in:
Jozef Nagy 2023-12-06 21:08:46 +01:00
parent e8574a6d8c
commit fabadd013c
No known key found for this signature in database
GPG Key ID: 5F72C3BF3BD614D8
3 changed files with 61 additions and 54 deletions

View File

@ -66,7 +66,7 @@ file(RELATIVE_PATH _PATH_PREFIX ${EXECTOS_BINARY_DIR} ${EXECTOS_SOURCE_DIR})
add_compiler_flags(-D__RELFILE__="&__FILE__[__FILE__[0] == '.' ? sizeof \\\"${_PATH_PREFIX}\\\" - 1 : sizeof XTOS_SOURCE_DIR]") add_compiler_flags(-D__RELFILE__="&__FILE__[__FILE__[0] == '.' ? sizeof \\\"${_PATH_PREFIX}\\\" - 1 : sizeof XTOS_SOURCE_DIR]")
# Set the virtual disk image size (in MiB) # Set the virtual disk image size (in MiB)
set_disk_image_size(128) set_disk_image_size(16)
# Build all subprojects # Build all subprojects
add_subdirectory(bootdata) add_subdirectory(bootdata)

View File

@ -13,7 +13,7 @@
* Parses configuration INI file contents. * Parses configuration INI file contents.
* *
* @param FileContents * @param FileContents
* Supplies a pointer to a string containing contents of the configuration file. * Supplies a pointer to a wide string containing contents of the configuration file.
* *
* @param Sections * @param Sections
* Supplies a pointer to a linked list which will be written by this routine. * Supplies a pointer to a linked list which will be written by this routine.
@ -27,76 +27,83 @@ XTSTATUS
BlConfigParseIniFile(IN PWCHAR FileContents, BlConfigParseIniFile(IN PWCHAR FileContents,
OUT PLIST_ENTRY Sections) OUT PLIST_ENTRY Sections)
{ {
PWCHAR Line, LastLine, Key, Value; PWCHAR CurrentSectionName, Key, Value;
SIZE_T Length, KeyLength; PWCHAR Input;
UINT SectionLength, KeyLength, ValueLength;
UINT Error; Input = FileContents;
CurrentSectionName = L"None";
/* There's not error at the moment */ while(*Input != 0)
Error = 0;
/* Tokenize provided options */
Line = RtlWideStringTokenize(FileContents, L"\n", &LastLine);
while(Line != NULL)
{ {
Length = RtlWideStringLength(Line, 0); SectionLength = 0;
KeyLength = 0;
ValueLength = 0;
/* Parse */ if(*Input == ';' || *Input == '#')
if (Length < 1)
{ {
/* Blank line */ /* Skip comments */
while(*Input != 0 && *Input != '\n')
{
Input++;
}
} }
else if (RtlWideStringCompare(Line, L"#", 1) == 0 || else if(*Input == '\n')
RtlWideStringCompare(Line, L";", 1) == 0)
{ {
/* Don't parse a comment */ /* Skip newline */
Input++;
} }
else if (RtlWideStringCompare(Line, L"[", 1) == 0 && else if(*Input == '[')
RtlWideStringCompare(Line + Length - 3, L"]", 1) == 0)
{ {
/* Get the section name */
/* Remove brackets around section name */ Input++;
PWCHAR SectionName = Line + 1; CurrentSectionName = Input;
SectionName[Length - 3] = 0;
/* Save section name */ /* Skip to the next line */
BlConsolePrint(L"Section: %S\n", SectionName); while(*Input != ']' && *Input != 0 && *Input != '\n')
{
Input++;
SectionLength++;
}
CurrentSectionName[SectionLength] = 0;
Input++;
BlConsolePrint(L"[%S]\n", CurrentSectionName);
} }
else else
{ {
KeyLength = 0; /* Get the length of key */
while(*Input != '=' && *Input != 0 && *Input != '\n')
/* Set key=value pair to the entire line for further processing */
Value = Line;
Key = Line;
/* Get the value */
while(Value != 0 &&
RtlWideStringCompare(Value, L"=", 1) != 0)
{ {
Value++; Input++;
KeyLength++; KeyLength++;
} }
/* Get rid of the leading '=' */ /* Set key */
Value++; Input -= KeyLength;
Key = Input;
/* Set the key */
Key[KeyLength] = 0; Key[KeyLength] = 0;
BlConsolePrint(L"%S=", Key);
BlConsolePrint(L"Key: %S\n Value: %S\n", Key, Value); /* Skip to the value */
Input += KeyLength + 1;
/* Get the length of value */
while(*Input != 0 && *Input != '\n')
{
Input++;
ValueLength++;
}
/* Set value */
Input -= ValueLength;
Value = Input;
Value[ValueLength] = 0;
BlConsolePrint(L"%S\n", Value);
Input += ValueLength;
Input++;
} }
if(Error == 1)
{
/* Display error message and exit */
BlConsolePrint(L"Syntax error\n");
return STATUS_INVALID_PARAMETER;
}
/* Take next line */
Line = RtlWideStringTokenize(NULL, L"\n", &LastLine);
} }
return STATUS_SUCCESS; return STATUS_SUCCESS;

View File

@ -38,13 +38,13 @@ BlStartXtLoader(IN EFI_HANDLE ImageHandle,
RtlInitializeListHead(&ConfigSections); RtlInitializeListHead(&ConfigSections);
/* Initialize UEFI console and early print XTLDR version */ /* Initialize UEFI console and early print XTLDR version */
BlConsoleInitialize(); BlpConsoleInitialize();
/* Parse INI Configuration file */ /* 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\n# contain a properties. Each property has a name and value delimited by an equal (=) character. Comments must start\n# with a semicolon (;) or a hash character (#) and run to the end of the line.\n#\n# Basic section is [XTLDR] which contains a bootloader specific options:\n# Debug - enables the debugging port and consists of two comma-separated parameters: com port and baud rate;\n# it is also possible to specify custom port address with: COM0:[address],[baud_rate]\n# Default - specifies which operating system listen in config file will be started if no choice is made\n# Theme - allows to set a custom theme to personalize XTLDR's look'n'feel\n# Timeout - sets the countdown timer (in seconds) before the default OS get started automatically\n# Tune - plays a tune on the pcspeaker right before the XTLDR loads\n#\n# Another type of section is [OS-Section] which adds a new position (operating system) to the boot menu. Each type\n# of the operating system provides a set of available parameters. If unsupported option is added, it is being ignored\n# by the XT Boot Loader. The available options are:\n# SystemName - sets a long operating system name that will be shown on the boot menu\n# SystemType - specifies an OS type from a predefined list of supported boot protocols\n# SystemPath - the ARC path, eg. multi(0)disk(0)rdisk(0)partition(1)\n# KernelFile - sets kernel filename with optional path relative to SystemPath\n# InitrdFile - sets initramfs image filename with optional path relative to SystemPath\n# HalFile - sets HAL filename with optional path relative to SystemPath\n# Parameters - specifies extra boot options for the kernel\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);
/* Initialize XTLDR and early print XTLDR version */ /* Initialize XTLDR and early print XTLDR version */
BlpInitializeEfiBootLoader(); BlpInitializeEfiBootLoader();
BlConfigParseIniFile(L"# This is the XT Boot Loader (XTLDR) configuration file. It follows an INI format and is divided into sections, which\n# contain a properties. Each property has a name and value delimited by an equal (=) character. Comments must start\n# with a semicolon (;) or a hash character (#) and run to the end of the line.\n#\n# Basic section is [XTLDR] which contains a bootloader specific options:\n# Debug - enables the debugging port and consists of two comma-separated parameters: com port and baud rate;\n# it is also possible to specify custom port address with: COM0:[address],[baud_rate]\n# Default - specifies which operating system listen in config file will be started if no choice is made\n# Timeout - sets the countdown timer (in seconds) before the default OS get started automatically\n# Tune - plays a tune on the pcspeaker right before the XTLDR loads\n#\n# Another type of section is [OS-Section] which adds a new position (operating system) to the boot menu. Each type\n# of the operating system provides a set of available parameters. If unsupported option is added, it is being ignored\n# by the XT Boot Loader. The available options are:\n# SystemName - sets a long operating system name that will be shown on the boot menu\n# SystemType - specifies an OS type from a predefined list of supported boot protocols\n# SystemPath - the ARC path, eg. multi(0)disk(0)rdisk(0)partition(1)\n# KernelFile - sets kernel filename with optional path relative to SystemPath\n# InitrdFile - sets initramfs image filename with optional path relative to SystemPath\n# HalFile - sets HAL filename with optional path relative to SystemPath\n# Parameters - specifies extra boot options for the kernel\n\n[XTLDR]\nTune=400 880 2 988 2 783 2 392 2 587 3\nDebug=COM1,115200\nTimeout=30\nDefault=ExectOS\n\n[ExectOS]\nSystemName=\"ExectOS Operating System\"\nSystemType=XTOS\nSystemPath=multi(0)disk(0)rdisk(0)partition(1)/ExectOS\nKernelFile=xtoskrnl.exe\nParameters=DEBUG DEBUGPORT=COM1,115200\n\n[Windows]\nSystemName=\"Microsoft Windows 2000\"\nSystemType=NT50\nSystemPath=multi(0)disk(0)rdisk(0)partition(2)/Windows\nKernelFile=ntoskrnl.exe\nHalFile=hal.dll\nParameters=/NOGUIBOOT /MININT\n\n[Linux]\nSystemName=\"GNU/Linux\"\nSystemType=LINUX\nSystemPath=multi(0)disk(0)rdisk(0)partition(3)/boot\nKernelFile=vmlinuz\nInitrdFile=initramfs.cpio.gz\nParameters=root=/dev/xvda3 rootfstype=ext4", &ConfigSections);
BlConsolePrint(L"XTLDR boot loader v%s\n", XTOS_VERSION); BlConsolePrint(L"XTLDR boot loader v%s\n", XTOS_VERSION);
/* Parse configuration options passed from UEFI shell */ /* Parse configuration options passed from UEFI shell */