Get modules with <kernaux/multiboot2.h>

This commit is contained in:
Alex Kotov 2020-11-30 02:21:45 +05:00
parent 3cebdbb432
commit 670a39c2a7
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 35 additions and 106 deletions

View File

@ -11,7 +11,6 @@ CFLAGS = -std=gnu99 -ffreestanding -nostdinc -fno-builtin -fno-stack-protector -
# Architecture-dependent
OBJS = start.s.o
OBJS += main.c.o
OBJS += multiboot.c.o
OBJS += panic.c.o
OBJS += paging.c.o
OBJS += page_dir.c.o

View File

@ -1,4 +1,3 @@
#include "multiboot.h"
#include "paging.h"
#include "info.h"
@ -95,8 +94,41 @@ void main(
}
}
if (!multiboot_parse(&kinfo, (unsigned long)multiboot2_info)) {
panic("Can not parse Multiboot 2 info.");
for (
const struct KernAux_Multiboot2_Tag_Module *tag =
(struct KernAux_Multiboot2_Tag_Module*)
KernAux_Multiboot2_first_tag_with_type(
multiboot2_info,
KERNAUX_MULTIBOOT2_TAGTYPE_MODULE
);
tag;
tag = (struct KernAux_Multiboot2_Tag_Module*)
KernAux_Multiboot2_tag_with_type_after(
multiboot2_info,
KERNAUX_MULTIBOOT2_TAGTYPE_MODULE,
(struct KernAux_Multiboot2_TagBase*)tag
)
) {
if (kinfo.modules_count >= KERNELMQ_INFO_MODULES_MAX) {
panic("Too many modules in Multiboot 2 info.");
}
unsigned int slen = kstrlen(tag->cmdline);
if (slen > KERNELMQ_INFO_CMDLINE_SLEN_MAX) {
panic("Multiboot 2 module cmd line is too long.");
}
struct KernelMQ_Info_Module *const module =
&kinfo.modules[kinfo.modules_count];
module->base = tag->mod_start;
module->limit = tag->mod_end;
module->size = module->limit - module->base + 1;
++kinfo.modules_count;
kinfo.modules_total_size += module->size;
}
kinfo.kernel_offset = (unsigned long)&_kernel_offset;

View File

@ -1,94 +0,0 @@
#include "multiboot.h"
#include "stdlib.h"
#define MULTIBOOT_TAG_TYPE_END 0
#define MULTIBOOT_TAG_TYPE_MODULE 3
struct multiboot_tag
{
unsigned int type;
unsigned int size;
};
struct multiboot_tag_string
{
unsigned int type;
unsigned int size;
char string[0];
};
struct multiboot_tag_module
{
unsigned int type;
unsigned int size;
unsigned int mod_start;
unsigned int mod_end;
char cmdline[0];
};
static unsigned char print_multiboot_tag(struct KernelMQ_Info *kinfo, const struct multiboot_tag *tag);
static unsigned char print_multiboot_tag_module(struct KernelMQ_Info *kinfo, const struct multiboot_tag_module *tag);
unsigned char multiboot_parse(struct KernelMQ_Info *kinfo, unsigned long base)
{
if (!kinfo) {
return 0;
}
// Unaligned address
if (base & 7) {
return 0;
}
for (
struct multiboot_tag *tag = (struct multiboot_tag*)(base + 8);
tag->type != MULTIBOOT_TAG_TYPE_END;
tag = (struct multiboot_tag*)((unsigned char*)tag + ((tag->size + 7) & ~7))
) {
if (!print_multiboot_tag(kinfo, tag)) {
return 0;
}
}
return 1;
}
unsigned char print_multiboot_tag(struct KernelMQ_Info *kinfo, const struct multiboot_tag *const tag)
{
switch (tag->type)
{
case MULTIBOOT_TAG_TYPE_MODULE:
return print_multiboot_tag_module(kinfo, (struct multiboot_tag_module*)tag);
}
return 1;
}
unsigned char print_multiboot_tag_module(struct KernelMQ_Info *kinfo, const struct multiboot_tag_module *const tag)
{
if (kinfo->modules_count >= KERNELMQ_INFO_MODULES_MAX) {
return 0;
}
unsigned int cmdline_slen = kstrlen(tag->cmdline);
if (cmdline_slen > KERNELMQ_INFO_CMDLINE_SLEN_MAX) {
return 0;
}
struct KernelMQ_Info_Module *module = &kinfo->modules[kinfo->modules_count];
kstrncpy(module->cmdline, tag->cmdline, cmdline_slen);
module->base = tag->mod_start;
module->limit = tag->mod_end;
module->size = module->limit - module->base + 1;
++kinfo->modules_count;
kinfo->modules_total_size += module->size;
return 1;
}

View File

@ -1,8 +0,0 @@
#ifndef KERNELMQ_INCLUDED_MULTIBOOT
#define KERNELMQ_INCLUDED_MULTIBOOT 1
#include "info.h"
unsigned char multiboot_parse(struct KernelMQ_Info *kinfo, unsigned long base);
#endif