Files
KatauOS/src/kernel/kernel.c
T
2025-08-16 21:43:42 +03:00

206 lines
5.2 KiB
C

#include "../include/isr.h"
#include "../include/screen.h"
#include "../include/stdio.h"
#include "../include/utils.h"
#include "../include/disk.h"
#include "../include/string.h"
#include "../include/pic.h"
#include "../include/pit.h"
#include "../include/paging.h"
#include "../include/fat.h"
#include "../include/task.h"
#include "../include/multiboot.h"
#include "../include/liballoc.h"
#include "../include/kheap.h"
#include "../include/exec_from_file.h"
#include "../include/atapi.h"
#include "../include/lib9660.h"
#include "../include/vfs.h"
#include <stdint.h>
void apply_pic_masks()
{
//0x21 is master pic, 0xa1 is slave pic
outb(0x21,0xfc);//0b11111100 (bit 0 is PIT, bit 1 is keyboard and so on)
outb(0xa1,0xff);
}
void test_iso9660()
{
l9660_dir g_root_dir;
l9660_file g_test_file;
l9660_status status;
// 2. Open the root directory
status = l9660_fs_open_root(&g_root_dir, &global_fs);
if (status != L9660_OK) {
printf("Error opening root directory!\n");
return;
}
printf("Files in root directory:\n");
for (;;) {
l9660_dirent *dent;
status = l9660_readdir(&g_root_dir, &dent);
if (dent == NULL || status != L9660_OK) {
break; // End of directory or error
}
// Print the filename. It's not null-terminated, so use the length field.
for (int i = 0; i < dent->name_len; ++i) {
printf("%c", dent->name[i]);
}
printf("\n");
}
printf("\nReading content of /TEST.TXT:\n");
// Re-open the root directory to reset its position before searching
l9660_fs_open_root(&g_root_dir, &global_fs);
l9660_dir boot_dir;
l9660_dir grub_dir;
status = l9660_opendirat(&boot_dir, &g_root_dir, "boot");
printf("STATUS %X\n", status);
status = l9660_opendirat(&grub_dir, &boot_dir, "grub");
printf("STATUS %X\n", status);
status = l9660_openat(&g_test_file, &grub_dir, "grub.cfg");
if (status != L9660_OK) {
printf("Error opening TEST.TXT;1. Error code: ");
printf("%X\n", status); // Print the error code
printf("\n");
} else {
char file_buffer[129];
size_t bytes_read;
uint32_t total_bytes;
for (;;) {
// Read the next chunk of the file
status = l9660_read(&g_test_file, file_buffer, 128, &bytes_read);
// Check for a read error
if (status != L9660_OK) {
printf("An error occurred during file read!\n");
break;
}
// Check for End-Of-File
if (bytes_read == 0) {
break; // We're done
}
// Process the data we just read
total_bytes += bytes_read;
file_buffer[bytes_read] = '\0'; // Null-terminate the chunk
printf(file_buffer); // Print the chunk
}
printf("\n--- EOF ---\n");
printf("Total bytes read: ");
printf("0x%X\n", total_bytes);
}
}
void kmain(unsigned int magic, unsigned int info)
{
struct multiboot_info mbi;
memcpy(&mbi, (void*)info, sizeof(struct multiboot_info));
terminal_initialize();
printf("KatauOS booting up\n");
if(magic != MULTIBOOT_BOOTLOADER_MAGIC) {
printf("invalid magic number!\n");
while(1){}
}
if(mbi.flags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
printf("bootloader: %s\n", mbi.boot_loader_name);
}
printf("init_allocator...");
init_allocator();
printf("done\n");
printf("grub_memory_map...");
grub_memory_map(magic, &mbi);
printf("done\n");
isr_install();
pic_remap(0x20, 0x28);
pit_init(100);
apply_pic_masks();
printf("done fucking with pages and memory map!\n");
char *test_str;
printf("test_str: %X, &test_str %X\n", test_str, &test_str);
test_str = alloc_page();
printf("test_str: %X, &test_str %X\n", test_str, &test_str);
free_page(test_str);
alloc_page();//TODO: fix this this, because now we MUST allocate a page for checks of successfulness of allocation to pass
//the first page is at 0x0 and in check we check by if(!<allocated page>) which returns 0 (false), yeah
//while(1){}
//paging_init();
//printf("paging_init done\n");
//test_paging();
//heap_init();
//printf("after heap_init\n");
kheap_init();
__asm__ __volatile__ ("sti");
scheduler_init();
printf("Kernel init sequence completed\n");
char yooo[256] = "heheh";
printf("test string: %s\n", yooo);
disk_info info1;
get_disk_info(&info1);
printf("CYL HEAD SECT: %X %X %X\n", info1.cylinders, info1.heads, info1.sectors);
//file_fuckery();
char *huy;
char *huy2;
char temp1[1024] = "rusya_krutoy";
char temp2[1024] = "katya_tozhe_krutaya";
huy = kvalloc(1);//alloc_page();
huy2 = kvalloc(2);//alloc_page();
memcpy(huy, temp1, sizeof(temp1));
memcpy(huy2, temp2, sizeof(temp1));
printf("huy1 address: 0x%X huy2 address:0x%X\n", &huy, &huy2);
printf("huy1 points to: 0x%X huy2 points to:0x%X\n", huy, huy2);
printf("huy1: %s\nhuy2: %s", huy, huy2);
mount_fs();
exec_from_file("main.", ".");
while(1)
{
/* if(inportb(0x64) & 0x1)
{
unsigned char key = inportb(0x60);
if(key == 2)
terminal_writestring("1\n");
else if(key == 4)
terminal_writestring("3\n");
else
terminal_writestring("dunno what is it\n");
}
*/ }
}