[FREELDR] The multiboot command-line and FREELOADER section options become global settings (#6803)

Dual-license settings.c: "old" code (CmdLineParse) stays BSD
and new code (mine) becomes MIT.
This commit is contained in:
Hermès Bélusca-Maïto 2024-04-24 19:51:14 +02:00
parent 2f4bb4084d
commit 7bee32d237
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
9 changed files with 152 additions and 156 deletions

View file

@ -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))

View file

@ -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]);

View file

@ -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);

View file

@ -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()

View file

@ -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);

View file

@ -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 <hermes.belusca-maito@reactos.org>
*/
#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 */

View file

@ -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)
{

View file

@ -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)

View file

@ -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 <ros.arm@reactos.org>
* Copyright 2015-2024 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
*/
/* 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 */