From af6087ecec939139265234fac9c29a5d985bd7e3 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 4 Jan 2004 22:00:00 -0500 Subject: [PATCH] Import backup from 2004-01-04 --- asmfuncs.asm | 33 +++++++++++++++++++++ fat12.c | 57 ++++++++++++++++++++++++++++++++++++ fat12.h | 19 ++++++++++++ kernel.c | 4 ++- rd.c | 7 +++-- rd.h | 3 +- vfs.c | 81 +++++++++++++++++++++++++++++++--------------------- vfs.h | 21 +++++++------- 8 files changed, 177 insertions(+), 48 deletions(-) create mode 100644 fat12.c create mode 100644 fat12.h diff --git a/asmfuncs.asm b/asmfuncs.asm index 431022b..37f2248 100644 --- a/asmfuncs.asm +++ b/asmfuncs.asm @@ -52,6 +52,39 @@ _read_cr3: mov eax, cr3; ret +;compares one string to another +;returns 0 if the strings are different +;extern dword strcmp(char *str1, char *str2); +[global _strcmp] +_strcmp: + push ebp + mov ebp, esp + push esi + push edi + + mov esi, [ebp+8] + mov edi, [ebp+12] +strcmp_loop1: + lodsb + mov ah, [edi] + inc edi + cmp ah, al + jnz strcmp_ne + or al, al + jz strcmp_e + jmp strcmp_loop1 +strcmp_e: + mov eax, 1 + jmp short strcmp_done +strcmp_ne: + xor eax, eax +strcmp_done: + + pop edi + pop esi + pop ebp + ret + ;copies a string from the source to the destination parameter ;extern void strcpy(char *dest, char *src); [global _strcpy] diff --git a/fat12.c b/fat12.c new file mode 100644 index 0000000..35064c2 --- /dev/null +++ b/fat12.c @@ -0,0 +1,57 @@ +// fat12.c +// Author: Josh Holtrop +// Created: 01/04/04 + + +byte *fat12_readFile(char *fileName, DiskDevice *dd) +{ +} + +Fat12File *fat12_getFileHandle(char *fileName) +{ + if (strlen(fileName) < 1) + return 0; + if (fileName[strlen(fileName)-1] == '/') + return 0; + dword numDirectories = 0; + int a = 0; + for (;;) //first count how many directories deep we are looking + { + if (fileName[a] == '/') + numDirectories++; + a++; + if (!fileName[a]) + break; + } + for (a = 0; ;a++) //next split apart the filename string into parts for each directory and file + { + if (fileName[a] == 0) + break; + if (fileName[a] == '/') + fileName[a] = 0; + } + char **levels = (char**)malloc((numDirectories+1)*sizeof(char *)); + char *dir = fileName; + for (a = 0; a <= numDirectories; a++) + { + levels[a] = dir; + for (;;) + { + dir++; + if (!(*dir)) + break; + } + dir++; + } + for (a = 0; a <= numDirectories; a++) + { + printf("directory: %s|\n", levels[a]); + } + //at this point we have an array of "strings" (character pointers) called levels + // its length is numDirectories+1, levels[numDirectores] points to the actual file name + + free(levels); +} + + + diff --git a/fat12.h b/fat12.h new file mode 100644 index 0000000..14d36fe --- /dev/null +++ b/fat12.h @@ -0,0 +1,19 @@ +// fat12.h +// Author: Josh Holtrop +// Created: 01/04/04 + + +typedef struct { + dword location; +} __attribute__((packed)) Fat12File; + +typedef struct { + dword location; +} __attribute__((packed)) Fat12Directory; + + +byte *fat12_readFile(char *fileName, DiskDevice *dd); +Fat12File *fat12_getFileHandle(char *fileName); +Fat12Directory *fat12_getDirectoryHandle(char *directory); + + diff --git a/kernel.c b/kernel.c index 6b240fe..1032a73 100644 --- a/kernel.c +++ b/kernel.c @@ -18,6 +18,7 @@ #include "kio.h" #include "vfs.h" #include "rd.h" +#include "fat12.h" void isr(dword num); void k_init(); @@ -38,6 +39,7 @@ extern void strcpy(char *dest, char *src); extern void memcpy(dword dest, dword src, dword n); extern dword strlen(char *str); +#include "fat12.c" #include "rd.c" #include "vfs.c" #include "kio.c" @@ -79,7 +81,7 @@ void k_init() printf("HOS 0.13 - Kernel File Size: %u kb\tData Size: %u bytes\n", kernel_size()>>10, (dword)(&_end)-(dword)(&_code)); printf("Memory available to OS: %u MB (%u bytes)\n", mm_megabytes, mm_totalmem); printf("Free memory: %u bytes (%u pages)\n", mm_freemem(), mm_freemem()>>12); - printf("Root Directory: %s/\n", rootDirectory); + printf("Root Directory: %s/\n", rootDevice->id); dword key = 0; for (;;) diff --git a/rd.c b/rd.c index 1b63060..6fd73e9 100644 --- a/rd.c +++ b/rd.c @@ -1,12 +1,13 @@ // rd.c // Author: Josh Holtrop // Created: 12/31/03 +// Modified: 01/04/04 -byte *rd_readSector(DiskDevice *rd, dword sector) +byte *rd_readSector(DiskDevice *rd, dword sector, byte *dest) { - dword dest = (dword)malloc(512); - memcpy(dest, rd->location + (sector * 512), 512); + memcpy((dword)dest, rd->location + (sector * 512), 512); + return dest; } diff --git a/rd.h b/rd.h index 4ec8c2d..ebba744 100644 --- a/rd.h +++ b/rd.h @@ -1,8 +1,9 @@ // rd.h // Author: Josh Holtrop // Created: 12/31/03 +// Modified: 01/04/04 -byte *rd_readSector(DiskDevice *rd, dword sector); +byte *rd_readSector(DiskDevice *rd, dword sector, byte *dest); diff --git a/vfs.c b/vfs.c index 579b2b5..c77b83c 100644 --- a/vfs.c +++ b/vfs.c @@ -1,53 +1,70 @@ // vfs.c // Author: Josh Holtrop // Created: 12/31/03 +// Modified: 01/04/04 void vfs_init() { - byte ramDiskPresent = *(byte *)0xC0090000; - if (ramDiskPresent) + firstDiskDevice = malloc(sizeof(DiskDevice)); + firstDiskDevice->type = VFS_DISK_FD; + firstDiskDevice->fileSystem = VFS_FS_FAT12; + firstDiskDevice->location = VFS_LOC_FD0; + firstDiskDevice->link = 0; + strcpy(firstDiskDevice->id, "fd0"); + rootDevice = firstDiskDevice; + if (*(byte *)0xC0090000) // the user chose to load an initial ram disk from the floppy { - strcpy(rootDirectory, "rd0"); - firstDevice = malloc(sizeof(DeviceEntry)); - firstDevice->link = 0; - firstDevice->diskDevice = malloc(sizeof(DiskDevice)); - firstDevice->diskDevice->type = VFS_DISK_RD; - firstDevice->diskDevice->fileSystem = VFS_FS_FAT12; - strcpy(firstDevice->diskDevice->id, "rd0"); - firstDevice->diskDevice->location = 0xC0200000; - } - else - { - rootDirectory[0] = 0; + DiskDevice *rd = malloc(sizeof(DiskDevice)); + firstDiskDevice->link = (dword)rd; + rd->type = VFS_DISK_RD; + rd->fileSystem = VFS_FS_FAT12; + rd->location = 0xC0200000; + rd->link = 0; + strcpy(rd->id, "rd0"); + rootDevice = rd; } } byte *vfs_readFile(char *fileName) { - char *newFileName = fileName; - char device[4]; + DiskDevice *dd; + dword fileStartPosition; if (fileName[0] == '/') { - newFileName = malloc(strlen(fileName)+4); - strcpy(newFileName, rootDirectory); - strcat(newFileName, fileName); + dd = rootDevice; + fileStartPosition = 1; } - strcpy(device, newFileName); - DiskDevice *dd = vfs_getDiskDeviceByID(device); - - if (newFileName != fileName) - free(newFileName); + else + { + if (strlen(fileName) < 5) //not a long enough file name + return 0; + if (!((fileName[3] == ':') && (fileName[4] == '/'))) //if we aren't using the root directory, then there must be a 3 character device explicitly specified + return 0; // followed by a colon and then a slash + char device[4]; + memcpy((dword)device, (dword)fileName, 3); //copy the 3 digit device id to device + device[3] = 0; //null-terminating character for device string + dd = vfs_getDiskDeviceByID(device); + if (!dd) //the specified device was not found + return 0; + fileStartPosition = 5; + } + switch (dd->fileSystem) + { + case VFS_FS_FAT12: + return fat12_readFile(fileName+fileStartPosition, dd); + } + return 0; } -byte *vfs_readSector(DiskDevice *dd, dword sector) +byte *vfs_readSector(DiskDevice *dd, dword sector, byte *dest) { - switch(dd->type) + switch (dd->type) { case VFS_DISK_RD: - return rd_readSector(dd, sector); + return rd_readSector(dd, sector, dest); } return 0; } @@ -55,13 +72,13 @@ byte *vfs_readSector(DiskDevice *dd, dword sector) DiskDevice *vfs_getDiskDeviceByID(char *id) { - DeviceEntry *de = firstDevice; + DiskDevice *dd = firstDiskDevice; for (;;) { - if (strcmp(de->diskDevice->id, id)) - return de->diskDevice; - de = (DeviceEntry *)de->link; - if (!de) + if (strcmp(dd->id, id)) + return dd; + dd = (DiskDevice *)dd->link; + if (!dd) break; } return 0; diff --git a/vfs.h b/vfs.h index cf401a8..b39131c 100644 --- a/vfs.h +++ b/vfs.h @@ -1,6 +1,7 @@ // vfs.h // Author: Josh Holtrop // Created: 12/31/03 +// Modified: 01/04/04 #define VFS_DISK_RD 1 @@ -9,26 +10,24 @@ #define VFS_FS_FAT12 1 +#define VFS_LOC_FD0 1 -typedef struct { - dword type; - dword fileSystem; - char id[4]; - dword location; -} __attribute__((packed)) DiskDevice; typedef struct { - DiskDevice *diskDevice; + dword type; + dword fileSystem; + dword location; dword link; -} __attribute__((packed)) DeviceEntry; + char id[4]; +} __attribute__((packed)) DiskDevice; void vfs_init(); byte *vfs_readFile(char *fileName); -byte *vfs_readSector(DiskDevice *dd, dword sector); +byte *vfs_readSector(DiskDevice *dd, dword sector, byte *dest); DiskDevice *vfs_getDiskDeviceByID(char *id); -char rootDirectory[4]; -DeviceEntry *firstDevice = 0; +DiskDevice *rootDevice = 0; +DiskDevice *firstDiskDevice = 0;