summaryrefslogtreecommitdiff
path: root/src/arch/amd64/mboot.c
blob: b7124dfb19b9618ae4c3e4397ad013334622f9da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <shim.h>
#include <lib.h>
#include <stdint.h>
#include <panic.h>

#include "mboot.h"

#define MBOOT_HEADER_MAGIC  0x36D76289

#define MBOOT_CMDLINE       1
#define MBOOT_MEMORY_MAP    6
#define MBOOT_ELF_SYMBOLS   9
#define MBOOT_OLD_RSDP     14
#define MBOOT_NEW_RSDP     15

extern char symtab;
#define kaddr(addr) ((uintptr_t)(&addr))

typedef uint8_t   mboot_uint8_t;
typedef uint16_t  mboot_uint16_t;
typedef uint32_t  mboot_uint32_t;
typedef uint64_t  mboot_uint64_t;

struct mboot_info {
	mboot_uint32_t total_size;
	mboot_uint32_t reserved;
	char tags[];
};

struct mboot_tag {
	mboot_uint32_t type;
	mboot_uint32_t size;
	char data[];
};

struct mboot_tag_elf_sections {
	mboot_uint32_t type;
	mboot_uint32_t size;
	mboot_uint16_t num;
	mboot_uint16_t entsize;
	mboot_uint16_t shndx;
	mboot_uint16_t reserved;
	char sections[];
};

struct mboot_tag_elf_sections_entry {
	mboot_uint32_t sh_name;
	mboot_uint32_t sh_type;
	mboot_uint64_t sh_flags;
	mboot_uint64_t sh_addr;
	mboot_uint64_t sh_offset;
	mboot_uint64_t sh_size;
	mboot_uint32_t sh_link;
	mboot_uint32_t sh_info;
	mboot_uint64_t sh_addralign;
	mboot_uint64_t sh_entsize;
};

struct mboot_mmap_entry {
	mboot_uint64_t addr;
	mboot_uint64_t len;
	mboot_uint32_t type;
	mboot_uint32_t zero;
};

struct mboot_tag_mmap {
	mboot_uint32_t type;
	mboot_uint32_t size;
	mboot_uint32_t entry_size;
	mboot_uint32_t entry_version;
	struct mboot_mmap_entry entries[];
};


struct mboot_tag_old_rsdp {
	mboot_uint32_t type;
	mboot_uint32_t size;
	mboot_uint8_t rsdp[];
};

struct mboot_tag_new_rsdp {
	mboot_uint32_t type;
	mboot_uint32_t size;
	mboot_uint8_t rsdp[];
};

struct mboot_tag_cmdline {
	mboot_uint32_t type;
	mboot_uint32_t size;
	mboot_uint8_t cmdline[];
};

static void read_symbols(
		struct boot_info *shim_info,
		struct mboot_tag_elf_sections *sections
) {

	shim_info->symbol_table = sections->sections;

//	struct mboot_elf_section_header *section =
//		(struct mboot_elf_section_header *) (layout->elf_section_headers);
//
//	for (mboot_uint32_t i = 0; i < layout->num; i++) {
//		char buf[20];
//
//		ultoa(i, buf, 10);
//		serial_out_str("[");
//        serial_out_str(buf);
//        serial_out_str("]\t");
//
//		serial_out_str((char *)(kaddr(symtab) + section->sh_name));
//		serial_out('\t');
//
//		ultoa(section->sh_type, buf, 16);
//		serial_out_str("type: 0x");
//        serial_out_str(buf);
//        serial_out('\t');
//
//        ultoa(section->sh_addr, buf, 16);
//        serial_out_str("addr: 0x");
//        serial_out_str(buf);
//        serial_out('\t');
//
//        ultoa(section->sh_offset, buf, 16);
//        serial_out_str("offset: 0x");
//        serial_out_str(buf);
//        serial_out('\n');
//
//        section++;
//	}
}

static void read_cmdline(
		struct boot_info *shim_info,
		struct mboot_tag_cmdline *cmdline
) {
	mboot_uint32_t size = cmdline->size - 8;
	if (size >= CMDLINE_MAX)
		size = CMDLINE_MAX; // truncate :(
	memcpy(shim_info->cmdline, cmdline->cmdline, size);
	shim_info->cmdline[size] = '\0';
}

static void read_memory_map(
		struct boot_info *shim_info,
		struct mboot_tag_mmap *map
) {
	int idx = 0;
	uintptr_t i = (uintptr_t)map->entries;
	for (	;
			i < (uintptr_t)map->entries + map->size;
			i += map->entry_size, idx++
	) {
		struct mboot_mmap_entry *seg = (struct mboot_mmap_entry *) i;
		shim_info->map.entries[idx].addr = seg->addr;
		shim_info->map.entries[idx].type = seg->type;
		shim_info->map.entries[idx].len = seg->len;
	}
	shim_info->map.entry_count = idx;
}

static void read_old_rsdp(
		struct boot_info *shim_info,
		struct mboot_tag_old_rsdp *rsdp
) {
	if (shim_info->acpi_table != NULL)
		return; // xsdp is newer and has been loaded
	shim_info->acpi_table = rsdp->rsdp;
}

static void read_new_rsdp(
		struct boot_info *shim_info,
		struct mboot_tag_new_rsdp *rsdp
) {
	shim_info->acpi_table = rsdp->rsdp;
}

void mboot_load_info(
		long mboot_magic,
		const void *mboot_data_ptr,
		struct boot_info *shim_info
) {

	if (mboot_magic != MBOOT_HEADER_MAGIC)
		panic("invalid multiboot magic");

	memset(shim_info, 0, sizeof(struct boot_info));

	struct mboot_info *mboot_info = (struct mboot_info *) mboot_data_ptr;
	const char *mboot_end = ((char *) mboot_info) + mboot_info->total_size;

	char *tag_ptr = mboot_info->tags;

	while (tag_ptr < mboot_end) {
	struct mboot_tag *tag = (struct mboot_tag *) tag_ptr;

		switch (tag->type) {
			case MBOOT_CMDLINE:
				read_cmdline(
						shim_info,
						(struct mboot_tag_cmdline *) tag
				);
				break;
			case MBOOT_MEMORY_MAP:
				read_memory_map(
						shim_info,
						(struct mboot_tag_mmap *) tag
				);
				break;
			case MBOOT_ELF_SYMBOLS:
				read_symbols(
						shim_info,
						(struct mboot_tag_elf_sections *) tag
				);
				break;
			case MBOOT_OLD_RSDP:
				read_old_rsdp(
						shim_info,
						(struct mboot_tag_old_rsdp *) tag
				);
				break;
			case MBOOT_NEW_RSDP:
				read_new_rsdp(
						shim_info,
						(struct mboot_tag_new_rsdp *) tag
				);
				break;
			default:
				break;
		}

		int size = tag->size;
		if (size % 8 != 0) {
			size += 8 - (size % 8);
		}

		tag_ptr += size;
	}
}