foreign content

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

foreign content

Post by digo_rp »

Hi all, does anyone knows what happens ? take a look at RootClusterNo 0DCCh all 512 bytes of contents of that cluster are all 0 inside...
almost all file that I do type file.ext. the second cluster read from fat when I read from disk the contents are 0...
then I do checksum of all bytes if it is 0 then I ignore and read next. I just show if the contents is != 0...
does anyone see that?
foreign content
foreign content
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: foreign content

Post by iansjack »

It's probably just me, but I can't make any sense of your post. Is it possible to rephrase it or perhaps explain in a little more detail what you are asking?
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

I´m so sorry I have a poor english, please forgive me...

Let try to explain.

I´m trying to create a fat16/32 read and write functions...

for now it´s working very well, but I saw that inside in a function like read_file_into_memory that error :

when I read cluster by cluster from the disk using a bios int 13h I got some cluster of 512 bytes with all those 512 bytes 0 zeros!
something like

cluster 1 <- I got from fat then call readLBA(); next readLBA... until end of file, right?

then I got...

example:

cluster 1 I read 1 sector with size of 512 bytes from disk using readlba...

cluster 1 valida data! 0129348--c94-1-038940 something like that for example!
cluster 2 no valid data 000000000000000000000000000000000000000000000000000000000000000000000000 just zeros! what is that! ???
cluster 3 valid data! 094395-230348558050-0-6-df0985934--sd0-384-0-96
cluster 4 valid data! ff930490-c-0kjh0598569066
...
until end of file...

I read step by step from microsoft fat103.pdf and I cannot figure out what happens...

one thing that I cannot understand is that like I ignore that zeroed cluster then continue with others one. and the file into memory is right ... byte by byte from the disk into memory.... it´s like one buffer that is not used, I don´t know...
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: foreign content

Post by iansjack »

cluster 1 <- I got from fat then call readLBA(); next readLBA... until end of file, right?
Well (perhaps this is what you mean?) you have to follow the chain of clusters from the File Allocation Table rather than just reading sequential clusters.
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

you right this is my code: Sorry it´s a little long:

Code: Select all

long cat_file(UCHAR Drive, UCHAR *procura) 
{
	USHORT	Index;
	ULONG	LBACluster;
	ULONG	NextCluster;

	UCHAR p[15];
	memset(&p, 0, sizeof(p));
	upper(&p, procura);	
	
	Index = 0;
	while (1) {
		if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
			break; // Encontramos o arquivo
		} else {
			Index++;
		}
			
		if (Index >= 1000) break;		
	}
	
	if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
		if (DirList[Index].is_dir == 1) return 0;
		RootClusterNo = DirList[Index].FirstClusterNo;
		LBACluster = getLBA(RootClusterNo);
		NextCluster = 0;
	} else {
		return -1; // Não encontrado!
	}

	UCHAR Buffer[512];
	ULONG C;
	C = 0;	
	ULONG CheckSum;
	USHORT i;
	
	while (NextCluster != 0x0FFFFFF8) {
		r = read_sectorLBA(Drive,  LBACluster, SectorsPerCluster, (UCHAR*)&Buffer);
		if (!r) {

			CheckSum = 0;
			
			for(i=0;i<512;i++) {
				CheckSum += Buffer[i];
			}
			
			if (CheckSum != 0) {
				for(i=0;i<512;i++) {
					//sprintf("%x ", Buffer[i]);
					//putch(Buffer[i]);
					CheckSum += Buffer[i];		
				}
			}
			kb_read();
			
			sprintf("\nRootClusterNo: %04Xh - checksum %08Xh", RootClusterNo, CheckSum);
			
			C += 512;
			
			if (RootClusterNo != 0xFFFFFFF) // Ultimo cluster, nada mais a ser lido do disco.
			{
				RootClusterNo = getFATEntrie(Drive, RootClusterNo);
				if (RootClusterNo != 0x0FFFFFF8)
				LBACluster = getLBA(NextCluster);
				NextCluster = RootClusterNo;
			}
			else
			{
				break;
			}		
		
		} else {
			sprintf("\nERRO: read_sectorLBA 0x%02X\nPressione qualquer tecla para continuar...", r);
			//readkey();
			puts("\n");
			break;
		}
	}
	return C;
}
Last edited by Candy on Fri Mar 13, 2015 2:48 am, edited 1 time in total.
Reason: Added code tags
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: foreign content

Post by Combuster »

174 posts and still no code tags?

And by the looks of it, the most essential code hasn't been posted nor debugged.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4707
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: foreign content

Post by iansjack »

It is difficult to read the code without it being formatted, but it does appear to me - at a first glance - that you are allocating a Buffer of 512 bytes and then reading into it 512 * (number of sectors per cluster) into it. I'm making some assumptions here about what read_sectorLBA() does, particularly the third parameter. Either that or you are only reading the first sector of every cluster. Everywhere there does seem to be a confusion between the size of a sector and the size of a cluster.

What I would suggest that you do is to run the code under a debugger; step through it, inspecting the variables, and it should become obvious what is happening.
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

I found one odd thing... on second time that we read the second entry from fat I got this cluster number 0x80F7 in all files that I´m reading the contents...

it´s crazy... I´m trying to find out what this number means... 0x80F7??? I never see that before... I now that 0xfff7 is a bad block mark, but that 0x80F7 not...

