os-k/kaleid/kernel/init/init.c

159 lines
6.7 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Kernel entry point //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <kernel/multiboot.h>
#include <kernel/panic.h>
#include <kernel/sched.h>
#include <kernel/term.h>
#include <kernel/heap.h>
#include <kernel/buf.h>
#include <kernel/mm.h>
//
// BootInfo_t initialization. It is necessary because grub will potentially be
// wiped since it is below 1MB.... And we must reorganize all that stuff.
//
void BtInitBootInfo(multiboot_info_t *mbi)
{
extern ulong MB_header;
extern ulong newKernelEnd;
// We need the multiboot structure
KalAlwaysAssert(mbi);
//Retrieves the bootloader flags to ensure infos are valid
BtGetBootInfo(btldr).grubFlags = mbi->flags;
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
BtGetBootInfo(btldr).grubName = (char*)(ulong)(mbi->boot_loader_name);
BtGetBootInfo(btldr).kernelAddr = (void*)&MB_header;
BtGetBootInfo(btldr).kernelEndAddr = (void*)newKernelEnd;
BtGetBootInfo(btldr).valid = 1;
}
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MODS) {
BtGetBootInfo(btldr).modulesCount = mbi->mods_count;
BtGetBootInfo(btldr).modulesAddr = (void*)(ulong)mbi->mods_addr;
}
//Retrieves the drives informations
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_DRIVE_INFO) {
BtGetBootInfo(drives).bufferLength = mbi->drives_length;
BtGetBootInfo(drives).bufferAddr = (void*)(ulong)mbi->drives_addr;
BtGetBootInfo(drives).bufferValid = 1;
}
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_BOOTDEV) {
BtGetBootInfo(drives).bootDrv = mbi->boot_device;
BtGetBootInfo(drives).drvValid = 1;
}
//Retrieves the memory informations
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MEMORY) {
BtGetBootInfo(memory).lowMemory = mbi->mem_lower;
BtGetBootInfo(memory).upMemory = mbi->mem_upper;
BtGetBootInfo(memory).memValid = 1;
}
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_MEM_MAP) {
BtGetBootInfo(memory).mapAddr = (void*)(ulong)mbi->mmap_addr;
BtGetBootInfo(memory).mapLength = mbi->mmap_length;
BtGetBootInfo(memory).mapValid = 1;
}
// Retrieves video mode informations
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_VBE_INFO) {
BtGetBootInfo(video).vbeControl = (void*)(ulong)mbi->vbe_control_info;
BtGetBootInfo(video).vbeModeInfo = (void*)(ulong)mbi->vbe_mode_info;
BtGetBootInfo(video).vbeMode = mbi->vbe_mode;
BtGetBootInfo(video).vbeInterfaceSeg = mbi->vbe_interface_seg;
BtGetBootInfo(video).vbeInterfaceOff = mbi->vbe_interface_off;
BtGetBootInfo(video).vbeInterfaceLen = mbi->vbe_interface_len;
BtGetBootInfo(video).vbeValid = 1;
}
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_FRAMEBUFFER_INFO) {
BtGetBootInfo(video).framebufferAddr = (void*)mbi->framebuffer_addr;
BtGetBootInfo(video).framebufferPitch = mbi->framebuffer_pitch;
BtGetBootInfo(video).framebufferWidth = mbi->framebuffer_width;
BtGetBootInfo(video).framebufferHeight= mbi->framebuffer_height;
BtGetBootInfo(video).framebufferBpp = mbi->framebuffer_bpp;
BtGetBootInfo(video).framebufferType = mbi->framebuffer_type;
BtGetBootInfo(video).fbuValid = 1;
}
// Retrieves the firmware infos
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_CONFIG_TABLE) {
BtGetBootInfo(firmware).romTable = mbi->config_table;
BtGetBootInfo(firmware).romValid = 1;
}
if (BtGetBootInfo(btldr).grubFlags & MULTIBOOT_INFO_APM_TABLE) {
BtGetBootInfo(firmware).apmTable = mbi->apm_table;
BtGetBootInfo(firmware).apmValid = 1;
}
}
extern void pstest(void);
//
// Entry point of the Kaleid kernel
//
noreturn void BtStartKern(multiboot_info_t *mbInfo, int mbMagic)
{
error_t mapBad;
// We're not ready to deal with interrupts
KeDisableIRQs();
//Initialize the BootInfo_t structure
BtInitBootInfo(mbInfo);
// Kernel terminals
InitTerms();
//Hello world because why not
KernLog("%c%c%c OS/K\n\n", 219, 219, 219);
KalAlwaysAssert(mbMagic == MULTIBOOT_BOOTLOADER_MAGIC);
KernLog("[Init] Kernel successfully loaded at %p with %x magic\n\n",
BtGetBootInfo(btldr).kernelAddr,
mbMagic
);
//Memory mapping
if ((mapBad = MmInitMemoryMap()))
KeStartPanic("[Init] The memory map failed to initialize. Error : %d",
mapBad
);
MmInitHeap();
PsInitSched();
Buffer_t *buf = BOpenLineBuf(NULL, BS_WRONLY, 80, 24, 1, NULL);
error_t rc = BPrintOnBuf(buf, "%+#05x", 0xcafeb00b);
if(rc)KernLog("error\n");
KernLog((char*)buf->buf);
// We're out
PsFiniSched();
KeCrashSystem(); //yay
}