multiboot2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**
  2. * @file multiboot2.h
  3. * @brief multiboot2 解析
  4. */
  5. #pragma once
  6. #include "stdint.h"
  7. #include "stdbool.h"
  8. #include "../../common/boot_info.h"
  9. #include "../acpi/acpi.h"
  10. /// @see Multiboot2 Specification version 2.0.pdf
  11. // 启动后,在 32 位内核进入点,机器状态如下:
  12. // 1. CS 指向基地址为 0x00000000,限长为4G – 1的代码段描述符。
  13. // 2. DS,SS,ES,FS 和 GS 指向基地址为0x00000000,限长为4G –
  14. // 1的数据段描述符。
  15. // 3. A20 地址线已经打开。
  16. // 4. 页机制被禁止。
  17. // 5. 中断被禁止。
  18. // 6. EAX = 0x2BADB002
  19. // 7. 系统信息和启动信息块的线性地址保存在 EBX中(相当于一个指针)。
  20. // 以下即为这个信息块的结构
  21. /**
  22. * @brief MULTIBOOT2 接口抽象
  23. */
  24. extern unsigned int multiboot2_magic;
  25. /* How many bytes from the start of the file we search for the header. */
  26. static const unsigned int MULTIBOOT_SEARCH = 32768;
  27. static const unsigned int MULTIBOOT_HEADER_ALIGN = 8;
  28. /* The magic field should contain this. */
  29. static const unsigned int MULTIBOOT2_HEADER_MAGIC = 0xe85250d6;
  30. /* This should be in %eax. */
  31. static const unsigned int MULTIBOOT2_BOOTLOADER_MAGIC = 0x36d76289;
  32. /* Alignment of multiboot modules. */
  33. static const unsigned int MULTIBOOT_MOD_ALIGN = 0x00001000;
  34. /* Alignment of the multiboot info structure. */
  35. static const unsigned int MULTIBOOT_INFO_ALIGN = 0x00000008;
  36. /* Flags set in the 'flags' member of the multiboot header. */
  37. static const unsigned int MULTIBOOT_TAG_ALIGN = 8;
  38. static const unsigned int MULTIBOOT_TAG_TYPE_END = 0;
  39. static const unsigned int MULTIBOOT_TAG_TYPE_CMDLINE = 1;
  40. static const unsigned int MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME = 2;
  41. static const unsigned int MULTIBOOT_TAG_TYPE_MODULE = 3;
  42. static const unsigned int MULTIBOOT_TAG_TYPE_BASIC_MEMINFO = 4;
  43. static const unsigned int MULTIBOOT_TAG_TYPE_BOOTDEV = 5;
  44. static const unsigned int MULTIBOOT_TAG_TYPE_MMAP = 6;
  45. static const unsigned int MULTIBOOT_TAG_TYPE_VBE = 7;
  46. static const unsigned int MULTIBOOT_TAG_TYPE_FRAMEBUFFER = 8;
  47. static const unsigned int MULTIBOOT_TAG_TYPE_ELF_SECTIONS = 9;
  48. static const unsigned int MULTIBOOT_TAG_TYPE_APM = 10;
  49. static const unsigned int MULTIBOOT_TAG_TYPE_EFI32 = 11;
  50. static const unsigned int MULTIBOOT_TAG_TYPE_EFI64 = 12;
  51. static const unsigned int MULTIBOOT_TAG_TYPE_SMBIOS = 13;
  52. static const unsigned int MULTIBOOT_TAG_TYPE_ACPI_OLD = 14;
  53. static const unsigned int MULTIBOOT_TAG_TYPE_ACPI_NEW = 15;
  54. static const unsigned int MULTIBOOT_TAG_TYPE_NETWORK = 16;
  55. static const unsigned int MULTIBOOT_TAG_TYPE_EFI_MMAP = 17;
  56. static const unsigned int MULTIBOOT_TAG_TYPE_EFI_BS = 18;
  57. static const unsigned int MULTIBOOT_TAG_TYPE_EFI32_IH = 19;
  58. static const unsigned int MULTIBOOT_TAG_TYPE_EFI64_IH = 20;
  59. static const unsigned int MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR = 21;
  60. static const unsigned int MULTIBOOT_HEADER_TAG_END = 0;
  61. static const unsigned int MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST =
  62. 1;
  63. static const unsigned int MULTIBOOT_HEADER_TAG_ADDRESS = 2;
  64. static const unsigned int MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS = 3;
  65. static const unsigned int MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS = 4;
  66. static const unsigned int MULTIBOOT_HEADER_TAG_FRAMEBUFFER = 5;
  67. static const unsigned int MULTIBOOT_HEADER_TAG_MODULE_ALIGN = 6;
  68. static const unsigned int MULTIBOOT_HEADER_TAG_EFI_BS = 7;
  69. static const unsigned int MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI32 =
  70. 8;
  71. static const unsigned int MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64 =
  72. 9;
  73. static const unsigned int MULTIBOOT_HEADER_TAG_RELOCATABLE = 10;
  74. static const unsigned int MULTIBOOT_ARCHITECTURE_I386 = 0;
  75. static const unsigned int MULTIBOOT_ARCHITECTURE_MIPS32 = 4;
  76. static const unsigned int MULTIBOOT_HEADER_TAG_OPTIONAL = 1;
  77. static const unsigned int MULTIBOOT_LOAD_PREFERENCE_NONE = 0;
  78. static const unsigned int MULTIBOOT_LOAD_PREFERENCE_LOW = 1;
  79. static const unsigned int MULTIBOOT_LOAD_PREFERENCE_HIGH = 2;
  80. static const unsigned int MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED =
  81. 1;
  82. static const unsigned int MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED =
  83. 2;
  84. static const unsigned int MULTIBOOT_MEMORY_AVAILABLE = 1;
  85. static const unsigned int MULTIBOOT_MEMORY_RESERVED = 2;
  86. static const unsigned int MULTIBOOT_MEMORY_ACPI_RECLAIMABLE = 3;
  87. static const unsigned int MULTIBOOT_MEMORY_NVS = 4;
  88. static const unsigned int MULTIBOOT_MEMORY_BADRAM = 5;
  89. struct multiboot_header_t
  90. {
  91. // Must be MULTIBOOT_MAGIC - see above.
  92. unsigned int magic;
  93. // ISA
  94. unsigned int architecture;
  95. // Total header length.
  96. unsigned int header_length;
  97. // The above fields plus this one must equal 0 mod 2^32.
  98. unsigned int checksum;
  99. };
  100. struct multiboot_header_tag_t
  101. {
  102. uint16_t type;
  103. uint16_t flags;
  104. unsigned int size;
  105. };
  106. struct multiboot_header_tag_information_request_t
  107. {
  108. uint16_t type;
  109. uint16_t flags;
  110. unsigned int size;
  111. unsigned int requests[0];
  112. };
  113. struct multiboot_header_tag_address_t
  114. {
  115. uint16_t type;
  116. uint16_t flags;
  117. unsigned int size;
  118. unsigned int header_addr;
  119. unsigned int load_addr;
  120. unsigned int load_end_addr;
  121. unsigned int bss_end_addr;
  122. };
  123. struct multiboot_header_tag_entry_address_t
  124. {
  125. uint16_t type;
  126. uint16_t flags;
  127. unsigned int size;
  128. unsigned int entry_addr;
  129. };
  130. struct multiboot_header_tag_console_flags_t
  131. {
  132. uint16_t type;
  133. uint16_t flags;
  134. unsigned int size;
  135. unsigned int console_flags;
  136. };
  137. struct multiboot_header_tag_framebuffer_t
  138. {
  139. uint16_t type;
  140. uint16_t flags;
  141. unsigned int size;
  142. unsigned int width;
  143. unsigned int height;
  144. unsigned int depth;
  145. };
  146. struct multiboot_header_tag_module_align_t
  147. {
  148. uint16_t type;
  149. uint16_t flags;
  150. unsigned int size;
  151. };
  152. struct multiboot_header_tag_relocatable_t
  153. {
  154. uint16_t type;
  155. uint16_t flags;
  156. unsigned int size;
  157. unsigned int min_addr;
  158. unsigned int max_addr;
  159. unsigned int align;
  160. unsigned int preference;
  161. };
  162. struct multiboot_color_t
  163. {
  164. uint8_t red;
  165. uint8_t green;
  166. uint8_t blue;
  167. };
  168. // multiboot2协议的内存区域信息
  169. struct multiboot_mmap_entry_t
  170. {
  171. uint64_t addr;
  172. uint64_t len;
  173. unsigned int type;
  174. unsigned int reserved;
  175. };
  176. struct multiboot_tag_t
  177. {
  178. unsigned int type;
  179. unsigned int size;
  180. };
  181. struct multiboot_tag_string_t
  182. {
  183. struct multiboot_tag_t tag_t;
  184. char string[0];
  185. };
  186. struct multiboot_tag_module_t
  187. {
  188. struct multiboot_tag_t tag_t;
  189. unsigned int mod_start;
  190. unsigned int mod_end;
  191. char cmdline[0];
  192. };
  193. struct multiboot_tag_basic_meminfo_t
  194. {
  195. struct multiboot_tag_t tag_t;
  196. unsigned int mem_lower;
  197. unsigned int mem_upper;
  198. };
  199. struct multiboot_tag_bootdev_t
  200. {
  201. struct multiboot_tag_t tag_t;
  202. unsigned int biosdev;
  203. unsigned int slice;
  204. unsigned int part;
  205. };
  206. struct multiboot_tag_mmap_t
  207. {
  208. struct multiboot_tag_t tag_t;
  209. unsigned int entry_size;
  210. unsigned int entry_version;
  211. struct multiboot_mmap_entry_t entries[0];
  212. };
  213. struct multiboot_vbe_info_block_t
  214. {
  215. uint8_t external_specification[512];
  216. };
  217. struct multiboot_vbe_mode_info_block_t
  218. {
  219. uint8_t external_specification[256];
  220. };
  221. // bootloader传递的VBE信息的结构体
  222. struct multiboot_tag_vbe_t
  223. {
  224. struct multiboot_tag_t tag_t;
  225. uint16_t vbe_mode;
  226. uint16_t vbe_interface_seg;
  227. uint16_t vbe_interface_off;
  228. uint16_t vbe_interface_len;
  229. // The fields ‘vbe_control_info’ and ‘vbe_mode_info’ contain VBE control information returned by the VBE Function 00h and VBE mode information
  230. // returned by the VBE Function 01h, respectively.
  231. struct multiboot_vbe_info_block_t vbe_control_info;
  232. struct multiboot_vbe_mode_info_block_t vbe_mode_info;
  233. };
  234. struct multiboot_tag_framebuffer_info_t
  235. {
  236. struct multiboot_tag_t tag_t;
  237. uint64_t framebuffer_addr;
  238. uint32_t framebuffer_pitch; // 帧缓存上界
  239. // width and height expressed in pixels except type=2
  240. // when type=2, they are expressed in characters
  241. uint32_t framebuffer_width;
  242. uint32_t framebuffer_height;
  243. // number of bits per pixel.
  244. uint8_t framebuffer_bpp;
  245. // 帧缓存的类型
  246. uint8_t framebuffer_type;
  247. uint8_t reserved;
  248. };
  249. // indexed color
  250. struct multiboot_tag_framebuffer_info_type0_t
  251. {
  252. struct multiboot_tag_framebuffer_info_t;
  253. uint32_t framebuffer_palette_num_colors;
  254. struct multiboot_color_t color_desc;
  255. };
  256. // direct RGB color
  257. struct multiboot_tag_framebuffer_info_type1_t
  258. {
  259. struct multiboot_tag_framebuffer_info_t;
  260. uint8_t framebuffer_red_field_position;
  261. uint8_t framebuffer_red_mask_size;
  262. uint8_t framebuffer_green_field_position;
  263. uint8_t framebuffer_green_mask_size;
  264. uint8_t framebuffer_blue_field_position;
  265. uint8_t framebuffer_blue_mask_size;
  266. };
  267. struct multiboot_tag_elf_sections_t
  268. {
  269. struct multiboot_tag_t tag_t;
  270. unsigned int num;
  271. unsigned int entsize;
  272. // 段字符串表索引
  273. unsigned int shndx;
  274. char sections[0];
  275. };
  276. struct multiboot_tag_apm_t
  277. {
  278. struct multiboot_tag_t tag_t;
  279. uint16_t version;
  280. uint16_t cseg;
  281. unsigned int offset;
  282. uint16_t cseg_16;
  283. uint16_t dseg;
  284. uint16_t flags;
  285. uint16_t cseg_len;
  286. uint16_t cseg_16_len;
  287. uint16_t dseg_len;
  288. };
  289. struct multiboot_tag_efi32_t
  290. {
  291. struct multiboot_tag_t tag_t;
  292. unsigned int pointer;
  293. };
  294. struct multiboot_tag_efi64_t
  295. {
  296. struct multiboot_tag_t tag_t;
  297. uint64_t pointer;
  298. };
  299. struct multiboot_tag_smbios_t
  300. {
  301. struct multiboot_tag_t tag_t;
  302. uint8_t major;
  303. uint8_t minor;
  304. uint8_t reserved[6];
  305. uint8_t tables[0];
  306. };
  307. struct multiboot_tag_old_acpi_t
  308. {
  309. struct multiboot_tag_t tag_t;
  310. //uint8_t rsdp[0];
  311. struct acpi_RSDP_t rsdp;
  312. };
  313. struct multiboot_tag_new_acpi_t
  314. {
  315. struct multiboot_tag_t tag_t;
  316. //uint8_t rsdp[0];
  317. struct acpi_RSDP_2_t rsdp;
  318. };
  319. struct multiboot_tag_network_t
  320. {
  321. struct multiboot_tag_t tag_t;
  322. uint8_t dhcpack[0];
  323. };
  324. struct multiboot_tag_efi_mmap_t
  325. {
  326. struct multiboot_tag_t tag_t;
  327. unsigned int descr_size;
  328. unsigned int descr_vers;
  329. uint8_t efi_mmap[0];
  330. };
  331. struct multiboot_tag_efi32_ih_t
  332. {
  333. struct multiboot_tag_t tag_t;
  334. unsigned int pointer;
  335. };
  336. struct multiboot_tag_efi64_ih_t
  337. {
  338. struct multiboot_tag_t tag_t;
  339. uint64_t pointer;
  340. };
  341. struct multiboot_tag_load_base_addr_t
  342. {
  343. struct multiboot_tag_t tag_t;
  344. unsigned int load_base_addr;
  345. };
  346. // 迭代变量
  347. // 与 multiboot_tag_t 相同
  348. struct iter_data_t
  349. {
  350. unsigned int type;
  351. unsigned int size;
  352. };
  353. /**
  354. * @brief 初始化
  355. * @return true 成功
  356. * @return false 失败
  357. */
  358. static bool multiboot2_init(void);
  359. /**
  360. * @brief 迭代器
  361. * @param _fun 迭代操作
  362. * @param _data 数据
  363. */
  364. void multiboot2_iter(bool (*_fun)(const struct iter_data_t *, void *, unsigned int *),
  365. void *_data, unsigned int *count);
  366. /**
  367. * @brief 获取multiboot2协议提供的内存区域信息
  368. *
  369. * @param _iter_data 要被迭代的信息的结构体
  370. * @param _data 返回信息的结构体指针
  371. * @param count 返回数组的长度
  372. * @return true
  373. * @return false
  374. */
  375. bool multiboot2_get_memory(const struct iter_data_t *_iter_data, void *_data, unsigned int *count);
  376. /**
  377. * @brief 获取VBE信息
  378. *
  379. * @param _iter_data 要被迭代的信息的结构体
  380. * @param _data 返回信息的结构体指针
  381. */
  382. bool multiboot2_get_VBE_info(const struct iter_data_t *_iter_data, void *_data, unsigned int *reserved);
  383. /**
  384. * @brief 获取帧缓冲区信息
  385. *
  386. * @param _iter_data 要被迭代的信息的结构体
  387. * @param _data 返回信息的结构体指针
  388. */
  389. bool multiboot2_get_Framebuffer_info(const struct iter_data_t *_iter_data, void *_data, unsigned int *reserved);
  390. /**
  391. * @brief 获取acpi旧版RSDP
  392. *
  393. * @param _iter_data 要被迭代的信息的结构体
  394. * @param _data old RSDP的结构体指针
  395. * @param reserved
  396. * @return uint8_t*
  397. */
  398. bool multiboot2_get_acpi_old_RSDP(const struct iter_data_t *_iter_data, void *data, unsigned int *reserved);
  399. /**
  400. * @brief 获取acpi新版RSDP
  401. *
  402. * @param _iter_data 要被迭代的信息的结构体
  403. * @param _data old RSDP的结构体指针
  404. * @param reserved
  405. * @return uint8_t* struct multiboot_tag_old_acpi_t
  406. */
  407. bool multiboot2_get_acpi_new_RSDP(const struct iter_data_t *_iter_data, void *data, unsigned int *reserved);