ReactOS 0.4.17-dev-684-ga6524ef
uefildr.c
Go to the documentation of this file.
1/*
2 * PROJECT: FreeLoader UEFI Support
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Entry point and helpers
5 * COPYRIGHT: Copyright 2022 Justin Miller <justinmiller100@gmail.com>
6 */
7
8#include <uefildr.h>
9
10#include <debug.h>
12
13/* GLOBALS ********************************************************************/
14
19
21
22/* FUNCTIONS ******************************************************************/
23
26 _In_ EFI_HANDLE ImageHandle,
27 _In_ EFI_SYSTEM_TABLE *SystemTable)
28{
29 PCSTR CmdLine = ""; // FIXME: Determine a command-line from UEFI boot options
30
31 SystemTable->ConOut->OutputString(SystemTable->ConOut, L"UEFI EntryPoint: Starting freeldr from UEFI");
32 GlobalImageHandle = ImageHandle;
33 GlobalSystemTable = SystemTable;
34
35 /* Load the default settings from the command-line */
37
38 /* Debugger pre-initialization */
40
42
43 /* UI pre-initialization */
44 if (!UiInitialize(FALSE))
45 {
46 UiMessageBoxCritical("Unable to initialize UI.");
47 goto Quit;
48 }
49
50 /* Initialize memory manager */
52 {
53 UiMessageBoxCritical("Unable to initialize memory manager.");
54 goto Quit;
55 }
56
57 /* Initialize I/O subsystem */
58 FsInit();
59
60 /* Initialize the module list */
62 {
63 UiMessageBoxCritical("Unable to initialize module list.");
64 goto Quit;
65 }
66
68 {
69 UiMessageBoxCritical("Error when detecting hardware.");
70 goto Quit;
71 }
72
73 /* 0x32000 is what UEFI defines, but we can go smaller if we want */
74 SIZE_T StackSize = 0x32000;
76
77 if (!AllocatedStackMem)
78 {
79 UiMessageBoxCritical("Unable to allocate OS loader stack.");
80 goto Quit;
81 }
82
83 /* Stacks grow downward so set the initial stack pointer to the top of the allocated region */
84 BasicStack = (PVOID)((ULONG_PTR)AllocatedStackMem + StackSize);
86
87Quit:
88 /* If we reach this point, something went wrong before, therefore reboot */
89 Reboot();
90
92 return 0;
93}
94
96void
98{
99 TRACE("ExecuteLoaderCleanly Entry\n");
100 UefiServiceStack = PreviousStack;
101
102 RunLoader();
103 Reboot();
105}
106
109{
110 WARN("Performing cold reset\n");
111
113 {
114 /* Attempt a native firmware cold reset */
118 0,
119 NULL
120 );
121 }
122
123 /* Fallback dead-loop if runtime services are missing or fail to respond */
124 for (;;)
125 {
126#if defined(_M_IX86) || defined(_M_AMD64)
127 __halt();
128#else
129 /* Fallback placeholder loop for other platforms */
130 NOTHING;
131#endif
132 }
133}
#define WARNING
Definition: BusLogic958.h:56
static ULONG StackSize
Definition: StackOverflow.c:21
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:31
#define EFI_SUCCESS
Definition: UefiBaseType.h:120
@ EfiResetCold
Definition: UefiSpec.h:975
void LoadSettings(void)
Definition: settings.c:53
#define WARN(fmt,...)
Definition: precomp.h:61
#define DebugInit(DebugString)
Definition: debug.h:120
#define DBG_DEFAULT_CHANNEL(ch)
Definition: debug.h:106
VOID FsInit(VOID)
Definition: fs.c:725
#define MachInitializeBootDevices()
Definition: machine.h:127
PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:31
BOOLEAN MmInitializeMemoryManager(VOID)
Definition: meminit.c:336
BOOTMGRINFO BootMgrInfo
Definition: settings.c:19
VOID UiMessageBoxCritical(_In_ PCSTR MessageText)
Definition: ui.c:372
BOOLEAN UiInitialize(BOOLEAN ShowUi)
Definition: ui.c:92
VOID RunLoader(VOID)
Definition: bootmgr.c:386
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define __cdecl
Definition: corecrt.h:121
#define DECLSPEC_NORETURN
Definition: corecrt.h:131
static const WCHAR CmdLine[]
Definition: install.c:48
#define L(x)
Definition: resources.c:13
#define NOTHING
Definition: input_list.c:10
__INTRIN_INLINE void __halt(void)
Definition: intrin_x86.h:1746
VOID MachInit(IN PCCH CommandLine)
Definition: macharm.c:182
#define _In_
Definition: no_sal2.h:158
#define UNREACHABLE
BOOLEAN PeLdrInitializeModuleList(VOID)
Definition: peloader.c:522
@ LoaderOsloaderStack
Definition: arc.h:301
#define TRACE(s)
Definition: solgame.cpp:4
EFI_RESET_SYSTEM ResetSystem
Definition: UefiSpec.h:1774
EFI_RUNTIME_SERVICES * RuntimeServices
Definition: UefiSpec.h:1955
PCSTR DebugString
Definition: settings.h:12
void * PVOID
Definition: typedefs.h:50
ULONG_PTR SIZE_T
Definition: typedefs.h:80
const char * PCSTR
Definition: typedefs.h:52
uint32_t ULONG_PTR
Definition: typedefs.h:65
EFI_STATUS EfiEntry(_In_ EFI_HANDLE ImageHandle, _In_ EFI_SYSTEM_TABLE *SystemTable)
Definition: uefildr.c:25
void _changestack(VOID)
PVOID BasicStack
Definition: uefildr.c:18
EFI_SYSTEM_TABLE * GlobalSystemTable
Definition: uefildr.c:16
DECLSPEC_NORETURN VOID __cdecl Reboot(VOID)
Definition: uefildr.c:108
DECLSPEC_NORETURN void ExecuteLoaderCleanly(PVOID PreviousStack)
Definition: uefildr.c:97
EFI_HANDLE GlobalImageHandle
Definition: uefildr.c:15
PVOID UefiServiceStack
Definition: uefildr.c:17