I´m still looking for fix it ...

tnx so much...

please forgive me my mistakes... I now its a stupid question... but I don´t know what happens to me... all the time I make the same mistakes... sorry really so sorry..

again tnx so much all
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

combuster what is that? code tags?
174 posts and still no code tags?
could you please help me?
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

Problem solved!

Thanx so much.

Sorry for my long code. I´ll post my fat.c it is at begnning but I would like to share...

Code: Select all

#include <defines.h>
#include <fat.h>
#include <x86.h>
#include <cpux86.h>
#include <main.h>
#include <ide.h>

UCHAR *PartType[] = {"00",
                     "01 FAT12",
                     "02 XENIX root",
                     "03 XENIX usr",
                     "04 FAT16 <32M",
                     "05 Extended",
                     "06 FAT16",
                     "07 HPFS/NTFS",
                     "08 AIX",
                     "09 AIX bootable",
                     "0A OS/2 Boot Manager",
                     "0B WIN95 OSR2 FAT32",
                     "0C WIN95 OSR2 FAT32, LBA-mapped",
					 "0D SILICON SAFE",
                     "0E WIN95: DOS 16-bit FAT, LBA-mapped",
                     "0F WIN95: Extended partition, LBA-mapped",
                     "10 OPUS",
                     "11 Hidden DOS 12-bit FAT",
                     "12 Compaq diagnost",
                     "13",
                     "14 Hidden DOS 16-bit FAT <32M",
                     "15",
                     "16 Hidden DOS 16-bit FAT >=32M",
                     "17 Hidden IFS (e.g., HPFS)",
                     "18 AST SmartSleep Partition",
                     "19", "1A",
                     "1B Hidden WIN95 OSR2 FAT32",
                     "1C Hidden WIN95 OSR2 FAT32, LBA-mapped",
                     "1D",
                     "1E Hidden WIN95 16-bit FAT, LBA-mapped",
                     "1F", "20", "21", "22", "23",
                     "24 NEC DOS",
                     "25", "26", 
					 "27 Windows RE hidden partition", 
					 "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", "30", "31", "32", "33", "34", "35", "36", "37", "38",
                     "39 Plan 9",
                     "3A", "3B",
                     "3C PartitionMagic",
                     "3D", "3E", "3F",
                     "40 Venix 80286",
                     "41 PPC PReP Boot",
                     "42 SFS (Secure Filesystem)",
                     "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C",
                     "4D QNX4.x",
                     "4E QNX4.x 2nd part",
                     "4F QNX4.x 3rd part",
                     "50 OnTrack DM",
                     "51 OnTrack DM6 Aux",
                     "52 CP/M",
                     "53 OnTrack DM6 Aux",
                     "54 OnTrackDM6",
                     "55 EZ-Drive",
                     "56 Golden Bow",
                     "57", "58", "59", "5A", "5B",
                     "5C Priam Edisk",
                     "5D", "5E", "5F", "60",
                     "61 SpeedStor",
                     "62",
                     "63 GNU HURD or Sys",
                     "64 Novell Netware",
                     "65 Novell Netware",
                     "66", "67", "68", "69", "6A", "6B", "6C", "6D", "6E", "6F",
                     "70 DiskSecure Mult",
                     "71", "72", "73", "74",
                     "75 PC/IX",
                     "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F",
                     "80 Old Minix",
                     "81 Minix / old Lin",
                     "82 Linux swap / So",
                     "83 Linux",
                     "84 OS/2 hidden C:",
                     "85 Linux extended",
                     "86 NTFS volume set",
                     "87 NTFS volume set",
                     "88 Linux plein tex",
                     "89", "8A", "8B", "8C", "8D",
                     "8E Linux LVM",
                     "8F", "90", "91", "92",
                     "93 Amoeba",
                     "94 Amoeba BBT",
                     "95", "96", "97", "98", "99", "9A", "9B", "9C", "9D", "9E",
                     "9F BSD/OS",
                     "A0 IBM Thinkpad hi",
                     "A1", "a2", "a3", "a4",
                     "A5 FreeBSD",
                     "A6 OpenBSD",
                     "A7 NeXTSTEP",
                     "A8 UFS Darwin",
                     "A9 NetBSD",
                     "AA",
                     "AB Amorce Darwin",
                     "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6",
                     "B7 BSDI fs",
                     "B8 BSDI swap",
                     "B9", "ba",
                     "BB Boot Wizard hid",
                     "BC", "BD",
                     "BE Amorce Solaris",
                     "BF Solaris",
                     "C0",
                     "C1 DRDOS/sec (FAT-",
                     "C2", "C3",
                     "C4 DRDOS/sec (FAT-",
                     "C5",
                     "C6 DRDOS/sec (FAT-",
                     "C7 Syrinx",
                     "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9",
                     "DA Non-FS data",
                     "DB CP/M / CTOS / .",
                     "DC", "dd",
                     "DE Dell Utility",
                     "DF BootIt",
                     "E0",
                     "E1 DOS access",
                     "E2",
                     "E3 DOS R/O",
                     "E4 SpeedStor",
                     "E5", "E6", "E7", "E8", "E9", "EA",
                     "EB BeOS fs",
                     "EC", "ED",
                     "EE GPT",
                     "EF EFI (FAT-12/16/",
                     "F0 Linux/PA-RISC b",
                     "F1 SpeedStor",
                     "F2 DOS secondary",
                     "F3",
                     "F4 SpeedStor",
                     "F5", "F6", "F7", "F8", "F9", "FA",
                     "FB VMware VMFS",
                     "FC VMware VMKCORE",
                     "FD Linux raid auto",
                     "FE LANstep",
                     "FF BBT"};

