Skip to content

Commit

Permalink
~ | Changing filesystem initrd
Browse files Browse the repository at this point in the history
  • Loading branch information
BolvicBolvicovic committed Sep 17, 2024
1 parent b638f11 commit cf60d75
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 131 deletions.
5 changes: 0 additions & 5 deletions filesystem/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ vfs/%.o: vfs/%.c
initrd/%.o: initrd/%.c
$(CC) $(FLAGS) -c $^ -o $@

generator: filesystem_generator.c
gcc filesystem_generator.c -o filesystem_generator
./filesystem_generator
rm -rf filesystem_generator

clean:
rm -rf $(VFS_OBJ)
rm -rf $(IRD_OBJ)
Expand Down
52 changes: 0 additions & 52 deletions filesystem/filesystem_generator.c

This file was deleted.

16 changes: 8 additions & 8 deletions filesystem/initrd/initrd.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "initrd.h"

initrd_header_t *initrd_header; // The header.
initrd_file_header_t *file_headers; // The list of file headers.
fs_node_t *initrd_root; // Our root directory node.
fs_node_t *initrd_dev; // We also add a directory node for /dev, so we can mount devfs later on.
fs_node_t *root_nodes; // List of file nodes.
int nroot_nodes; // Number of file nodes.
initrd_header_t *initrd_header; // The header.
initrd_file_header_t *file_headers; // The list of file headers.
PFILE initrd_root; // Our root directory node.
PFILE initrd_dev; // We also add a directory node for /dev, so we can mount devfs later on.
PFILE root_nodes; // List of file nodes.
uint32_t nroot_nodes; // Number of file nodes.

dir_entry_t dirent;
dentry dir_entry;

