Question : Format overwrites MBR

I'm making an USB stick bootable. First I wipe the first 64 sectors of th USB stick with all 0x00. Then I write the freeBSD bootrecord to the first sector, then create a partitiontable.
(UFDprep.exe, MS tool generates the same partitiontable). Finally I call formatEX to format the drive. After that sometimes the MBR is overwritten and the device is not bootable anymore. If I use format from windows on the drive after the MBR is written, the same happens. So I expect that it is not related to my call to formatEX.

When I copy sector 0 to sector 1 (two MBR's behind eachother) and the format in some cases it is going right. In some cases setting SetFilePointer (createfile opened physicaldisk) 512 bytes forward.

I'm out of ideas what may cause this. (code is written in c#) attached is sector 0 (bootrecord and partition table of a 1GB disk).

Does anybody have a idea to solve this?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
byte[] rawData = {
	0xFC, 0x31, 0xC0, 0x8E, 0xC0, 0x8E, 0xD8, 0x8E, 0xD0, 0xBC, 0x00, 0x7C,
	0xBE, 0x1A, 0x7C, 0xBF, 0x1A, 0x06, 0xB9, 0xE6, 0x01, 0xF3, 0xA4, 0xE9,
	0x00, 0x8A, 0x31, 0xF6, 0xBB, 0xBE, 0x07, 0xB1, 0x04, 0x38, 0x2F, 0x74,
	0x08, 0x7F, 0x75, 0x85, 0xF6, 0x75, 0x71, 0x89, 0xDE, 0x80, 0xC3, 0x10,
	0xE2, 0xEF, 0x85, 0xF6, 0x75, 0x02, 0xCD, 0x18, 0x80, 0xFA, 0x80, 0x72,
	0x0B, 0x8A, 0x36, 0x75, 0x04, 0x80, 0xC6, 0x80, 0x38, 0xF2, 0x72, 0x02,
	0x8A, 0x14, 0x89, 0xE7, 0x8A, 0x74, 0x01, 0x8B, 0x4C, 0x02, 0xBB, 0x00,
	0x7C, 0xF6, 0x06, 0xBD, 0x07, 0x80, 0x74, 0x2D, 0x51, 0x53, 0xBB, 0xAA,
	0x55, 0xB4, 0x41, 0xCD, 0x13, 0x72, 0x20, 0x81, 0xFB, 0x55, 0xAA, 0x75,
	0x1A, 0xF6, 0xC1, 0x01, 0x74, 0x15, 0x5B, 0x66, 0x6A, 0x00, 0x66, 0xFF,
	0x74, 0x08, 0x06, 0x53, 0x6A, 0x01, 0x6A, 0x10, 0x89, 0xE6, 0xB8, 0x00,
	0x42, 0xEB, 0x05, 0x5B, 0x59, 0xB8, 0x01, 0x02, 0xCD, 0x13, 0x89, 0xFC,
	0x72, 0x0F, 0x81, 0xBF, 0xFE, 0x01, 0x55, 0xAA, 0x75, 0x0C, 0xFF, 0xE3,
	0xBE, 0xB9, 0x06, 0xEB, 0x11, 0xBE, 0xD1, 0x06, 0xEB, 0x0C, 0xBE, 0xF0,
	0x06, 0xEB, 0x07, 0xBB, 0x07, 0x00, 0xB4, 0x0E, 0xCD, 0x10, 0xAC, 0x84,
	0xC0, 0x75, 0xF4, 0xEB, 0xFE, 0x49, 0x6E, 0x76, 0x61, 0x6C, 0x69, 0x64,
	0x20, 0x70, 0x61, 0x72, 0x74, 0x69, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x74,
	0x61, 0x62, 0x6C, 0x65, 0x00, 0x45, 0x72, 0x72, 0x6F, 0x72, 0x20, 0x6C,
	0x6F, 0x61, 0x64, 0x69, 0x6E, 0x67, 0x20, 0x6F, 0x70, 0x65, 0x72, 0x61,
	0x74, 0x69, 0x6E, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x00,
	0x4D, 0x69, 0x73, 0x73, 0x69, 0x6E, 0x67, 0x20, 0x6F, 0x70, 0x65, 0x72,
	0x61, 0x74, 0x69, 0x6E, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D,
	0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
	0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x80, 0x00, 0x02, 0x00, 0x0B, 0xFE, 0x3F, 0x7C, 0x01, 0x00,
	0x00, 0x00, 0xFF, 0xBF, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
};

Answer : Format overwrites MBR

Backup you MBR before formatting and then restore after format is done.
Random Solutions  
 
programming4us programming4us