tFATBootSector		bpb;
tFATBootSector		bpb2;
ULONG 				RootDirSectors;
ULONG				FirstDataSector;
ULONG				FirstSectorofCluster;
ULONG				DataSec;
ULONG				TotSec;
ULONG				CountofClusters;
ULONG				ThisFATSecNum;
ULONG				ThisFATEntOffset;
ULONG				FirstRootDirSecNum;
union				REGS regsin;
union				REGS regsout;
USHORT				CylinderInicial;
USHORT				CylinderFinal;
UCHAR				HeadInicial;
UCHAR				HeadFinal;
UCHAR				SectorInicial;
UCHAR				SectorFinal;
UCHAR				VolumeLabel[12];
UCHAR				FileSystemType[9];
ULONG				RootEntriesCount;
USHORT				BytesPerSector;
ULONG				ReservedSectorsCount;
UCHAR				SectorsPerCluster;
ULONG				FATSz;
USHORT				NumFATs;
ULONG				TotSec;
USHORT				cyls;
UCHAR				heads;
UCHAR				secs;
UCHAR				FATType;
ULONG				FATOffset;
USHORT				FAT16ClusEntryVal;
ULONG				FAT32ClusEntryVal;
UCHAR				X;
ULONG				RootClusterNo;
ULONG				Cluster;
ULONG				NextCluster;	
THDPARAM			hdparam;

TDIR				dir[128];

UCHAR				buff01[512];
UCHAR				buff02[512];
UCHAR				SecBuff[512];
short				r;

TPART				*tpart1;
TPART				*tpart2;
TPART				*tpart3;
TPART				*tpart4;

TDIRStruct			DirStruct[256];
TLONGDIRNameStruct	LongDirStruct;
UCHAR				BootDrive;
TDIRLIST			DirList[1000];
UCHAR				Diretorio_Atual[12] = {"/"};

short read_sector(USHORT cylinder, UCHAR head, UCHAR sector, UCHAR drive, UCHAR* buff)
{
	memset(&regsin, 0, sizeof(union REGS));
	memset(&regsout, 0, sizeof(union REGS));	
	
	UCHAR c = (cylinder & 255);
	UCHAR h = head;
	UCHAR s = ((cylinder & 768) >> 8) | (sector & 63);

	int attempts = 5;
	while (attempts--) {
		regsin.h.ah = 0x02;
		regsin.h.al = 1;
		regsin.h.ch = c;
		regsin.h.cl = s; 
		regsin.h.dh = h;
		regsin.h.dl = drive;
		regsin.x.bx = 0x0000; // 0x0500;
		regsin.d.es = 0x07E0;
		regsin.x.bp = 0xffff;
		regsin.d.cs = regsin.d.ds = regsin.d.fs = regsin.d.gs = regsin.d.ss = 0x9000;
		int86x (0x13, &regsin, &regsout);

		if (regsout.h.ah != 0 && regsout.h.ah != 11)
			hd_reset(drive);
		
		if (regsout.h.ah == 0) break;
	}
	
	if (regsout.h.ah == 0 && regsout.h.al == 1) {
		UCHAR *buffer = (void*)0x7E00;//0x500;
		memset(buff, 0, 512);
		memcpy(buff, buffer, 512);
		return regsout.h.ah;
	} else {
		return regsout.h.ah;
	}
}

short read_sectorLBA(UCHAR Disk, ULONG LBA, USHORT Setores, UCHAR* Buff) {
	
	USHORT I;
	ULONG CheckSum;
	UCHAR BUFF[512];
	
	memset(&regsin, 0, sizeof(union REGS));
	memset(&regsout, 0, sizeof(union REGS));
	
	TDAPACK tdapack;

	memset(&tdapack, 0, sizeof(TDAPACK));
	
	tdapack.Size     = sizeof(TDAPACK);              // Size of packet (16 bytes)
	tdapack.Reserved = 0;                            // Always 0
	tdapack.Sectors  = Setores;                      // Number of sectors to transfer (max 127 on some BIOSes)
	tdapack.Buffer   = FP_TO_LINEAR(0x0800, 0x0000); // Address of Buff
	tdapack.LoLBA    = LBA;                          // LBA 00..31
	tdapack.HiLBA    = 0;	                         // LBA 32..63
	
	memcpy(0x7E00, &tdapack, sizeof(TDAPACK));

	int attempts = 5;

	while (attempts--) {
		regsin.h.ah  = 0x42;
		regsin.h.dl  = Disk;
		regsin.x.si  = 0x0000; //0x0500;
		regsin.d.ds  = 0x07E0; // 0x0000;
		regsin.x.bp  = 0xffff;
		regsin.d.cs  = regsin.d.es = regsin.d.fs = regsin.d.gs = regsin.d.ss = 0x0900;
		                                                                         
		int86x (0x13, &regsin, &regsout);
		
		if (regsout.h.ah != 0 && regsout.h.ah != 0x11)
			hd_reset(Disk);
		
		if (regsout.h.ah == 0) {
			memset(&BUFF, 0, 512);
			CheckSum = 0;
			memcpy(&BUFF, 0x8000, 512 * Setores);
			
			for (I=0;I<512;I++) {
				CheckSum += BUFF[I];
			}
			
			if (CheckSum == 0) break;
		}
	}

	if (regsout.h.ah == 0) {
		memset(Buff, 0, 512 * Setores);
		memcpy(Buff, 0x8000, 512 * Setores);
	}
	
	return regsout.h.ah;
}