static uint32_t initrd_read(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
initrd_file_header_t header = file_headers[node->inode];
Expand All @@ -17,7 +17,7 @@ static uint32_t initrd_read(fs_node_t *node, uint32_t offset, uint32_t size,
return size;
}

static dir_entry_t *initrd_readdir(fs_node_t *node, uint32_t index) {
static dentry *initrd_readdir(fs_node_t *node, uint32_t index) {
if (node == initrd_root && index == 0)
{
strcpy(dirent.name, "dev");
Expand Down
29 changes: 13 additions & 16 deletions filesystem/vfs/vfs.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
#include "vfs.h"

struct fs_node_s *fs_root = NULL;
PFILE root;

uint32_t fs_write(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer) {
if (!node->write) return node->write(node, offset, size, buffer);
uint32_t fs_read(PFILE node, uint32_t offset, uint32_t size, char* buffer) {
if (node->node_operations.read) return node->node_operations.read(node, offset, size, buffer);
return 0;
}

uint32_t fs_read(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer) {
if (!node->read) return node->read(node, offset, size, buffer);
uint32_t fs_write(PFILE node,uint32_t offset, uint32_t size, char* buffer) {
if (node->node_operations.write) return node->node_operations.write(node, offset, size, buffer);
return 0;
}

void fs_open(struct fs_node_s* node, uint8_t read, uint8_t write) {
if (!node->open) (node->open(node));
}

void fs_close(struct fs_node_s* node) {
if (!node->close) (node->close(node));
uint32_t fs_open(PFILE node, enum open_flags flags) {
if (node->node_operations.open) return node->node_operations.open(node, flags);
return 0;
}

dir_entry_t* readdir_fs(struct fs_node_s* node, uint32_t index) {
if (!node->readdir && (node->flags & 7 == FS_DIRECTORY)) return node->readdir(node, index);
return NULL;
uint32_t fs_close(PFILE node) {
if (node->node_operations.close) return node->node_operations.close(node);
return 0;
}

struct fs_node_s* finddir_fs(struct fs_node_s* node, char* name) {
if (!node->finddir && (node->flags & 7 == FS_DIRECTORY)) return node->finddir(node, name);
dentry* fs_readdir(PFILE node, uint32_t index) {
if ((node->type & FS_DIR) && node->node_operations.readdir) return node->readdir(node, index);
return NULL;
}
86 changes: 40 additions & 46 deletions filesystem/vfs/vfs.h
Original file line number Diff line number Diff line change
@@ -1,58 +1,52 @@
#ifndef VFS_H
#define VFS_H

#include <stdint.h>
#include <stduint32_t.h>
#include <stddef.h>
#include "../../lib/string/string.h"
#include "../../memory/vmm/vmm.h"

enum fs_flags {
FS_FILE = 1,
FS_DIRECTORY = 2,
FS_CHARDEVICE = 3,
FS_BLOCKDEVICE = 4,
FS_PIPE = 5,
FS_SYMLINK = 6,
FS_MOUNTPOINT = 8
typedef enum node_type {
FS_FILE,
FS_DIR,
FS_CHARDEVICE,
FS_BLOCKDEVICE,
FS_PIPE,
FS_SYMLINK,
FS_MOUNTPOINT
};

typedef uint32_t (*rw_t)(struct fs_node_s*, uint32_t, uint32_t, uint8_t*);
typedef void (*oc_t)(struct fs_node_s*);
typedef struct dir_entry_s * (*readdir_t)(struct fs_node_s*, uint32_t);
typedef struct fs_node_s * (*finddir_t)(struct fs_node_s*, char* name);

typedef struct fs_node_s {
char name[128]; // Should move it to directory node
uint32_t permissions;
uint32_t uid;
uint32_t gid;
uint32_t flags;
uint32_t inode; // Provide to a filesystem a way to identify nodes
uint32_t len;
uint32_t impl; // Implementation-defined number
// These are pointers to callbacks
rw_t read;
rw_t write;
oc_t open;
oc_t close;
readdir_t readdir;
finddir_t finddir;
struct fs_node_s *ptr; // For symlink and mountpoint
} fs_node_t;

typedef struct dir_entry_s {
char name[128];
uint32_t inode;
} dir_entry_t;


uint32_t fs_write(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer);
uint32_t fs_read(struct fs_node_s* node, uint32_t offset, uint32_t size, uint8_t* buffer);
void fs_open(struct fs_node_s* node, uint8_t read, uint8_t write);
void fs_close(struct fs_node_s* node);
dir_entry_t* readdir_fs(struct fs_node_s* node, uint32_t index);
struct fs_node_s* finddir_fs(struct fs_node_s*, char* name);
typedef enum open_flags {
READ,
WRITE
};

extern fs_node_t* fs_root;
typedef struct _dentry {
char name[32];
uint32_t inode;
} dentry;

typedef struct _FILE {
char name[32];
uint32_t permissions;
enum node_type type;
uint32_t uid;
uint32_t sid;
uint32_t inode;
uint32_t length;
struct node_operations {
uint32_t (*read)(struct FILE*,uint32_t,uint32_t,char*buffer);
uint32_t (*write)(struct FILE*,uint32_t,uint32_t,char*buffer);
uint32_t (*open)(struct FILE*, enum open_flags);
uint32_t (*close)(struct FILE*);
dentry* (*readdir)(struct FILE*,uint32_t);
};
} FILE, *PFILE;

uint32_t fs_close(PFILE);
uint32_t fs_write(PFILE,uint32_t,uint32_t,char*buffer);
uint32_t fs_read(PFILE,uint32_t,uint32_t,char*buffer);
uint32_t fs_open(PFILE, enum open_flags);
dentry* fs_readdir(PFILE,uint32_t);

#endif
8 changes: 4 additions & 4 deletions kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ void kernel_main(uint32_t magic, uint32_t addr) {
multiboot_info_t* mbi = (multiboot_info_t*)addr;
struct multiboot_mmap_entry* region = (struct multiboot_mmap_entry*) mbi->mmap_addr;
uint32_t mem_size = MAX_MEMORY_SIZE;
uint32_t initrd_location = *(uint32_t*)(mbi->mods_addr);
uint32_t initrd_end = *(uint32_t*)(mbi->mods_addr + 4);
// uint32_t initrd_location = *(uint32_t*)(mbi->mods_addr);
// uint32_t initrd_end = *(uint32_t*)(mbi->mods_addr + 4);

init_current_screen(BLUE, WHITE);
term_clear();
Expand All @@ -54,6 +54,6 @@ void kernel_main(uint32_t magic, uint32_t addr) {
asm volatile("mov %%cr0, %0" : "=r" (cr0));
if (cr0 & 0x80000000) printf("Paging enabled: cr0 == %p\n", cr0);
else printf("Paging disabled: cr0 == %p\n", cr0);
if (!mbi->mods_count) printf("no mods added\n");
fs_root = initialise_initrd(initrd_location);
// if (!mbi->mods_count) printf("no mods added\n");
// fs_root = initialise_initrd(initrd_location);
}

0 comments on commit cf60d75

Please sign in to comment.