70 lines
2.3 KiB
C
70 lines
2.3 KiB
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#include "commands.h"
|
|
#include "debug.h"
|
|
#include "kpanic.h"
|
|
#include "memory.h"
|
|
#include "multiboot.h"
|
|
#include "utils.h"
|
|
#include "vbe.h"
|
|
|
|
uint32_t multiboot_page_table[1024] __attribute__((aligned(PAGE_SIZE)));
|
|
uint32_t vbe_page_table[1024] __attribute__((aligned(PAGE_SIZE)));
|
|
multiboot_memory_map_t *mmap_addr;
|
|
multiboot_uint32_t mmap_length;
|
|
|
|
static void init_mmap(multiboot_info_t *mbd_virt, size_t *pt_index)
|
|
{
|
|
// Index mbd->mmap_addr pointers
|
|
uint32_t i = 0;
|
|
for (; i < mbd_virt->mmap_length; i++)
|
|
multiboot_page_table[i + *pt_index] =
|
|
((mbd_virt->mmap_addr + i * PAGE_SIZE) & PAGE_MASK) |
|
|
INIT_FLAGS;
|
|
mmap_addr = (multiboot_memory_map_t *)(GET_PAGE_ADDR(1023, *pt_index) +
|
|
(uint32_t)mbd_virt->mmap_addr %
|
|
PAGE_SIZE);
|
|
mmap_length = mbd_virt->mmap_length / sizeof(multiboot_memory_map_t);
|
|
*pt_index += i;
|
|
}
|
|
|
|
static void init_vbe(multiboot_info_t *mbd_virt)
|
|
{
|
|
const uint32_t framebuffer_size =
|
|
mbd_virt->framebuffer_height * mbd_virt->framebuffer_pitch;
|
|
for (uint32_t i = 0; i < CEIL(framebuffer_size, PAGE_SIZE); i++) {
|
|
vbe_page_table[i % 1024] =
|
|
((mbd_virt->framebuffer_addr + i * PAGE_SIZE) & PAGE_MASK) |
|
|
INIT_FLAGS;
|
|
}
|
|
kernel_pd[800] = ((uint32_t)vbe_page_table - HEAP_END) | 0x03;
|
|
display.buff = (uint32_t *)GET_PAGE_ADDR(800, 0) +
|
|
(mbd_virt->framebuffer_addr % PAGE_SIZE);
|
|
display.height = mbd_virt->framebuffer_height;
|
|
display.width = mbd_virt->framebuffer_width;
|
|
display.pitch = mbd_virt->framebuffer_pitch;
|
|
display.bpp = mbd_virt->framebuffer_bpp;
|
|
}
|
|
|
|
void init_multiboot(multiboot_info_t *mbd, uint32_t magic)
|
|
{
|
|
if (magic != MULTIBOOT_BOOTLOADER_MAGIC)
|
|
kpanic("invalid magic number! (git good skill issue)");
|
|
|
|
init_page_table(multiboot_page_table, 0);
|
|
kernel_pd[1023] = ((uint32_t)multiboot_page_table - HEAP_END) | 0x03;
|
|
size_t pt_index = CEIL(
|
|
(uint32_t)mbd % PAGE_SIZE + sizeof(multiboot_info_t), PAGE_SIZE);
|
|
|
|
// Index multiboot_info_t struct
|
|
for (uint32_t i = 0; i < pt_index; i++)
|
|
multiboot_page_table[i] =
|
|
(((uint32_t)mbd + PAGE_SIZE * i) & PAGE_MASK) | INIT_FLAGS;
|
|
multiboot_info_t *mbd_virt =
|
|
(multiboot_info_t *)(GET_PAGE_ADDR(1023, 0) +
|
|
(uint32_t)mbd % PAGE_SIZE);
|
|
init_mmap(mbd_virt, &pt_index);
|
|
init_vbe(mbd_virt);
|
|
}
|