short read_sectorsLBA(UCHAR Disk, ULONG LBA, USHORT Setores, UCHAR* Buff) {
	USHORT SectorsToRead = 0;
	USHORT PlusOffset    = 0;
	while (SectorsToRead < Setores)	{
		read_sectorLBA(Disk, LBA+SectorsToRead, Setores, Buff+PlusOffset);
		PlusOffset    += 512;
		SectorsToRead += 1;
	}
}

void BIOS_LBA2CHS (tFATBPB* pBPB, ULONG LBA, USHORT* pCylinder, UCHAR* pHead, UCHAR* pSector)
{
	ULONG tmp = LBA / pBPB->BPB1.SectorsPerTrack;
	*pSector  = (LBA % pBPB->BPB1.SectorsPerTrack) + 1;
	*pCylinder = tmp / pBPB->BPB1.HeadsPerCylinder;
	*pHead     = tmp % pBPB->BPB1.HeadsPerCylinder;
}

char hdtable_init(UCHAR drive)
{
	memset(&regsin, 0, sizeof(union REGS));
	memset(&regsout, 0, sizeof(union REGS));	

	regsin.h.al = 0x09;
	regsin.h.dl = drive;
	regsin.x.bp = 0xffff;
	regsin.d.cs = regsin.d.es = regsin.d.ds = regsin.d.fs = regsin.d.gs = regsin.d.ss = 0x9000;
	int86x (0x13, &regsin, &regsout);
	
	return regsout.h.ah;
}

int hd_reset(UCHAR Drive) 
{
	union REGS inregs, outregs;

	inregs.h.ah = 0x00;
	inregs.h.dl = Drive;
	inregs.x.bp = 0xffff;
	inregs.d.cs  = inregs.d.ds = inregs.d.fs = inregs.d.gs = inregs.d.ss = 0x9000;
	int86x (0x13, &inregs, &outregs);

	return !outregs.x.eflags;
}

void showPartitions(char Show) {
	if (Show) {
		sprintf("Particao=01, drive=0x%02X, tipo=0x%02X - %s\n", tpart1->MarcadeInicializacao, tpart1->TipodaParticao, PartType[tpart1->TipodaParticao]);
		sprintf("Particao=02, drive=0x%02X, tipo=0x%02X - %s\n", tpart2->MarcadeInicializacao, tpart2->TipodaParticao, PartType[tpart2->TipodaParticao]);
		sprintf("Particao=03, drive=0x%02X, tipo=0x%02X - %s\n", tpart3->MarcadeInicializacao, tpart3->TipodaParticao, PartType[tpart3->TipodaParticao]);
		sprintf("Particao=04, drive=0x%02X, tipo=0x%02X - %s\n", tpart4->MarcadeInicializacao, tpart4->TipodaParticao, PartType[tpart4->TipodaParticao]);
	}
}

void showBPBParams01(char Show)
{
	if (Show)
	{
		printf("Bytes per Sector %d\n", BytesPerSector);
		printf("Sectors per Cluster %d\n", SectorsPerCluster);
		printf("Number of reserved sectors %d\n", ReservedSectorsCount);
		printf("Number of FATS %d\n", NumFATs);
		printf("Root entries Count %d\n", RootEntriesCount);
	}
}

void showBPBParams02(char Show)
{
	if (Show)
	{
		printf("RootDirSectors.........: %d\n", RootDirSectors);
		printf("FirstDataSector........: %d\n", FirstDataSector);
		printf("FirstSectorofCluster...: %d\n", FirstSectorofCluster);
		printf("TotSec.................: %d\n", TotSec);
		printf("DataSec................: %d\n", DataSec);
		printf("CountofClusters........: %d\n", CountofClusters);
		printf("FirstRootDirSecNum.....: %d\n", FirstRootDirSecNum);
		printf("ThisFATSecNum..........: %d\n", ThisFATSecNum);
		printf("ThisFATEntOffset.......: %d\n", ThisFATEntOffset);
		printf("RootDirectoryClusterNo.: %d\n", bpb.BPB.BPB2.FAT32.RootDirectoryClusterNo);
	}
}

