1
0
Fork 0
mirror of https://github.com/tailix/kernel.git synced 2025-02-10 15:36:37 -05:00

Get kernel info from multiboot

This commit is contained in:
Braiden Vasco 2017-11-04 11:43:55 +00:00
parent 97ec95cec7
commit e1f671e8a7
6 changed files with 102 additions and 26 deletions

View file

@ -1,5 +1,9 @@
#include "multiboot.h" #include "multiboot.h"
#include "kprintf.h"
#include <kernelmq/stdlib.h>
#define MULTIBOOT_TAG_TYPE_END 0 #define MULTIBOOT_TAG_TYPE_END 0
#define MULTIBOOT_TAG_TYPE_CMDLINE 1 #define MULTIBOOT_TAG_TYPE_CMDLINE 1
#define MULTIBOOT_TAG_TYPE_MODULE 3 #define MULTIBOOT_TAG_TYPE_MODULE 3
@ -61,12 +65,12 @@ struct multiboot_tag_mmap
struct multiboot_mmap_entry entries[0]; struct multiboot_mmap_entry entries[0];
}; };
static void print_multiboot_tag(struct KernelMQ_Info *kinfo, const struct multiboot_tag *tag); static unsigned char print_multiboot_tag(struct KernelMQ_Info *kinfo, const struct multiboot_tag *tag);
static void print_multiboot_tag_cmdline (struct KernelMQ_Info *kinfo, const struct multiboot_tag_string *tag); static unsigned char print_multiboot_tag_cmdline (struct KernelMQ_Info *kinfo, const struct multiboot_tag_string *tag);
static void print_multiboot_tag_module (struct KernelMQ_Info *kinfo, const struct multiboot_tag_module *tag); static unsigned char print_multiboot_tag_module (struct KernelMQ_Info *kinfo, const struct multiboot_tag_module *tag);
static void print_multiboot_tag_basic_meminfo(struct KernelMQ_Info *kinfo, const struct multiboot_tag_basic_meminfo *tag); static unsigned char print_multiboot_tag_basic_meminfo(struct KernelMQ_Info *kinfo, const struct multiboot_tag_basic_meminfo *tag);
static void print_multiboot_tag_mmap (struct KernelMQ_Info *kinfo, const struct multiboot_tag_mmap *tag); static unsigned char print_multiboot_tag_mmap (struct KernelMQ_Info *kinfo, const struct multiboot_tag_mmap *tag);
unsigned char multiboot_parse(struct KernelMQ_Info *kinfo, unsigned long addr) unsigned char multiboot_parse(struct KernelMQ_Info *kinfo, unsigned long addr)
{ {
@ -84,50 +88,92 @@ unsigned char multiboot_parse(struct KernelMQ_Info *kinfo, unsigned long addr)
tag->type != MULTIBOOT_TAG_TYPE_END; tag->type != MULTIBOOT_TAG_TYPE_END;
tag = (struct multiboot_tag*)((unsigned char*)tag + ((tag->size + 7) & ~7)) tag = (struct multiboot_tag*)((unsigned char*)tag + ((tag->size + 7) & ~7))
) { ) {
print_multiboot_tag(kinfo, tag); if (!print_multiboot_tag(kinfo, tag)) {
return 0;
}
} }
return 1; return 1;
} }
void print_multiboot_tag(struct KernelMQ_Info *kinfo, const struct multiboot_tag *const tag) unsigned char print_multiboot_tag(struct KernelMQ_Info *kinfo, const struct multiboot_tag *const tag)
{ {
switch (tag->type) switch (tag->type)
{ {
case MULTIBOOT_TAG_TYPE_CMDLINE: case MULTIBOOT_TAG_TYPE_CMDLINE:
print_multiboot_tag_cmdline(kinfo, (struct multiboot_tag_string*)tag); return print_multiboot_tag_cmdline(kinfo, (struct multiboot_tag_string*)tag);
break;
case MULTIBOOT_TAG_TYPE_MODULE: case MULTIBOOT_TAG_TYPE_MODULE:
print_multiboot_tag_module(kinfo, (struct multiboot_tag_module*)tag); return print_multiboot_tag_module(kinfo, (struct multiboot_tag_module*)tag);
break;
case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO: case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
print_multiboot_tag_basic_meminfo(kinfo, (struct multiboot_tag_basic_meminfo*)tag); return print_multiboot_tag_basic_meminfo(kinfo, (struct multiboot_tag_basic_meminfo*)tag);
break;
case MULTIBOOT_TAG_TYPE_MMAP: case MULTIBOOT_TAG_TYPE_MMAP:
print_multiboot_tag_mmap(kinfo, (struct multiboot_tag_mmap*)tag); return print_multiboot_tag_mmap(kinfo, (struct multiboot_tag_mmap*)tag);
break;
} }
return 1;
} }
void print_multiboot_tag_cmdline(struct KernelMQ_Info *kinfo, const struct multiboot_tag_string *const tag) unsigned char print_multiboot_tag_cmdline(struct KernelMQ_Info *kinfo, const struct multiboot_tag_string *const tag)
{ {
unsigned int length = kstrlen(tag->string);
if (length >= KERNELMQ_INFO_CMDLINE_SIZE_MAX) {
return 0;
}
kstrncpy(kinfo->cmdline, tag->string, length);
kprintf("Kernel command line: %s\n", tag->string); kprintf("Kernel command line: %s\n", tag->string);
return 1;
} }
void print_multiboot_tag_module(struct KernelMQ_Info *kinfo, const struct multiboot_tag_module *const tag) 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_length = kstrlen(tag->cmdline);
if (cmdline_length >= KERNELMQ_INFO_CMDLINE_SIZE_MAX) {
return 0;
}
struct KernelMQ_Info_Module *module = &kinfo->modules[kinfo->modules_count];
kstrncpy(module->cmdline, tag->cmdline, cmdline_length);
module->base = tag->mod_start;
module->limit = tag->mod_end;
module->size = module->limit - module->base + 1;
++kinfo->modules_count;
kprintf("Module at 0x%x-0x%x, command line: %s\n", tag->mod_start, tag->mod_end, tag->cmdline); kprintf("Module at 0x%x-0x%x, command line: %s\n", tag->mod_start, tag->mod_end, tag->cmdline);
return 1;
} }
void print_multiboot_tag_basic_meminfo(struct KernelMQ_Info *kinfo, const struct multiboot_tag_basic_meminfo *const tag) unsigned char print_multiboot_tag_basic_meminfo(struct KernelMQ_Info *kinfo, const struct multiboot_tag_basic_meminfo *const tag)
{ {
kinfo->mem_lower_base = KERNELMQ_INFO_MEM_LOWER_BASE;
kinfo->mem_lower_size = tag->mem_lower * 1024;
kinfo->mem_lower_limit = kinfo->mem_lower_base + kinfo->mem_lower_size - 1;
kinfo->mem_upper_base = KERNELMQ_INFO_MEM_UPPER_BASE;
kinfo->mem_upper_size = tag->mem_upper * 1024;
kinfo->mem_upper_limit = kinfo->mem_upper_base + kinfo->mem_upper_size - 1;
kprintf("mem_lower = %uKB, mem_upper = %uKB\n", tag->mem_lower, tag->mem_upper); kprintf("mem_lower = %uKB, mem_upper = %uKB\n", tag->mem_lower, tag->mem_upper);
return 1;
} }
void print_multiboot_tag_mmap(struct KernelMQ_Info *kinfo, const struct multiboot_tag_mmap *const tag) unsigned char print_multiboot_tag_mmap(struct KernelMQ_Info *kinfo, const struct multiboot_tag_mmap *const tag)
{ {
kprintf("Memory map:\n"); kprintf("Memory map:\n");
@ -136,6 +182,20 @@ void print_multiboot_tag_mmap(struct KernelMQ_Info *kinfo, const struct multiboo
(unsigned char*)mmap < (unsigned char*)tag + tag->size; (unsigned char*)mmap < (unsigned char*)tag + tag->size;
mmap = (multiboot_memory_map_t*)((unsigned long) mmap + tag->entry_size) mmap = (multiboot_memory_map_t*)((unsigned long) mmap + tag->entry_size)
) { ) {
if (kinfo->areas_count >= KERNELMQ_INFO_AREAS_MAX) {
return 0;
}
struct KernelMQ_Info_Area *area = &kinfo->areas[kinfo->areas_count];
area->base = mmap->addr;
area->size = mmap->len;
area->limit = area->base + area->size - 1;
area->is_available = mmap->type == MULTIBOOT_MEMORY_AVAILABLE;
++kinfo->areas_count;
kprintf( kprintf(
" base_addr = 0x%x%x, length = 0x%x%x, type = 0x%x\n", " base_addr = 0x%x%x, length = 0x%x%x, type = 0x%x\n",
(unsigned)(mmap->addr >> 32), (unsigned)(mmap->addr >> 32),
@ -145,4 +205,6 @@ void print_multiboot_tag_mmap(struct KernelMQ_Info *kinfo, const struct multiboo
(unsigned)mmap->type (unsigned)mmap->type
); );
} }
return 1;
} }

