WIP - ATA filesystem connections
This commit is contained in:
parent
8671007f19
commit
abc1a05379
17
init/init.c
17
init/init.c
@ -8,6 +8,23 @@ int main(int argc, char **argv)
|
||||
(void) argv;
|
||||
|
||||
|
||||
FILE *fp = fopen("/dev/sda", "r+");
|
||||
char *buffer = malloc(512);
|
||||
|
||||
fread(buffer, 512, 1, fp);
|
||||
printf("Read: %hhx %hhx %hhx %hhx\n", buffer[0], buffer[1], buffer[510], buffer[511]);
|
||||
|
||||
buffer[511] ^= 0x1;
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
fwrite(buffer, 512, 1, fp);
|
||||
fclose(fp);
|
||||
|
||||
fp = fopen("/dev/sda", "r");
|
||||
fread(buffer, 512, 1, fp);
|
||||
printf("Read: %hhx %hhx %hhx %hhx\n", buffer[0], buffer[1], buffer[510], buffer[511]);
|
||||
fclose(fp);
|
||||
|
||||
|
||||
for(;;);
|
||||
return 0;
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ int kmain(uint64_t multiboot_magic, void *multiboot_data)
|
||||
fs_mount(0, "/");
|
||||
fs_mount(&debug_file, "/dev/debug");
|
||||
|
||||
ata_init();
|
||||
ata_fs_init();
|
||||
|
||||
fs_write(&debug_file, "TESTING DEBUG FILE", 18, 0);
|
||||
|
||||
|
88
kernel/fs/ata_fs.c
Normal file
88
kernel/fs/ata_fs.c
Normal file
@ -0,0 +1,88 @@
|
||||
#include <vfs.h>
|
||||
#include <ata.h>
|
||||
#include <string.h>
|
||||
#include <debug.h>
|
||||
#include <mem.h>
|
||||
|
||||
size_t ata_read(file_t *file, void *buffer, size_t nbyte, size_t offset)
|
||||
{
|
||||
ata_drive *drv = file->data;
|
||||
if(!drv->exists)
|
||||
return 0;
|
||||
|
||||
uint64_t blocksize = 512;
|
||||
char blk_buf[blocksize];
|
||||
char *out_buf = buffer;
|
||||
|
||||
size_t count = 0;
|
||||
while(count < nbyte)
|
||||
{
|
||||
uint64_t block = offset/blocksize;
|
||||
uint64_t bloff = offset%blocksize;
|
||||
uint64_t len = (nbyte-count > blocksize-bloff)?blocksize-bloff:nbyte-count;
|
||||
|
||||
if(len == blocksize)
|
||||
{
|
||||
ata_read_block(drv, block, &out_buf[count]);
|
||||
} else {
|
||||
ata_read_block(drv, block, blk_buf);
|
||||
memcpy(&out_buf[count], &blk_buf[bloff], len);
|
||||
}
|
||||
|
||||
count += len;
|
||||
offset += len;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t ata_write(file_t *file, void *buffer, size_t nbyte, size_t offset)
|
||||
{
|
||||
ata_drive *drv = file->data;
|
||||
if(!drv->exists)
|
||||
return 0;
|
||||
|
||||
uint64_t blocksize = 512;
|
||||
char blk_buf[blocksize];
|
||||
char *in_buf = buffer;
|
||||
|
||||
size_t count = 0;
|
||||
while(count < nbyte)
|
||||
{
|
||||
uint64_t block = offset/blocksize;
|
||||
uint64_t bloff = offset%blocksize;
|
||||
uint64_t len = (nbyte-count > blocksize-bloff)?blocksize-bloff:nbyte-count;
|
||||
|
||||
if(len == blocksize)
|
||||
{
|
||||
ata_write_block(drv, block, &in_buf[count]);
|
||||
} else {
|
||||
ata_read_block(drv, block, blk_buf);
|
||||
memcpy(&blk_buf[bloff], &in_buf[count], len);
|
||||
ata_write_block(drv, block, blk_buf);
|
||||
}
|
||||
|
||||
count += len;
|
||||
offset += len;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
fs_driver_t ata_fs = {
|
||||
.read = ata_read,
|
||||
.write = ata_write,
|
||||
};
|
||||
|
||||
void ata_fs_init()
|
||||
{
|
||||
ata_init();
|
||||
file_t *files = kcalloc(4, sizeof(file_t));
|
||||
char path[] = "/dev/sdX";
|
||||
for(int i=0; i<4; i++)
|
||||
{
|
||||
files[i].type = FS_FILE;
|
||||
files[i].driver = &ata_fs;
|
||||
files[i].data = &drives[i];
|
||||
path[strlen(path)-1] = 'a' + i;
|
||||
fs_mount(&files[i], path);
|
||||
}
|
||||
}
|
@ -60,3 +60,5 @@ file_t *fs_namef(const char *path);
|
||||
|
||||
file_t debug_file;
|
||||
int pipe(file_t **reader, file_t **writer);
|
||||
|
||||
void ata_fs_init();
|
||||
|
Loading…
x
Reference in New Issue
Block a user