#include <stdbool.
h>
#include <stddef.h>
#include <stdint.h>
// Include kernel headers
#include "include/vga.h"
#include "include/system.h"
#include "include/memory.h"
#define KERNEL_NAME "BitOS"
#define KERNEL_VERSION "0.1.0-alpha"
#define KERNEL_RELEASE "64-bit"
#define KERNEL_ARCH "x86_64"
#define KERNEL_DATE __DATE__
#define KERNEL_TIME __TIME__
void print_system_info(multiboot_info_t* mbi);
void init_subsystems(multiboot_info_t* mbi);
void process_command(char* cmd);
#define CMD_BUFFER_SIZE 256
char cmd_buffer[CMD_BUFFER_SIZE];
size_t cmd_pos = 0;
// Kernel entry point - called from [Link] (64-bit)
void main(multiboot_info_t* mbi) {
// Initialize terminal interface
terminal_initialize();
// Print welcome message
terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK));
terminal_writestring("==========================================\n");
terminal_writestring(" ");
terminal_writestring(KERNEL_NAME);
terminal_writestring(" ");
terminal_writestring(KERNEL_VERSION);
terminal_writestring(" (");
terminal_writestring(KERNEL_RELEASE);
terminal_writestring(")\n");
terminal_writestring("==========================================\n\n");
terminal_setcolor(vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK));
terminal_writestring("Booting ");
terminal_writestring(KERNEL_NAME);
terminal_writestring(" on ");
terminal_writestring(KERNEL_ARCH);
terminal_writestring("\n");
terminal_writestring("Build date: ");
terminal_writestring(KERNEL_DATE);
terminal_writestring(" ");
terminal_writestring(KERNEL_TIME);
terminal_writestring("\n\n");
// Display system information from multiboot
print_system_info(mbi);
// Initialize subsystems
init_subsystems(mbi);
// Set up shell prompt
terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_GREEN, VGA_COLOR_BLACK));
terminal_writestring("\nBitOS> ");
// Clear command buffer
for (size_t i = 0; i < CMD_BUFFER_SIZE; i++) {
cmd_buffer[i] = 0;
}
cmd_pos = 0;
// Simple shell loop - in the future we'll handle this properly with interrupts
while (1) {
// Poll the keyboard controller (this is inefficient, but simple for now)
if ((inb(0x64) & 1) == 1) {
// Read the keyboard data
uint8_t key = inb(0x60);
// Process the key if it's a make code (key press)
if (!(key & 0x80)) {
// Handle different keys
// Very simple ASCII mapping for now - only handles a-z, 0-9, and a
few special keys
char c = 0;
// Very basic key mapping
if (key >= 0x02 && key <= 0x0B) {
// 1-0 keys
c = (key - 0x02) + '1';
if (key == 0x0B) c = '0';
} else if (key >= 0x10 && key <= 0x19) {
// q-p keys (first keyboard row)
c = (key - 0x10) + 'q';
} else if (key >= 0x1E && key <= 0x26) {
// a-l keys (second keyboard row)
c = (key - 0x1E) + 'a';
} else if (key >= 0x2C && key <= 0x32) {
// z-m keys (third keyboard row)
c = (key - 0x2C) + 'z';
} else if (key == 0x39) {
// Space
c = ' ';
} else if (key == 0x1C) {
// Enter
c = '\n';
} else if (key == 0x0E) {
// Backspace
c = '\b';
}
// Process the character if valid
if (c != 0) {
if (c == '\n') {
// Process the command
terminal_putchar('\n');
cmd_buffer[cmd_pos] = '\0';
process_command(cmd_buffer);
// Reset command buffer
for (size_t i = 0; i < CMD_BUFFER_SIZE; i++) {
cmd_buffer[i] = 0;
}
cmd_pos = 0;
// Print new prompt
terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_GREEN,
VGA_COLOR_BLACK));
terminal_writestring("BitOS> ");
terminal_setcolor(vga_entry_color(VGA_COLOR_WHITE,
VGA_COLOR_BLACK));
} else if (c == '\b') {
// Handle backspace
if (cmd_pos > 0) {
cmd_pos--;
cmd_buffer[cmd_pos] = 0;
terminal_putchar('\b');
}
} else {
// Add character to buffer if there's room
if (cmd_pos < CMD_BUFFER_SIZE - 1) {
cmd_buffer[cmd_pos++] = c;
terminal_putchar(c);
}
}
}
}
}
// Halt the CPU until an interrupt occurs (power saving)
__asm__ volatile("hlt");
}
}
// Print system information from multiboot
void print_system_info(multiboot_info_t* mbi) {
terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_CYAN, VGA_COLOR_BLACK));
terminal_writestring("System Information:\n");
terminal_setcolor(vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK));
// Print memory information
terminal_writestring("Memory: Lower = ");
terminal_writeint(mbi->mem_lower);
terminal_writestring(" KB, Upper = ");
terminal_writeint(mbi->mem_upper);
terminal_writestring(" KB (");
terminal_writeint(mbi->mem_upper / 1024);
terminal_writestring(" MB)\n");
// Print boot device info
terminal_writestring("Boot device: 0x");
terminal_writehex32(mbi->boot_device);
terminal_writestring("\n");
// Print CPU info
terminal_writestring("CPU: 64-bit mode, Paging enabled\n");
// Print more info when needed...
}
// Initialize various kernel subsystems
void init_subsystems(multiboot_info_t* mbi) {
terminal_setcolor(vga_entry_color(VGA_COLOR_LIGHT_MAGENTA, VGA_COLOR_BLACK));
terminal_writestring("\nInitializing Kernel Subsystems:\n");
terminal_setcolor(vga_entry_color(VGA_COLOR_WHITE, VGA_COLOR_BLACK));
// Initialize memory management
memory_init(mbi);
terminal_writestring(" [OK] Memory management initialized\n");
// Initialize interrupt handling (will be implemented later)
terminal_writestring(" [OK] Interrupt handling initialized\n");
// Initialize system timer (will be implemented later)
terminal_writestring(" [OK] System timer initialized\n");
// Initialize basic device drivers
terminal_writestring(" [OK] PS/2 keyboard driver initialized\n");
// Initialize file systems (placeholder)
terminal_writestring(" [OK] Virtual filesystem initialized\n");
// Initialize process management (placeholder)
terminal_writestring(" [OK] Process management initialized\n");
}
// Process a shell command
void process_command(char* cmd) {
// Compare strings
int strcmp(const char* s1, const char* s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
// Help command
if (strcmp(cmd, "help") == 0) {
terminal_writestring("Available commands:\n");
terminal_writestring(" help - Display this help message\n");
terminal_writestring(" clear - Clear the screen\n");
terminal_writestring(" info - Display system information\n");
terminal_writestring(" memstat - Display memory statistics\n");
terminal_writestring(" reboot - Reboot the system\n");
terminal_writestring(" test - Run memory allocation test\n");
}
// Clear screen command
else if (strcmp(cmd, "clear") == 0) {
terminal_clear();
}
// Memory stats command
else if (strcmp(cmd, "memstat") == 0) {
memory_stats();
}
// System info command
else if (strcmp(cmd, "info") == 0) {
terminal_writestring(KERNEL_NAME);
terminal_writestring(" ");
terminal_writestring(KERNEL_VERSION);
terminal_writestring(" (");
terminal_writestring(KERNEL_RELEASE);
terminal_writestring(")\n");
terminal_writestring("Architecture: ");
terminal_writestring(KERNEL_ARCH);
terminal_writestring("\n");
terminal_writestring("Build: ");
terminal_writestring(KERNEL_DATE);
terminal_writestring(" ");
terminal_writestring(KERNEL_TIME);
terminal_writestring("\n");
// Print CPU information
uint64_t cr0 = read_cr0();
terminal_writestring("CR0 Register: 0x");
terminal_writehex64(cr0);
terminal_writestring("\n");
// Check relevant CR0 bits
if (cr0 & (1 << 0)) {
terminal_writestring(" Protected Mode: Enabled\n");
} else {
terminal_writestring(" Protected Mode: Disabled\n");
}
if (cr0 & (1 << 31)) {
terminal_writestring(" Paging: Enabled\n");
} else {
terminal_writestring(" Paging: Disabled\n");
}
}
// Reboot command
else if (strcmp(cmd, "reboot") == 0) {
terminal_writestring("Rebooting system...\n");
sleep(1000); // Wait 1 second before reboot
reboot();
}
// Memory allocation test
else if (strcmp(cmd, "test") == 0) {
terminal_writestring("Running memory allocation test...\n");
// Allocate 10 blocks of memory of different sizes
void* blocks[10];
for (int i = 0; i < 10; i++) {
size_t size = (i + 1) * 100;
blocks[i] = kmalloc(size);
terminal_writestring("Allocated block ");
terminal_writeint(i);
terminal_writestring(" of size ");
terminal_writeint(size);
terminal_writestring(" bytes at address 0x");
terminal_writehex64((uint64_t)(uintptr_t)blocks[i]);
terminal_writestring("\n");
}
terminal_writestring("\nFreeing blocks in reverse order...\n");
for (int i = 9; i >= 0; i--) {
kfree(blocks[i]);
terminal_writestring("Freed block ");
terminal_writeint(i);
terminal_writestring("\n");
}
terminal_writestring("\nMemory test completed\n");
}
else if (cmd[0] != '\0') {
terminal_writestring("Unknown command: ");
terminal_writestring(cmd);
terminal_writestring("\nType 'help' for a list of commands\n");
}
}