[1012] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | import os |
---|
| 4 | import sys |
---|
| 5 | import subprocess |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | def hdd_img(partition_root, target, fs_type, bootloader_name): |
---|
| 11 | |
---|
| 12 | # echo "Dont forget to check the numbers of sectors for each file" |
---|
| 13 | # QM: what does that mean? |
---|
| 14 | |
---|
| 15 | if fs_type != "fat32" and fs_type != "ext2": |
---|
| 16 | print "*** Error: unsupported filesystem type:", fs_type |
---|
| 17 | sys.exit() |
---|
| 18 | |
---|
| 19 | if not(os.path.isdir(partition_root)): |
---|
| 20 | print "*** Error: Partition root is not a valid directory" |
---|
| 21 | sys.exit() |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | print sys.argv[0], ": Target = %s, fs type = %s, Partition root = %s" % (target, fs_type, partition_root) |
---|
| 25 | |
---|
| 26 | if not os.path.isfile(bootloader_name): |
---|
| 27 | print "*** Error: bootloader file name invalid: %s" % (bootloader_name) |
---|
| 28 | sys.exit() |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | size_bytes = 512000 |
---|
| 32 | sector_size = 4096 # must be 512 Bytes for generic_leti platform |
---|
| 33 | sectors_per_cluster = 8 |
---|
| 34 | boot_size = os.path.getsize(bootloader_name) |
---|
| 35 | |
---|
| 36 | sectors_boot = (boot_size / sector_size) + 1 |
---|
| 37 | |
---|
| 38 | # reserved sector will begin from sector 2 and will contain the boot_loader code, |
---|
| 39 | # the kernel image and the architecture informations |
---|
| 40 | # We put the backup of VBR (MBR) at the end of the reserved sector |
---|
| 41 | offset = 2 |
---|
| 42 | reserved_sectors = offset + sectors_boot + 1 |
---|
| 43 | back_up_sector = reserved_sectors - 1 # last reserved sector |
---|
| 44 | |
---|
| 45 | print("******** HDD_IMG *********") |
---|
| 46 | print("%d reserved sectors --> backup at back up sector %d" % (reserved_sectors, back_up_sector)) |
---|
| 47 | # the first two cluster are not in the data region |
---|
| 48 | # data_region_clusters=$cluster_size-2 |
---|
| 49 | |
---|
| 50 | #create partition |
---|
| 51 | if fs_type == "fat32": |
---|
| 52 | cmd = ['mkfs.vfat', '-C', '-F', '32', '-f', '1', |
---|
| 53 | '-r', '512', |
---|
| 54 | '-R', str(reserved_sectors), |
---|
| 55 | '-S', str(sector_size), |
---|
| 56 | '-s', str(sectors_per_cluster), |
---|
| 57 | '-b', str(back_up_sector), |
---|
| 58 | '-i', 'f7120c4f', |
---|
| 59 | '-n', 'hdd', |
---|
| 60 | '-v', target, str(size_bytes)] |
---|
| 61 | print(subprocess.list2cmdline(cmd)) |
---|
| 62 | subprocess.call(cmd) |
---|
| 63 | # copy root |
---|
| 64 | for dir_or_file in os.listdir(partition_root): |
---|
| 65 | cmd = ['mcopy', '-s', '-i', target, os.path.join(partition_root, dir_or_file), '::/'] |
---|
| 66 | print(subprocess.list2cmdline(cmd)) |
---|
| 67 | subprocess.call(cmd) |
---|
| 68 | |
---|
| 69 | else: |
---|
| 70 | cmd = ['mkfs.ext2', target] |
---|
| 71 | print(subprocess.list2cmdline(cmd)) |
---|
| 72 | subprocess.call(cmd) |
---|
| 73 | |
---|
| 74 | # copy bootloader, arch-info (boot-info) and kernel-img in reserved sectors from sector 2 |
---|
| 75 | print("Insert boot_loader at sector $offset") |
---|
| 76 | |
---|
| 77 | cmd = ['dd', 'bs=%d' % sector_size, 'seek=%d' % offset, 'count=%d' % sectors_boot, 'conv=notrunc', 'if=%s' % bootloader_name, 'of=%s' % target] |
---|
| 78 | print(subprocess.list2cmdline(cmd)) |
---|
| 79 | subprocess.call(cmd) |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | if __name__ == '__main__': |
---|
| 83 | |
---|
| 84 | if len(sys.argv) != 5: |
---|
| 85 | print("Usage: %s path/to/partition/root <hdd-filename> <fs_type> <bootloader-filename>" % sys.argv[0]) |
---|
| 86 | sys.exit() |
---|
| 87 | |
---|
| 88 | partition_root = sys.argv[1] |
---|
| 89 | target = sys.argv[2] # hdd wanted filename |
---|
| 90 | fs_type = sys.argv[3] |
---|
| 91 | bootloader_name = sys.argv[4] |
---|
| 92 | |
---|
| 93 | hdd_img(partition_root, target, fs_type, bootloader_name) |
---|
| 94 | |
---|