Compare commits

..

9 Commits

7 changed files with 219 additions and 6 deletions

@ -38,7 +38,9 @@ OBJS = \
pic/pic.o \ pic/pic.o \
pic/keyboard.o \ pic/keyboard.o \
pic/pit.o \ pic/pit.o \
utils/ring_buffer.o utils/ring_buffer.o \
iso.o \
fd.o
DEPS = $(OBJS:.o=.d) DEPS = $(OBJS:.o=.d)

@ -17,7 +17,8 @@ static void busy_wait(uint16_t drive)
static void wait_device_selection(uint16_t drive) static void wait_device_selection(uint16_t drive)
{ {
for (int i = 0; i < 400; ++i) for (int i = 0; i < 4000; ++i)
//printf("wait\r\n");
inb(drive); inb(drive);
} }

38
k/fd.c Normal file

@ -0,0 +1,38 @@
#include "fd.h"
struct file_entry fd_table[MAX_FD] = { 0 };
static int find_next_fd(void)
{
for (int fd = 0; fd < MAX_FD; ++fd)
{
if (!fd_table[fd].open)
return fd;
}
return -1;
}
int register_fd(u32 lba, u32 file_size, u8 is_dir)
{
int free_fd = find_next_fd();
if (free_fd < 0)
return -1;
struct file_entry f = { 1, lba, 0, file_size, is_dir };
fd_table[free_fd] = f;
return free_fd;
}
int destroy_fd(int fd)
{
if (fd > MAX_FD || !fd_table[fd].open)
return -1;
fd_table[fd].open = 0;
fd_table[fd].lba = 0;
fd_table[fd].file_offset = 0;
fd_table[fd].file_size = 0;
return 0;
}

22
k/fd.h Normal file

@ -0,0 +1,22 @@
#ifndef FD_H
#define FD_H
#include <k/types.h>
#define MAX_FD 65535
struct file_entry
{
u8 open;
u32 lba;
u32 file_offset;
u32 file_size;
u8 is_dir;
};
extern struct file_entry fd_table[MAX_FD];
int register_fd(u32 lba, u32 file_size, u8 is_dir);
int destroy_fd(int fd);
#endif /* !FD_H */

133
k/iso.c Normal file

@ -0,0 +1,133 @@
#include <k/iso9660.h>
#include <k/types.h>
#include "atapi.h"
#include "fd.h"
#include "iso.h"
#include "stdio.h"
#include "string.h"
static u32 get_filename_length(const char *path, u32 *index)
{
u32 len = 0;
for (; path[*index]; ++(*index))
{
if (path[*index] != '/')
++len;
else {
while (path[*index] == '/')
++(*index);
break;
}
}
return len;
}
int open(const char *pathname, int flags)
{
if (flags != O_RDONLY)
return -1;
struct iso_prim_voldesc *iso_prim_block = read_block(ISO_PRIM_VOLDESC_BLOCK);
if (strncmp(iso_prim_block->std_identifier, "CD001", 5))
return -1;
struct iso_dir *root = &iso_prim_block->root_dir;
struct iso_dir *curr = read_block(root->data_blk.le);
u32 index = 0;
while (pathname[index] == '/')
++index;
for (int depth = 0; depth < MAX_DIR_DEPTH; ++depth)
{
char found = 0;
u32 start = index;
u32 length = get_filename_length(pathname, &index);
// we found the file
if (!length)
return register_fd(curr->data_blk.le, curr->file_size.le, 1);
// iteration for one directory
while (curr->dir_size != 0)
{
if (length == curr->idf_len &&
!strncmp(pathname + start, curr->idf, length))
{
if (!(curr->type & ISO_FILE_ISDIR))
return register_fd(curr->data_blk.le, curr->file_size.le, 0);
curr = read_block(curr->data_blk.le);
found = 1;
break;
}
else
curr = (void *)curr + curr->dir_size;
}
if (!found)
return -1;
}
return -1;
}
s64 read(int fd, void *buf, size_t count)
{
if (fd > MAX_FD)
return -1;
struct file_entry *f = &fd_table[fd];
if (!f->open)
return -1;
u32 nb_block = f->file_offset / ISO_BLOCK_SIZE;
char *content = read_block(f->lba + nb_block);
size_t i = 0;
for (; i < count && f->file_offset < f->file_size; ++i)
{
((char *)buf)[i] = content[f->file_offset];
++(f->file_offset);
if (f->file_offset / ISO_BLOCK_SIZE > nb_block)
{
++nb_block;
content = read_block(f->lba + nb_block);
}
}
return i;
}
s64 seek(int fd, u32 offset, int whence)
{
if (fd > MAX_FD)
return -1;
struct file_entry *f = &fd_table[fd];
if (!f->open)
return -1;
switch (whence)
{
case SEEK_SET:
f->file_offset = offset;
break;
case SEEK_CUR:
f->file_offset += offset;
break;
case SEEK_END:
f->file_offset = f->file_size + offset;
break;
}
return f->file_offset;
}
int close(int fd)
{
return destroy_fd(fd);
}

20
k/iso.h Normal file

@ -0,0 +1,20 @@
#ifndef ISO_H
#define ISO_H
#include <k/types.h>
/* open flags */
#define O_RDONLY 0
/* seek flags */
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
/* functions */
int open(const char *path, int flags);
s64 read(int fd, void *buf, size_t count);
s64 seek(int fd, u32 offset, int whence);
int close(int fd);
#endif /* !ISO_H */

5
k/k.c

@ -31,6 +31,7 @@
#include "serial.h" #include "serial.h"
#include "stdio.h" #include "stdio.h"
#include "iso.h" #include "iso.h"
#include "atapi.h"
static void k_init(void) static void k_init(void)
{ {
@ -52,10 +53,6 @@ void k_main(unsigned long magic, multiboot_info_t *info)
k_init(); k_init();
//printf("bonjour\r\n");
poggers();
for (unsigned i = 0; ; ) { for (unsigned i = 0; ; ) {
*fb = star[i++ % 4]; *fb = star[i++ % 4];
} }