Page 1 of 1

exfat_header->signature

Posted: 23 Sep 2019, 19:07
by mesajflaviu
Wanting to understand this code, I met something interesting:

Code: Select all

int search_exFAT_backup(unsigned char* buffer, disk_t* disk, partition_t* partition)
{
	if (disk->pread(disk, buffer, DEFAULT_SECTOR_SIZE, partition->part_offset) != DEFAULT_SECTOR_SIZE)
		return -1;
	{
		const struct exfat_super_block* exfat_header = (const struct exfat_super_block *)buffer;
		/* EXFAT recovery using backup sector */
		int a = exfat_header->signature;
		int b = le16(exfat_header->signature);
		int c = 0xAA55;
		int d = le16(exfat_header->signature) == 0xAA55;
// the condition from below is never TRUE at all ... why exfat_header->signature is swaping byte ? (lower/high byte) ?
		if (le16(exfat_header->signature) == 0xAA55 &&
			recover_exFAT(disk, exfat_header, partition) == 0)
		{
			/* part_offset has already been updated if found using backup sector */
			return 1;
		}
	}
	return 0;
}
Result:

Code: Select all

a = 43605; (in hexa: 0xAA55)
b = 21930; (in hexa: 0x55AA)
c = 43605;
d = 0;
of course, the condition from below is not TRUE:

Code: Select all

if (le16(exfat_header->signature) == 0xAA55)
why is swaping exfat_header->signature lower byte with high byte ?

Thank you for any explaination !

Re: exfat_header->signature

Posted: 23 Sep 2019, 20:02
by recuperation
Hello,

just an idea (I am not able to program in C and did not look at the rest of the code):

The software runs on different platforms where the byte order may differ. See:
https://en.wikipedia.org/wiki/Endianness

Re: exfat_header->signature

Posted: 24 Sep 2019, 07:13
by mesajflaviu
Hello. Yes, I think that could be the reason why that value is swapped ... but I wonder whether is ok to modify that condition in order to made that condition TRUE ?

Re: exfat_header->signature

Posted: 24 Sep 2019, 10:34
by recuperation
As I said above:

It is the processor platform that should make that condition become true - I guess.

Re: exfat_header->signature

Posted: 24 Sep 2019, 13:26
by mesajflaviu
@recuperation: I owe you. Thank you. I'll try to write to Grenier this part of code, when I modified by my hunch, seem to work that part of code, but I am thinking where these situation could come up later ?

Re: exfat_header->signature

Posted: 24 Sep 2019, 17:28
by recuperation
recuperation wrote:
24 Sep 2019, 10:34
As I said above:

It is the processor platform that should make that condition become true - I guess.

Re: exfat_header->signature

Posted: 25 Sep 2019, 06:10
by cgrenier
On a little endian plateform (ie. Intel), le16() is defined as

Code: Select all

#define le16(x)  (x)
It has no effect.

Re: exfat_header->signature

Posted: 26 Sep 2019, 13:23
by mesajflaviu
Thank you, I have understood.