multiboot2.h 12 KB

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