bootload.h 16 KB

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