bootload.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*++
  2. Copyright (c) 2012 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. bootload.h
  9. Abstract:
  10. This header contains definitions for the boot loader shared between the
  11. loader and the kernel, as well as system initialization functions.
  12. Author:
  13. Evan Green 30-Jul-2012
  14. --*/
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <minoca/kernel/kdebug.h>
  19. #include <minoca/kernel/sysres.h>
  20. //
  21. // ---------------------------------------------------------------- Definitions
  22. //
  23. #define BOOT_INITIALIZATION_BLOCK_VERSION 4
  24. #define KERNEL_INITIALIZATION_BLOCK_VERSION 4
  25. //
  26. // Define boot initialization flags.
  27. //
  28. #define BOOT_INITIALIZATION_FLAG_SCREEN_CLEAR 0x00000001
  29. #define BOOT_INITIALIZATION_FLAG_64BIT 0x00000002
  30. //
  31. // Define the initial size of the memory allocation to hand to the hardware
  32. // module support.
  33. //
  34. #define HARDWARE_MODULE_INITIAL_ALLOCATION_SIZE 0x4000
  35. #define HARDWARE_MODULE_INITIAL_DEVICE_ALLOCATION_SIZE 0x4000
  36. //
  37. // ------------------------------------------------------ Data Type Definitions
  38. //
  39. /*++
  40. Structure Description:
  41. This structure stores a region of reserved memory that may or may not
  42. already be marked in the firmware memory map. The boot manager uses these
  43. descriptors to stake out its own memory in the loader on legacy PC/AT
  44. systems.
  45. Members:
  46. Address - Stores the base address of the reserved region.
  47. Size - Stores the size of the reserved region in bytes.
  48. Flags - Stores flags describing the region.
  49. --*/
  50. typedef struct _BOOT_RESERVED_REGION {
  51. ULONGLONG Address;
  52. ULONGLONG Size;
  53. ULONGLONG Flags;
  54. } BOOT_RESERVED_REGION, *PBOOT_RESERVED_REGION;
  55. /*++
  56. Structure Description:
  57. This structure stores the information passed between the boot manager and
  58. OS loader or other boot application. Future versions of this structure must
  59. be backwards compatible as newer boot managers may pass control over to
  60. older OS loaders. Pointers here are saved as 64-bit values because this
  61. structure may be passed from a 32-bit boot manager to a 64-bit OS loader.
  62. Members:
  63. Version - Stores the version number of the loader initialization block.
  64. Set to BOOT_INITIALIZATION_BLOCK_VERSION.
  65. BootConfigurationFileSize - Stores the size of the boot configuration file
  66. buffer in bytes.
  67. BootConfigurationFile - Stores a pointer to a buffer containing the
  68. contents of the boot configuration file.
  69. BootEntryFlags - Stores the flags associated with this boot entry. See
  70. BOOT_ENTRY_FLAG_* definitions.
  71. BootEntryId - Stores the identifier of the selected boot entry.
  72. ReservedRegionCount - Stores the number of reserved region structures in
  73. the array.
  74. ReservedRegions - Stores a pointer to an array of reserved regions of
  75. memeory that may or may not be in the firmware memory map. This array
  76. is of type BOOT_RESERVED_REGION.
  77. StackTop - Stores a pointer to the top of the stack.
  78. StackSize - Stores the size of the boot stack region, in bytes.
  79. EfiImageHandle - Stores a pointer to the EFI image handle used to launch
  80. the boot application that launched this boot application. Note the
  81. type here is an EFI_HANDLE *, not an EFI_HANDLE.
  82. EfiSystemTable - Stores a pointer to the EFI system table as passed to the
  83. original EFI boot application. The type here is an EFI_SYSTEM_TABLE *.
  84. PartitionOffset - Stores the offset in blocks from the beginning of the
  85. disk to the OS partition if the firmware doesn't support partitions
  86. natively.
  87. ApplicationName - Stores a pointer to a string containing the file name of
  88. the application being launched.
  89. ApplicationLowestAddress - Stores the lowest address of the boot
  90. application image.
  91. ApplicationSize - Stores the size of the loaded boot application image in
  92. bytes.
  93. ApplicationArguments - Stores a pointer to a null terminated string
  94. containing the command-line-esque arguments to the application.
  95. PageDirectory - Stores the address of the top level page table in use.
  96. DriveNumber - Stores the drive number of the OS partition for legacy PC/AT
  97. systems.
  98. Flags - Stores flags describing the environment state. See
  99. BOOT_INITIALIZATION_FLAG_* definitions.
  100. --*/
  101. typedef struct _BOOT_INITIALIZATION_BLOCK {
  102. ULONG Version;
  103. ULONG BootConfigurationFileSize;
  104. ULONGLONG BootConfigurationFile;
  105. ULONGLONG BootEntryFlags;
  106. ULONG BootEntryId;
  107. ULONG ReservedRegionCount;
  108. ULONGLONG ReservedRegions;
  109. ULONGLONG StackTop;
  110. ULONGLONG StackSize;
  111. ULONGLONG EfiImageHandle;
  112. ULONGLONG EfiSystemTable;
  113. ULONGLONG PartitionOffset;
  114. ULONGLONG ApplicationName;
  115. ULONGLONG ApplicationLowestAddress;
  116. ULONGLONG ApplicationSize;
  117. ULONGLONG ApplicationArguments;
  118. ULONGLONG PageDirectory;
  119. ULONG DriveNumber;
  120. ULONG Flags;
  121. } BOOT_INITIALIZATION_BLOCK, *PBOOT_INITIALIZATION_BLOCK;
  122. typedef
  123. INT
  124. (*PBOOT_APPLICATION_ENTRY) (
  125. PBOOT_INITIALIZATION_BLOCK Parameters
  126. );
  127. /*++
  128. Routine Description:
  129. This routine is the entry point into a boot application.
  130. Arguments:
  131. Parameters - Supplies a pointer to the initialization information.
  132. Return Value:
  133. 0 or does not return on success.
  134. Returns a non-zero value on failure.
  135. --*/
  136. /*++
  137. Structure Description:
  138. This structure stores pointers to all of the static tables provided by the
  139. firmware. An array of virtual addresses is expected to immediately follow
  140. this structure.
  141. Members:
  142. TableCount - Supplies the number of tables in the following array.
  143. --*/
  144. typedef struct _FIRMWARE_TABLE_DIRECTORY {
  145. ULONG TableCount;
  146. } FIRMWARE_TABLE_DIRECTORY, *PFIRMWARE_TABLE_DIRECTORY;
  147. /*++
  148. Structure Description:
  149. This structure stores information about a buffer provided by the loader to
  150. the kernel.
  151. Members:
  152. Buffer - Stores a pointer to the data buffer.
  153. Size - Stores the size of the buffer, in bytes.
  154. --*/
  155. typedef struct _LOADER_BUFFER {
  156. PVOID Buffer;
  157. UINTN Size;
  158. } LOADER_BUFFER, *PLOADER_BUFFER;
  159. /*++
  160. Structure Description:
  161. This structure stores information needed by the kernel to initialize. It
  162. is provided by the loader when the kernel is launched.
  163. Members:
  164. Version - Stores the version number of the loader block. This is used to
  165. detect version mismatch between the loader and the kernel.
  166. Size - Stores the total size of the initialization block structure, in
  167. bytes. This field can also be used to detect mismatch or corruption
  168. between the loader and the kernel.
  169. FirmwareTables - Stores a pointer to the directory of static tables
  170. provided by the platform firmware.
  171. MemoryMap - Stores a pointer to the memory map of the machine, including
  172. any regions defined by the firmware, and regions allocated by the
  173. loader.
  174. VirtualMap - Stores a pointer to the virtual memory map created for the
  175. kernel.
  176. PageDirectory - Stores a pointer to the top level paging structure.
  177. PageTables - Stores a pointer to the page tables.
  178. PageTableStage - Stores a pointer to the initial page table staging area.
  179. The mapping for this virtual does *not* correspond to any valid memory,
  180. but a page table has been set up for this VA to prevent infinite loops.
  181. MmInitMemory - Stores a buffer of memory that the memory manager can use
  182. to initialize itself. This memory is mapped as loader permanent.
  183. ImageList - Stores the head of the list of images loaded by the kernel.
  184. Entries on this list are of type LOADED_IMAGE.
  185. KernelModule - Stores a pointer to the module information for the kernel
  186. itself. This data should also be in the loaded modules list.
  187. LoaderModule - Stores a pointer to the module information for the OS
  188. loader. This data should also be in the loaded modules list.
  189. KernelStack - Stores the kernel stack buffer that processor 0 should use.
  190. DeviceToDriverFile - Stores the location of the file containing the mapping
  191. between devices and drivers.
  192. DeviceMapFile - Stores the location of the file containing a list of
  193. unenumerable devices that exist on the system.
  194. SystemResourceListHead - Stores the list of system resources provided to
  195. the kernel by the loader. All system resources begin with a
  196. SYSTEM_RESOURCE_HEADER.
  197. BootEntry - Stores a pointer to the boot entry that was launched.
  198. BootTime - Stores the boot time of the system.
  199. FirmwareType - Stores the system firmware type.
  200. EfiRuntimeServices - Stores a pointer to the EFI runtime services table.
  201. This is only valid on EFI based systems.
  202. CycleCounterFrequency - Stores an estimate of the frequency of the cycle
  203. counter, used for very early stall services. On some architectures or
  204. platforms this may be 0.
  205. --*/
  206. typedef struct _KERNEL_INITIALIZATION_BLOCK {
  207. ULONG Version;
  208. ULONG Size;
  209. PFIRMWARE_TABLE_DIRECTORY FirmwareTables;
  210. PMEMORY_DESCRIPTOR_LIST MemoryMap;
  211. PMEMORY_DESCRIPTOR_LIST VirtualMap;
  212. PVOID PageDirectory;
  213. PVOID PageTables;
  214. PVOID PageTableStage;
  215. LOADER_BUFFER MmInitMemory;
  216. LIST_ENTRY ImageList;
  217. PDEBUG_MODULE KernelModule;
  218. PDEBUG_MODULE LoaderModule;
  219. LOADER_BUFFER KernelStack;
  220. LOADER_BUFFER DeviceToDriverFile;
  221. LOADER_BUFFER DeviceMapFile;
  222. LIST_ENTRY SystemResourceListHead;
  223. PVOID BootEntry;
  224. SYSTEM_TIME BootTime;
  225. SYSTEM_FIRMWARE_TYPE FirmwareType;
  226. PVOID EfiRuntimeServices;
  227. ULONGLONG CycleCounterFrequency;
  228. } KERNEL_INITIALIZATION_BLOCK, *PKERNEL_INITIALIZATION_BLOCK;
  229. /*++
  230. Structure Description:
  231. This structure stores information needed by an application processor to
  232. initialize.
  233. Members:
  234. StackBase - Stores the base of the stack that the initialization is
  235. running on.
  236. StackSize - Stores the size of the stack that the initialization is
  237. running on.
  238. StackPointer - Stores the stack pointer to set.
  239. Started - Stores a boolean set by the processor when it has successfully
  240. run through the initial assembly stub.
  241. ProcessorNumber - Stores the number of the processor.
  242. ProcessorStructures - Stores the processor structures buffer used for
  243. early architecture specific initialization.
  244. SwapPage - Stores a pointer to the virtual address reservation the
  245. processor should use for quick dispatch level mappings.
  246. --*/
  247. #pragma pack(push, 1)
  248. struct _PROCESSOR_START_BLOCK {
  249. PVOID StackBase;
  250. ULONG StackSize;
  251. PVOID StackPointer;
  252. ULONG Started;
  253. ULONG ProcessorNumber;
  254. PVOID ProcessorStructures;
  255. PVOID SwapPage;
  256. } PACKED;
  257. #pragma pack(pop)
  258. //
  259. // -------------------------------------------------------------------- Globals
  260. //
  261. //
  262. // -------------------------------------------------------- Function Prototypes
  263. //
  264. VOID
  265. AcpiInitializePreDebugger (
  266. PKERNEL_INITIALIZATION_BLOCK Parameters
  267. );
  268. /*++
  269. Routine Description:
  270. This routine pre-initializes ACPI to the extent that the debugger requires
  271. it. This routine is *undebuggable* as it is called before debug services
  272. are online.
  273. Arguments:
  274. Parameters - Supplies the kernel parameter block coming from the loader.
  275. Return Value:
  276. None.
  277. --*/
  278. KSTATUS
  279. AcpiInitialize (
  280. PKERNEL_INITIALIZATION_BLOCK Parameters
  281. );
  282. /*++
  283. Routine Description:
  284. This routine initializes ACPI.
  285. Arguments:
  286. Parameters - Supplies the kernel parameter block coming from the loader.
  287. Return Value:
  288. Status code.
  289. --*/
  290. KSTATUS
  291. MmInitialize (
  292. PKERNEL_INITIALIZATION_BLOCK Parameters,
  293. PPROCESSOR_START_BLOCK StartBlock,
  294. ULONG Phase
  295. );
  296. /*++
  297. Routine Description:
  298. This routine initializes the kernel Memory Manager.
  299. Arguments:
  300. Parameters - Supplies a pointer to the initialization block from the loader.
  301. StartBlock - Supplies a pointer to the processor start block if this is an
  302. application processor.
  303. Phase - Supplies the phase of initialization. Valid values are 0 through 4.
  304. Return Value:
  305. Status code.
  306. --*/
  307. KSTATUS
  308. MmPrepareForProcessorLaunch (
  309. PPROCESSOR_START_BLOCK StartBlock
  310. );
  311. /*++
  312. Routine Description:
  313. This routine initializes a processor start block in preparation for
  314. launching a new processor.
  315. Arguments:
  316. StartBlock - Supplies a pointer to the start block that will be passed to
  317. the new core.
  318. Return Value:
  319. Status code.
  320. --*/
  321. VOID
  322. MmDestroyProcessorStartBlock (
  323. PPROCESSOR_START_BLOCK StartBlock
  324. );
  325. /*++
  326. Routine Description:
  327. This routine destroys structures initialized by MM in preparation for a
  328. (now failed) processor launch.
  329. Arguments:
  330. StartBlock - Supplies a pointer to the start block.
  331. Return Value:
  332. None.
  333. --*/
  334. KSTATUS
  335. KeInitialize (
  336. ULONG Phase,
  337. PKERNEL_INITIALIZATION_BLOCK Parameters
  338. );
  339. /*++
  340. Routine Description:
  341. This routine initializes the Kernel Executive subsystem.
  342. Arguments:
  343. Phase - Supplies the initialization phase. Valid values are 0 through 3.
  344. Parameters - Supplies a pointer to the kernel initialization block.
  345. Return Value:
  346. Status code.
  347. --*/
  348. PPROCESSOR_START_BLOCK
  349. KePrepareForProcessorLaunch (
  350. VOID
  351. );
  352. /*++
  353. Routine Description:
  354. This routine prepares the kernel's internal structures for a new processor
  355. coming online.
  356. Arguments:
  357. None.
  358. Return Value:
  359. Returns a pointer to an allocated and filled out processor start block
  360. structure. At this point the kernel will be ready for this processor to
  361. come online at any time.
  362. NULL on failure.
  363. --*/
  364. VOID
  365. KeFreeProcessorStartBlock (
  366. PPROCESSOR_START_BLOCK StartBlock,
  367. BOOL FreeResourcesInside
  368. );
  369. /*++
  370. Routine Description:
  371. This routine frees a processor start block structure.
  372. Arguments:
  373. StartBlock - Supplies a pointer to the start block structure to free.
  374. FreeResourcesInside - Supplies a boolean indicating whether or not to free
  375. the resources contained inside the start block.
  376. Return Value:
  377. None.
  378. --*/
  379. KSTATUS
  380. PsInitialize (
  381. ULONG Phase,
  382. PKERNEL_INITIALIZATION_BLOCK Parameters,
  383. PVOID IdleThreadStackBase,
  384. ULONG IdleThreadStackSize
  385. );
  386. /*++
  387. Routine Description:
  388. This routine initializes the process and thread subsystem.
  389. Arguments:
  390. Phase - Supplies the initialization phase. Valid values are 0 and 1.
  391. Parameters - Supplies an optional pointer to the kernel initialization
  392. block. It's only required for processor 0.
  393. IdleThreadStackBase - Supplies the base of the stack for the one thread
  394. currently running.
  395. IdleThreadStackSize - Supplies the size of the stack for the one thread
  396. currently running.
  397. Return Value:
  398. STATUS_SUCCESS on success.
  399. STATUS_INSUFFICIENT_RESOURCES if memory for the kernel process or thread
  400. could not be allocated.
  401. --*/
  402. KSTATUS
  403. IoInitialize (
  404. ULONG Phase,
  405. PKERNEL_INITIALIZATION_BLOCK Parameters
  406. );
  407. /*++
  408. Routine Description:
  409. This routine initializes the I/O subsystem.
  410. Arguments:
  411. Phase - Supplies the initialization phase.
  412. Parameters - Supplies a pointer to the kernel's initialization information.
  413. Return Value:
  414. Status code.
  415. --*/
  416. VOID
  417. HlInitializePreDebugger (
  418. PKERNEL_INITIALIZATION_BLOCK Parameters,
  419. ULONG Processor,
  420. PDEBUG_DEVICE_DESCRIPTION *DebugDevice
  421. );
  422. /*++
  423. Routine Description:
  424. This routine implements extremely early hardware layer initialization. This
  425. routine is *undebuggable*, as it is called before the debugger is brought
  426. online.
  427. Arguments:
  428. Parameters - Supplies an optional pointer to the kernel initialization
  429. parameters. This parameter may be NULL.
  430. Processor - Supplies the processor index of this processor.
  431. DebugDevice - Supplies a pointer where a pointer to the debug device
  432. description will be returned on success.
  433. Return Value:
  434. None.
  435. --*/
  436. KSTATUS
  437. HlInitialize (
  438. PKERNEL_INITIALIZATION_BLOCK Parameters,
  439. ULONG Phase
  440. );
  441. /*++
  442. Routine Description:
  443. This routine initializes the core system hardware. During phase 0, on
  444. application processors, this routine enters at low run level and exits at
  445. dispatch run level.
  446. Arguments:
  447. Parameters - Supplies a pointer to the kernel's initialization information.
  448. Phase - Supplies the initialization phase.
  449. Return Value:
  450. Status code.
  451. --*/