diff --git a/src/sys/include/ubixfs/ubixfs.h b/src/sys/include/ubixfs/ubixfs.h index 0c4ebc7..8883d5a 100755 --- a/src/sys/include/ubixfs/ubixfs.h +++ b/src/sys/include/ubixfs/ubixfs.h @@ -27,12 +27,33 @@ #include #include -struct fileTableEntry { - uChar fileName[22]; - uShort start; - uShort length; - uLong size; - uShort attributes; + //Partition Information +struct partitionInformation { + uLong size; //Size In Sectors + uLong startSector; //Base Sector Of Partition + uLong blockAllocationTable; //Base Sector Of BAT + uLong rootDirectory; //Base Sector Of Root Directory + }; + +//Block Allocation Table Entry +struct blockAllocationTableEntry { + long attributes; //Block Attributes + long realSector; //Real Sector + long nextBlock; //Sector Of Next Block + long reserved; //Reserved + }; + +//UbixFS Directory Entry +struct directoryEntry { + uLong startCluster; //Starting Cluster Of File + uLong size; //Size Of File + uLong creationDate; //Date Created + uLong lastModified; //Date Last Modified + uLong uid; //UID Of Owner + uLong gid; //GID Of Owner + uShort attributes; //Files Attributes + uShort permissions; //Files Permissions + char fileName[256]; //File Name }; struct bootSect { @@ -48,13 +69,16 @@ uInt TotalHeads; uLong TotalSectors; uChar code[479]; - }; - + }; + void initUbixFS(); int findFile(char *file,fileDescriptor *fd); int readFile(char *file); -int getFileByte(fileDescriptor *fd,int offset); +int getFileByte(fileDescriptor *fd,long offset); extern int startSector; +extern int partitionSize; +extern int rootDirectory; +extern struct blockAllocationTableEntry *blockAllocationTable; #endif diff --git a/src/sys/ubixfs/file.c b/src/sys/ubixfs/file.c index 5cf5034..9c4fc2c 100755 --- a/src/sys/ubixfs/file.c +++ b/src/sys/ubixfs/file.c @@ -166,6 +166,7 @@ if (tmpFd == fd) { ch = getFileByte(tmpFd,tmpFd->offset); tmpFd->offset++; +// kprintf("file byte [%c]",ch); return(ch); } } diff --git a/src/sys/ubixfs/ubixfs.c b/src/sys/ubixfs/ubixfs.c index 2f3d742..cfec9bd 100755 --- a/src/sys/ubixfs/ubixfs.c +++ b/src/sys/ubixfs/ubixfs.c @@ -29,34 +29,36 @@ #include #include -int startSector; +int startSector = 0; +int partitionSize = 0; +int rootDirectory = 0; +struct blockAllocationTableEntry *blockAllocationTable = 0x0; void initUbixFS() { struct bootSect *bs = 0x0; - char data[512]; - readBlock(0,data,1); - bs = (void *)data; + struct partitionInformation *pInfo = 0x0; + bs = (struct bootSect *)kmalloc(512); + pInfo = (struct partitionInformation *)kmalloc(512); + readBlock(0,(char *)bs,(long)1); if ((bs[0].id[0] == 'U') && (bs[0].id[1] == 'b') && (bs[0].id[2] == 'i') && (bs[0].id[3] == 'x') && (bs[0].id[4] == 'F') && (bs[0].id[5] == 'S')) { - startSector = bs[0].fsStart; + readBlock(bs->fsStart,(char *)pInfo,(long)1); + startSector = pInfo[0].blockAllocationTable; + partitionSize = pInfo[0].size; + rootDirectory = pInfo[0].rootDirectory; + blockAllocationTable = (struct blockAllocationTableEntry *)kmalloc(((rootDirectory-startSector)*512)); + readBlock(startSector,(char *)blockAllocationTable,(rootDirectory-startSector)); kprintf("UbixFS Initialized\n"); } else { + kfree(bs); + kfree(pInfo); kprintf("Insert System Disk\n"); initUbixFS(); } -/* fdTable = kmalloc(sizeof(struct fileDescriptor)); - tmpFdTable = fdTable; - for (i=0;istatus = fdAvail; - tmpFdTable->id = i; - if (i+1 == maxFd) { - tmpFdTable->next = 0x0; - } - else { - tmpFdTable->next = kmalloc(sizeof(struct fileDescriptor)); - } - tmpFdTable = tmpFdTable->next; - } */ + kfree(bs); + kfree(pInfo); + //Return + return; } int kstrcmp(char *str1, char *str2) { @@ -66,55 +68,72 @@ if(*str1 < *str2) return -1; return -1; } - +/* int readFile(char *file) { - struct fileTableEntry ft[1]; + struct directoryEntry file[1]; char data[2048]; -// findFile(file,ft); if (ft->start > 1000) { } else { readBlock(ft->start,data,ft->length); kprint(data); } return(0); - } + } */ -int getFileByte(fileDescriptor *fd,int offset) { - int ch=0,mp=0; - mp = offset/512; - if ((offset%512 == 0) && (fd->status == fdRead)) { - fd->status = fdOpen; + +/************************************************************************ + +Function: int getFileByte(fileDescriptor *fd,long offset); +Description: Return The Byte At Offset For File Descriptor +Notes: + +************************************************************************/ +int getFileByte(fileDescriptor *fd,long offset) { + int ch = 0x0,blockCount = 0x0,batIndex = fd->start,i = 0x0; + short bSect = 0x0; + blockCount = (offset/4096); + //Find Out Which File Block We Should Be In + for (i=1;i<=blockCount;i++) { + batIndex = blockAllocationTable[batIndex].nextBlock; } + //Calculate Which Block Sector We Should Load + bSect = ((offset - ((i-1)*4096))/512); + //Fix FD Status So We Dont Load A Sector Again + if ((offset%512 == 0) && (fd->status == fdRead)) { + fd->status = fdOpen; + } + //If The Offset Is Less Then The File Size Return The Char At It if (offset < fd->size) { + //If FD Status Is Not fdRead Then Load A Sector From Disk if (fd->status != fdRead) { - readBlock(fd->start+mp,fd->buffer,1); + readBlock(blockAllocationTable[batIndex].realSector+bSect,fd->buffer,1); fd->status = fdRead; } - ch = fd->buffer[offset-(mp*512)]; + //This Sets The Char To Return + ch = fd->buffer[offset-(bSect*512)]; } else { + //Set File EOF If There Is Nothing To Do ch = -1; fd->status = fdEof; } + //Return return(ch); } int findFile(char *file,fileDescriptor *fd) { int x=0; bool ret = TRUE; - struct fileTableEntry *ft; - char *data = kmalloc(4096); - ret = (bool)readBlock(startSector,data,8); - ft = (void *)data; - for (x=0;x<(4096/sizeof(struct fileTableEntry));x++) { - if ((int)!kstrcmp(ft[x].fileName,file)) { - fd->start = ft[x].start; - fd->length = ft[x].length; - fd->size = ft[x].size; - kfree(data); + struct directoryEntry *dirEntry = (struct directoryEntry *)kmalloc(4096); + ret = (bool)readBlock(rootDirectory,(char *)dirEntry,8); + for (x=0;x<(4096/sizeof(struct directoryEntry));x++) { + if ((int)!kstrcmp(dirEntry[x].fileName,file)) { + fd->start = dirEntry[x].startCluster; + fd->size = dirEntry[x].size; + kfree(dirEntry); return((int)1); } } - kfree(data); + kfree(dirEntry); return((int)0); } \ No newline at end of file diff --git a/src/tools/Makefile b/src/tools/Makefile index 6dd8ead..3b5fb7d 100755 --- a/src/tools/Makefile +++ b/src/tools/Makefile @@ -40,7 +40,7 @@ # Clean up the junk clean: - $(REMOVE) $(OBJS) $(BINARY) + $(REMOVE) $(OBJS) $(BINARY) *.core format-dsk: (cp ../bin/init/init ./) @@ -48,7 +48,7 @@ (cp ../bin/test/test ./) (cp ../bin/ls/ls ./) (cp ../bin/pwd/pwd ./) - (./format 101 init shell test ls pwd) + (./format 101 2000 init shell test ls pwd) (rm init) (rm shell) (rm test) diff --git a/src/tools/format.c b/src/tools/format.c index 29de328..9e291b4 100755 --- a/src/tools/format.c +++ b/src/tools/format.c @@ -1,80 +1,97 @@ -/************************************************************************************** - Copyright (c) 2002 The UbixOS Project - All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - -Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors. -Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors -in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its -contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED -WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - $Id$ - -**************************************************************************************/ - #include #include -#include "ubixfs.h" +#include "./ubixfs.h" -int main(int argc,char **argv) { - FILE *fd,*fd2; - int i=0,x=0,blocks=0,sb=9; - struct fileTableEntry ft[128]; - fd = fopen("/dev/fd0","wb"); - for (i=1;i<128;i++) { - ft[i].fileName[0] = '0'; - sprintf(ft[i].fileName,"File-%i",i); - } - fseek(fd,4096+(atoi(argv[1])*512),0); - sb = atoi(argv[1])+8; - for (i=2;isize = (size/512); + partInfo->startSector = (startSector+1); + partInfo->blockAllocationTable = (startSector+1); + partInfo->rootDirectory = ((startSector+1) + (batSize/512)); + fseek(driveFd,(startSector * 512),0); + fwrite(partInfo,512,1,driveFd); + startSector++; + BAT[0].nextBlock = 0x0; + BAT[0].attributes = 1; + BAT[0].realSector = (startSector + (batSize/512) + 1); + for (i=1;i<(size/4096);i++) { + BAT[i].nextBlock = 0x0; + BAT[i].attributes = 0x0; + BAT[i].realSector = (startSector + (batSize/512) + (i*8)); + BAT[i].reserved = 0x0; + } + file = 0; + while (x < argc) { + counter = 0x0; + blockCount = 0x0; + fileFd = fopen(argv[x],"rb"); + block = findFreeBlock(-1,(size/4096)); + dirEntry[file].startCluster = block; + rewind(driveFd); + //fseek(driveFd,((BAT[startSector].realSector * 512) + ((batSize/512)+(BAT[block].realSector*4096))),0); + fseek(driveFd,(BAT[block].realSector * 512),0); + while (!feof(fileFd)) { + if (4096 == (counter - (blockCount * 4096))) { + block = findFreeBlock(block,(size/4096)); + rewind(driveFd); + //fseek(driveFd,((startSector * 512) + ((batSize/512)+(BAT[block].realSector*4096))),0); + fseek(driveFd,(BAT[block].realSector * 512),0); + blockCount++; + } + fputc(fgetc(fileFd),driveFd); + counter++; } - sprintf(ft[i].fileName,"File-%i\0",i+1); - ft[i].start = i+1; - ft[i].length = i+2; - ft[i].attributes = i+3; - ft[i].reserved = i+4; -*/ + fclose(fileFd); + dirEntry[file].size = counter; + sprintf(dirEntry[file].fileName,"%s",argv[x]); + x++; + file++; + } + rewind(driveFd); + fseek(driveFd,(long)((startSector * 512) + batSize),0); + fwrite(dirEntry,4096,1,driveFd); + rewind(driveFd); + fseek(driveFd,(startSector * 512),0); + if (fwrite(BAT,batSize,1,driveFd) >= 1) { + printf("Formatted!\n"); + } + else { + printf("Error Formatting!!\n"); + } + fclose(driveFd); + return(0); + } diff --git a/src/tools/ubixfs.h b/src/tools/ubixfs.h index 2491412..cc34116 100755 --- a/src/tools/ubixfs.h +++ b/src/tools/ubixfs.h @@ -24,16 +24,41 @@ #ifndef _UBIXFS_H #define _UBIXFS_H -#include "../sys/include/types.h" +typedef unsigned long uLong; +typedef unsigned short uShort; +typedef unsigned char uChar; +typedef unsigned int uInt; -struct fileTableEntry { - uChar fileName[22]; - uShort start; - uShort length; - uLong size; - uShort attributes; +//Partition Information +struct partitionInformation { + uLong size; //Size In Sectors + uLong startSector; //Base Sector Of Partition + uLong blockAllocationTable; //Base Sector Of BAT + uLong rootDirectory; //Base Sector Of Root Directory }; +//Block Allocation Table Entry +struct blockAllocationTableEntry { + long attributes; //Block Attributes + long realSector; //Real Sector + long nextBlock; //Sector Of Next Block + long reserved; //Reserved + }; + +//UbixFS Directory Entry +struct directoryEntry { + uLong startCluster; //Starting Cluster Of File + uLong size; //Size Of File + uLong creationDate; //Date Created + uLong lastModified; //Date Last Modified + uLong uid; //UID Of Owner + uLong gid; //GID Of Owner + uShort attributes; //Files Attributes + uShort permissions; //Files Permissions + char fileName[256]; //File Name + }; + + struct bootSect { uChar jmp[4]; uChar id[6];