ExFAT boot checksum的python實現__python

來源:互聯網
上載者:User

前面的文章提到了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 osif __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。


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.