main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*++
  2. Copyright (c) 2014 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. main.c
  9. Abstract:
  10. This module implements the PC/AT main function. This is called by the file
  11. system loader code.
  12. Author:
  13. Evan Green 21-Feb-2014
  14. Environment:
  15. Boot
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include <minoca/kernel/kernel.h>
  21. #include "firmware.h"
  22. #include "bootlib.h"
  23. #include "bootman.h"
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. //
  28. // Define the number of builtin reserved regions. One is for the image and
  29. // one is for the stack.
  30. //
  31. #define BOOT_MANAGER_RESERVED_REGION_COUNT 2
  32. //
  33. // ------------------------------------------------------ Data Type Definitions
  34. //
  35. //
  36. // ----------------------------------------------- Internal Function Prototypes
  37. //
  38. //
  39. // -------------------------------------------------------------------- Globals
  40. //
  41. //
  42. // Variables defined in the linker script that mark the start and end of the
  43. // image.
  44. //
  45. extern UCHAR _end;
  46. extern UCHAR __executable_start;
  47. //
  48. // Store a fake boot initialization block, since the boot manager is launched
  49. // by the firmware.
  50. //
  51. BOOT_INITIALIZATION_BLOCK BmBootBlock;
  52. //
  53. // Store regions for the boot manager image and stack.
  54. //
  55. BOOT_RESERVED_REGION BmBootRegions[BOOT_MANAGER_RESERVED_REGION_COUNT];
  56. //
  57. // ------------------------------------------------------------------ Functions
  58. //
  59. __USED
  60. INT
  61. BmPcatApplicationMain (
  62. PVOID TopOfStack,
  63. ULONG StackSize,
  64. ULONGLONG PartitionOffset,
  65. ULONG BootDriveNumber
  66. )
  67. /*++
  68. Routine Description:
  69. This routine is the entry point for the PC/AT Boot Manager.
  70. Arguments:
  71. StartOfLoader - Supplies the address where the loader begins in memory.
  72. TopOfStack - Supplies the top of the stack that has been set up for the
  73. loader.
  74. StackSize - Supplies the total size of the stack set up for the loader, in
  75. bytes.
  76. PartitionOffset - Supplies the offset, in sectors, from the start of the
  77. disk where this boot partition resides.
  78. BootDriveNumber - Supplies the drive number for the device that booted
  79. the system.
  80. Return Value:
  81. Returns the step number that failed.
  82. --*/
  83. {
  84. ULONG PageSize;
  85. PageSize = MmPageSize();
  86. RtlZeroMemory(&BmBootBlock, sizeof(BOOT_INITIALIZATION_BLOCK));
  87. BmBootBlock.Version = BOOT_INITIALIZATION_BLOCK_VERSION;
  88. BmBootBlock.StackTop = (UINTN)TopOfStack;
  89. BmBootBlock.StackSize = StackSize;
  90. BmBootBlock.PartitionOffset = PartitionOffset;
  91. BmBootBlock.DriveNumber = BootDriveNumber;
  92. BmBootBlock.ApplicationName = (UINTN)"bootman";
  93. BmBootBlock.ApplicationLowestAddress = (UINTN)&__executable_start;
  94. BmBootBlock.ApplicationSize = (UINTN)&_end - (UINTN)&__executable_start;
  95. BmBootBlock.ApplicationArguments = (UINTN)"";
  96. //
  97. // Initialize the reserved regions for the image itself and the stack.
  98. //
  99. BmBootRegions[0].Address =
  100. ALIGN_RANGE_DOWN((UINTN)(&__executable_start), PageSize);
  101. BmBootRegions[0].Size =
  102. ALIGN_RANGE_UP((UINTN)&_end - (UINTN)&__executable_start,
  103. PageSize);
  104. BmBootRegions[0].Flags = 0;
  105. BmBootRegions[1].Address = ALIGN_RANGE_DOWN((UINTN)TopOfStack - StackSize,
  106. PageSize);
  107. BmBootRegions[1].Size = ALIGN_RANGE_UP((UINTN)TopOfStack, PageSize) -
  108. BmBootRegions[1].Address;
  109. BmBootRegions[1].Flags = 0;
  110. BmBootBlock.ReservedRegions = (UINTN)BmBootRegions;
  111. BmBootBlock.ReservedRegionCount = BOOT_MANAGER_RESERVED_REGION_COUNT;
  112. //
  113. // Call the main application.
  114. //
  115. return BmMain(&BmBootBlock);
  116. }
  117. //
  118. // --------------------------------------------------------- Internal Functions
  119. //