first commit

This commit is contained in:
corey 2024-01-13 13:19:22 -06:00
commit 1d1357192b
12 changed files with 284 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/rootfs
*.o
.*.swp

11
Makefile Normal file
View File

@ -0,0 +1,11 @@
CFLAGS= -Werror=discarded-qualifiers -Werror=ignored-qualifiers -Wfatal-errors -Wall -Wextra
LDFLAGS=
OBJS= main.o fs.o vfs.o init.o
all: rootfs
rootfs: $(OBJS)
$(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS)
%.o: %.c %.h
$(CC) -c $< $(CFLAGS)
clean:
$(RM) *.o $(OBJS)

8
TODO Normal file
View File

@ -0,0 +1,8 @@
* shell
* diagnostics
* tests
Example Output:
_______________
ls /: '/ /usr /home'
ls /usr: '/usr/cp /usr/dd'

4
err.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
typedef uint32_t err_t;
enum{EOK=0,ENULL,};

74
fs.c Normal file
View File

@ -0,0 +1,74 @@
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"fs.h"
#include"err.h"
#define GENID() 0
// Global filesystem for OS mounting
FsHdr filesystem={0};
err_t fs_mount_root(FsHdr*fs)
{
if(!fs||fs->address==NULL)
{
printf("error: cannot mount NULL filesystem\n");
return ENULL;
}
filesystem=*fs;
return EOK;
}
static const char*fs_humanreadable(size_t val)
{
static char buffer[1024]={0};
const char*append="?";
const char*const prefixes[]={"B","KiB","MiB","GiB","TiB"};
for(size_t i=0;i<sizeof(prefixes);++i)
{
if(val<1024)
{
append=prefixes[i];
break;
}
else
val/=1024;
}
sprintf(buffer,"%lu%s",val,append);
return (const char*)buffer;
}
void fs_print(const FsHdr*const fs)
{
if(!fs)
{
printf("error: cannot print NULL fs\n");
return;
}
printf("id: %#08x\n",fs->id);
printf("address: %p\n",fs->address);
printf("length: %lu (%s)\n",fs->length,fs_humanreadable(fs->length));
}
err_t fs_pathto_inode(const char*const path)
{
if(!path)
return ENULL;
return EOK;
}
err_t fs_stat(const char*const path)
{
if(!path)
return ENULL;
return EOK;
}

48
fs.h Normal file
View File

@ -0,0 +1,48 @@
/*****
*
* Interface for OS with
* filesystem modules
* fd table: connect file
* descriptors with actual
* files (inodes)
*
*****/
#pragma once
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"err.h"
#define BLOCKSIZE 4096
#define VFS_GENID() 0
typedef uint32_t fstype_t;
// An inode represents an actual file
typedef struct inode
{
uint32_t atime;
uint8_t flags;
} inode;
// Filesystem header/super_block
typedef struct FsHdr
{
uint8_t*address;
size_t length;
uint32_t id;
fstype_t type;
}FsHdr;
extern FsHdr filesystem;
// Function type to read from filesystem
typedef void(*fs_read_t)(uint32_t offset,uint32_t size);
typedef void(*fs_read_inode_t)(inode*inode_buffer,uint32_t offset);
err_t fs_mount_root(FsHdr*fs);
err_t fs_stat(const char*const path);
void fs_print(const FsHdr*const fs);

30
init.c Normal file
View File

@ -0,0 +1,30 @@
/*****
*
* Mount rootfs filesystem for bootup
*
*****/
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"fs.h"
#include"vfs.h"
#include"init.h"
#include"err.h"
err_t init_mkrootfs(void)
{
FsHdr fs=vfs_mkfs(128);
if(fs_mount_root(&fs)==ENULL)
{
printf("error: cannot mount NULL filesystem\n");
return ENULL;
}
return EOK;
}
void init_releasefs(void)
{
vfs_free(&filesystem);
}

12
init.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"fs.h"
#include"err.h"
#include"vfs.h"
err_t init_mkrootfs(void);
void init_releasefs(void);

33
main.c Normal file
View File

@ -0,0 +1,33 @@
/*****
*
* Simulate a filesystem
* not necessarily a shell
*
*****/
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"err.h"
#include"fs.h"
#include"vfs.h"
#include"init.h"
int main(void)
{
if(init_mkrootfs()!=EOK)
printf("error: failed to initialize filesystem");
// allocate memory for virtual filesystem
// (VFS) and run commands to create inodes
// and then view their stats.
printf("mounted filesystem:\n");
fs_print(&filesystem);
init_releasefs();
printf("\nmounted filesystem:\n");
fs_print(&filesystem);
}

17
result.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct Result
{
bool success;
size_t size;
void*buffer;
} Result;
Result result_clone(void*buffer,size_t size)
{
Result res={.success=false,.size=size,.buffer=buffer};
}

26
vfs.c Normal file
View File

@ -0,0 +1,26 @@
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"fs.h"
FsHdr vfs_mkfs(const size_t nblocks)
{
const size_t length=BLOCKSIZE*nblocks;
const FsHdr fs={
.id=VFS_GENID(),
.length=length,
.address=malloc(length),
};
return fs;
}
void vfs_free(FsHdr*const fs)
{
if(!fs)return;
if(fs->address)
free(fs->address);
fs->address=NULL;
fs->length=0;
}

18
vfs.h Normal file
View File

@ -0,0 +1,18 @@
/*****
*
* Virtual filesystem for rootfs
*
*****/
#pragma once
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"fs.h"
#define VFS_FSTYPE 0x0001
FsHdr vfs_mkfs(const size_t nblocks);
void vfs_free(FsHdr*const fs);