char read_bpb(UCHAR drive) 
{
	hdtable_init(drive);
	get_hd_param(drive, &hdparam);
	getPartitions();
	r = read_sector(0, 0, 1, drive, &buff01);
	if (!r)
	{
		HeadInicial     = tpart1->HeadInicial;
		SectorInicial   = (tpart1->SectorInicial & 63);
		CylinderInicial = ((tpart1->SectorInicial & 192) << 2) | tpart1->CylinderInicial;
		
		HeadFinal       = tpart1->HeadFinal;
		SectorFinal     = (tpart1->SectorFinal & 63);
		CylinderFinal   = ((tpart1->SectorFinal & 192) << 2) | tpart1->CylinderFinal;
		
		ULONG LBA       = ((CylinderInicial*heads) + HeadInicial) * (secs+(SectorInicial-1));
		
		showPartitions(0);
		
		//r = read_sectorLBA(drive, tpart1->LBAInicial, 1, &bpb);
		r = read_sectorLBA(drive, tpart1->LBAInicial, 1, &bpb);
		if (!r)
		{
			memset(VolumeLabel, 0, 12);
			memcpy(VolumeLabel, bpb.BPB.BPB2.FAT32.VolumeLabel, 11);
			VolumeLabel[11] = '\0';
	
			memset(FileSystemType, 0, 9);
			memcpy(FileSystemType, bpb.BPB.BPB2.FAT32.FileSystemName, 8);
			FileSystemType[8]    = '\0';
			
			RootEntriesCount     = bpb.BPB.BPB1.RootEntriesCount;
			BytesPerSector       = bpb.BPB.BPB1.BytesPerSector;
			ReservedSectorsCount = bpb.BPB.BPB1.ReservedSectorsCount;
			NumFATs              = bpb.BPB.BPB1.NumberOfFATs;
			SectorsPerCluster    = bpb.BPB.BPB1.SectorsPerCluster;

			showBPBParams01(0);
				
			if (bpb.BPB.BPB1.SectorsPerFAT1x != 0)
				FATSz = bpb.BPB.BPB1.SectorsPerFAT1x;
			else
				FATSz = bpb.BPB.BPB2.FAT32.SectorsPerFAT32;

			if (bpb.BPB.BPB1.TotalSectorsCount16 != 0)
				TotSec = bpb.BPB.BPB1.TotalSectorsCount16;
			else
				TotSec = bpb.BPB.BPB1.TotalSectorsCount32;

			RootClusterNo = bpb.BPB.BPB2.FAT32.RootDirectoryClusterNo;
			RootDirSectors = ((RootEntriesCount * 32) + (BytesPerSector - 1)) / BytesPerSector;
			DataSec = TotSec - (ReservedSectorsCount + (NumFATs  * FATSz) + RootDirSectors);
			CountofClusters = DataSec / SectorsPerCluster;
			getFATType();
			
			// Note that on a FAT32 volume, the BPB_RootEntCnt value is always 0; so on a FAT32 volume,
			// RootDirSectors is always 0.
			
			if (FATType == fat32)
			{
				RootEntriesCount = 0;
				RootDirSectors = 0;
			}
		}
	}
	
}

void getPartitions(void) {
	tpart1 = (void*)&buff01+0x1BE;
	tpart2 = (void*)&buff01+0x1CE;
	tpart3 = (void*)&buff01+0x1DE;
	tpart4 = (void*)&buff01+0x1EE;
}

char get_hd_param(UCHAR drive, THDPARAM *hdparam)
{
	memset(&regsin, 0, sizeof(union REGS));
	memset(&regsout, 0, sizeof(union REGS));
	
	regsin.h.ah = 0x48;
	regsin.h.dl = drive;
	regsin.x.si = 0x0000; //0x0500;
	regsin.d.ds = 0x07E0;
	regsin.x.bp = 0xffff;
	regsin.d.cs = regsin.d.es = regsin.d.fs = regsin.d.gs = regsin.d.ss = 0x9000;
	int86x (0x13, &regsin, &regsout);

	if (regsout.h.ah == 0)
	{
		THDPARAM * hdpar = (THDPARAM *)0x7E00;//0x0500;
		memcpy(hdparam, hdpar, hdpar->BufferSize);
		cyls  = hdparam->Cylinders;
		heads = hdparam->Heads;
		secs  = hdparam->SectorsPerTrack;
	}

	return regsout.h.ah;
}

UCHAR *get_dir_name(UCHAR *dnEntrie, UCHAR *filename)
{
	UCHAR n = 0;
	memset(filename, 0, 12);

	for (UCHAR i = 0; i < 11; i++)
	{
		if (i == 8 && dnEntrie[i+1] != 0 && dnEntrie[i] != ' ')
			filename[n++] = '.';
			
		if (dnEntrie[i] != 0 && dnEntrie[i] != ' ')
			filename[n++] = dnEntrie[i];
	}
	filename[n] = '\0';
}

UCHAR getFATType(void)
{
	if (CountofClusters < 4085) {
		FATType = fat12;
	} else if(CountofClusters < 65525) {
		FATType = fat16;
	} else {
		FATType = fat32;
	}
}

ULONG getLBA(ULONG cluster)
{
	RootDirSectors = ((RootEntriesCount * 32) + (BytesPerSector - 1)) / BytesPerSector;
	FirstDataSector = ReservedSectorsCount + (NumFATs * FATSz) + RootDirSectors;
	FirstSectorofCluster = ((cluster-2) * SectorsPerCluster) + FirstDataSector;
	return tpart1->LBAInicial+FirstSectorofCluster;
}

