multiboot2.h 11 KB

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