multiboot2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @file multiboot2.h
  3. * @brief multiboot2 解析
  4. * @author Zone.N ([email protected])
  5. * @version 1.0
  6. * @date 2021-09-18
  7. * @copyright MIT LICENSE
  8. * https://github.com/Simple-XX/SimpleKernel
  9. * @par change log:
  10. * <table>
  11. * <tr><th>Date<th>Author<th>Description
  12. * <tr><td>2021-09-18<td>digmouse233<td>迁移到 doxygen
  13. * </table>
  14. */
  15. #ifndef _MULTIBOOT2_H_
  16. #define _MULTIBOOT2_H_
  17. #include "stdint.h"
  18. #include "stdbool.h"
  19. #include "boot_info.h"
  20. /// @see Multiboot2 Specification version 2.0.pdf
  21. // 启动后,在 32 位内核进入点,机器状态如下:
  22. // 1. CS 指向基地址为 0x00000000,限长为4G – 1的代码段描述符。
  23. // 2. DS,SS,ES,FS 和 GS 指向基地址为0x00000000,限长为4G –
  24. // 1的数据段描述符。
  25. // 3. A20 地址线已经打开。
  26. // 4. 页机制被禁止。
  27. // 5. 中断被禁止。
  28. // 6. EAX = 0x2BADB002
  29. // 7. 系统信息和启动信息块的线性地址保存在 EBX中(相当于一个指针)。
  30. // 以下即为这个信息块的结构
  31. /**
  32. * @brief MULTIBOOT2 接口抽象
  33. */
  34. class MULTIBOOT2 {
  35. private:
  36. /* How many bytes from the start of the file we search for the header. */
  37. static constexpr const uint32_t MULTIBOOT_SEARCH = 32768;
  38. static constexpr const uint32_t MULTIBOOT_HEADER_ALIGN = 8;
  39. /* The magic field should contain this. */
  40. static constexpr const uint32_t MULTIBOOT2_HEADER_MAGIC = 0xe85250d6;
  41. /* This should be in %eax. */
  42. static constexpr const uint32_t MULTIBOOT2_BOOTLOADER_MAGIC = 0x36d76289;
  43. /* Alignment of multiboot modules. */
  44. static constexpr const uint32_t MULTIBOOT_MOD_ALIGN = 0x00001000;
  45. /* Alignment of the multiboot info structure. */
  46. static constexpr const uint32_t MULTIBOOT_INFO_ALIGN = 0x00000008;
  47. /* Flags set in the 'flags' member of the multiboot header. */
  48. static constexpr const uint32_t MULTIBOOT_TAG_ALIGN = 8;
  49. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_END = 0;
  50. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_CMDLINE = 1;
  51. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME = 2;
  52. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_MODULE = 3;
  53. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_BASIC_MEMINFO = 4;
  54. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_BOOTDEV = 5;
  55. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_MMAP = 6;
  56. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_VBE = 7;
  57. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_FRAMEBUFFER = 8;
  58. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_ELF_SECTIONS = 9;
  59. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_APM = 10;
  60. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_EFI32 = 11;
  61. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_EFI64 = 12;
  62. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_SMBIOS = 13;
  63. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_ACPI_OLD = 14;
  64. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_ACPI_NEW = 15;
  65. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_NETWORK = 16;
  66. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_EFI_MMAP = 17;
  67. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_EFI_BS = 18;
  68. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_EFI32_IH = 19;
  69. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_EFI64_IH = 20;
  70. static constexpr const uint32_t MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR = 21;
  71. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_END = 0;
  72. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST =
  73. 1;
  74. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_ADDRESS = 2;
  75. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 3;
  76. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS = 4;
  77. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 5;
  78. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_MODULE_ALIGN = 6;
  79. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_EFI_BS = 7;
  80. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 =
  81. 8;
  82. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 =
  83. 9;
  84. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_RELOCATABLE = 10;
  85. static constexpr const uint32_t MULTIBOOT_ARCHITECTURE_I386 = 0;
  86. static constexpr const uint32_t MULTIBOOT_ARCHITECTURE_MIPS32 = 4;
  87. static constexpr const uint32_t MULTIBOOT_HEADER_TAG_OPTIONAL = 1;
  88. static constexpr const uint32_t MULTIBOOT_LOAD_PREFERENCE_NONE = 0;
  89. static constexpr const uint32_t MULTIBOOT_LOAD_PREFERENCE_LOW = 1;
  90. static constexpr const uint32_t MULTIBOOT_LOAD_PREFERENCE_HIGH = 2;
  91. static constexpr const uint32_t MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED =
  92. 1;
  93. static constexpr const uint32_t MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED =
  94. 2;
  95. struct multiboot_header_t {
  96. // Must be MULTIBOOT_MAGIC - see above.
  97. uint32_t magic;
  98. // ISA
  99. uint32_t architecture;
  100. // Total header length.
  101. uint32_t header_length;
  102. // The above fields plus this one must equal 0 mod 2^32.
  103. uint32_t checksum;
  104. };
  105. struct multiboot_header_tag_t {
  106. uint16_t type;
  107. uint16_t flags;
  108. uint32_t size;
  109. };
  110. struct multiboot_header_tag_information_request_t : multiboot_header_tag_t {
  111. uint32_t requests[0];
  112. };
  113. struct multiboot_header_tag_address_t : multiboot_header_tag_t {
  114. uint32_t header_addr;
  115. uint32_t load_addr;
  116. uint32_t load_end_addr;
  117. uint32_t bss_end_addr;
  118. };
  119. struct multiboot_header_tag_entry_address_t : multiboot_header_tag_t {
  120. uint32_t entry_addr;
  121. };
  122. struct multiboot_header_tag_console_flags_t : multiboot_header_tag_t {
  123. uint32_t console_flags;
  124. };
  125. struct multiboot_header_tag_framebuffer_t : multiboot_header_tag_t {
  126. uint32_t width;
  127. uint32_t height;
  128. uint32_t depth;
  129. };
  130. struct multiboot_header_tag_module_align_t : multiboot_header_tag_t {
  131. ;
  132. };
  133. struct multiboot_header_tag_relocatable_t : multiboot_header_tag_t {
  134. uint32_t min_addr;
  135. uint32_t max_addr;
  136. uint32_t align;
  137. uint32_t preference;
  138. };
  139. struct multiboot_color_t {
  140. uint8_t red;
  141. uint8_t green;
  142. uint8_t blue;
  143. };
  144. static constexpr const uint32_t MULTIBOOT_MEMORY_AVAILABLE = 1;
  145. static constexpr const uint32_t MULTIBOOT_MEMORY_RESERVED = 2;
  146. static constexpr const uint32_t MULTIBOOT_MEMORY_ACPI_RECLAIMABLE = 3;
  147. static constexpr const uint32_t MULTIBOOT_MEMORY_NVS = 4;
  148. static constexpr const uint32_t MULTIBOOT_MEMORY_BADRAM = 5;
  149. struct multiboot_mmap_entry_t {
  150. uint64_t addr;
  151. uint64_t len;
  152. uint32_t type;
  153. uint32_t zero;
  154. };
  155. struct multiboot_tag_t {
  156. uint32_t type;
  157. uint32_t size;
  158. };
  159. struct multiboot_tag_string_t : multiboot_tag_t {
  160. char string[0];
  161. };
  162. struct multiboot_tag_module_t : multiboot_tag_t {
  163. uint32_t mod_start;
  164. uint32_t mod_end;
  165. char cmdline[0];
  166. };
  167. struct multiboot_tag_basic_meminfo_t : multiboot_tag_t {
  168. uint32_t mem_lower;
  169. uint32_t mem_upper;
  170. };
  171. struct multiboot_tag_bootdev_t : multiboot_tag_t {
  172. uint32_t biosdev;
  173. uint32_t slice;
  174. uint32_t part;
  175. };
  176. struct multiboot_tag_mmap_t : multiboot_tag_t {
  177. uint32_t entry_size;
  178. uint32_t entry_version;
  179. multiboot_mmap_entry_t entries[0];
  180. };
  181. struct multiboot_vbe_info_block_t {
  182. uint8_t external_specification[512];
  183. };
  184. struct multiboot_vbe_mode_info_block_t {
  185. uint8_t external_specification[256];
  186. };
  187. struct multiboot_tag_vbe_t : multiboot_tag_t {
  188. uint16_t vbe_mode;
  189. uint16_t vbe_interface_seg;
  190. uint16_t vbe_interface_off;
  191. uint16_t vbe_interface_len;
  192. multiboot_vbe_info_block_t vbe_control_info;
  193. multiboot_vbe_mode_info_block_t vbe_mode_info;
  194. };
  195. struct multiboot_tag_elf_sections_t : multiboot_tag_t {
  196. uint32_t num;
  197. uint32_t entsize;
  198. // 段字符串表索引
  199. uint32_t shndx;
  200. char sections[0];
  201. };
  202. struct multiboot_tag_apm_t : multiboot_tag_t {
  203. uint16_t version;
  204. uint16_t cseg;
  205. uint32_t offset;
  206. uint16_t cseg_16;
  207. uint16_t dseg;
  208. uint16_t flags;
  209. uint16_t cseg_len;
  210. uint16_t cseg_16_len;
  211. uint16_t dseg_len;
  212. };
  213. struct multiboot_tag_efi32_t : multiboot_tag_t {
  214. uint32_t pointer;
  215. };
  216. struct multiboot_tag_efi64_t : multiboot_tag_t {
  217. uint64_t pointer;
  218. };
  219. struct multiboot_tag_smbios_t : multiboot_tag_t {
  220. uint8_t major;
  221. uint8_t minor;
  222. uint8_t reserved[6];
  223. uint8_t tables[0];
  224. };
  225. struct multiboot_tag_old_acpi_t : multiboot_tag_t {
  226. uint8_t rsdp[0];
  227. };
  228. struct multiboot_tag_new_acpi_t : multiboot_tag_t {
  229. uint8_t rsdp[0];
  230. };
  231. struct multiboot_tag_network_t : multiboot_tag_t {
  232. uint8_t dhcpack[0];
  233. };
  234. struct multiboot_tag_efi_mmap_t : multiboot_tag_t {
  235. uint32_t descr_size;
  236. uint32_t descr_vers;
  237. uint8_t efi_mmap[0];
  238. };
  239. struct multiboot_tag_efi32_ih_t : multiboot_tag_t {
  240. uint32_t pointer;
  241. };
  242. struct multiboot_tag_efi64_ih_t : multiboot_tag_t {
  243. uint64_t pointer;
  244. };
  245. struct multiboot_tag_load_base_addr_t : multiboot_tag_t {
  246. uint32_t load_base_addr;
  247. };
  248. // 迭代变量
  249. // 与 multiboot_tag_t 相同
  250. struct iter_data_t {
  251. uint32_t type;
  252. uint32_t size;
  253. };
  254. public:
  255. /**
  256. * @brief 初始化
  257. * @return true 成功
  258. * @return false 失败
  259. */
  260. static bool multiboot2_init(void);
  261. /**
  262. * @brief 迭代器
  263. * @param _fun 迭代操作
  264. * @param _data 数据
  265. */
  266. static void multiboot2_iter(bool (*_fun)(const iter_data_t *, void *),
  267. void *_data);
  268. /**
  269. * @brief 获取内存信息
  270. * @param _iter_data 迭代变量
  271. * @param _data 数据
  272. * @return true 成功
  273. * @return false 失败
  274. */
  275. static bool get_memory(const iter_data_t *_iter_data, void *_data);
  276. };
  277. namespace BOOT_INFO {
  278. /// 魔数
  279. extern "C" uint32_t multiboot2_magic;
  280. };
  281. #endif /* _MULTIBOOT2_H_ */