ULONG getFATEntrie(UCHAR Drive, ULONG Cluster)
{
	if (FATType == 16)
		FATOffset = Cluster * 2;
	else if (FATType == 32)
		FATOffset = Cluster * 4;

	ThisFATSecNum    = ReservedSectorsCount + (FATOffset / BytesPerSector);
	ThisFATEntOffset = (FATOffset % BytesPerSector);
	ThisFATSecNum   += tpart1->LBAInicial; // <- Relativo ao inicio da Partição!
	
	/*sprintf("\nThisFATSecNum %d  NumberOfFATs %d  SectorsPerFAT %d  FATSz %d\n",
			ThisFATSecNum,
			bpb.BPB.BPB1.NumberOfFATs,
			bpb.BPB.BPB2.FAT32.SectorsPerFAT32,
			FATSz);
	*/
	//r = read_sectorLBA(Drive, ThisFATSecNum, 1, (UCHAR*)&SecBuff);
	r = read_sectorLBA(Drive, ThisFATSecNum, 1, SecBuff);
	if (!r)
	{
		if (FATType == 16)
			FAT16ClusEntryVal = *((USHORT *) &SecBuff[ThisFATEntOffset]);
		else
			FAT32ClusEntryVal = (*((ULONG *) &SecBuff[ThisFATEntOffset])) & 0x0FFFFFFF;			
	}
	else
	{
		sprintf("Erro ao ler FAT, codigo de erro: 0x%02X\n", r);
		return -1;
	}
	
	return FAT32ClusEntryVal;
}

fprintf(ULONG n)
{
	USHORT n1 = n % 1000;
	USHORT n2 = (n % 1000000) / 1000;
	USHORT n3 = n / 1000000;
	
	if (n3 > 0)
		printf("%d.", n3);
	
	if (n2 > 0)
		printf("%d.", n2);
		
	printf("%d", n1);
}

char list_dir(UCHAR Drive, UCHAR *parametros) {
	printf("O Volume na Unidade e %s\n", VolumeLabel);
	printf("O Numero de Serie da unidade e %x-%x\n", ((bpb.BPB.BPB2.FAT32.VolumeSerialNumber & 0xFFFF0000) >> 16), ((bpb.BPB.BPB2.FAT32.VolumeSerialNumber & 0xFFFF)));
	printf("O Tipo do sistema de arquivos e %s\n", FileSystemType);

	USHORT i;
	USHORT Arquivos;
	USHORT Pastas;
	ULONG TotalUtilizado;
	ULONG TotalDisponivel;
	USHORT l;
	
	i = 0;
	l = 0;
	Arquivos = 0;
	Pastas = 0;
	TotalUtilizado = 0;
	TotalDisponivel = CountofClusters * SectorsPerCluster * BytesPerSector;
	while (DirList[i].in_use == 1) {
			if (DirList[i].is_dir == 1) {
				if (DirList[i].ShortName[0] != 0) {
					sprintf("\n%12s	<DIR>", DirList[i].ShortName);
					
					sprintf("	%02d/%02d/%04d %02d:%02d", 
					DirList[i].Days,
					DirList[i].Months, 
					DirList[i].Years,
					DirList[i].Hours,
					DirList[i].Minutes,
					DirList[i].Seconds);
					Pastas++;
				}
			} else {
				if (DirList[i].ShortName[0] != 0) {
					sprintf("\n%12s", DirList[i].ShortName);

					sprintf("		%02d/%02d/%04d %02d:%02d", 
					DirList[i].Days,
					DirList[i].Months, 
					DirList[i].Years,
					DirList[i].Hours,
					DirList[i].Minutes,
					DirList[i].Seconds);
				
					sprintf(" %f", DirList[i].FileSize);
					TotalUtilizado += DirList[i].FileSize;
					Arquivos++;
				}
			}
		i++;
		l++;
		if (i >= 1000) break;
		if (strcmp((UCHAR*)parametros, "|more") == 0) {
			if (l == 23) {
				l = 0;
				puts("\nPressione qualquer tecla para continuar...");
				//readkey();
				puts("\n");
			}
		}
	}
	sprintf("\n		%d Arquivo(s)	%f bytes\n		%d Pastas	%f bytes disponiveis\n", Arquivos, TotalUtilizado, Pastas, TotalDisponivel-TotalUtilizado);
}

void Setup_FAT(UCHAR drive) {
	read_bpb(drive);
	BootDrive = drive;
	read_root();
}

char read_root(void) {
	change_dir("/"); // Iremos ler o diretório principal!
}

