mboot parsing

This commit is contained in:
Freya Murphy 2024-01-27 03:01:43 -05:00
parent edc40ed8b1
commit 481b4e303a
Signed by: freya
GPG key ID: 744AB800E383AE52
3 changed files with 106 additions and 1 deletions

View file

@ -8,7 +8,7 @@ ISO_NAME=os_image.iso
CC=cc
LD=ld
CFLAGS=-std=c2x -ffreestanding -g -Wall -Wextra -pedantic -lgcc -isystem $(INCLUDE_DIR)
CFLAGS=-std=c2x -ffreestanding -fno-stack-protector -g -Wall -Wextra -pedantic -lgcc -isystem $(INCLUDE_DIR)
C_SRC=$(shell find $(SRC_DIR) -type f -name "*.c")

65
src/arch/amd64/mboot.c Normal file
View file

@ -0,0 +1,65 @@
#include "mboot.h"
#include <stddef.h>
#include <lib.h>
static void read_cmdline(struct mboot_info *info, struct mboot_tag *tag, char *data, uint8_t len) {
if (len >= CMDLINE_MAX)
len = CMDLINE_MAX; // truncate :(
memcpy(tag->data.cmdline, data, len);
tag->data.cmdline[len] = '\0';
info->tags[MBOOT_CMDLINE] = *tag;
}
static void read_xsdp(struct mboot_info *info, struct mboot_tag *tag, char *data) {
tag->data.rootsdp = (void *) data;
info->tags[MBOOT_XSDP] = *tag;
}
static uint32_t *read_tag(struct mboot_info *info, uint32_t *data) {
struct mboot_tag tag;
tag.type = ((uint16_t*)data)[0];
tag.size = data[1];
tag.valid = 1;
uint8_t data_len = tag.size - 2 * sizeof(uint32_t);
switch (tag.type) {
case MBOOT_CMDLINE:
read_cmdline(info, &tag, (char *)(data + 2), data_len);
break;
case MBOOT_XSDP:
read_xsdp(info, &tag, (char *) (data + 2));
break;
default:
break;
}
if(tag.size % 8 != 0) {
tag.size += 8 - (tag.size % 8);
}
return data + tag.size / sizeof(uint32_t);
}
struct mboot_info mboot_load_info(void *mboot_info) {
struct mboot_info info = {0};
uint32_t* data = (uint32_t*) mboot_info;
info.total_size = *data++;
info.reserved = *data++;
while((uint8_t*) data < (uint8_t*) mboot_info + info.total_size) {
data = read_tag(&info, data);
}
return info;
}
struct mboot_tag *mboot_get_tag(struct mboot_info *info, enum mboot_tag_type type) {
if (info->tags[type].valid) {
return &info->tags[type];
} else {
return NULL;
}
}

40
src/arch/amd64/mboot.h Normal file
View file

@ -0,0 +1,40 @@
#pragma once
#include <stdint.h>
#define CMDLINE_MAX 32
struct mboot_tag {
uint8_t valid; // if the tag at this location is set
uint32_t type;
uint32_t size;
union {
char cmdline[CMDLINE_MAX + 1];
void *rootsdp;
} data;
};
enum mboot_tag_type {
MBOOT_CMDLINE = 0,
MBOOT_XSDP = 14
};
struct mboot_info {
uint32_t total_size;
uint32_t reserved;
struct mboot_tag tags[21];
};
/**
* Loads the multi boot information
* @param mboot_info - the pointer passed from multiboot2
*/
struct mboot_info mboot_load_info(void *mboot_info);
/**
* Gets a tag from multiboot
* @param type - the tag type
* @returns NULL - tag not loaded
* @returns tag - tag was loaded
*/
struct mboot_tag *mboot_get_tag(struct mboot_info *info, enum mboot_tag_type type);