Hack together open()

This commit is contained in:
Byron Lathi
2023-12-05 18:04:08 -08:00
parent 9ae1593957
commit 48b39eb92d
7 changed files with 130 additions and 31 deletions

View File

@@ -43,4 +43,8 @@ int8_t fat32_get_cluster_by_name(char* name, struct fat32_directory_entry* dentr
int8_t fat32_read_cluster(uint32_t cluster, void* buf);
uint32_t fat32_next_cluster(uint32_t cluster);
int8_t fat32_file_open(const int8_t* filename);
int8_t fat32_file_read(int8_t fd, const void* buf, int8_t nbytes);
int8_t fat32_file_write(int8_t fd, const void* buf, int8_t nbytes);
int8_t fat32_file_close(int8_t fd);
#endif

View File

@@ -1,10 +1,67 @@
#include "fat32.h"
#include <devices/sd_card.h>
#include <process/process.h>
#include <conio.h>
#include <stdint.h>
#include <string.h>
#include "fat32.h"
#include "fs.h"
static struct fops fat32_file_ops = {fat32_file_open, fat32_file_close, fat32_file_read, fat32_file_write};
int8_t fd_val;
//TODO
int8_t fat32_file_write(int8_t fd, const void* buf, int8_t nbytes) {
return -1;
}
int8_t fat32_file_close(int8_t fd) {
return -1;
}
int8_t fat32_file_open(const int8_t* filename) {
int8_t ret;
int8_t i;
int8_t fd;
struct fat32_directory_entry dentry;
struct pcb* pcb = get_pcb_ptr();
ret = fat32_get_cluster_by_name(filename, &dentry);
if (ret) {
cprintf("Error finding cluster for filename");
return -1;
}
/* try to find an empty file desciptor, fail otherwise */
//TODO We start at 3 here because 0, 1, 2 will be reserved later
for (i = 3; i < FILE_DESC_SIZE; i++) {
if (pcb->file_desc_array[i].flags == !IN_USE) {
fd = i;
break;
}
}
if (fd == -1){
return -1;
}
/* add process */
pcb->file_desc_array[fd].f32_dentry = dentry;
pcb->file_desc_array[fd].flags = IN_USE;
pcb->file_desc_array[fd].file_pos = 0;
pcb->file_desc_array[fd].file_ops = &fat32_file_ops;
return fd;
}
int8_t fat32_file_read(int8_t fd, const void* buf, int8_t nbytes) {
struct pcb* pcb = get_pcb_ptr();
}
int8_t fat32_read_cluster(uint32_t cluster, void* buf) {
uint8_t error;
uint32_t addr = (cluster - 2) + data_start_sector;

View File

@@ -3,16 +3,18 @@
#include <stdint.h>
#include <process/process.h>
/* syscalls for files */
int8_t file_read(int8_t fd, void* buf, int8_t nbytes);
int8_t file_write(int8_t fd, const void* buf, int8_t nbytes);
int8_t file_open(const uint8_t* filename);
int8_t file_open(const int8_t* filename);
int8_t file_close(int8_t fd);
/* syscalls for directories */
int8_t directory_read(int8_t fd, void* buf, int8_t nbytes);
int8_t directory_write(int8_t fd, const void* buf, int8_t nbytes);
int8_t directory_open(const uint8_t* filename);
int8_t directory_open(const int8_t* filename);
int8_t directory_close(int8_t fd);

View File

@@ -0,0 +1,6 @@
#include <process/process.h>
struct pcb* get_pcb_ptr() {
return 0;
}

View File

@@ -0,0 +1,46 @@
#ifndef _PROCESS_H
#define _PROCESS_H
#include <stdint.h>
#include <filesystems/fat32.h>
#define FILE_DESC_SIZE 8
#define IN_USE 1
struct fops {
int8_t (*open)(const int8_t* filename);
int8_t (*close)(int8_t fd);
int8_t (*read)(int8_t fd, void* buf, int8_t nbytes);
int8_t (*write)(int8_t fd, const void* buf, int8_t nbytes);
};
struct file_desc {
struct fops* file_ops;
uint8_t fs_type;
union {
struct fat32_directory_entry f32_dentry;
};
uint32_t file_pos;
uint32_t flags;
};
/* Process Control Block struct */
struct pcb {
struct file_desc file_desc_array[FILE_DESC_SIZE];
int32_t is_vidmapped;
uint8_t args[128];
uint32_t execute_return;
int32_t pid;
int32_t parent_pid;
uint32_t parent_esp;
uint32_t parent_ebp;
};
struct pcb* get_pcb_ptr();
#endif

View File

@@ -1,9 +1,17 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <process/process.h>
#define FILE_PATH "fs.fat"
struct pcb fake_pcb;
//TODO
struct pcb* get_pcb_ptr() {
return &fake_pcb;
}
uint32_t lmulii(uint16_t a, uint16_t b) {
printf("lmulii: %x * %x = %x\n", a, b, a*b);
return a * b;

View File

@@ -5,35 +5,11 @@
void fat32_init(void);
int main(void) {
struct fat32_directory_entry dentry;
int i;
uint32_t offset = 0;
uint32_t cluster;
uint8_t cluster_count;
int8_t fd;
fat32_init();
/* This is what is going to be part of open */
fat32_get_cluster_by_name("VERYLA~1TXT", &dentry);
cprintf("attr1: %x\n", dentry.attr1);
cprintf("cluster: %x%x\n", dentry.cluster_high, dentry.cluster_low);
cprintf("File Size: %lx\n", dentry.file_size);
fd = fat32_file_open("VERYLA~1TXT");
/* This will become part of read */
cluster = (dentry.cluster_high << 16) | dentry.cluster_low;
cluster++;
fat32_read_cluster(cluster + offset/512, sd_buf);
cluster_count = (dentry.file_size/512)/sectors_per_cluster;
cprintf("Cluster count: %x\n", cluster_count);
while (cluster < 0xffffff0) {
for (i = 0; i < dentry.file_size && i < 512; i++) {
cprintf("%c", sd_buf[i]);
}
cluster = fat32_next_cluster(cluster);
fat32_read_cluster(cluster + offset/512, sd_buf);
}
return 0;
cprintf("fd: %x\n", fd);
}