summaryrefslogtreecommitdiff
path: root/kernel/mboot/mboot.c
blob: 8163f04ebdfd474e93ed6fea811e7563b090124c (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
#include <lib.h>
#include <comus/mboot.h>

#include "mboot.h"

static volatile void *mboot = NULL;

extern char kernel_end[];

void mboot_init(long magic, volatile void *ptr)
{
	if (magic != MULTIBOOT2_BOOTLOADER_MAGIC)
		panic("invalid multiboot magic: %#08lx", magic);
	mboot = ptr;
}

void *mboot_end(void)
{
	if (mboot == NULL)
		return NULL;

	struct multiboot *info = (struct multiboot *)mboot;
	uintptr_t mboot_end, initrd_end;
	size_t initrd_len;

	mboot_end = (uintptr_t)info + info->total_size;
	initrd_end = (uintptr_t)mboot_get_initrd_phys(&initrd_len);
	if (initrd_end)
		initrd_end += initrd_len;

	return (void *)MAX(mboot_end, initrd_end);
}

void *locate_mboot_table(uint32_t type)
{
	if (mboot == NULL)
		return NULL;

	struct multiboot *info = (struct multiboot *)mboot;

	const char *mboot_end = ((char *)info) + info->total_size;
	char *tag_ptr = info->tags;

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

		if (tag->type == type)
			return tag;

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

	return NULL;
}