Implemented multiboot2 in initialize.c

Removed multiboot2 C++ code
This commit is contained in:
2021-04-12 00:58:46 -05:00
parent 7888c9df30
commit 4a0f8e4f78
3 changed files with 152 additions and 212 deletions

View File

@@ -1,5 +1,9 @@
#include "kernel.h"
#include "pageallocator.h"
#include "memorymap.h"
#include "tty.h"
#include "string.h"
#include "module.h"
#include "types/memorytype.h"
#include <stdint.h>
#include <stddef.h>
@@ -16,22 +20,95 @@ struct interrupt_descriptor_t
uint16_t m_offset2;
};
struct idt_info_t
{
uint16_t size;
void *location;
};
enum multiboot2_tag_types
{
MB_END_TAG = 0,
MB_BOOT_COMMAND = 1,
MB_BOOTLOADER = 2,
MB_MODULE = 3,
MB_MEMORY_INFO = 4,
MB_BIOS_BOOT_DEVICE = 5,
MB_MEMORY_MAP = 6,
MB_VBE = 7,
MB_FRAMEBUFFER = 8,
MB_ELF_SYMBOLS = 9,
MB_APM = 10,
MB_EFI32_SYSTEM_TABLE = 11,
MB_EFI64_SYSTEM_TABLE = 12,
MB_SMBIOS = 13,
MB_ACPI10_RSDP = 14,
MB_ACPT20_RSDP = 15,
MB_NETOWRK = 16,
MB_EFI_MEMORY_MAP = 17,
MB_EFI_BOOT_SERVICES = 18,
MB_EFI32_IMAGE = 19,
MB_EFI64_IMAGE = 20,
MB_LOAD_ADDRESS = 21
};
enum multiboot2_memory_types
{
MB_AVAILABLE = 1,
MB_ACPI = 3,
MB_DEFECTIVE = 5
};
struct multiboot2_string_t
{
uint32_t type;
uint32_t size;
char str;
};
struct multiboot2_module_t
{
uint32_t type;
uint32_t size;
uint32_t start;
uint32_t end;
char str;
};
struct multiboot2_map_entry_t
{
uint64_t base;
uint64_t length;
uint32_t type;
};
struct multiboot2_memory_map_t
{
uint32_t type;
uint32_t size;
uint32_t entry_size;
uint32_t entry_version;
struct multiboot2_map_entry_t entries;
};
const size_t module_limit = 8;
struct boot_info_t
{
struct memory_map_t map;
char *bootloader;
char *parameters;
size_t module_count;
struct memory_map_t map;
struct module_t modules[module_limit];
};
extern int _kernelEnd;
struct interrupt_descriptor_t idt[256];
struct idt_info_t
{
uint16_t size;
void *location;
};
struct page_stack_t page_stack;
struct kernel_t kernel;
void lidt()
{
@@ -68,20 +145,70 @@ int startPaging(uint32_t *directory, uint32_t *table, uint32_t *identityTable)
void *read_multiboot_table(struct boot_info_t *boot_info, void *table)
{
uint32_t *int_table = (uint32_t *)table;
switch (*int_table)
{
case MB_END_TAG:
return NULL;
case MB_MEMORY_MAP:
unsigned int tag_size = ((struct multiboot2_memory_map_t*) table)->size - 16;
unsigned int entry_size = ((struct multiboot2_memory_map_t*) table)->entry_size;
struct multiboot2_map_entry_t *entry = &((struct multiboot2_memory_map_t*) table)->entries;
while(tag_size)
{
unsigned int entry_type =
entry->type == MB_AVAILABLE ? M_AVAILABLE
: (entry->type == MB_DEFECTIVE ? M_DEFECTIVE
: M_UNAVAILABLE);
insert_region(&boot_info->map, entry->base, entry->length, entry_type);
entry = (struct multiboot2_map_entry_t*) ((void*) entry + entry_size);
tag_size -= entry_size;
}
break;
case MB_MODULE:
if(boot_info->module_count < 8)
{
boot_info->modules[boot_info->module_count].start = ((struct multiboot2_module_t*) table)->start;
boot_info->modules[boot_info->module_count].end = ((struct multiboot2_module_t*) table)->end;
strcpy(boot_info->modules[boot_info->module_count].str, ((struct multiboot2_module_t*) table)->str);
insert_region(&boot_info->map,
((struct multiboot2_module_t*) table)->start,
((struct multiboot2_module_t*) table)->end - ((struct multiboot2_module_t*) table)->start,
M_UNAVAILABLE);
boot_info->module_count++;
}
else
{
printf("WARNING: Too many modules, must skip one.\n");
}
break;
case MB_BOOT_COMMAND:
strcpy(boot_info->parameters, &((struct multiboot2_string_t*) table)->str);
break;
case MB_BOOTLOADER:
strcpy(boot_info->bootloader, &((struct multiboot2_string_t*) table)->str);
break;
default:
break;
}
size_t size = (int_table[1] + 7) - ((int_table[1] + 7) % 8);
return table + size;
}
int initialize(void *multiboot_info)
{
struct memory_region_t map_array[16];
char bootloader_name[64];
char kernel_parameters[64];
struct boot_info_t boot_info = {
.bootloader = bootloader_name,
.parameters = kernel_parameters,
.module_count = 0,
.map = {
.array = map_array,
.size = 0,
.capacity = 16
},
.bootloader = NULL,
.parameters = NULL
};
.capacity = 16}};
multiboot_info += 8;
while (multiboot_info != NULL)
{
multiboot_info = read_multiboot_table(&boot_info, multiboot_info);
@@ -92,7 +219,15 @@ int initialize(void *multiboot_info)
{
printf("%i\t\t\t%08X\t\t%i\n", boot_info.map.array[i].type, boot_info.map.array[i].location, boot_info.map.array[i].size);
}
// TODO: Initialize page allocator
// TODO: Initialize process queue, add entry for each module
return 0;
page_stack.base_pointer = 0xFFC00000;
page_stack.stack_pointer = 0xFFC00000;
page_stack.limit_pointer = 0xFF900000;
initialize_page_stack(&page_stack, &boot_info.map, 4096);
// TODO: Initialize process queue
for(int i = 0; i < boot_info.module_count; i++)
{
load_module(&kernel, &boot_info.modules[i]);
}
// TODO: enter first process
}

