Trying to implement the exFAT boot checksum as described in section 3.4 of:
https://learn.microsoft.com/en-us/windows/win32/fileio/exfat-specification
My code does not produce the correct checksums. :(
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
/* test for filename in parameters */
if ( argc != 2 )
{
/* assume argv[0] has the program name */
printf( "usage: %s filename", argv[0] );
}
else
{
/* assume argv[1] has the filename to process */
FILE *filename = fopen( argv[1], "rb" );
/* check that file exists */
if ( filename == 0 )
{
printf( "Could not open %s\n", argv[1] );
exit (1);
}
else
{
unsigned char cbytes[5632];
int ibytes = fread(cbytes, 1, sizeof(cbytes), filename);
if (ibytes != 5632)
{
printf( "Can't read 5632 bytes from %s\n", argv[1] );
exit (1);
}
fclose( filename );
uint32_t chksum=0;
for (int index = 0; index < 5632; index++)
{
if ((index == 106) || (index == 107) || (index == 112))
{ continue; }
chksum = ((chksum&1) ? 0x80000000 : 0) + (chksum>>1) + cbytes[index];
}
printf("%8x\n", chksum);
}
}
}
Yes I have examined this past question (the author apparently never could get the correct checksums either).
Can anyone spot what I have done wrong?