Add fs.c.

This commit is contained in:
Alexander Myltsev 2022-12-13 00:50:28 +03:00
parent 8cafc15116
commit babf3897f4
7 changed files with 64 additions and 7 deletions

View File

@ -59,7 +59,8 @@ image.bin: mbr.bin fs.img
cat $^ >$@ cat $^ >$@
kernel.bin: kernel.o console.o drivers/vga.o drivers/keyboard.o \ kernel.bin: kernel.o console.o drivers/vga.o drivers/keyboard.o \
string.o drivers/ata.o cpu/vectors.o cpu/idt.o cpu/gdt.o drivers/uart.o string.o drivers/ata.o cpu/vectors.o cpu/idt.o cpu/gdt.o drivers/uart.o \
fs/fs.o
$(LD) $(LDFLAGS) -o $@ -Ttext 0x1000 $^ $(LD) $(LDFLAGS) -o $@ -Ttext 0x1000 $^
%.o: %.c %.o: %.c

View File

@ -1,5 +1,4 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
void read_sectors_ATA_PIO(uint32_t target_address, uint32_t LBA, uint8_t sector_count); void read_sectors_ATA_PIO(void* target_address, uint32_t LBA, uint8_t sector_count);
void write_sectors_ATA_PIO(uint32_t LBA, uint8_t sector_count, uint32_t* bytes);

39
fs/fs.c Normal file
View File

@ -0,0 +1,39 @@
#include "fs.h"
#include "../string.h"
#include "../drivers/ata.h"
enum {
fs_start = 1, // sector where the FS starts
};
int stat(const char* name, struct stat *buf) {
struct dir dir;
read_sectors_ATA_PIO(&dir, fs_start, 1);
for (int i = 0; i < ents_in_dir; ++i) {
struct dirent *e = &dir.entries[i];
if (!strncmp(e->name, name, sizeof(e->name))) {
buf->size = e->size_bytes;
buf->reserved[0] = e->offset_sectors;
return 0;
}
}
return -1;
}
/* Find file by name and read it into buffer with size bufsize.
* At most (bufsize & -512) bytes will be read.
* Return number of bytes read or -1 on failure. */
int read_file(const char* name, void* buf, uint32_t bufsize) {
struct stat statbuf;
if (stat(name, &statbuf) != 0) {
return -1;
}
uint32_t sector = fs_start + statbuf.reserved[0];
while (bufsize >= sector_size) {
read_sectors_ATA_PIO(buf, sector, 1);
sector++;
bufsize -= sector_size;
buf += sector_size;
}
return 0;
}

View File

@ -21,6 +21,7 @@ size is in bytes, name is 0-terminated.
enum { enum {
sector_size = 512, sector_size = 512,
ents_in_dir = 15,
}; };
struct dirent { struct dirent {
@ -32,7 +33,7 @@ struct dirent {
struct dir { struct dir {
char reserved[32]; char reserved[32];
struct dirent entries[15]; struct dirent entries[ents_in_dir];
}; };
struct stat { struct stat {

View File

@ -8,6 +8,7 @@ asm(".asciz \"kernel start\"");
#include "drivers/ata.h" #include "drivers/ata.h"
#include "drivers/misc.h" #include "drivers/misc.h"
#include "drivers/uart.h" #include "drivers/uart.h"
#include "fs/fs.h"
void _start() { void _start() {
load_gdt(); load_gdt();
@ -15,13 +16,16 @@ void _start() {
uartinit(); uartinit();
load_idt(); load_idt();
sti(); sti();
char buf[512]; char buf[4096 + 512];
vga_clear_screen(); vga_clear_screen();
printk("YABLOKO\n"); printk("YABLOKO\n");
read_sectors_ATA_PIO((uint32_t)buf, 10, 1); if (read_file("kernel.bin", buf, sizeof(buf)) == 0) {
printk(buf); printk(buf + 4096);
} else {
printk("failed to read file\n");
}
while (1) { while (1) {
asm("hlt"); asm("hlt");

View File

@ -14,3 +14,15 @@ void kmemmove(char* dst, char* src, size_t size) {
} }
} }
} }
int strncmp(const char* s1, const char* s2, size_t size) {
while (size && *s1 && *s2 && *s1 == *s2) {
size--;
s1++;
s2++;
}
if (!size) {
return 0;
}
return (unsigned char)(*s1) - (unsigned char)(*s2);
}

View File

@ -3,3 +3,4 @@
typedef unsigned size_t; typedef unsigned size_t;
void kmemmove(char* dst, char* src, size_t size); void kmemmove(char* dst, char* src, size_t size);
int strncmp(const char* s1, const char* s2, size_t size);