rk/kmain.c

126 lines
3.6 KiB
C
Raw Normal View History

2024-05-23 01:31:32 -04:00
#include "ide.h"
2024-05-20 05:59:07 -04:00
#include <multiboot.h>
#include <acpi.h>
2024-05-20 17:39:20 -04:00
#include <vga.h>
2024-05-21 22:07:04 -04:00
#include <pci.h>
2024-05-20 17:39:20 -04:00
#include <printf.h>
#include <strcmp.h>
2024-05-21 22:07:04 -04:00
#include <gdt.h>
#include <idt.h>
#include <pic8259.h>
#include <ps2.h>
2024-05-20 05:59:07 -04:00
struct kernel_context {
struct rsdp *rsdp;
struct xsdp *xsdp;
struct rsdt *rsdt;
};
struct kernel_context ctx = {0};
2024-05-21 22:07:04 -04:00
struct idt_entry g_idt[256] = {0};
struct idtr g_idtr = {0};
2024-05-20 05:59:07 -04:00
2024-05-21 22:07:04 -04:00
extern void exception_handler();
extern void ps2_isr();
2024-05-20 17:39:20 -04:00
2024-05-21 22:07:04 -04:00
#define MAGIC_BREAKPOINT __asm__ volatile("xchgw %bx, %bx");
2024-05-20 17:39:20 -04:00
2024-05-20 05:59:07 -04:00
void kmain(struct multiboot_info *info) {
2024-05-20 17:39:20 -04:00
clear_screen();
set_color(VGA_COLOR_CYAN);
2024-05-21 22:07:04 -04:00
// Check if the bootloader gave us the upper and lower memory
2024-05-20 05:59:07 -04:00
if (!(info->flags & 0x1)) goto halt;
2024-05-21 22:07:04 -04:00
// Create and load an IDT
for (int i = 0; i < 32; i++) {
encode_idt_entry(g_idt, i, (uint32_t)&exception_handler, GDT_SEGMENT_SELECTOR(GDT_KERNEL_CODE, 0x00), TRAP_GATE_32 | INT_RING0 | INT_PRESENT);
}
g_idtr.size = (sizeof(struct idt_entry) * 256) - 1;
g_idtr.base = (uint32_t) &g_idt;
asm volatile ("lidt %0" :: "m"(g_idtr));
struct rsdp *found_rsdp = acpi_locate_rsdp();
2024-05-20 17:39:20 -04:00
if (!found_rsdp) {
set_color(VGA_COLOR_RED);
2024-05-20 17:39:20 -04:00
printf("Failed to find RSDP signature\n");
goto halt;
}
2024-05-20 05:59:07 -04:00
if (found_rsdp->Revision == ACPI_VER_1) {
2024-05-20 17:39:20 -04:00
if (!acpi_validate_rsdp_checksum(found_rsdp)) {
set_color(VGA_COLOR_RED);
2024-05-20 17:39:20 -04:00
printf("RSDP has an invalid checksum\n");
goto halt;
}
2024-05-20 05:59:07 -04:00
ctx.rsdp = found_rsdp;
ctx.xsdp = 0;
2024-05-20 17:39:20 -04:00
if (!acpi_validate_sdt_checksum((struct ACPISDTHeader *)found_rsdp->RsdtAddress)) {
set_color(VGA_COLOR_RED);
2024-05-20 17:39:20 -04:00
printf("RSDT has an invalid checksum\n");
2024-05-20 05:59:07 -04:00
goto halt;
2024-05-20 17:39:20 -04:00
}
2024-05-20 05:59:07 -04:00
ctx.rsdt = (struct rsdt*)found_rsdp->RsdtAddress;
} else if (found_rsdp->Revision == ACPI_VER_OTHER) {
set_color(VGA_COLOR_RED);
printf("ACPI versions higher than 1.0 are not yet supported because I'm lazy\n");
2024-05-20 05:59:07 -04:00
goto halt;
2024-05-20 17:39:20 -04:00
} else {
set_color(VGA_COLOR_RED);
2024-05-20 17:39:20 -04:00
printf("Invalid RSDP\n");
goto halt;
}
printf("Using ACPI v1.0\n");
2024-05-21 22:07:04 -04:00
struct fadt *fadt = acpi_locate_sdt(ctx.rsdt, "FACP");
if (!fadt) {
set_color(VGA_COLOR_RED);
2024-05-21 22:07:04 -04:00
printf("Failed to find FADT\n");
2024-05-20 17:39:20 -04:00
goto halt;
}
2024-05-21 22:07:04 -04:00
printf("Found FADT at 0x%x\n", fadt);
if (fadt->Flags & 1)
printf("Legacy devices are supported\n");
2024-05-22 00:32:44 -04:00
else {
2024-05-23 01:31:32 -04:00
/*
set_color(VGA_COLOR_RED);
2024-05-22 00:32:44 -04:00
printf("Legacy devices are not supported. I'm too lazy to support modern devices, bye bye.\n");
goto halt;
2024-05-23 01:31:32 -04:00
*/
2024-05-22 00:32:44 -04:00
}
2024-05-21 22:07:04 -04:00
pic_remap(PIC_1_START, PIC_2_START);
// ACPI version 1.0 is so old that we assume that our PC supports the 8042 ps/2 controller
initialize_8042ps2();
encode_idt_entry(g_idt, 0x21, (uint32_t)&ps2_isr, GDT_SEGMENT_SELECTOR(GDT_KERNEL_CODE, 0x00), INT_GATE_32 | INT_RING0 | INT_PRESENT);
asm volatile ("sti" ::);
struct mcfg *mcfg = acpi_locate_sdt(ctx.rsdt, "MCFG");
if (!mcfg) {
set_color(VGA_COLOR_RED);
printf("Failed to find MCFG\n");
2024-05-21 22:07:04 -04:00
} else {
2024-05-20 17:39:20 -04:00
printf("Looks like you are using PCIe- Found MCFG at 0x%x\n", mcfg);
2024-05-20 17:39:20 -04:00
2024-05-21 22:07:04 -04:00
struct pci_config_space *ide = pcie_find_device(mcfg, MASS_STORAGE_CONTROLLER, IDE_INTERFACE);
2024-05-23 01:31:32 -04:00
if (ide) {
printf("IDE controller detected. Program Interface: %X\n", ide->program_interface);
uint16_t ide_buf[256] = {0};
ide_identify(ATA_PRIMARY, ATA_MASTER, ide_buf);
}
2024-05-21 22:07:04 -04:00
}
set_color(VGA_COLOR_WHITE);
2024-05-23 01:31:32 -04:00
printf("\nYou are now being dropped into a kernel shell\n$ ");
2024-05-20 05:59:07 -04:00
halt:
for (;;) {}
}