WriteFile to exFAT backup boot sector has no effect on drive

J. Taphorn 1 Reputation point
2021-02-01T07:32:30.293+00:00

Hello all,
here is a short explanation of what I am trying to do:

Reading out VolumeSerialNumber of an USB flash drive
Increment the VolumeSerialNumber
Writing it back to the flash drive.
In general it works fine for USB drives using FAT or FAT32 as filesystem but I can't get it working with filesystem exFAT. As read in the filesystem specifications (https://learn.microsoft.com/en-us/windows/win32/fileio/exfat-specification) I already do some 'special handling' what seems to be necessary for exFAT:

Set VolumeFlag/VolumeDirty to true, in Main- and Backup Boot Sector
Reading, incrementing and writing back serial number, in Main- and Backup Boot Sector
Recalculating and writing back the BootChecksum in the corresponding main and backup checksum sector
Setting back the VolumeDirty flag to false for both sectors.

I have a general issue when trying to write into the Backup sectors. When I call ReadFile after WriteFile has been called I always get the old value back. The SetFilePointer seems to be correct as I get the expected values/range from ReadFile but values don't get changed after calling WriteFile.

This does only happens in the Backup Sectors. Writing inside the Main sectors seems to work. But because of different values in the end in Main- and backup sector Windows prompt me to format the USB drive again.

I am using C# / .Net Framework. Here is a code snipping for maybe a better understanding (not complete)

Entering a code snippet directly here was due to editing issues not possible. Please take a look at the attached example.

62349-examplecode.txt

Thanks for any help or suggestions in advance.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,108 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.