1
0

bootload.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. struct _PROCESSOR_START_BLOCK {
  248. PVOID StackBase;
  249. ULONG StackSize;
  250. PVOID StackPointer;
  251. ULONG Started;
  252. ULONG ProcessorNumber;
  253. PVOID ProcessorStructures;
  254. PVOID SwapPage;
  255. } PACKED;
  256. //
  257. // -------------------------------------------------------------------- Globals
  258. //
  259. //
  260. // -------------------------------------------------------- Function Prototypes
  261. //
  262. VOID
  263. AcpiInitializePreDebugger (
  264. PKERNEL_INITIALIZATION_BLOCK Parameters
  265. );
  266. /*++
  267. Routine Description:
  268. This routine pre-initializes ACPI to the extent that the debugger requires
  269. it. This routine is *undebuggable* as it is called before debug services
  270. are online.
  271. Arguments:
  272. Parameters - Supplies the kernel parameter block coming from the loader.
  273. Return Value:
  274. None.
  275. --*/
  276. KSTATUS
  277. AcpiInitialize (
  278. PKERNEL_INITIALIZATION_BLOCK Parameters
  279. );
  280. /*++
  281. Routine Description:
  282. This routine initializes ACPI.
  283. Arguments:
  284. Parameters - Supplies the kernel parameter block coming from the loader.
  285. Return Value:
  286. Status code.
  287. --*/
  288. KSTATUS
  289. MmInitialize (
  290. PKERNEL_INITIALIZATION_BLOCK Parameters,
  291. PPROCESSOR_START_BLOCK StartBlock,
  292. ULONG Phase
  293. );
  294. /*++
  295. Routine Description:
  296. This routine initializes the kernel Memory Manager.
  297. Arguments:
  298. Parameters - Supplies a pointer to the initialization block from the loader.
  299. StartBlock - Supplies a pointer to the processor start block if this is an
  300. application processor.
  301. Phase - Supplies the phase of initialization. Valid values are 0 through 4.
  302. Return Value:
  303. Status code.
  304. --*/
  305. KSTATUS
  306. MmPrepareForProcessorLaunch (
  307. PPROCESSOR_START_BLOCK StartBlock
  308. );
  309. /*++
  310. Routine Description:
  311. This routine initializes a processor start block in preparation for
  312. launching a new processor.
  313. Arguments:
  314. StartBlock - Supplies a pointer to the start block that will be passed to
  315. the new core.
  316. Return Value:
  317. Status code.
  318. --*/
  319. VOID
  320. MmDestroyProcessorStartBlock (
  321. PPROCESSOR_START_BLOCK StartBlock
  322. );
  323. /*++
  324. Routine Description:
  325. This routine destroys structures initialized by MM in preparation for a
  326. (now failed) processor launch.
  327. Arguments:
  328. StartBlock - Supplies a pointer to the start block.
  329. Return Value:
  330. None.
  331. --*/
  332. KSTATUS
  333. KeInitialize (
  334. ULONG Phase,
  335. PKERNEL_INITIALIZATION_BLOCK Parameters
  336. );
  337. /*++
  338. Routine Description:
  339. This routine initializes the Kernel Executive subsystem.
  340. Arguments:
  341. Phase - Supplies the initialization phase. Valid values are 0 through 3.
  342. Parameters - Supplies a pointer to the kernel initialization block.
  343. Return Value:
  344. Status code.
  345. --*/
  346. PPROCESSOR_START_BLOCK
  347. KePrepareForProcessorLaunch (
  348. VOID
  349. );
  350. /*++
  351. Routine Description:
  352. This routine prepares the kernel's internal structures for a new processor
  353. coming online.
  354. Arguments:
  355. None.
  356. Return Value:
  357. Returns a pointer to an allocated and filled out processor start block
  358. structure. At this point the kernel will be ready for this processor to
  359. come online at any time.
  360. NULL on failure.
  361. --*/
  362. VOID
  363. KeFreeProcessorStartBlock (
  364. PPROCESSOR_START_BLOCK StartBlock,
  365. BOOL FreeResourcesInside
  366. );
  367. /*++
  368. Routine Description:
  369. This routine frees a processor start block structure.
  370. Arguments:
  371. StartBlock - Supplies a pointer to the start block structure to free.
  372. FreeResourcesInside - Supplies a boolean indicating whether or not to free
  373. the resources contained inside the start block.
  374. Return Value:
  375. None.
  376. --*/
  377. KSTATUS
  378. PsInitialize (
  379. ULONG Phase,
  380. PKERNEL_INITIALIZATION_BLOCK Parameters,
  381. PVOID IdleThreadStackBase,
  382. ULONG IdleThreadStackSize
  383. );
  384. /*++
  385. Routine Description:
  386. This routine initializes the process and thread subsystem.
  387. Arguments:
  388. Phase - Supplies the initialization phase. Valid values are 0 and 1.
  389. Parameters - Supplies an optional pointer to the kernel initialization
  390. block. It's only required for processor 0.
  391. IdleThreadStackBase - Supplies the base of the stack for the one thread
  392. currently running.
  393. IdleThreadStackSize - Supplies the size of the stack for the one thread
  394. currently running.
  395. Return Value:
  396. STATUS_SUCCESS on success.
  397. STATUS_INSUFFICIENT_RESOURCES if memory for the kernel process or thread
  398. could not be allocated.
  399. --*/
  400. KSTATUS
  401. IoInitialize (
  402. ULONG Phase,
  403. PKERNEL_INITIALIZATION_BLOCK Parameters
  404. );
  405. /*++
  406. Routine Description:
  407. This routine initializes the I/O subsystem.
  408. Arguments:
  409. Phase - Supplies the initialization phase.
  410. Parameters - Supplies a pointer to the kernel's initialization information.
  411. Return Value:
  412. Status code.
  413. --*/
  414. VOID
  415. HlInitializePreDebugger (
  416. PKERNEL_INITIALIZATION_BLOCK Parameters,
  417. ULONG Processor,
  418. PDEBUG_DEVICE_DESCRIPTION *DebugDevice
  419. );
  420. /*++
  421. Routine Description:
  422. This routine implements extremely early hardware layer initialization. This
  423. routine is *undebuggable*, as it is called before the debugger is brought
  424. online.
  425. Arguments:
  426. Parameters - Supplies an optional pointer to the kernel initialization
  427. parameters. This parameter may be NULL.
  428. Processor - Supplies the processor index of this processor.
  429. DebugDevice - Supplies a pointer where a pointer to the debug device
  430. description will be returned on success.
  431. Return Value:
  432. None.
  433. --*/
  434. KSTATUS
  435. HlInitialize (
  436. PKERNEL_INITIALIZATION_BLOCK Parameters,
  437. ULONG Phase
  438. );
  439. /*++
  440. Routine Description:
  441. This routine initializes the core system hardware. During phase 0, on
  442. application processors, this routine enters at low run level and exits at
  443. dispatch run level.
  444. Arguments:
  445. Parameters - Supplies a pointer to the kernel's initialization information.
  446. Phase - Supplies the initialization phase.
  447. Return Value:
  448. Status code.
  449. --*/