Skip to content

Instantly share code, notes, and snippets.

@SharpCoder
Created June 28, 2016 16:04
Show Gist options
  • Select an option

  • Save SharpCoder/8e148cd6dfa3b058016e5f5473c63f0f to your computer and use it in GitHub Desktop.

Select an option

Save SharpCoder/8e148cd6dfa3b058016e5f5473c63f0f to your computer and use it in GitHub Desktop.
// *******************************
// FILE: io.h
// AUTHOR: SharpCoder
// DATE: 2015-03-31
// ABOUT: This is the disk IO header. It must be implemented
// in order for the filesystem to work.
//
// LICENSE: Provided "AS IS". USE AT YOUR OWN RISK.
// *******************************
#ifndef __PIFS_IO_H_
#define __PIFS_IO_H_
#include "conf.h"
#include "../libs/mem.h"
// These are the IO methods used to simulate writing to a disk.
// For now, we will simply use memory. Since we're the kernel,
// we can do that!
int openDisk(int nbytes);
int readBlock(int disk, int blocknr, void* buf, int bytes);
int writeBlock(int disk, int blocknr, void* block, int bytes, uint32 foffset);
void syncDisk();
// Create the disk
char* disk_base;
// Allocate some disk.
int f_mkfs( int nbytes ) {
// Wipe the memory.
disk_base = (char*)malloc(nbytes);
return 1;
}
int readBlock(int disk, int blocknr, void* buf, int bytes) {
char* addr = (char*)disk_base;
char* targ = (char*)buf;
// Add the offset to the address.
addr += blocknr * PAGE_SIZE;
// Check for a buffer overflow type situation.
if ( bytes > PAGE_SIZE ) return -1;
for ( uint32 index = 0; index < bytes; index++ ) {
*targ = *addr;
targ++;
addr++;
}
return 1;
}
int writeBlock(int disk, int blocknr, void* buf, int bytes, uint32 foffset) {
char* addr = (char*)disk_base;
char* orig = (char*)buf;
// Add the offset to the address.
addr += blocknr * PAGE_SIZE;
addr += foffset;
// Check for a buffer overflow type situation.
if ( bytes > PAGE_SIZE ) return -1;
for ( uint32 index = 0; index < bytes; index++ ) {
*addr = *orig;
orig++;
addr++;
}
return 1;
}
void syncDisk() {
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment