exFAT Boot checksum python implementation __python

Source: Internet
Author: User

The previous article mentioned the error reminders for exFAT boot checksum error, and if you need to verify that the boot checksum is correct, you need to implement the boot Checusum algorithm.

Python is used to implement this.

The boot checksum algorithm references the following:

exFAT Boot Checksum

This sector contains a repeating 32-bit checksum of the previous. 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 = =] | |
Checksum = (checksum<<31) | (checksum>> 1) + data[i];
}
Returnchecksum;

Here need to extract to the exFAT boot sectors, you can use Winhex to obtain, through the Winhex to open the corresponding disk,copy of the first 12 sector data to the text.

Refer to the following example:

You can see one sector is the checksum of the former 0~10 sectors, which is a DWORD integer that repeats in this sector.

With the home algorithm and data, we can implement and verify the algorithm, Python code is as follows:

#!/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 Enumer Ate (FD):
        if num > Break-
        for-start in range (0,len), 2:
            if 106==ind or 107==ind or 112==ind:< C12/>ind + + 1
                continue
            if start >= len (line)-2: Break
            #print '%s:%d '% (line[start:start+2), int (line[start:start+2])
            Checksum = Long (((checksum<<31) &0xffffffff) | ((checksum>>1) & 0xFFFFFFFF) + Long (line[start:start+2))
            print ' sector:%d, byte:%d, checksum:%08x ' % (NUM, IND, CHECKSUM)
            ind + + 1
    print '%08x '%checksum

Experimental results refer to:

Checksum is 0xd9de1e08, consistent with one sector in the above data.

Algorithm validation OK.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.