Compare commits

...

4 Commits

7 changed files with 144 additions and 6 deletions

View File

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

View File

@ -17,7 +17,8 @@ static void busy_wait(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);
}

37
k/fd.c Normal file
View File

@ -0,0 +1,37 @@
#include "fd.h"
static 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)
{
int free_fd = find_next_fd();
if (free_fd < 0)
return -1;
struct file_entry f = { .open = 1, .lba = lba, .file_offset = 0 };
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;
return 0;
}

18
k/fd.h Normal file
View File

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

73
k/iso.c Normal file
View File

@ -0,0 +1,73 @@
#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);
// iteration for one directory
while (curr->dir_size != 0)
{
if (length == curr->idf_len &&
!strncmp(pathname + start, curr->idf, length))
{
curr = read_block(curr->data_blk.le);
found = 1;
break;
}
else
curr = (void *)curr + curr->dir_size;
}
if (!found)
return -1;
}
return -1;
}

10
k/iso.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef ISO_H
#define ISO_H
#include <k/types.h>
#define O_RDONLY 0
int open(const char *path, int flags);
#endif /* !ISO_H */

5
k/k.c
View File

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