前面的文章提到了ExFAT boot checksum出错时的错误提醒,这里如果需要确认boot checksum是否正确,需要将boot checusum的算法实现一遍.
这里采用了python来实现.
boot checksum算法参考下面:
exFAT Boot Checksum
This sector contains a repeating 32-bit checksum of the previous 11 sectors. The checksum calculation excludes VolumeFlags and PercentInUse fields in Boot Sector (bytes 106, 107, 112). The checksum is repeated until the end of the sector. The number of repetitions depends on the size of the sector.
UNIT32 BootChecksum(constunsignedchardata[], intbytes) { UINT32 checksum = 0; for (inti = 0; i < bytes; i++) { if (i == 106 || i == 107 || i == 112) continue; checksum = (checksum<<31) | (checksum>> 1) + data[i]; } returnchecksum;
这里需要提取到ExFAT的boot sectors,可以采用WinHex来获取,通过winHex来open对应的disk,copy前12个sector的数据到文本即可.
参考下面这个例子:
可以看到11 sector是前0~10 sectors 的checksum,它是一个DWORD型的整数,在这个sector中一直重复.
有了上门的算法和数据,我们就可以来实现并验证算法,python code如下:
#!/usr/bin/python #import os if __name__ == '__main__': dir = 'C:\\Users\\admin\\Desktop\\python_script\\' file = 'ExFATCheckSum.TXT' fd = open(dir + file, 'r') checksum = 0 ind = 0 for (num,line) in enumerate(fd): if num > 10: break for start in range(0,len(line),2): if 106==ind or 107==ind or 112==ind: ind += 1 continue if start >= len(line)-2: break #print '%s:%d' %(line[start:start+2], int(line[start:start+2], 16)) checksum = long(((checksum<<31)&0xFFFFFFFF) | ((checksum>>1) & 0xFFFFFFFF) + long(line[start:start+2], 16)) print 'sector:%d, byte:%d, checksum:%08x' %(num, ind, checksum) ind += 1 print '%08x' %checksum
实验结果参考:
checksum为0xD9DE1E08,与上面的数据中的11 sector是一致的。
算法验证OK。