From fbe59c8ffc9e57c449f0ad5f5614e53f621b9f33 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sun, 23 Aug 2026 16:23:33 +0200 Subject: [PATCH] feat(boot): parse multiboot2 boot information structure --- include/multiboot2.h | 42 ++++++++++++++++++++++++++++++++++++++++++ src/multiboot.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 include/multiboot2.h create mode 100644 src/multiboot.c diff --git a/include/multiboot2.h b/include/multiboot2.h new file mode 100644 index 0000000..edf7e8c --- /dev/null +++ b/include/multiboot2.h @@ -0,0 +1,42 @@ +#ifndef FROSTBYTE_MULTIBOOT2_H +#define FROSTBYTE_MULTIBOOT2_H + +#include "types.h" + +#define multibootTagTypeEnd 0 +#define multibootTagTypeModule 3 +#define multibootTagTypeMmap 6 + +#define multibootMemoryAvailable 1 + +typedef struct MultibootTag { + uint32 type; + uint32 size; +} __attribute__((packed)) MultibootTag; + +typedef struct MultibootTagModule { + uint32 type; + uint32 size; + uint32 moduleStart; + uint32 moduleEnd; + char string[]; +} __attribute__((packed)) MultibootTagModule; + +typedef struct MultibootMmapEntry { + uint64 baseAddr; + uint64 length; + uint32 type; + uint32 reserved; +} __attribute__((packed)) MultibootMmapEntry; + +typedef struct MultibootTagMmap { + uint32 type; + uint32 size; + uint32 entrySize; + uint32 entryVersion; + MultibootMmapEntry entries[]; +} __attribute__((packed)) MultibootTagMmap; + +bool multibootFindModule(uint64 multibootInfo, uint64* start, uint64* end); + +#endif diff --git a/src/multiboot.c b/src/multiboot.c new file mode 100644 index 0000000..ba84a8b --- /dev/null +++ b/src/multiboot.c @@ -0,0 +1,28 @@ +#include "multiboot2.h" + +static uint64 alignUp(uint64 value, uint64 alignment) +{ + return (value + alignment - 1) & ~(alignment - 1); +} + +bool multibootFindModule(uint64 multibootInfo, uint64* start, uint64* end) +{ + uint32 totalSize = *(uint32*)multibootInfo; + uint64 tagAddr = multibootInfo + 8; + uint64 endAddr = multibootInfo + totalSize; + + while (tagAddr < endAddr) { + MultibootTag* tag = (MultibootTag*)tagAddr; + if (tag->type == multibootTagTypeEnd) + break; + if (tag->type == multibootTagTypeModule) { + MultibootTagModule* module = (MultibootTagModule*)tag; + *start = module->moduleStart; + *end = module->moduleEnd; + return true; + } + tagAddr = alignUp(tagAddr + tag->size, 8); + } + + return false; +}