View File

@@ -1,83 +0,0 @@
#include "multiboot2.hpp"
#include "../memorytype.hpp"
using namespace kernelns;
Multiboot2Info::Multiboot2Info(void* tableLocation)
{
m_commandLine = NULL;
m_bootloader = NULL;
m_moduleCount = 0;
uint32_t* ptr = (uint32_t*) tableLocation;
ptr += 2;
while(*ptr != 0)
{
if(*ptr == (uint32_t) Tag::BootCommand)
{
m_commandLine = &reinterpret_cast<TagString*>(ptr)->str;
}
else if(*ptr == (uint32_t) Tag::Bootloader)
{
m_bootloader = &reinterpret_cast<TagString*>(ptr)->str;
}
else if(*ptr == (uint32_t) Tag::MemoryMap)
{
unsigned int tagSize = reinterpret_cast<TagMemoryMap*>(ptr)->size - 16;
unsigned int entrySize = reinterpret_cast<TagMemoryMap*>(ptr)->entrySize;
MemoryMapEntry* entry = &reinterpret_cast<TagMemoryMap*>(ptr)->entries;
while(tagSize > 0)
{
unsigned int regionType =
(entry->type == (unsigned int) Multiboot2MemoryType::Available) ? (unsigned int) MemoryType::Available
: ((entry->type == (unsigned int) Multiboot2MemoryType::Defective) ? (unsigned int) MemoryType::Defective
: (unsigned int) MemoryType::Unavailable);
m_memmap.insertEntry(entry->base, entry->length, entry->type);
entry = (MemoryMapEntry*) (reinterpret_cast<char*>(entry) + entrySize);
tagSize -= entrySize;
}
}
else if(*ptr == (uint32_t) Tag::Module)
{
TagModule* moduleTag = reinterpret_cast<TagModule*>(ptr);
m_modules[m_moduleCount] = Module(moduleTag->start, moduleTag->end, &moduleTag->str);
m_memmap.insertEntry(moduleTag->start, moduleTag->end - moduleTag->start, (unsigned int) MemoryType::Unavailable);
}
unsigned int size = (ptr[1] + 7) - ((ptr[1] + 7) % 8);
ptr += size / sizeof(uint32_t);
}
}
Multiboot2Info::~Multiboot2Info()
{
}
bool Multiboot2Info::isValid() const
{
return true;
}
MemoryMap& Multiboot2Info::getMemoryMap()
{
return m_memmap;
}
const Module* Multiboot2Info::getModules() const
{
return m_modules;
}
unsigned int Multiboot2Info::getModuleCount() const
{
return m_moduleCount;
}
const char* Multiboot2Info::getCommandLine() const
{
return m_commandLine;
}
const char* Multiboot2Info::getBootloaderName() const
{
return m_bootloader;
}

View File

@@ -1,112 +0,0 @@
#ifndef MULTIBOOT2_H
#define MULTIBOOT2_H
#include "../memorymap.hpp"
#include "../module.hpp"
namespace kernelns
{
class Multiboot2Info
{
public:
enum class Tag
{
BootCommand = 1,
Bootloader = 2,
Module = 3,
MemoryInfo = 4,
BIOSBootDevice = 5,
MemoryMap = 6,
VBE = 7,
Framebuffer = 8,
ELFSymbols = 9,
APM = 10,
EFI32SystemTable = 11,
EFI64SystemTable = 12,
SMBIOS = 13,
ACPI10RSDP = 14,
ACPT20RSDP = 15,
Network = 16,
EFIMemoryMap = 17,
EFIBootServices = 18,
EFI32Image = 19,
EFI64Image = 20,
LoadAddress = 21
};
enum class Multiboot2MemoryType
{
Available = 1,
ACPI = 3,
Defective = 5
};
struct TagString
{
uint32_t type;
uint32_t size;
char str;
};
struct TagModule
{
uint32_t type;
uint32_t size;
uint32_t start;
uint32_t end;
char str;
};
struct MemoryMapEntry
{
uint64_t base;
uint64_t length;
uint32_t type;
};
struct TagMemoryMap
{
uint32_t type;
uint32_t size;
uint32_t entrySize;
uint32_t entryVersion;
MemoryMapEntry entries;
};
Multiboot2Info(void* tableLocation);
~Multiboot2Info();
bool isValid() const;
MemoryMap& getMemoryMap();
const Module* getModules() const;
unsigned int getModuleCount() const;
const char* getCommandLine() const;
const char* getBootloaderName() const;
private:
Module m_modules[16];
unsigned int m_moduleCount;
MemoryMap m_memmap;
char* m_commandLine;
char* m_bootloader;
void* m_tableLocation;
};
}
#endif