View file

@ -24,9 +24,9 @@ struct KernelMQ_Info_Module {
}; };
struct KernelMQ_Info_Area { struct KernelMQ_Info_Area {
unsigned long base; unsigned long long base;
unsigned long size; unsigned long long size;
unsigned long limit; unsigned long long limit;
unsigned char is_available; unsigned char is_available;
}; };

View file

@ -5,8 +5,12 @@
extern "C" { extern "C" {
#endif #endif
void kmemset(void *buffer, unsigned char value, unsigned int size); void kmemset(void *buffer, unsigned char value, unsigned long size);
unsigned int kstrlen(const char *s); unsigned int kstrlen(const char *s);
char *kstrncpy(char *dest, const char *src, unsigned long length);
void kitoa(char *buf, int base, int d); void kitoa(char *buf, int base, int d);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -5,7 +5,7 @@ CFLAGS = -std=gnu99 -ffreestanding -nostdinc -fno-builtin -fno-stack-protector -
OUTPUT = libk.a OUTPUT = libk.a
OBJS = memset.o strlen.o itoa.o OBJS = memset.o strlen.o itoa.o strncpy.o
all: $(OUTPUT) all: $(OUTPUT)

View file

@ -1,8 +1,8 @@
void kmemset(void *const buffer, const unsigned char value, const unsigned int size) void kmemset(void *const buffer, const unsigned char value, const unsigned long size)
{ {
unsigned char *const s = buffer; unsigned char *const s = buffer;
for (unsigned int i = 0; i < size; ++i) { for (unsigned long i = 0; i < size; ++i) {
s[i] = value; s[i] = value;
} }
} }

10
libk/strncpy.c Normal file
View file

@ -0,0 +1,10 @@
char *kstrncpy(char *const dest, const char *const src, const unsigned long length)
{
for (unsigned long i = 0; i < length; ++i) {
dest[i] = src[i];
}
dest[length] = 0;
return dest;
}