From d4b9c55000459a10d0f2a3a5f5340e7d8f372f11 Mon Sep 17 00:00:00 2001 From: aaron Date: Fri, 4 May 2018 21:50:09 +0000 Subject: [PATCH] git-svn-id: https://spexeah.com:8443/svn/Asuro@660 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c --- src/driver/storage/fat32.pas | 141 ++++++++++++++++++++--- src/driver/storage/storagemanagement.pas | 27 ++++- 2 files changed, 144 insertions(+), 24 deletions(-) diff --git a/src/driver/storage/fat32.pas b/src/driver/storage/fat32.pas index 63c1422b..a4fd5b03 100644 --- a/src/driver/storage/fat32.pas +++ b/src/driver/storage/fat32.pas @@ -62,8 +62,6 @@ type end; PBootRecord = ^TBootRecord; - byteArray8 = array[0..7] of char; - TDirectory = packed record fileName : array[0..7] of char; fileExtension : array[0..2] of char; @@ -141,13 +139,15 @@ var buffer : puint32; fatEntriesPerSector : uint32; sectorLocation : uint32; + dataStart : uint32; begin buffer:= puint32(kalloc(bootRecord^.sectorsize)); memset(uint32(buffer), 0, bootRecord^.sectorsize); fatEntriesPerSector:= bootRecord^.sectorsize div 4; - sectorLocation:= cluster div fatEntriesPerSector + (volume^.sectorStart + 1 + bootRecord^.rsvSectors); + sectorLocation:= cluster div fatEntriesPerSector; + dataStart:= (volume^.sectorStart + 1 + bootRecord^.rsvSectors); - volume^.device^.readcallback(volume^.device, sectorLocation, 1, buffer); + volume^.device^.readcallback(volume^.device, datastart + sectorLocation, 1, buffer); //console.writehexln(uint32(buffer[1])); //console.writeint(cluster); @@ -157,7 +157,12 @@ begin //console.writeint(fatEntriesPerSector); //console.writestringln(') '); console.redrawWindows(); - readFat:= buffer[cluster - ((cluster - 1) * fatEntriesPerSector)]; + readFat:= buffer[cluster - (sectorLocation * fatEntriesPerSector)]; + + //console.writehexlnWND(readFat(volume, 0, bootRecord) ,getTerminalHWND()); + //console.writehexlnWND(readFat(volume, 1, bootRecord) ,getTerminalHWND()); + //console.writehexlnWND(readFat(volume, 2, bootRecord) ,getTerminalHWND()); + //console.writehexlnWND(readFat(volume, 3, bootRecord) ,getTerminalHWND()); kfree(buffer); end; @@ -167,15 +172,17 @@ var buffer : puint32; fatEntriesPerSector : uint32; sectorLocation : uint32; + dataStart : uint32; begin buffer:= puint32(kalloc(bootRecord^.sectorsize)); memset(uint32(buffer), 0, bootRecord^.sectorsize); fatEntriesPerSector:= bootRecord^.sectorsize div 4; - sectorLocation:= cluster div fatEntriesPerSector + (volume^.sectorStart + 1 + bootRecord^.rsvSectors); + sectorLocation:= cluster div fatEntriesPerSector; + dataStart:= (volume^.sectorStart + 1 + bootRecord^.rsvSectors); - volume^.device^.readcallback(volume^.device, sectorLocation, 1, buffer); + volume^.device^.readcallback(volume^.device, dataStart + sectorLocation, 1, buffer); buffer[cluster - (sectorLocation * fatEntriesPerSector)]:= value; - volume^.device^.writecallback(volume^.device, sectorLocation, 1, buffer); + volume^.device^.writecallback(volume^.device, dataStart + sectorLocation, 1, buffer); kfree(buffer); end; @@ -314,12 +321,7 @@ begin directories:= getDirEntries(volume, bootRecord^.rootCluster, bootRecord); - if LL_size(directoryStrings) > 0 then begin - - console.writeintln(123123); - redrawWindows(); - for i:=0 to LL_Size(directoryStrings) - 1 do begin ii:=0; while true do begin @@ -340,9 +342,6 @@ begin directories:= getDirEntries(volume, cluster, bootRecord); end; end else begin - console.writeintln(LL_Size(directories)); //nneds to be fixed currently only 1 - redrawWindows(); - while true do begin if ii > LL_Size(directories) - 1 then break; dirEntry:= PDirectory(LL_Get(directories, ii)); @@ -356,6 +355,98 @@ begin kfree(puint32(bootRecord)); end; +procedure writeDirectory(volume : PStorage_volume; directory : pchar; dirName : pchar; attributes : uint32; status : puint32); // need to handle parent table cluster overflow, need to take attributes +var + directories : PLinkedListBase; + parentDirectory : PDirectory; + parentCluster : uint32; + clusters : PLinkedListBase; + cluster : uint32; + bootRecord : PBootRecord; + buffer : puint32; + bufferPointer : PDirectory; + dataStart : uint32; + EntriesPerSector : uint32; + sectorLocation : uint32; + dataOffset : uint32; + i : uint32; + + thisArray : byteArray8 = ('.',' ',' ',' ',' ',' ',' ',' '); + parentArray : byteArray8 = ('.','.',' ',' ',' ',' ',' ',' '); +begin + push_trace('fat32.writeDirectory'); + directories:= readDirectory(volume, directory, status); //TODO error check first + bootRecord:= readBootRecord(volume); + datastart:= volume^.sectorStart + 1 + bootRecord^.FATSize + bootRecord^.rsvSectors; + + parentDirectory:= PDirectory(LL_Get(directories, 0)); + parentCluster:= uint32(parentDirectory^.clusterlow); + + console.writeintlnWND(parentDirectory^.clusterlow, getTerminalHWND()); + console.writeintlnWND(parentDirectory^.clusterhigh, getTerminalHWND()); + console.writeintlnWND(parentCluster, getTerminalHWND()); + + clusters:= findFreeClusters(volume, 1, bootRecord); + cluster:= uint32(LL_Get(clusters, 0)^); + + if attributes = $10 then begin // if directory + buffer:= puint32(kalloc(bootRecord^.sectorSize)); + memset(uint32(buffer), 0, bootRecord^.sectorSize); + + bufferPointer:= @PDirectory(buffer)[0]; + bufferPointer^.fileName:= thisArray; //TODO implement time + bufferPointer^.attributes:= attributes; + bufferPointer^.clusterLow:= cluster; + bufferPointer^.clusterHigh:= (cluster shr 16) and $0000FFFF; + + bufferPointer:= @PDirectory(buffer)[1]; + bufferPointer^.fileName:= parentArray; //TODO implement time + bufferPointer^.attributes:= attributes; + bufferPointer^.clusterLow:= parentCluster; + bufferPointer^.clusterHigh:= (parentCluster shr 16) and $0000FFFF; + + //write to disk + volume^.device^.writecallback(volume^.device, dataStart + (cluster * bootRecord^.spc), 1, buffer); + + //write fat + writeFat(volume, cluster, $FFFFFFF8, bootRecord); //TODO check if working + end; + + memset(uint32(buffer), 0, bootRecord^.sectorSize); + //calculate write cluster using directories and parentCluster + sectorLocation:= LL_size(directories) * sizeof(PDirectory) div bootRecord^.sectorSize; + sectorLocation:= sectorLocation + (parentCluster * bootRecord^.spc); + //dataOffset:= datastart + ( (LL_size(directories) * sizeof(PDirectory)) - (sizeUsed * bootRecord^.sectorSize)); + volume^.device^.readcallback(volume^.device, dataStart + sectorLocation, 1, buffer); + + //construct my dir entry + bufferPointer:= @PDirectory(buffer)[LL_size(directories)]; + bufferPointer^.fileName:= pbytearray8(dirName)^; + bufferPointer^.attributes:= attributes; + bufferPointer^.clusterLow:= cluster; + bufferPointer^.clusterHigh:= (cluster shr 16) and $0000FFFF; + + + console.writestringlnWND(dirName, getTerminalHWND()); + printmemory(uint32(buffer),256,32,' ',true); + + console.writeintlnWND(dataStart, getTerminalHWND()); //parent cluster is 0 what the fuck + console.writeintlnWND(uint32(LL_size(directories)), getTerminalHWND()); + + for i:=0 to 4 do begin + console.writestringlnWND( PChar( PDirectory(LL_Get(directories, i))^.fileName) , getTerminalHWND()); + end; + + redrawWindows(); + + //write to disk + volume^.device^.writecallback(volume^.device, dataStart + sectorLocation, 1, buffer); + + kfree(buffer); + kfree(puint32(bootRecord)); + LL_Free(directories); +end; + procedure create_volume(disk : PStorage_Device; sectors : uint32; start : uint32; config : puint32); var @@ -371,6 +462,10 @@ var fatArray : byteArray8 = ('F','A','T','3','2',' ',' ',' '); thisArray : byteArray8 = ('.',' ',' ',' ',' ',' ',' ',' '); parentArray : byteArray8 = ('.','.',' ',' ',' ',' ',' ',' '); + + asuroFileArray : byteArray8 = ('A','S','U','R','O',' ',' ',' '); + mountFileArray : byteArray8 = ('M','O','U','N','T',' ',' ',' '); + programFileArray : byteArray8 = ('P','R','O','G','R','A','M','S'); rootCluster : uint32 = 1; begin push_trace('fat32.create_volume()'); @@ -444,10 +539,18 @@ begin PDirectory(buffer)[1].clusterLow := 1; //Temp for testing other functions - PDirectory(buffer)[2].fileName := fatArray; + PDirectory(buffer)[2].fileName := asuroFileArray; PDirectory(buffer)[2].attributes := $10; PDirectory(buffer)[2].clusterLow := 1; - // + + PDirectory(buffer)[3].fileName := mountFileArray; + PDirectory(buffer)[3].attributes := $10; + PDirectory(buffer)[3].clusterLow := 1; + + PDirectory(buffer)[4].fileName := programFileArray; + PDirectory(buffer)[4].attributes := $10; + PDirectory(buffer)[4].clusterLow := 1; + disk^.writecallback(disk, dataStart + (config^ * rootCluster), 1, buffer); @@ -492,8 +595,10 @@ begin push_trace('fat32.init()'); filesystem.sName:= 'FAT32'; filesystem.readDirCallback:= @readDirectory; + filesystem.createDirCallback:= @writeDirectory; filesystem.createcallback:= @create_volume; filesystem.detectcallback:= @detect_volumes; + storagemanagement.register_filesystem(@filesystem); end; diff --git a/src/driver/storage/storagemanagement.pas b/src/driver/storage/storagemanagement.pas index dda4f10f..102c4b55 100644 --- a/src/driver/storage/storagemanagement.pas +++ b/src/driver/storage/storagemanagement.pas @@ -28,15 +28,16 @@ type PStorage_volume = ^TStorage_Volume; PStorage_device = ^TStorage_Device; APStorage_Volume = array[0..10] of PStorage_volume; + byteArray8 = array[0..7] of char; + PByteArray8 = ^byteArray8; PPIOHook = procedure(volume : PStorage_volume; directory : pchar; byteCount : uint32; buffer : puint32); PPHIOHook = procedure(volume : PStorage_device; addr : uint32; sectors : uint32; buffer : puint32); PPCreateHook = procedure(disk : PStorage_Device; sectors : uint32; start : uint32; config : puint32); PPDetectHook = procedure(disk : PStorage_Device); - PPCreateDirHook = function(volume : PStorage_volume; directory : pchar; dirname : pchar; attributes : uint32) : uint8; + PPCreateDirHook = procedure(volume : PStorage_volume; directory : pchar; dirname : pchar; attributes : uint32; status : puint32); PPReadDirHook = function(volume : PStorage_volume; directory : pchar; status : puint32) : PLinkedListBase; //returns: 0 = success, 1 = dir not exsist, 2 = not directory, 3 = error //returns: 0 = success, 1 = dir not exsist, 2 = not directory, 3 = error - PPHIOHook_ = procedure; TFilesystem = record @@ -157,17 +158,29 @@ end; procedure mkdir_command(params : PParamList); var dir : pchar; + temp : pchar; + dirName : pbyteArray8; device : PStorage_Device; volume : PStorage_Volume; error : uint32; + i : uint32; begin + push_trace('mkdir'); if paramCount(params) > 0 then begin dir := getParam(0, params); + temp:= getParam(1, params); + + //for i:=0 to 7 do begin + // dirname[i]:= pbyteArray8(temp)[i]; + //end; + + //console.writestringlnWND(pchar(dirname), getTerminalHWND()); + device:= PStorage_Device(LL_Get(storageDevices, 0)); volume:= PStorage_Volume(LL_Get(device^.volumes, 0)); - //error:= volume^.filesystem^.createDirCallback(volume, dir, $10); - if error <> 1 then console.writestringln('ERROR'); + volume^.filesystem^.createDirCallback(volume, dir, temp, $10, @error); + //if error <> 1 then console.writestringln('ERROR'); end; end; @@ -205,15 +218,17 @@ begin device:= PStorage_Device(LL_Get(storageDevices, 0)); volume:= PStorage_Volume(LL_Get(device^.volumes, 0)); - dirs:= volume^.filesystem^.readDirCallback(volume, '', @error); + dirs:= volume^.filesystem^.readDirCallback(volume, dir, @error); //if error <> 1 then console.writestringln('ERROR'); for i:=2 to LL_Size(dirs) - 1 do begin - console.writestring(' '); + console.writestringWND(' /', getTerminalHWND); dirp:= PDirectory(LL_Get(dirs, i)); console.writestringlnWND(pchar(dirp^.fileName), getTerminalHWND); end; pop_trace(); + end else begin + console.writestringlnWND('Specifiy a folder', getTerminalHWND); end; end;