start messing with fat32

This commit is contained in:
2025-03-24 22:03:19 +03:00
parent bf6ab81791
commit bb5d94bb0c
6 changed files with 2108 additions and 8 deletions
+30 -4
View File
@@ -1,5 +1,5 @@
# Define the object files # Define the object files
OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o bin/keyboard.o bin/paging.o bin/page.o OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o bin/keyboard.o bin/paging.o bin/page.o bin/fat.o
# Define the compiler and assembler # Define the compiler and assembler
CC = gcc -D__is_katauos CC = gcc -D__is_katauos
@@ -10,7 +10,7 @@ CFLAGS = -std=gnu99 -ffreestanding -O0 -Wall -Wextra -m32 -g
ASFLAGS = -f elf32 ASFLAGS = -f elf32
# Define the linker flags # Define the linker flags
LDFLAGS = -ffreestanding -O0 -nostdlib -m32 -g LDFLAGS = -ffreestanding -O0 -nostdlib -m32 -g -lgcc
LDSCRIPT = link.ld LDSCRIPT = link.ld
# Define the output file # Define the output file
@@ -18,9 +18,32 @@ OUTPUT = myos.bin
all: $(OUTPUT) all: $(OUTPUT)
image: create-image:
cp myos.bin grubshit/boot/ cp myos.bin grubshit/boot/
grub-mkrescue -o boot.iso grubshit #grub-mkrescue -o boot.iso grubshit
dd if=/dev/zero of=boot.iso bs=1M count=256
fdisk boot.iso
#sudo mkdosfs -F 32 -I boot.iso
sudo losetup /dev/loop0 boot.iso
sudo losetup /dev/loop1 boot.iso -o 1048576
#sudo mkdosfs -F32 -f 2 /dev/loop1
sudo mkfs.vfat -F 32 -f 2 /dev/loop1
sudo mount /dev/loop1 /mnt
sudo grub-install --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot biosdisk" /dev/loop0 #there was biosdev also
sudo cp -r grubshit/boot/ /mnt/
sync
sudo umount /mnt
sudo losetup -D
image:
cp myos.bin grubshit/boot
sudo losetup /dev/loop0 boot.iso
sudo losetup /dev/loop1 boot.iso -o 1048576
sudo mount /dev/loop1 /mnt
sudo cp -r grubshit/boot /mnt/
sync
sudo umount /mnt
sudo losetup -D
test: test:
qemu-system-i386 -drive file=boot.iso,media=disk,format=raw -m 512M -d int qemu-system-i386 -drive file=boot.iso,media=disk,format=raw -m 512M -d int
@@ -73,6 +96,9 @@ bin/paging.o: src/paging.c
bin/page.o: src/page.asm bin/page.o: src/page.asm
$(AS) $(ASFLAGS) $< -o $@ $(AS) $(ASFLAGS) $< -o $@
bin/fat.o: src/fat.c
$(CC) $(CFLAGS) -c $< -o $@
clean: clean:
rm -rf bin/ rm -rf bin/
rm -f $(OUTPUT) rm -f $(OUTPUT)
+3 -2
View File
@@ -2,6 +2,7 @@
#define DISK_H #define DISK_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
typedef struct { typedef struct {
unsigned short cylinders; unsigned short cylinders;
@@ -11,7 +12,7 @@ typedef struct {
void get_disk_info(disk_info *info); void get_disk_info(disk_info *info);
int ide_check_disk_present(); int ide_check_disk_present();
uint8_t ata_read(uint8_t *buffer, uint32_t lba); bool ata_read(uint8_t *buffer, uint32_t lba);
void ata_write(uint8_t *buffer, uint32_t lba); bool ata_write(const uint8_t *buffer, uint32_t lba);
#endif #endif
+159
View File
@@ -0,0 +1,159 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2025, Bjørn Brodtkorb. All rights reserved.
#ifndef FAT_H
#define FAT_H
//------------------------------------------------------------------------------
#include <stdbool.h>
#include <stdint.h>
//#include <stdio.h>
#include <stdarg.h>
//------------------------------------------------------------------------------
#define SECT_SIZE 512
//------------------------------------------------------------------------------
enum
{
FAT_ERR_NONE = 0,
FAT_ERR_NOFAT = -1,
FAT_ERR_BROKEN = -2,
FAT_ERR_DISK = -3,
FAT_ERR_PARAM = -4,
FAT_ERR_PATH = -5,
FAT_ERR_EOF = -6,
FAT_ERR_DENIED = -7,
FAT_ERR_FULL = -8,
};
enum
{
FAT_ATTR_NONE = 0x00,
FAT_ATTR_RO = 0x01,
FAT_ATTR_HIDDEN = 0x02,
FAT_ATTR_SYS = 0x04,
FAT_ATTR_LABEL = 0x08,
FAT_ATTR_DIR = 0x10,
FAT_ATTR_ARCHIVE = 0x20,
FAT_ATTR_LFN = 0x0f,
};
enum
{
FAT_SEEK_SET,
FAT_SEEK_END,
FAT_SEEK_CURR,
};
//------------------------------------------------------------------------------
typedef struct
{
bool (*read)(uint8_t* buf, uint32_t sect);
bool (*write)(const uint8_t* buf, uint32_t sect);
} disk_ops_t;
typedef struct
{
uint8_t hour;
uint8_t min;
uint8_t sec;
uint8_t day;
uint8_t month;
uint16_t year;
} timestamp_t;
typedef struct fat_t
{
struct fat_t* next;
disk_ops_t ops;
char path[32];
int pathlen;
uint32_t sect_per_clust;
uint32_t clust_cnt;
uint32_t info_sect;
uint32_t fat_sect;
uint32_t data_sect;
uint32_t root_sect;
uint32_t info_last;
uint32_t info_cnt;
bool info_dirty;
uint8_t win[SECT_SIZE];
uint32_t win_sect;
bool win_dirty;
uint8_t name[260];
int namelen;
uint8_t crc;
} fat_t;
typedef struct
{
timestamp_t created;
timestamp_t modified;
uint32_t size;
uint8_t attr;
char name[256];
int namelen;
} dir_info_t;
typedef struct
{
fat_t* fat;
uint8_t attr;
uint32_t first_clust;
uint32_t clust;
uint32_t sect;
int idx;
} dir_t;
typedef struct
{
dir_t dir;
uint32_t first_clust;
uint32_t clust;
uint32_t sect;
uint32_t off;
uint32_t size;
uint8_t buf[SECT_SIZE];
uint32_t buf_sect;
bool buf_dirty;
bool modified;
bool accessed;
bool read;
bool write;
} file_t;
//------------------------------------------------------------------------------
int fat_mount(disk_ops_t* ops, int part_num, fat_t* fs, const char* path);
int fat_umount(fat_t* fs);
int fat_fopen(file_t* file, const char* path, const char* mode);
int fat_fclose(file_t* file);
int fat_fread(file_t* file, void* buf, int len);
int fat_fwrite(file_t* file, const void* buf, int len);
int fat_fprintf(file_t* file, const char* str, ...);
int fat_fseek(file_t* file, int offset, int seek);
int fat_ftell(file_t* file);
int fat_fsize(file_t* file);
int fat_fsync(file_t* file);
int fat_unlink(const char* path);
int fat_mkdir(const char* path);
int fat_opendir(dir_t* dir, const char* path);
int fat_readdir(dir_t* dir, dir_info_t* info);
int fat_nextdir(dir_t* dir);
const char* fat_get_error(int err);
void fat_get_timestamp(timestamp_t* dt);
#endif
+5 -2
View File
@@ -161,8 +161,9 @@ void ata_poll()
//https://wiki.osdev.org/ATA_PIO_Mode //https://wiki.osdev.org/ATA_PIO_Mode
//x86 directions section //x86 directions section
uint8_t ata_read(uint8_t *buffer, uint32_t lba) bool ata_read(uint8_t *buffer, uint32_t lba)
{ {
printf("ata_read: 0x%X\n", lba);
outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F));//E0 is for master, F0 is for slave outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F));//E0 is for master, F0 is for slave
//outb(0x1F1, 0x00);//TODO: this is probably ignored so could be removed maybe? //outb(0x1F1, 0x00);//TODO: this is probably ignored so could be removed maybe?
outb(0x1F2, 1);//1 is sector count, how many sectors are we reading outb(0x1F2, 1);//1 is sector count, how many sectors are we reading
@@ -177,9 +178,10 @@ uint8_t ata_read(uint8_t *buffer, uint32_t lba)
*(uint16_t*)(buffer+i*2) = data; *(uint16_t*)(buffer+i*2) = data;
} }
ata_400ns_delay(); ata_400ns_delay();
return true;
} }
void ata_write(uint8_t *buffer, uint32_t lba) bool ata_write(const uint8_t *buffer, uint32_t lba)
{ {
outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F)); outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F));
@@ -197,4 +199,5 @@ void ata_write(uint8_t *buffer, uint32_t lba)
ata_400ns_delay(); ata_400ns_delay();
} }
outb(0x1F7, 0xE7);//Cache flush outb(0x1F7, 0xE7);//Cache flush
return true;
} }
+1829
View File
File diff suppressed because it is too large Load Diff
+82
View File
@@ -7,9 +7,90 @@
#include "../include/pic.h" #include "../include/pic.h"
#include "../include/pit.h" #include "../include/pit.h"
#include "../include/paging.h" #include "../include/paging.h"
#include "../include/fat.h"
#include <stdint.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);
if (cnt < 512)
break;
}
return fat_fclose(&file);
}
static int ls(const char* path)
{
dir_t dir;
dir_info_t info;
printf("\nListing items in %s:\n\n", path);
int err = fat_opendir(&dir, path);
printf("opening %s\n", path);
if (err)
return err;
for (;;)
{
printf("reading dir...\n");
err = fat_readdir(&dir, &info);
if (err == FAT_ERR_EOF)
return FAT_ERR_NONE;
if (err)
return err;
printf("%X %s %d %d:%d %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 file_fuckery()
{
int err;
err = fat_mount(&ops, 0, &g_fat, "/");
if(err)
printf("KATYAKATYAKATYAKATYAKATYA: %X\n", err);
printf("%X\n", FAT_ERR_NOFAT);
err = ls(".");
//err = fat_fclose(&file);
if(err)
printf("ERROR! %X\n", err);
}
void apply_pic_masks() void apply_pic_masks()
{ {
//0x21 is master pic, 0xa1 is slave pic //0x21 is master pic, 0xa1 is slave pic
@@ -37,6 +118,7 @@ void kmain()
disk_info info; disk_info info;
get_disk_info(&info); get_disk_info(&info);
printf("CYL HEAD SECT: %X %X %X\n", info.cylinders, info.heads, info.sectors); printf("CYL HEAD SECT: %X %X %X\n", info.cylinders, info.heads, info.sectors);
file_fuckery();
while(1) while(1)
{ {
/* if(inportb(0x64) & 0x1) /* if(inportb(0x64) & 0x1)