aboutsummaryrefslogtreecommitdiffstats
path: root/multiboot.cpp
blob: 46c505b6ed75e9aac5ebf9141b482a84301aeddb (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
#include "textoutput.hpp"

#include <cstdint>

extern TextOutput& term;

struct multiboot2_tag
{
    alignas(8)
    std::uint16_t id;
    std::uint16_t flags;
    std::uint32_t length;
    std::uint32_t data[1];
} __attribute__((packed));

template<int N>
struct multiboot2
{
    static constexpr std::uint32_t MAGIC    = 0xE85250D6;
    static constexpr std::uint32_t FLAGS    = 0;
    static constexpr std::uint32_t LENGTH   = 16;
    static constexpr std::uint32_t CHECKSUM = -(MAGIC + FLAGS + LENGTH);

    alignas(8)
    std::uint32_t magic = MAGIC;
    std::uint32_t flags = FLAGS;
    std::uint32_t length = LENGTH;
    std::uint32_t checksum = CHECKSUM;

    multiboot2_tag tags[N];
} __attribute__((packed));

__attribute__((section(".multiboot2")))
multiboot2 multibootHeader = {
    .tags = {
        {
            1, 0, sizeof(multiboot2_tag) + sizeof(std::uint32_t),
            {4}
        },
        {
            0, 0, 8, {}
        }
    }
};

std::uint32_t multiboot_magic;
std::uint32_t *multiboot_ptr;

std::uint32_t lowerMem = 0;
std::uint32_t upperMem = 0;
std::uint32_t *acpiRsdp = nullptr;
std::uint32_t *acpiRsdpV2 = nullptr;

bool multiboot_initialize()
{
    if (multiboot_magic != 0x36d76289) {
        term.write("Not multiboot!");
        return false;
    }

    term.write("Found multiboot headers: ");

    auto ptr = multiboot_ptr + 2;
    while (ptr[0] != 0 && ptr[1] != 8) {
        term.write(ptr[0]);
        term.write(", ");

        switch (ptr[0]) {
        case 4:
            lowerMem = ptr[2] * 1024;
            upperMem = ptr[3] * 1024;
            break;
        case 14:
            acpiRsdp = ptr + 2;
            break;
        case 15:
            acpiRsdpV2 = ptr + 2;
            break;
        default:
            break;
        }
    
        auto next = reinterpret_cast<std::uintptr_t>(ptr);
        next += ptr[1];
        next = (next + 7) & ~7;
        ptr = reinterpret_cast<std::uint32_t *>(next);
    }

    term.write('\n');
    return true;
}