Help with bit function for exFAT

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys, I have a function that returns a bit from the exFAT heap to mark a new cluster.  But for some reason there are errors when I create thousands of folders and files to test the system.

 

When I test the routine to start returning bits using the following, it works:

 

Basically, the function returns the first free bit it finds.

//	test to see if the problem is in this routine!!!!!!!!
	static unsigned int test = 8;
	clear_cluster(stream, test);

	//return address of bit allocated
	return test++;

This is the function:

unsigned int dir_get_cluster_bit(FILEX * stream){

	// base the pointer
	unsigned char * bitMap = stream->cache;

	/* Bit Map table, its a bit like the FAT table in FAT32 only it is in bits*/
	unsigned int fat_table = data_to_cpu32(stream->exFAT_Boot_Sector->ClusterHeapOffset) + stream->partition_index;

	// read first sector
	dma_hardware_read(1, fat_table, 1, stream->cache, 0);

	unsigned int byte = 0, sector = 0;

	// scan sector for a free bit, i.e. one or more bits are Zero
	while(* bitMap == 0xff){

		byte++, bitMap++;

		// stop arithmetic errors when byte is zero
		if(((byte % 512) == 0) && (byte != 0)){

			// move to next sector
			sector++;
			byte = 0;

			// reset bitmap to start of sector
			bitMap = (unsigned char *)&bitMap[0];

			// read next sector
			dma_hardware_read(1, fat_table + sector, 1, stream->cache, 0);
		}
	}

	// UnMap free bit location
	unsigned int bit = free_bit[* bitMap];

	// write bit to target byte and update
	* bitMap |= (unsigned char)1 << bit;

	// write first FAT bit entry
	dma_hardware_write(1, fat_table + sector, 1, stream->cache, 0);

	unsigned int new_cluster = ((sector * 512) + (byte * 8) + bit + 2);

	clear_cluster(stream, new_cluster);

//	test to see if the problem is in this routine!!!!!!!!
//	static unsigned int test = 8;
//	clear_cluster(stream, test);

	//return address of bit allocated
	return new_cluster;
}

So, when I find a free cluster bit, i.e. a zero int the byte, I mark it and return the address that the bit was found.

This topic has a solution.
Last Edited: Sat. Sep 18, 2021 - 07:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys, I'm going to shoot myself.  The error occurs when test = 4098.  So take the 2 away and the error occurs at 4096.  This is clearly a math error. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:
'm going to shoot myself.

Let us know where to send flowers before you off yourself!

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Belfast, Ireland.  God's Own Country.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

			// reset bitmap to start of sector
			bitMap = (unsigned char *)&bitMap[0];

 

What on Earth is that supposed to mean? How is it going to "reset bitmap to start of sector"? This is an obfuscated, but still fairly obvious NO-OP. It doesn't do anything.

 

Why don't you just do the plain and obvious

 

bitMap = stream->cache;

to "reset bitmap to start of sector"?

Dessine-moi un mouton

Last Edited: Wed. Sep 15, 2021 - 03:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need help with COVID FAT, and not just a bit! smiley

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Was away for a view days holiday.  When I came back yesterday I realised I had forgot to multiply the sector by:

 

((sector * 512) * 8)!!!!!!!

 

unsigned int dir_get_cluster_bit(FILEX * stream){


	// base the pointer
	unsigned char * bitMap = stream->cache;

	/* Bit Map table, its a bit like the FAT table in FAT32 only it is in bits*/
	unsigned int fat_table = data_to_cpu32(stream->exFAT_Boot_Sector->ClusterHeapOffset) + stream->partition_index;

	// read first sector
	dma_hardware_read(1, fat_table, 1, &bitMap[0], 0);

	unsigned int byte = 0, sector_cnt = 0;

	// scan sector for a free bit, i.e. one or more bits are Zero
	while(bitMap[byte] == 0xff){

		byte++;

		// stop arithmetic errors when byte is zero
		if(((byte % bytes_per_sector(stream)) == 0) && (byte != 0)){

			// move to next sector
			sector_cnt++;
			byte = 0;

			// reset bitmap to start of sector
			bitMap = (unsigned char *)stream->cache;

			// read next sector
			dma_hardware_read(1, fat_table + sector_cnt, 1, stream->cache, 0);
		}
	}

	// UnMap free bit location
	unsigned int bit = free_bit[bitMap[byte]];

	// write bit to target byte and update
	bitMap[byte] |= (unsigned char)1 << bit;

	// write first FAT bit entry **UPDATE**
	dma_hardware_write(1, fat_table + sector_cnt, 1, stream->cache, 0);

	// calculate cluster bit number
	unsigned int new_cluster = ((sector_cnt * (bytes_per_sector(stream) * 8)) + (byte * 8)) + bit + 2;

	// clear newly allocated cluster
	clear_cluster(stream, new_cluster);

	//return address of bit allocated
	return new_cluster;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

When I came back yesterday I realised I had forgot to multiply the sector by:

 

((sector * 512) * 8)!!!!!!!

 

... and still quietly replaced the nonsensical 

bitMap = (unsigned char *)&bitMap[0];

with 

 

bitMap = stream->cache;

Dessine-moi un mouton

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AndreyT wrote:

Fianawarrior wrote:

When I came back yesterday I realised I had forgot to multiply the sector by:

 

((sector * 512) * 8)!!!!!!!

 

... and still quietly replaced the nonsensical 

bitMap = (unsigned char *)&bitMap[0];

with 

 

bitMap = stream->cache;

It worked with that statement, I just replaced it to be neater.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

It worked with that statement, I just replaced it to be neater.

 

No, it did not work. As I said above, you original version is a no-op. 

Dessine-moi un mouton

Last Edited: Mon. Sep 20, 2021 - 03:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AndreyT wrote:

Fianawarrior wrote:

It worked with that statement, I just replaced it to be neater.

 

No, it did not work. As I said above, you original version is a no-op. 

 

Oh but it did.  I said it was working, but that it appeared to be a math error which it was. In plain sight.