char change_dir(UCHAR *procura) {
	USHORT	Index;
	ULONG	LBACluster;
	ULONG	NextCluster;

	UCHAR p[15];
	memset(&p, 0, sizeof(p));
	upper(&p, procura);	

	Index = 0;
	if (strcmp((UCHAR*)p, "/") == 0) {
		RootClusterNo = bpb.BPB.BPB2.FAT32.RootDirectoryClusterNo;
		LBACluster = getLBA(RootClusterNo);
		NextCluster = 0;
	} else {
		Index = 0;
		while (1) {
			if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
				break; // Encontramos o diretório
			} else {
				Index++;
			}
			
			if (Index >= 1000) break;		
		}
	
		if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
			if (DirList[Index].FirstClusterNo == 0) {
				RootClusterNo = bpb.BPB.BPB2.FAT32.RootDirectoryClusterNo;
			} else {
				RootClusterNo = DirList[Index].FirstClusterNo;
			}
			LBACluster = getLBA(RootClusterNo);
			NextCluster = 0;
		} else {
			return -1;
		}
	}
	
	Index = 0;
	memset(&DirList, 0, sizeof(DirList));
	while (RootClusterNo != 0x0FFFFFF8)
	{
		memset((UCHAR*)&DirStruct, 0, sizeof(DirStruct));
		
//		r = read_sectorLBA(BootDrive,  LBACluster, SectorsPerCluster, (UCHAR*)&DirStruct);
		r = read_sectorLBA(BootDrive,  LBACluster, SectorsPerCluster, DirStruct);
		
		if (!r)
		{	
			UCHAR ShortName[12];
			UCHAR LongName[260];
			UCHAR LngEnt = 0;
			UCHAR n = 0;
			
			memset(&LongName, 0, sizeof(LongName));
			
			for (UCHAR i=0; i<128; i++)
			{

				if (DirStruct[i].DIR_Attr == 0x08) {
					get_dir_name(DirStruct[i].DIR_Name, ShortName);
					memcpy(VolumeLabel, ShortName, sizeof(ShortName));
				}
				
				if ((DirStruct[i].DIR_Name[0] != 0xE5) &&
				    (DirStruct[i].DIR_Attr != 0x08)    &&
					(DirStruct[i].DIR_Attr != 0x0F)    &&
					(DirStruct[i].DIR_Attr != 0x0))
				{
					get_dir_name(DirStruct[i].DIR_Name, ShortName);
						
					if (DirStruct[i].DIR_Attr & 0x10) 
					{
						memcpy(DirList[Index].ShortName, ShortName, sizeof(ShortName));

						DirList[Index].Attrib			= DirStruct[i].DIR_Attr;						
						DirList[Index].Days				= DirStruct[i].CrtDate.Days;
						DirList[Index].Months			= DirStruct[i].CrtDate.Months;
						DirList[Index].Years			= DirStruct[i].CrtDate.Years + 1980;
						DirList[Index].Hours			= DirStruct[i].CrtTime.Hours;
						DirList[Index].Minutes			= DirStruct[i].CrtTime.Minutes;
						DirList[Index].Seconds			= DirStruct[i].CrtTime.Seconds; 
						DirList[Index].FileSize			= DirStruct[i].DIR_FileSize;
						DirList[Index].FirstClusterNo	= ((DirStruct[i].DIR_FstClusHI << 16) | DirStruct[i].DIR_FstClusLO);
						DirList[Index].is_dir			= 1;	
						DirList[Index].in_use			= 1;
						Index++;
					}
					else
					{
						memcpy(DirList[Index].ShortName, ShortName, sizeof(ShortName));

						DirList[Index].Attrib			= DirStruct[i].DIR_Attr;						
						DirList[Index].Days    			= DirStruct[i].CrtDate.Days;
						DirList[Index].Months			= DirStruct[i].CrtDate.Months;
						DirList[Index].Years			= DirStruct[i].CrtDate.Years + 1980;
						DirList[Index].Hours			= DirStruct[i].CrtTime.Hours;
						DirList[Index].Minutes			= DirStruct[i].CrtTime.Minutes;
						DirList[Index].Seconds			= DirStruct[i].CrtTime.Seconds; 
						DirList[Index].FileSize			= DirStruct[i].DIR_FileSize;
						DirList[Index].FirstClusterNo	= ((DirStruct[i].DIR_FstClusHI << 16) | DirStruct[i].DIR_FstClusLO);
						DirList[Index].is_dir			= 0;	
						DirList[Index].in_use			= 1;

						Index++;
					}				
				}
			}

			if (RootClusterNo != 0xFFFFFFF) // Ultimo cluster, nada mais a ser lido do disco.
			{
				RootClusterNo = getFATEntrie(BootDrive, RootClusterNo);
				if (RootClusterNo != 0x0FFFFFF8)
				LBACluster = getLBA(NextCluster);
				NextCluster = RootClusterNo;
			}
			else
			{
				break;
			}
			
		} else {
			sprintf("\nERRO: read_sectorLBA 0x%02X\nPressione qualquer tecla para continuar...", r);
			//readkey();
			puts("\n");
			break;
		}
	}
	return 1;
}

char find_file(UCHAR *nome) 
{

} 

char find_attr(UCHAR attr) 
{

}

char set_label(UCHAR *nome) 
{

}

char rename_file(UCHAR *dest, UCHAR *src) 
{

}

char delete_file(UCHAR *nome) 
{

}

char mkdir(UCHAR *nome) 
{

}

long cat_file(UCHAR Drive, UCHAR *procura) 
{
	USHORT	Index;
	ULONG	LBACluster;
	ULONG	NextCluster;

	UCHAR p[15];
	memset(&p, 0, sizeof(p));
	upper(&p, procura);	
	
	Index = 0;
	while (1) {
		if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
			break; // Encontramos o arquivo
		} else {
			Index++;
		}
			
		if (Index >= 1000) break;		
	}
	
	if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
		if (DirList[Index].is_dir == 1) return 0;
		RootClusterNo = DirList[Index].FirstClusterNo;
		LBACluster    = getLBA(RootClusterNo);
		NextCluster   = 0;
	} else {
		return -1; // Não encontrado!
	}

	UCHAR Buffer[512];
	ULONG C;
	C = 0;	
	ULONG CheckSum;
	USHORT i;
	
	while (NextCluster != 0x0FFFFFF8) 
	{

		r = read_sectorLBA(Drive,  LBACluster, SectorsPerCluster, Buffer);

		if (!r)
		{
			for(i = 0; i < 512; i++)
			{
				putch(Buffer[i]);
			}
	
			C += 512;
			
			if (RootClusterNo != 0xFFFFFFF) // Ultimo cluster, nada mais a ser lido do disco.
			{
				RootClusterNo = getFATEntrie(Drive, RootClusterNo);

				if (RootClusterNo >= 0x0FFFFFF8) break;

				if (RootClusterNo != 0x0FFFFFF8)
					LBACluster = getLBA(RootClusterNo);				

				NextCluster = RootClusterNo;
			} else {
				break;
			}
		} else {
			sprintf("\nERRO: read_sectorLBA 0x%02X\nPressione qualquer tecla para continuar...", r);
			puts("\n");
			break;
		}
	}
	return C;
}

