Changes in tools/mkfat.py [3c80f2b:432f68a] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/mkfat.py

    r3c80f2b r432f68a  
    4646                return size
    4747       
    48         return (((size / alignment) + 1) * alignment)
     48        return ((size // alignment) + 1) * alignment
    4949
    5050def subtree_size(root, cluster_size, dirent_size):
     
    7979        first = 0
    8080       
    81         inf = file(path, "r")
     81        inf = open(path, "rb")
    8282        rd = 0;
    8383        while (rd < size):
     
    9292                prev = empty_cluster
    9393               
    94                 data = inf.read(cluster_size);
     94                data = bytes(inf.read(cluster_size));
    9595                outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size)
    9696                outf.write(data)
     
    120120                prev = empty_cluster
    121121               
    122                 data = ''
     122                data = bytes()
    123123                data_len = 0
    124124                while ((i < length) and (data_len < cluster_size)):
     
    211211        dir_entry = xstruct.create(DIR_ENTRY)
    212212       
    213         dir_entry.name = mangle_fname(name)
    214         dir_entry.ext = mangle_ext(name)
     213        dir_entry.name = mangle_fname(name).encode('ascii')
     214        dir_entry.ext = mangle_ext(name).encode('ascii')
    215215       
    216216        if (directory):
     
    239239       
    240240        dir_entry.signature = 0x2e
    241         dir_entry.name = '       '
    242         dir_entry.ext = '   '
     241        dir_entry.name = b'       '
     242        dir_entry.ext = b'   '
    243243        dir_entry.attr = 0x10
    244244       
     
    258258       
    259259        dir_entry.signature = [0x2e, 0x2e]
    260         dir_entry.name = '      '
    261         dir_entry.ext = '   '
     260        dir_entry.name = b'      '
     261        dir_entry.ext = b'   '
    262262        dir_entry.attr = 0x10
    263263       
     
    343343def usage(prname):
    344344        "Print usage syntax"
    345         print prname + " <PATH> <IMAGE>"
     345        print(prname + " <EXTRA_BYTES> <PATH> <IMAGE>")
    346346
    347347def main():
    348         if (len(sys.argv) < 3):
     348        if (len(sys.argv) < 4):
    349349                usage(sys.argv[0])
    350350                return
    351351       
    352         path = os.path.abspath(sys.argv[1])
     352        if (not sys.argv[1].isdigit()):
     353                print("<EXTRA_BYTES> must be a number")
     354                return
     355       
     356        extra_bytes = int(sys.argv[1])
     357       
     358        path = os.path.abspath(sys.argv[2])
    353359        if (not os.path.isdir(path)):
    354                 print "<PATH> must be a directory"
     360                print("<PATH> must be a directory")
    355361                return
    356362       
     
    364370        reserved_clusters = 2
    365371       
    366         # Make sure the filesystem is large enought for FAT16
    367         size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size
    368         while (size / cluster_size < fat16_clusters):
     372        # Make sure the filesystem is large enough for FAT16
     373        size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
     374        while (size // cluster_size < fat16_clusters):
    369375                if (cluster_size > sector_size):
    370                         cluster_size /= 2
    371                         size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size
     376                        cluster_size = cluster_size // 2
     377                        size = subtree_size(path, cluster_size, dirent_size) + reserved_clusters * cluster_size + extra_bytes
    372378                else:
    373379                        size = fat16_clusters * cluster_size + reserved_clusters * cluster_size
     
    375381        root_size = align_up(root_entries(path) * dirent_size, cluster_size)
    376382       
    377         fat_size = align_up(align_up(size, cluster_size) / cluster_size * fatent_size, sector_size)
    378        
    379         sectors = (cluster_size + fat_count * fat_size + root_size + size) / sector_size
     383        fat_size = align_up(align_up(size, cluster_size) // cluster_size * fatent_size, sector_size)
     384       
     385        sectors = (cluster_size + fat_count * fat_size + root_size + size) // sector_size
    380386        root_start = cluster_size + fat_count * fat_size
    381387        data_start = root_start + root_size
    382388       
    383         outf = file(sys.argv[2], "w")
     389        outf = open(sys.argv[3], "wb")
    384390       
    385391        boot_sector = xstruct.create(BOOT_SECTOR)
    386392        boot_sector.jmp = [0xEB, 0x3C, 0x90]
    387         boot_sector.oem = "MSDOS5.0"
     393        boot_sector.oem = b'MSDOS5.0'
    388394        boot_sector.sector = sector_size
    389         boot_sector.cluster = cluster_size / sector_size
    390         boot_sector.reserved = cluster_size / sector_size
     395        boot_sector.cluster = cluster_size // sector_size
     396        boot_sector.reserved = cluster_size // sector_size
    391397        boot_sector.fats = fat_count
    392         boot_sector.rootdir = root_size / dirent_size
     398        boot_sector.rootdir = root_size // dirent_size
    393399        if (sectors <= 65535):
    394400                boot_sector.sectors = sectors
     
    396402                boot_sector.sectors = 0
    397403        boot_sector.descriptor = 0xF8
    398         boot_sector.fat_sectors = fat_size / sector_size
     404        boot_sector.fat_sectors = fat_size // sector_size
    399405        boot_sector.track_sectors = 63
    400406        boot_sector.heads = 6
     
    408414        boot_sector.extboot_signature = 0x29
    409415        boot_sector.serial = random.randint(0, 0x7fffffff)
    410         boot_sector.label = "HELENOS"
    411         boot_sector.fstype = "FAT16   "
     416        boot_sector.label = b'HELENOS'
     417        boot_sector.fstype = b'FAT16   '
    412418        boot_sector.boot_signature = [0x55, 0xAA]
    413419       
     
    417423       
    418424        # Reserved sectors
    419         for i in range(1, cluster_size / sector_size):
     425        for i in range(1, cluster_size // sector_size):
    420426                outf.write(empty_sector.pack())
    421427       
    422428        # FAT tables
    423429        for i in range(0, fat_count):
    424                 for j in range(0, fat_size / sector_size):
     430                for j in range(0, fat_size // sector_size):
    425431                        outf.write(empty_sector.pack())
    426432       
    427433        # Root directory
    428         for i in range(0, root_size / sector_size):
     434        for i in range(0, root_size // sector_size):
    429435                outf.write(empty_sector.pack())
    430436       
    431437        # Data
    432         for i in range(0, size / sector_size):
     438        for i in range(0, size // sector_size):
    433439                outf.write(empty_sector.pack())
    434440       
    435         fat = array.array('L', [0] * (fat_size / fatent_size))
     441        fat = array.array('L', [0] * (fat_size // fatent_size))
    436442        fat[0] = 0xfff8
    437443        fat[1] = 0xffff
     
    443449        for i in range(0, fat_count):
    444450                outf.seek(cluster_size + i * fat_size)
    445                 for j in range(0, fat_size / fatent_size):
     451                for j in range(0, fat_size // fatent_size):
    446452                        fat_entry.next = fat[j]
    447453                        outf.write(fat_entry.pack())
Note: See TracChangeset for help on using the changeset viewer.