Files
KatauOS/src/kernel/kernel.c
T

233 lines
5.1 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 <stdint.h>
disk_ops_t ops =
{
.read = ata_read,
.write = ata_write,
};
static fat_t g_fat;
static const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static int cat(const char* path)
{
file_t file;
int err = fat_fopen(&file, path, "r");
if (err)
return err;
uint8_t buf[512];
for (;;)
{
int cnt = fat_fread(&file, buf, 512);
if (cnt < 0)
return cnt;
printf("%.*s\n", cnt, buf);
printf("%s\n", buf);
if (cnt < 512)
break;
}
return fat_fclose(&file);
}
static int ls(const char* path)
{
dir_t dir;
dir_info_t info;
int err = fat_opendir(&dir, path);
if (err)
return err;
for (;;)
{
err = fat_readdir(&dir, &info);
if (err == FAT_ERR_EOF)
return FAT_ERR_NONE;
if (err)
return err;
printf("%X %s %x %x:%x %x %s%c\n",
info.size, months[info.modified.month], info.modified.day,
info.modified.hour, info.modified.min,
info.namelen, info.name, info.attr & FAT_ATTR_DIR ? '/' : ' ');
err = fat_nextdir(&dir);
if (err)
return err;
}
}
void test(int err, const char* huy)
{
if(err != 0)
printf("ERROR: %X %s\n", err, huy);
else
printf("%s done", huy);
}
void file_fuckery()
{
int err, cnt;
err = fat_mount(&ops, 0, &g_fat, "ebalo");
if(err)
printf("KATYAKATYAKATYAKATYAKATYA: %X\n", err);
printf("%X\n", err);
err = ls("/ebalo");
printf("\n\n\n");
err = fat_mkdir("/ebalo/numbers");
err = fat_mkdir("/ebalo/numbers/nigga");
printf("MKDIR ERR: %X\n", err);
err = fat_unlink("/ebalo/numbers/nigga/");
printf("UNLINK ERR: %X\n", err);
err = fat_unlink("/ebalo/numbers/");
printf("UNLINK ERR: %X\n", err);
/*err = fat_mkdir("/ebalo/numbers");
//err = cat("/ebalo/boot/grub/grub.cfg");
char *filenames[] = {"/ebalo/numbers/d","/ebalo/numbers/e"};
for(int i = 0; i < 2; i++){
printf(filenames[i]);
file_t file;
err = fat_fopen(&file, filenames[i], "w");
if (err)
{printf("1 err %X\n", err); goto unmount;}
for (int i = 0; i < 10; i++)
{
cnt = fat_fprintf(&file, "This is test number %d\n", i);
if (cnt < 0)
{printf("2 cnt: %X\n", cnt); goto unmount;}
}
err = fat_fclose(&file);
}
*/
unmount:
printf("err%X\n", err);
err = fat_umount(&g_fat);
test(err, "unmount");
}
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 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");
__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 = malloc(64);//alloc_page();//heap_alloc(1024);
huy2 = malloc(64);//alloc_page();//heap_alloc(1024);
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);
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");
}
*/ }
}