long load_file_into_memory(UCHAR Drive, UCHAR *filename, UCHAR *addr) {
	USHORT	Index;
	ULONG	LBACluster;
	ULONG	NextCluster;

	UCHAR p[15];
	memset(&p, 0, sizeof(p));
	upper(&p, filename);	
	
	Index = 0;

	while (1) {
		if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
			break; // Encontramos o diretório
		} else {
			Index++;
		}
			
		if (Index >= 1000) break;		
	}
	
	if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
		if (DirList[Index].is_dir == 1) return 0;
		RootClusterNo = DirList[Index].FirstClusterNo;
		LBACluster = getLBA(RootClusterNo);
		NextCluster = 0;
	} else {
		return -1; // Não encontrado!
	}

	ULONG C;
	C = 0;
	UCHAR Buffer[512];
	USHORT i;
	ULONG CheckSum;
	
	while (NextCluster != 0x0FFFFFF8) 
	{

		r = read_sectorLBA(Drive,  LBACluster, SectorsPerCluster, Buffer);

		if (!r)
		{
			memcpy((UCHAR*)addr+C, (UCHAR*)&Buffer, 512);
	
			C += 512;
			
			if (RootClusterNo != 0xFFFFFFF) // Ultimo cluster, nada mais a ser lido do disco.
			{
				RootClusterNo = getFATEntrie(Drive, RootClusterNo);

				if (RootClusterNo >= 0x0FFFFFF8) break;

				if (RootClusterNo != 0x0FFFFFF8)
					LBACluster = getLBA(RootClusterNo);				

				NextCluster = RootClusterNo;
			} else {
				break;
			}
		} else {
			sprintf("\nERRO: read_sectorLBA 0x%02X\nPressione qualquer tecla para continuar...", r);
			puts("\n");
			break;
		}
	}	

	return C;
}

ULONG fopen(UCHAR *arquivo) {

}

ULONG fclose(UCHAR arquivo) {

}

ULONG fread(UCHAR arquivo, UCHAR *buffer, ULONG n) {

}

ULONG fwrite(UCHAR arquivo, UCHAR *buffer, ULONG n) {

}

ULONG getfilesize(UCHAR *filename) {
	USHORT	Index;
	ULONG	LBACluster;
	ULONG	NextCluster;

	UCHAR p[15];
	memset(&p, 0, sizeof(p));
	upper(&p, filename);	
	
	Index = 0;

	while (1) {
		if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
			break; // Encontramos o diretório
		} else {
			Index++;
		}
			
		if (Index >= 1000) break;		
	}
	
	if (strcmp((UCHAR*)DirList[Index].ShortName, (UCHAR*)p) == 0) {
		if (DirList[Index].is_dir == 1) return 0;
		RootClusterNo = DirList[Index].FirstClusterNo;
		LBACluster = getLBA(RootClusterNo);
		NextCluster = 0;
		return DirList[Index].FileSize;
	} else {
		return -1; // Não encontrado!
	}
}
Last edited by thepowersgang on Fri Mar 13, 2015 11:30 pm, edited 1 time in total.
Reason: Adding code tags
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

ok! man you right, I don´t speak english...

I cannot understand it at some cases.

if you see my previous post, I ask combuster what is tags!

I don´t know what is that...

but all the time I have been asking here for some help...

forgive all my faults.. please!

I´ll not do that anymore... really...

I do not want to upset anyone here...
User avatar
Muazzam
Member
Member
Posts: 543
Joined: Mon Jun 16, 2014 5:59 am
Location: Shahpur, Layyah, Pakistan

Re: foreign content

Post by Muazzam »

digo_rp wrote:combuster what is that? code tags?
174 posts and still no code tags?
could you please help me?
Code tags look like this:

Code: Select all

Use them like this:
[code]
//Write your code here.
[/code]
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

I´m just want to say tnx for all.
sorry. :oops:
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: foreign content

Post by kzinti »

digo_rp wrote:I´m just want to say tnx for all.
sorry. :oops:
You're welcome here. Not everyone has some issues to vent in public. Please feel free to ask more questions. English isn't my first language either and I have no problem understanding you.
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Re: foreign content

Post by digo_rp »

kzinti

thank you for understanding me, I know that here is not psychological's office however is super annoying someone virtually wipe you out, I'm trying to understand the development of operating systems since about 2001. I try every day to understand English too, I am Brazilian and then we speak Portuguese, portuguese is a language different medium. but try not to make that mistake. when posted my code from my fat.c tried to share with all ..

once again thank you, if I can help in something, I will be very happy ....
Post Reply