From 9ae73010c219bf7195a3b4be2897269c1e57a0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Tue, 19 Apr 2022 00:09:49 +0200 Subject: [PATCH] [FREELDR] Factor duplicated code into a GetOSLoadingMethod() helper This removes duplicated code present in both LoadOperatingSystem() and EditOperatingSystemEntry(). + Add SAL annotations to the related functions. --- boot/freeldr/freeldr/bootmgr.c | 120 ++++++++++++------------- boot/freeldr/freeldr/include/freeldr.h | 11 ++- 2 files changed, 69 insertions(+), 62 deletions(-) diff --git a/boot/freeldr/freeldr/bootmgr.c b/boot/freeldr/freeldr/bootmgr.c index 2c837951fec..98abf5ed955 100644 --- a/boot/freeldr/freeldr/bootmgr.c +++ b/boot/freeldr/freeldr/bootmgr.c @@ -26,31 +26,33 @@ DBG_DEFAULT_CHANNEL(WARNING); /* GLOBALS ********************************************************************/ -typedef -VOID +typedef VOID (*EDIT_OS_ENTRY_PROC)( - IN OUT OperatingSystemItem* OperatingSystem); + _Inout_ OperatingSystemItem* OperatingSystem); static VOID EditCustomBootReactOSSetup( - IN OUT OperatingSystemItem* OperatingSystem) + _Inout_ OperatingSystemItem* OperatingSystem) { EditCustomBootReactOS(OperatingSystem, TRUE); } static VOID EditCustomBootNTOS( - IN OUT OperatingSystemItem* OperatingSystem) + _Inout_ OperatingSystemItem* OperatingSystem) { EditCustomBootReactOS(OperatingSystem, FALSE); } -static const struct +typedef struct _OS_LOADING_METHOD { PCSTR BootType; EDIT_OS_ENTRY_PROC EditOsEntry; ARC_ENTRY_POINT OsLoader; -} OSLoadingMethods[] = +} OS_LOADING_METHOD, *POS_LOADING_METHOD; + +static const OS_LOADING_METHOD +OSLoadingMethods[] = { {"ReactOSSetup", EditCustomBootReactOSSetup, LoadReactOSSetup}, @@ -71,13 +73,40 @@ static const struct /* FUNCTIONS ******************************************************************/ +static const OS_LOADING_METHOD* +GetOSLoadingMethod( + _In_ ULONG_PTR SectionId) +{ + ULONG i; + CHAR BootType[80]; + + /* The operating system section has been opened by InitOperatingSystemList() */ + ASSERT(SectionId != 0); + + /* Try to read the boot type. We must have the value (it + * has been possibly added by InitOperatingSystemList()) */ + *BootType = ANSI_NULL; + IniReadSettingByName(SectionId, "BootType", BootType, sizeof(BootType)); + ASSERT(*BootType); + + /* Find the suitable OS loading method */ + for (i = 0; ; ++i) + { + if (i >= RTL_NUMBER_OF(OSLoadingMethods)) + return NULL; + if (_stricmp(BootType, OSLoadingMethods[i].BootType) == 0) + return &OSLoadingMethods[i]; + } + UNREACHABLE; +} + /* * This function converts the list of key=value options in the given operating * system section into an ARC-compatible argument vector, providing in addition * the extra mandatory Software Loading Environment Variables, following the * ARC specification. */ -PCHAR* +static PCHAR* BuildArgvForOsLoader( IN PCSTR LoadIdentifier, IN ULONG_PTR SectionId, @@ -177,23 +206,25 @@ BuildArgvForOsLoader( return Argv; } -VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem) +VOID +LoadOperatingSystem( + _In_ OperatingSystemItem* OperatingSystem) { ULONG_PTR SectionId = OperatingSystem->SectionId; - ULONG i; + const OS_LOADING_METHOD* OSLoadingMethod; ULONG Argc; PCHAR* Argv; - CHAR BootType[80]; - /* The operating system section has been opened by InitOperatingSystemList() */ - ASSERT(SectionId != 0); + /* Find the suitable OS loader to start */ + OSLoadingMethod = GetOSLoadingMethod(SectionId); + if (!OSLoadingMethod) + return; + ASSERT(OSLoadingMethod->OsLoader); - /* Try to read the boot type */ - *BootType = ANSI_NULL; - IniReadSettingByName(SectionId, "BootType", BootType, sizeof(BootType)); - - /* We must have the "BootType" value (it has been possibly added by InitOperatingSystemList()) */ - ASSERT(*BootType); + /* Build the ARC-compatible argument vector */ + Argv = BuildArgvForOsLoader(OperatingSystem->LoadIdentifier, SectionId, &Argc); + if (!Argv) + return; // Unexpected failure. #ifdef _M_IX86 #ifndef UEFIBOOT @@ -202,56 +233,25 @@ VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem) #endif #endif - /* Find the suitable OS loader to start */ - for (i = 0; ; ++i) - { - if (i >= RTL_NUMBER_OF(OSLoadingMethods)) - return; - if (_stricmp(BootType, OSLoadingMethods[i].BootType) == 0) - break; - } - - /* Build the ARC-compatible argument vector */ - Argv = BuildArgvForOsLoader(OperatingSystem->LoadIdentifier, SectionId, &Argc); - if (!Argv) - return; // Unexpected failure. - /* Start the OS loader */ - OSLoadingMethods[i].OsLoader(Argc, Argv, NULL); + OSLoadingMethod->OsLoader(Argc, Argv, NULL); FrLdrHeapFree(Argv, TAG_STRING); } #ifdef HAS_OPTION_MENU_EDIT_CMDLINE - -VOID EditOperatingSystemEntry(IN OperatingSystemItem* OperatingSystem) +VOID +EditOperatingSystemEntry( + _Inout_ OperatingSystemItem* OperatingSystem) { - ULONG_PTR SectionId = OperatingSystem->SectionId; - ULONG i; - CHAR BootType[80]; - - /* The operating system section has been opened by InitOperatingSystemList() */ - ASSERT(SectionId != 0); - - /* Try to read the boot type */ - *BootType = ANSI_NULL; - IniReadSettingByName(SectionId, "BootType", BootType, sizeof(BootType)); - - /* We must have the "BootType" value (it has been possibly added by InitOperatingSystemList()) */ - ASSERT(*BootType); - - /* Find the suitable OS entry editor */ - for (i = 0; ; ++i) + /* Find the suitable OS entry editor and open it */ + const OS_LOADING_METHOD* OSLoadingMethod = + GetOSLoadingMethod(OperatingSystem->SectionId); + if (OSLoadingMethod) { - if (i >= RTL_NUMBER_OF(OSLoadingMethods)) - return; - if (_stricmp(BootType, OSLoadingMethods[i].BootType) == 0) - break; + ASSERT(OSLoadingMethod->EditOsEntry); + OSLoadingMethod->EditOsEntry(OperatingSystem); } - - /* Run it */ - OSLoadingMethods[i].EditOsEntry(OperatingSystem); } - #endif // HAS_OPTION_MENU_EDIT_CMDLINE static LONG diff --git a/boot/freeldr/freeldr/include/freeldr.h b/boot/freeldr/freeldr/include/freeldr.h index 77ba2b0461b..f37e585b232 100644 --- a/boot/freeldr/freeldr/include/freeldr.h +++ b/boot/freeldr/freeldr/include/freeldr.h @@ -125,10 +125,17 @@ #endif VOID __cdecl BootMain(IN PCCH CmdLine); -VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem); + +VOID +LoadOperatingSystem( + _In_ OperatingSystemItem* OperatingSystem); + #ifdef HAS_OPTION_MENU_EDIT_CMDLINE -VOID EditOperatingSystemEntry(IN OperatingSystemItem* OperatingSystem); +VOID +EditOperatingSystemEntry( + _Inout_ OperatingSystemItem* OperatingSystem); #endif + VOID RunLoader(VOID); VOID FrLdrCheckCpuCompatibility(VOID);