diff --git a/boot/freeldr/freeldr/arch/uefi/uefildr.c b/boot/freeldr/freeldr/arch/uefi/uefildr.c index 9f801d978a8..69d5041d2ca 100644 --- a/boot/freeldr/freeldr/arch/uefi/uefildr.c +++ b/boot/freeldr/freeldr/arch/uefi/uefildr.c @@ -26,17 +26,19 @@ EfiEntry( _In_ EFI_HANDLE ImageHandle, _In_ EFI_SYSTEM_TABLE *SystemTable) { + PCSTR CmdLine = ""; // FIXME: Determine a command-line from UEFI boot options + SystemTable->ConOut->OutputString(SystemTable->ConOut, L"UEFI EntryPoint: Starting freeldr from UEFI"); GlobalImageHandle = ImageHandle; GlobalSystemTable = SystemTable; - /* Needed for default settings */ - CmdLineParse(""); + /* Load the default settings from the command-line */ + LoadSettings(CmdLine); /* Debugger pre-initialization */ - DebugInit(0); + DebugInit(BootMgrInfo.DebugString); - MachInit(""); + MachInit(CmdLine); /* UI pre-initialization */ if (!UiInitialize(FALSE)) diff --git a/boot/freeldr/freeldr/bootmgr.c b/boot/freeldr/freeldr/bootmgr.c index f843e6addb9..04d8683a64d 100644 --- a/boot/freeldr/freeldr/bootmgr.c +++ b/boot/freeldr/freeldr/bootmgr.c @@ -322,28 +322,6 @@ EditOperatingSystemEntry( } #endif // HAS_OPTION_MENU_EDIT_CMDLINE -static LONG -GetTimeOut( - IN ULONG_PTR FrLdrSectionId) -{ - LONG TimeOut = -1; - CHAR TimeOutText[20]; - - TimeOut = CmdLineGetTimeOut(); - if (TimeOut >= 0) - return TimeOut; - - TimeOut = -1; - - if ((FrLdrSectionId != 0) && - IniReadSettingByName(FrLdrSectionId, "TimeOut", TimeOutText, sizeof(TimeOutText))) - { - TimeOut = atoi(TimeOutText); - } - - return TimeOut; -} - BOOLEAN MainBootMenuKeyPressFilter( IN ULONG KeyPress, @@ -370,14 +348,12 @@ MainBootMenuKeyPressFilter( VOID RunLoader(VOID) { - ULONG_PTR SectionId; - LONG TimeOut; - ULONG OperatingSystemCount; OperatingSystemItem* OperatingSystemList; - PCSTR* OperatingSystemDisplayNames; - ULONG DefaultOperatingSystem; - ULONG SelectedOperatingSystem; - ULONG i; + PCSTR* OperatingSystemDisplayNames; + ULONG OperatingSystemCount; + ULONG DefaultOperatingSystem; + ULONG SelectedOperatingSystem; + ULONG i; if (!MachInitializeBootDevices()) { @@ -395,24 +371,23 @@ VOID RunLoader(VOID) #endif #endif + /* Open FREELDR.INI and load the global FreeLoader settings */ if (!IniFileInitialize()) { UiMessageBoxCritical("Error initializing .ini file."); return; } - - /* Open the [FreeLoader] section */ - if (!IniOpenSection("FreeLoader", &SectionId)) + LoadSettings(NULL); +#if 0 + if (FALSE) { - UiMessageBoxCritical("Section [FreeLoader] not found in freeldr.ini."); + UiMessageBoxCritical("Could not load global FreeLoader settings."); return; } +#endif /* Debugger main initialization */ - DebugInit(SectionId); - - /* Retrieve the default timeout */ - TimeOut = GetTimeOut(SectionId); + DebugInit(BootMgrInfo.DebugString); /* UI main initialization */ if (!UiInitialize(TRUE)) @@ -421,8 +396,7 @@ VOID RunLoader(VOID) return; } - OperatingSystemList = InitOperatingSystemList(SectionId, - &OperatingSystemCount, + OperatingSystemList = InitOperatingSystemList(&OperatingSystemCount, &DefaultOperatingSystem); if (!OperatingSystemList) { @@ -446,7 +420,7 @@ VOID RunLoader(VOID) } /* Find all the message box settings and run them */ - UiShowMessageBoxesInSection(SectionId); + UiShowMessageBoxesInSection(BootMgrInfo.FrLdrSection); for (;;) { @@ -461,7 +435,7 @@ VOID RunLoader(VOID) OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, - TimeOut, + BootMgrInfo.TimeOut, &SelectedOperatingSystem, FALSE, MainBootMenuKeyPressFilter, @@ -471,7 +445,7 @@ VOID RunLoader(VOID) goto Reboot; } - TimeOut = -1; + BootMgrInfo.TimeOut = -1; /* Load the chosen operating system */ LoadOperatingSystem(&OperatingSystemList[SelectedOperatingSystem]); diff --git a/boot/freeldr/freeldr/freeldr.c b/boot/freeldr/freeldr/freeldr.c index da9f8964fdb..c7bac46d93f 100644 --- a/boot/freeldr/freeldr/freeldr.c +++ b/boot/freeldr/freeldr/freeldr.c @@ -42,10 +42,11 @@ CCHAR FrLdrBootPath[MAX_PATH] = ""; VOID __cdecl BootMain(IN PCCH CmdLine) { - CmdLineParse(CmdLine); + /* Load the default settings from the command-line */ + LoadSettings(CmdLine); /* Debugger pre-initialization */ - DebugInit(0); + DebugInit(BootMgrInfo.DebugString); MachInit(CmdLine); diff --git a/boot/freeldr/freeldr/include/debug.h b/boot/freeldr/freeldr/include/debug.h index 3c9d741dac6..6460cacd2c3 100644 --- a/boot/freeldr/freeldr/include/debug.h +++ b/boot/freeldr/freeldr/include/debug.h @@ -40,7 +40,10 @@ #if DBG - VOID DebugInit(IN ULONG_PTR FrLdrSectionId); + VOID + DebugInit( + _In_ PCSTR DebugString); + ULONG DbgPrint(const char *Format, ...); VOID DbgPrint2(ULONG Mask, ULONG Level, const char *File, ULONG Line, char *Format, ...); VOID DebugDumpBuffer(ULONG Mask, PVOID Buffer, ULONG Length); @@ -114,7 +117,7 @@ void MEMORY_WRITE_BREAKPOINT4(unsigned long addr); #define UNIMPLEMENTED - #define DebugInit(FrLdrSectionId) + #define DebugInit(DebugString) #define BugCheck(fmt, ...) #define DbgDumpBuffer(mask, buf, len) #define DebugDisableScreenPort() diff --git a/boot/freeldr/freeldr/include/oslist.h b/boot/freeldr/freeldr/include/oslist.h index b4bf62f9d43..4356b99659f 100644 --- a/boot/freeldr/freeldr/include/oslist.h +++ b/boot/freeldr/freeldr/include/oslist.h @@ -29,6 +29,5 @@ typedef struct tagOperatingSystemItem OperatingSystemItem* InitOperatingSystemList( - IN ULONG_PTR FrLdrSectionId, - OUT PULONG OperatingSystemCount, - OUT PULONG DefaultOperatingSystem); + _Out_ PULONG OperatingSystemCount, + _Out_ PULONG DefaultOperatingSystem); diff --git a/boot/freeldr/freeldr/include/settings.h b/boot/freeldr/freeldr/include/settings.h index 47a733b60ef..8c60a145068 100644 --- a/boot/freeldr/freeldr/include/settings.h +++ b/boot/freeldr/freeldr/include/settings.h @@ -1,28 +1,24 @@ /* - * FreeLdr boot loader - * Copyright (C) 2002, 2003 ReactOS Team - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * PROJECT: FreeLoader + * LICENSE: MIT (https://spdx.org/licenses/MIT) + * PURPOSE: Command-line parsing and global settings management + * COPYRIGHT: Copyright 2024 Hermès Bélusca-Maïto */ #pragma once -VOID CmdLineParse(IN PCSTR CmdLine); +typedef struct _BOOTMGRINFO +{ + PCSTR DebugString; + PCSTR DefaultOs; + LONG TimeOut; + ULONG_PTR FrLdrSection; +} BOOTMGRINFO, *PBOOTMGRINFO; -PCSTR CmdLineGetDebugString(VOID); -PCSTR CmdLineGetDefaultOS(VOID); -LONG CmdLineGetTimeOut(VOID); +extern BOOTMGRINFO BootMgrInfo; + +VOID +LoadSettings( + _In_opt_ PCSTR CmdLine); /* EOF */ diff --git a/boot/freeldr/freeldr/lib/debug.c b/boot/freeldr/freeldr/lib/debug.c index 4edb35e4bae..c1f9d954cd3 100644 --- a/boot/freeldr/freeldr/lib/debug.c +++ b/boot/freeldr/freeldr/lib/debug.c @@ -59,11 +59,14 @@ VOID ARMWriteToUART(UCHAR Data); #endif -VOID DebugInit(IN ULONG_PTR FrLdrSectionId) +VOID +DebugInit( + _In_ PCSTR DebugString) { - PCHAR CommandLine, PortString, BaudString, IrqString; + static BOOLEAN Initialized = FALSE; + PSTR CommandLine, PortString, BaudString, IrqString; ULONG Value; - CHAR DebugString[256]; + CHAR DbgStringBuffer[256]; /* Always reset the debugging channels */ @@ -87,29 +90,20 @@ VOID DebugInit(IN ULONG_PTR FrLdrSectionId) DbgChannels[DPRINT_WINDOWS] = MAX_LEVEL; #endif - /* Check for pre- or main initialization phase */ - if (FrLdrSectionId == 0) + CommandLine = NULL; + if (!DebugString || !*DebugString) { - /* Pre-initialization phase: use the FreeLdr command-line debugging string */ - CommandLine = (PCHAR)CmdLineGetDebugString(); - - /* If no command-line is provided, initialize the debug port with default settings */ - if (CommandLine == NULL) + /* No command-line is provided: during pre-initialization, + * initialize the debug port with default settings; + * otherwise just return during main initialization */ + if (!Initialized) goto Done; - - strcpy(DebugString, CommandLine); - } - else - { - /* Main initialization phase: use the FreeLdr INI debugging string */ - if (!IniReadSettingByName(FrLdrSectionId, "Debug", DebugString, sizeof(DebugString))) - { - return; - } + return; } - /* Get the Command Line */ - CommandLine = DebugString; + /* Get a copy of the command-line */ + strcpy(DbgStringBuffer, DebugString); + CommandLine = DbgStringBuffer; /* Upcase it */ _strupr(CommandLine); @@ -193,6 +187,8 @@ VOID DebugInit(IN ULONG_PTR FrLdrSectionId) } Done: + Initialized = TRUE; + /* Try to initialize the port; if it fails, remove the corresponding flag */ if (DebugPort & RS232) { diff --git a/boot/freeldr/freeldr/oslist.c b/boot/freeldr/freeldr/oslist.c index 3a05ebd2fba..a472345aa87 100644 --- a/boot/freeldr/freeldr/oslist.c +++ b/boot/freeldr/freeldr/oslist.c @@ -44,15 +44,12 @@ static PCSTR CopyString(PCSTR Source) OperatingSystemItem* InitOperatingSystemList( - IN ULONG_PTR FrLdrSectionId, - OUT PULONG OperatingSystemCount, - OUT PULONG DefaultOperatingSystem) + _Out_ PULONG OperatingSystemCount, + _Out_ PULONG DefaultOperatingSystem) { - ULONG DefaultOS = 0; - PCSTR DefaultOSName = NULL; - CHAR DefaultOSText[80]; - OperatingSystemItem* Items; + PCSTR DefaultOSName; + ULONG DefaultOS = 0; ULONG Count; ULONG i; ULONG_PTR OsSectionId, SectionId; @@ -63,7 +60,7 @@ InitOperatingSystemList( CHAR SettingName[260]; CHAR SettingValue[260]; CHAR BootType[80]; - CHAR TempBuffer[sizeof(SettingValue)/sizeof(CHAR)]; + CHAR TempBuffer[_countof(SettingValue)]; /* Open the [Operating Systems] section */ if (!IniOpenSection("Operating Systems", &OsSectionId)) @@ -77,16 +74,8 @@ InitOperatingSystemList( if (!Items) return NULL; - /* Retrieve which OS is the default one */ - DefaultOSName = CmdLineGetDefaultOS(); - if (!DefaultOSName || !*DefaultOSName) - { - if ((FrLdrSectionId != 0) && - IniReadSettingByName(FrLdrSectionId, "DefaultOS", DefaultOSText, sizeof(DefaultOSText))) - { - DefaultOSName = DefaultOSText; - } - } + /* Retrieve the default OS */ + DefaultOSName = BootMgrInfo.DefaultOs; /* Now loop through the operating system section and load each item */ for (i = 0; i < Count; ++i) diff --git a/boot/freeldr/freeldr/settings.c b/boot/freeldr/freeldr/settings.c index c41466e6163..82051714bcc 100644 --- a/boot/freeldr/freeldr/settings.c +++ b/boot/freeldr/freeldr/settings.c @@ -1,9 +1,10 @@ /* - * PROJECT: ReactOS Boot Loader - * LICENSE: BSD - See COPYING.ARM in the top level directory - * FILE: boot/freeldr/freeldr/cmdline.c - * PURPOSE: FreeLDR Command Line Parsing - * PROGRAMMERS: ReactOS Portable Systems Group + * PROJECT: FreeLoader + * LICENSE: BSD - See COPYING.ARM in the top level directory + * or MIT (https://spdx.org/licenses/MIT) + * PURPOSE: Command-line parsing and global settings management + * COPYRIGHT: Copyright 2008-2010 ReactOS Portable Systems Group + * Copyright 2015-2024 Hermès Bélusca-Maïto */ /* INCLUDES *******************************************************************/ @@ -12,32 +13,27 @@ /* GLOBALS ********************************************************************/ -typedef struct tagCMDLINEINFO -{ - PCSTR DebugString; - PCSTR DefaultOs; - LONG TimeOut; -} CMDLINEINFO, *PCMDLINEINFO; - -CCHAR DebugString[256]; -CCHAR DefaultOs[256]; -CMDLINEINFO CmdLineInfo; +static CCHAR DebugString[256]; +static CCHAR DefaultOs[256]; +BOOTMGRINFO BootMgrInfo = {0}; /* FUNCTIONS ******************************************************************/ -VOID -CmdLineParse(IN PCSTR CmdLine) +static VOID +CmdLineParse( + _In_ PCSTR CmdLine) { PCHAR End, Setting; ULONG_PTR Length, Offset = 0; /* Set defaults */ - CmdLineInfo.DebugString = NULL; - CmdLineInfo.DefaultOs = NULL; - CmdLineInfo.TimeOut = -1; + BootMgrInfo.DebugString = NULL; + BootMgrInfo.DefaultOs = NULL; + BootMgrInfo.TimeOut = -1; + // BootMgrInfo.FrLdrSection = 0; /* - * Get debug string, in the following format: + * Get the debug string, in the following format: * "debug=option1=XXX;option2=YYY;..." * and translate it into the format: * "OPTION1=XXX OPTION2=YYY ..." @@ -62,18 +58,18 @@ CmdLineParse(IN PCSTR CmdLine) Setting++; } - CmdLineInfo.DebugString = DebugString; + BootMgrInfo.DebugString = DebugString; } - /* Get timeout */ + /* Get the timeout */ Setting = strstr(CmdLine, "timeout="); if (Setting) { - CmdLineInfo.TimeOut = atoi(Setting + + BootMgrInfo.TimeOut = atoi(Setting + sizeof("timeout=") - sizeof(ANSI_NULL)); } - /* Get default OS */ + /* Get the default OS */ Setting = strstr(CmdLine, "defaultos="); if (Setting) { @@ -84,10 +80,10 @@ CmdLineParse(IN PCSTR CmdLine) /* Copy the default OS */ RtlStringCbCopyNA(DefaultOs, sizeof(DefaultOs), Setting, Length); - CmdLineInfo.DefaultOs = DefaultOs; + BootMgrInfo.DefaultOs = DefaultOs; } - /* Get ramdisk base address */ + /* Get the ramdisk base address */ Setting = strstr(CmdLine, "rdbase="); if (Setting) { @@ -97,7 +93,7 @@ CmdLineParse(IN PCSTR CmdLine) NULL, 0); } - /* Get ramdisk size */ + /* Get the ramdisk size */ Setting = strstr(CmdLine, "rdsize="); if (Setting) { @@ -106,7 +102,7 @@ CmdLineParse(IN PCSTR CmdLine) NULL, 0); } - /* Get ramdisk offset */ + /* Get the ramdisk offset */ Setting = strstr(CmdLine, "rdoffset="); if (Setting) { @@ -119,20 +115,60 @@ CmdLineParse(IN PCSTR CmdLine) gInitRamDiskBase = (PVOID)((ULONG_PTR)gInitRamDiskBase + Offset); } -PCSTR -CmdLineGetDebugString(VOID) +VOID +LoadSettings( + _In_opt_ PCSTR CmdLine) { - return CmdLineInfo.DebugString; + /* Pre-initialization: The settings originate from the command-line. + * Main initialization: Overwrite them if needed with those from freeldr.ini */ + if (CmdLine) + { + CmdLineParse(CmdLine); + return; + } + else if (IsListEmpty(&IniFileSectionListHead)) + { + // ERR("LoadSettings() called but no freeldr.ini\n"); + return; + } + + /* Open the [FreeLoader] section and load the settings */ + if ((BootMgrInfo.FrLdrSection == 0) && + !IniOpenSection("FreeLoader", &BootMgrInfo.FrLdrSection)) + { + UiMessageBoxCritical("Section [FreeLoader] not found in freeldr.ini"); + return; + } + + /* Get the debug string. Always override it with the one from freeldr.ini */ + if (IniReadSettingByName(BootMgrInfo.FrLdrSection, "Debug", + DebugString, sizeof(DebugString))) + { + BootMgrInfo.DebugString = DebugString; + } + + /* Get the timeout. Keep the existing one if it is valid, + * otherwise retrieve it from freeldr.ini */ + if (BootMgrInfo.TimeOut < 0) + { + CHAR TimeOutText[20]; + BootMgrInfo.TimeOut = -1; + if (IniReadSettingByName(BootMgrInfo.FrLdrSection, "TimeOut", + TimeOutText, sizeof(TimeOutText))) + { + BootMgrInfo.TimeOut = atoi(TimeOutText); + } + } + + /* Get the default OS */ + if (!BootMgrInfo.DefaultOs || !*BootMgrInfo.DefaultOs) + { + if (IniReadSettingByName(BootMgrInfo.FrLdrSection, "DefaultOS", + DefaultOs, sizeof(DefaultOs))) + { + BootMgrInfo.DefaultOs = DefaultOs; + } + } } -PCSTR -CmdLineGetDefaultOS(VOID) -{ - return CmdLineInfo.DefaultOs; -} - -LONG -CmdLineGetTimeOut(VOID) -{ - return CmdLineInfo.TimeOut; -} +/* EOF */