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