1 /*
2  * Copyright (c) 2009 Corey Tabaka
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 /* from https://www.gnu.org/software/grub/manual/multiboot/multiboot.html */
11 
12 /* magic number for multiboot header */
13 #define MULTIBOOT_HEADER_MAGIC      0x1BADB002
14 
15 /* magic number passed by multiboot-compliant boot loaders */
16 #define MULTIBOOT_BOOTLOADER_MAGIC  0x2BADB002
17 
18 /* Alignment of multiboot modules. */
19 #define MULTIBOOT_MOD_ALIGN         0x00001000
20 
21 /* Alignment of the multiboot info structure. */
22 #define MULTIBOOT_INFO_ALIGN        0x00000004
23 
24 /* Flags set in the ’flags’ member of the multiboot header. */
25 
26 /* Align all boot modules on i386 page (4KB) boundaries. */
27 #define MULTIBOOT_PAGE_ALIGN        0x00000001
28 
29 /* Must pass memory information to OS. */
30 #define MULTIBOOT_MEMORY_INFO       0x00000002
31 
32 /* Must pass video information to OS. */
33 #define MULTIBOOT_VIDEO_MODE        0x00000004
34 
35 /* This flag indicates the use of the address fields in the header. */
36 #define MULTIBOOT_AOUT_KLUDGE       0x00010000
37 
38 #ifndef ASSEMBLY
39 
40 #include <sys/types.h>
41 #include <assert.h>
42 
43 /* multiboot header */
44 typedef struct multiboot_header {
45     uint32_t magic;
46     uint32_t flags;
47     uint32_t checksum;
48     uint32_t header_addr;
49     uint32_t load_addr;
50     uint32_t load_end_addr;
51     uint32_t bss_end_addr;
52     uint32_t entry_addr;
53 } multiboot_header_t;
54 
55 /* symbol table for a.out */
56 typedef struct aout_symbol_table {
57     uint32_t tabsize;
58     uint32_t strsize;
59     uint32_t addr;
60     uint32_t reserved;
61 } aout_symbol_table_t;
62 
63 /* section header table for ELF */
64 typedef struct elf_section_header_table {
65     uint32_t num;
66     uint32_t size;
67     uint32_t addr;
68     uint32_t shndx;
69 } elf_section_header_table_t;
70 
71 /* multiboot info */
72 typedef struct multiboot_info {
73     uint32_t flags;
74     uint32_t mem_lower;
75     uint32_t mem_upper;
76     uint32_t boot_device;
77     uint32_t cmdline;
78     uint32_t mods_count;
79     uint32_t mods_addr;
80     union {
81         aout_symbol_table_t aout_sym;
82         elf_section_header_table_t elf_sec;
83     } u;
84 
85     uint32_t mmap_length;
86     uint32_t mmap_addr;
87 
88     uint32_t drives_length;
89     uint32_t drives_addr;
90 
91     uint32_t config_table;
92 
93     uint32_t boot_loader_name;
94 
95     uint32_t apm_table;
96 
97     uint32_t vbe_control_info;
98     uint32_t vbe_mode_info;
99     uint16_t vbe_mode;
100     uint16_t vbe_interface_seg;
101     uint16_t vbe_interface_off;
102     uint16_t vbe_interface_len;
103 
104     uint64_t framebuffer_addr;
105     uint32_t framebuffer_pitch;
106     uint32_t framebuffer_width;
107     uint32_t framebuffer_height;
108     uint8_t  framebuffer_bpp;
109     uint8_t  framebuffer_type;
110     union {
111         struct {
112             uint32_t framebuffer_palette_addr;
113             uint16_t framebuffer_palette_num_colors;
114         };
115         struct {
116             uint8_t framebuffer_red_field_position;
117             uint8_t framebuffer_red_mask_size;
118             uint8_t framebuffer_green_field_position;
119             uint8_t framebuffer_green_mask_size;
120             uint8_t framebuffer_blue_field_position;
121             uint8_t framebuffer_blue_mask_size;
122         };
123     };
124 } multiboot_info_t;
125 
126 enum {
127     MB_INFO_MEM_SIZE    = 0x001,
128     MB_INFO_BOOT_DEV    = 0x002,
129     MB_INFO_CMD_LINE    = 0x004,
130     MB_INFO_MODS        = 0x008,
131     MB_INFO_SYMS        = 0x010,
132     MB_INFO_MMAP        = 0x040,
133     MB_INFO_DRIVES      = 0x080,
134     MB_INFO_CONFIG      = 0x100,
135     MB_INFO_BOOT_LOADER = 0x200,
136     MB_INFO_APM_TABLE   = 0x400,
137     MB_INFO_VBE         = 0x800,
138     MB_INFO_FRAMEBUFFER = 0x1000,
139 };
140 
141 /* module structure */
142 typedef struct module {
143     uint32_t mod_start;
144     uint32_t mod_end;
145     uint32_t string;
146     uint32_t reserved;
147 } module_t;
148 
149 /* memory map - be careful that the offset 0 is base_addr_low without size */
150 typedef struct memory_map {
151     uint32_t size;
152     uint32_t base_addr_low;
153     uint32_t base_addr_high;
154     uint32_t length_low;
155     uint32_t length_high;
156     uint32_t type;
157 } memory_map_t;
158 
159 /* memory map entry types */
160 enum {
161     MB_MMAP_TYPE_AVAILABLE      = 0x01,
162     MB_MMAP_TYPE_RESERVED       = 0x02,
163     MB_MMAP_TYPE_ACPI_RECLAIM   = 0x03,
164     MB_MMAP_TYPE_ACPI_NVS       = 0x04,
165     MB_MMAP_TYPE_BADRAM         = 0x05,
166 };
167 
168 /* framebuffer types */
169 enum {
170     MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED  = 0,
171     MULTIBOOT_FRAMEBUFFER_TYPE_RGB      = 1,
172     MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT = 2,
173 };
174 
175 #endif // ASSEMBLY
176 
177