xref: /reactos/base/setup/lib/utils/bldrsup.h (revision c7295b2c)
1 /*
2  * PROJECT:     ReactOS Setup Library
3  * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:     Boot Stores Management functionality, with support for
5  *              NT 5.x family (MS Windows <= 2003, and ReactOS) bootloaders.
6  * COPYRIGHT:   Copyright 2017-2018 Hermes Belusca-Maito
7  */
8 
9 // TODO: Add support for NT 6.x family! (detection + BCD manipulation).
10 
11 #pragma once
12 
13 typedef enum _BOOT_STORE_TYPE
14 {
15     FreeLdr,    // ReactOS' FreeLoader
16     NtLdr,      // Windows <= 2k3 NT "FlexBoot" OS Loader NTLDR
17 //  BootMgr,    // Vista+ BCD-oriented BOOTMGR
18     BldrTypeMax
19 } BOOT_STORE_TYPE;
20 
21 /*
22  * Some references about EFI boot entries:
23  * https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/overview-of-boot-options-in-efi
24  * https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/identifying-backup-files-for-existing-boot-entries
25  */
26 
27 /*
28  * This structure is inspired from the EFI boot entry structure
29  * BOOT_OPTIONS that is defined in ndk/iotypes.h .
30  */
31 typedef struct _BOOT_STORE_OPTIONS
32 {
33     // ULONG Length;
34     ULONG Timeout;  //< Timeout in seconds before the default boot entry is started.
35     ULONG_PTR CurrentBootEntryKey;  //< Selected boot entry for the current boot (informative only).
36     ULONG_PTR NextBootEntryKey;     //< The boot entry for the next boot.
37     // WCHAR HeadlessRedirection[ANYSIZE_ARRAY];
38 } BOOT_STORE_OPTIONS, *PBOOT_STORE_OPTIONS;
39 
40 /* FieldsToChange flags for SetBootStoreOptions() */
41 #define BOOT_OPTIONS_TIMEOUT                1
42 #define BOOT_OPTIONS_NEXT_BOOTENTRY_KEY     2
43 // #define BOOT_OPTIONS_HEADLESS_REDIRECTION   4
44 
45 /*
46  * These macros are used to set a value for the BootEntryKey member of a
47  * BOOT_STORE_ENTRY structure, much in the same idea as MAKEINTRESOURCE and
48  * IS_INTRESOURCE macros for Win32 resources.
49  *
50  * A key consists of either a boot ID number, comprised between 0 and
51  * MAXUSHORT == 0xFFFF == 65535, or can be a pointer to a human-readable
52  * string (section name), as in the case of FreeLDR, or to a GUID, as in the
53  * case of BOOTMGR.
54  *
55  * If IS_INTKEY(BootEntryKey) == TRUE, i.e. the key is <= 65535, this means
56  * the key is a boot ID number, otherwise it is typically a pointer to a string.
57  */
58 #define MAKESTRKEY(i)   ((ULONG_PTR)(i))
59 #define MAKEINTKEY(i)   ((ULONG_PTR)((USHORT)(i)))
60 #define IS_INTKEY(i)    (((ULONG_PTR)(i) >> 16) == 0)
61 
62 /*
63  * This structure is inspired from the EFI boot entry structures
64  * BOOT_ENTRY and FILE_PATH that are defined in ndk/iotypes.h .
65  */
66 typedef struct _BOOT_STORE_ENTRY
67 {
68     ULONG Version;          // BOOT_STORE_TYPE value
69     // ULONG Length;
70     ULONG_PTR BootEntryKey; //< Boot entry "key"
71     PCWSTR FriendlyName;    //< Human-readable boot entry description        // LoadIdentifier
72     PCWSTR BootFilePath;    //< Path to e.g. osloader.exe, or winload.efi    // EfiOsLoaderFilePath
73     ULONG OsOptionsLength;  //< Loader-specific options blob (can be a string, or a binary structure...)
74     UCHAR OsOptions[ANYSIZE_ARRAY];
75 /*
76  * In packed form, this structure would contain offsets to 'FriendlyName'
77  * and 'BootFilePath' strings and, after the OsOptions blob, there would
78  * be the following data:
79  *
80  *  WCHAR FriendlyName[ANYSIZE_ARRAY];
81  *  FILE_PATH BootFilePath;
82  */
83 } BOOT_STORE_ENTRY, *PBOOT_STORE_ENTRY;
84 
85 /* "NTOS" (aka. ReactOS or MS Windows NT) <= 5.x options */
86 typedef struct _NTOS_OPTIONS
87 {
88     UCHAR Signature[8];     // "NTOS_5\0\0"
89     // ULONG Version;
90     // ULONG Length;
91     PCWSTR OsLoadPath;      // The OS SystemRoot path   // OsLoaderFilePath // OsFilePath
92     PCWSTR OsLoadOptions;                               // OsLoadOptions
93 /*
94  * In packed form, this structure would contain an offset to the 'OsLoadPath'
95  * string, and the 'OsLoadOptions' member would be:
96  *  WCHAR OsLoadOptions[ANYSIZE_ARRAY];
97  * followed by:
98  *  FILE_PATH OsLoadPath;
99  */
100 } NTOS_OPTIONS, *PNTOS_OPTIONS;
101 
102 #define NTOS_OPTIONS_SIGNATURE "NTOS_5\0\0"
103 
104 /* Options for boot-sector boot entries */
105 typedef struct _BOOT_SECTOR_OPTIONS
106 {
107     UCHAR Signature[8];     // "BootSect"
108     // ULONG Version;
109     // ULONG Length;
110     PCWSTR Drive;
111     PCWSTR Partition;
112     PCWSTR BootSectorFileName;
113 } BOOT_SECTOR_OPTIONS, *PBOOT_SECTOR_OPTIONS;
114 
115 #define BOOT_SECTOR_OPTIONS_SIGNATURE "BootSect"
116 
117 
118 typedef NTSTATUS
119 (NTAPI *PENUM_BOOT_ENTRIES_ROUTINE)(
120     IN BOOT_STORE_TYPE Type,
121     IN PBOOT_STORE_ENTRY BootEntry,
122     IN PVOID Parameter OPTIONAL);
123 
124 
125 NTSTATUS
126 FindBootStore( // By handle
127     IN HANDLE PartitionDirectoryHandle, // OPTIONAL
128     IN BOOT_STORE_TYPE Type,
129     OUT PULONG VersionNumber OPTIONAL);
130 
131 
132 typedef enum _BOOT_STORE_OPENMODE
133 {
134     BS_CheckExisting = 0, // See FindBootStore()
135     BS_CreateNew     = 1, // BS_CreateOnly
136     BS_OpenExisting  = 2, // BS_OpenOnly
137     BS_OpenAlways    = 3,
138     BS_RecreateExisting = 4, // BS_RecreateOnly
139     BS_CreateAlways  = 5,
140 } BOOT_STORE_OPENMODE;
141 
142 typedef enum _BOOT_STORE_ACCESS
143 {
144     // BS_NoAccess = 0,
145     BS_ReadAccess  = 1,
146     BS_WriteAccess = 2,
147     BS_ReadWriteAccess = (BS_ReadAccess | BS_WriteAccess)
148 } BOOT_STORE_ACCESS;
149 
150 NTSTATUS
151 OpenBootStoreByHandle(
152     _Out_ PVOID* Handle,
153     _In_ HANDLE PartitionDirectoryHandle, // _In_opt_
154     _In_ BOOT_STORE_TYPE Type,
155     _In_ BOOT_STORE_OPENMODE OpenMode,
156     _In_ BOOT_STORE_ACCESS Access);
157 
158 NTSTATUS
159 OpenBootStore_UStr(
160     _Out_ PVOID* Handle,
161     _In_ PUNICODE_STRING SystemPartitionPath,
162     _In_ BOOT_STORE_TYPE Type,
163     _In_ BOOT_STORE_OPENMODE OpenMode,
164     _In_ BOOT_STORE_ACCESS Access);
165 
166 NTSTATUS
167 OpenBootStore(
168     _Out_ PVOID* Handle,
169     _In_ PCWSTR SystemPartition,
170     _In_ BOOT_STORE_TYPE Type,
171     _In_ BOOT_STORE_OPENMODE OpenMode,
172     _In_ BOOT_STORE_ACCESS Access);
173 
174 NTSTATUS
175 CloseBootStore(
176     _In_ PVOID Handle);
177 
178 NTSTATUS
179 AddBootStoreEntry(
180     IN PVOID Handle,
181     IN PBOOT_STORE_ENTRY BootEntry,
182     IN ULONG_PTR BootEntryKey);
183 
184 NTSTATUS
185 DeleteBootStoreEntry(
186     IN PVOID Handle,
187     IN ULONG_PTR BootEntryKey);
188 
189 NTSTATUS
190 ModifyBootStoreEntry(
191     IN PVOID Handle,
192     IN PBOOT_STORE_ENTRY BootEntry);
193 
194 NTSTATUS
195 QueryBootStoreEntry(
196     IN PVOID Handle,
197     IN ULONG_PTR BootEntryKey,
198     OUT PBOOT_STORE_ENTRY BootEntry); // Technically this should be PBOOT_STORE_ENTRY*
199 
200 NTSTATUS
201 QueryBootStoreOptions(
202     IN PVOID Handle,
203     IN OUT PBOOT_STORE_OPTIONS BootOptions
204 /* , IN PULONG BootOptionsLength */ );
205 
206 NTSTATUS
207 SetBootStoreOptions(
208     IN PVOID Handle,
209     IN PBOOT_STORE_OPTIONS BootOptions,
210     IN ULONG FieldsToChange);
211 
212 NTSTATUS
213 EnumerateBootStoreEntries(
214     IN PVOID Handle,
215 //  IN ULONG Flags, // Determine which data to retrieve
216     IN PENUM_BOOT_ENTRIES_ROUTINE EnumBootEntriesRoutine,
217     IN PVOID Parameter OPTIONAL);
218 
219 /* EOF */
220