|
| 1 | +import os, getopt, sys, zlib |
| 2 | +#we can use binascii instead of zlib |
| 3 | + |
| 4 | +def usage(exit_code): |
| 5 | + print("usage:") |
| 6 | + print(sys.argv[0] + " --arc-id 0x52 --image u-boot.bin --elf u-boot") |
| 7 | + sys.exit(exit_code) |
| 8 | + |
| 9 | +def main(): |
| 10 | + try: |
| 11 | + opts, args = getopt.getopt(sys.argv[1:], |
| 12 | + "ha:i:l:e:", ["help", "arc-id=", "image=", "elf=", "env="]) |
| 13 | + except getopt.GetoptError as err: |
| 14 | + print(err) |
| 15 | + usage(2) |
| 16 | + |
| 17 | + # default filenames |
| 18 | + uboot_bin_filename = "u-boot.bin" |
| 19 | + uboot_elf_filename = "u-boot" |
| 20 | + headerised_filename = "u-boot.head" |
| 21 | + uboot_default_env = "u-boot-env.txt" |
| 22 | + uboot_patched_env = "u-boot-p-env.txt" |
| 23 | + |
| 24 | + # initial header values |
| 25 | + arc_id = 0x52 |
| 26 | + uboot_img_size = 0x0497fc |
| 27 | + check_sum = 0x61 |
| 28 | + image_copy_adr = 0x81000000 |
| 29 | + magic1 = 0xdeadbeafaf # big endian byte order |
| 30 | + jump_address = 0x81000000 |
| 31 | + flash_address = 0x0 |
| 32 | + flash_type = 0x0 # 0 - SPI flash, 1 - NOR flash |
| 33 | + magic2 = [ # big endian byte order |
| 34 | + 0x20202a2020202020202020202a20202020207c5c2e20202020202e2f7c20202020207c2d, |
| 35 | + 0x2e5c2020202f2e2d7c20202020205c2020602d2d2d6020202f20202020202f205f202020, |
| 36 | + 0x205f20205c20202020207c205f60712070205f207c2020202020272e5f3d2f205c3d5f2e, |
| 37 | + 0x272020202020202020605c202f60202020202020202020202020206f2020202020202020] |
| 38 | + |
| 39 | + for opt, arg in opts: |
| 40 | + if opt in ('-h', "--help"): usage(0) |
| 41 | + if opt in ('-a', "--arc-id"): arc_id = int(arg, 16) |
| 42 | + if opt in ('-i', "--image"): uboot_bin_filename = arg |
| 43 | + if opt in ('-l', "--elf"): uboot_elf_filename = arg |
| 44 | + if opt in ('-e', "--env"): uboot_default_env = arg |
| 45 | + |
| 46 | + # verify args: |
| 47 | + if arc_id not in [0x52, 0x53]: |
| 48 | + print("unknown ARC ID: " + hex(arc_id)) |
| 49 | + sys.exit(2) |
| 50 | + |
| 51 | + if not os.path.isfile(uboot_bin_filename): |
| 52 | + print("uboot bin file not exists: " + uboot_bin_filename) |
| 53 | + sys.exit(2) |
| 54 | + |
| 55 | + if not os.path.isfile(uboot_default_env): |
| 56 | + print("uboot defaul environment file not exists: " + uboot_default_env) |
| 57 | + sys.exit(2) |
| 58 | + |
| 59 | + uboot_img_size = os.path.getsize(uboot_bin_filename) |
| 60 | + |
| 61 | + # Calculate u-boot image check_sum: it is sum of all u-boot binary bytes |
| 62 | + with open(uboot_bin_filename, "rb") as file: |
| 63 | + ba = bytearray(file.read()) |
| 64 | + check_sum = sum(ba) & 0xFF |
| 65 | + |
| 66 | + # write header to file |
| 67 | + with open(headerised_filename, "wb") as file: |
| 68 | + file.write(arc_id.to_bytes(2, byteorder='little')) |
| 69 | + file.write(uboot_img_size.to_bytes(4, byteorder='little')) |
| 70 | + file.write(check_sum.to_bytes(1, byteorder='little')) |
| 71 | + file.write(image_copy_adr.to_bytes(4, byteorder='little')) |
| 72 | + file.write(magic1.to_bytes(5, byteorder='big')) |
| 73 | + file.write(jump_address.to_bytes(4, byteorder='little')) |
| 74 | + for i in range(12): file.write(0xFF.to_bytes(1, byteorder='little')) |
| 75 | + for byte in magic2: file.write(byte.to_bytes(36, byteorder='big')) |
| 76 | + for i in range(208 - len(magic2) * 36): |
| 77 | + file.write(0xFF.to_bytes(1, byteorder='little')) |
| 78 | + file.write(flash_address.to_bytes(4, byteorder='little')) |
| 79 | + for i in range(11): file.write(0xFF.to_bytes(1, byteorder='little')) |
| 80 | + file.write(flash_type.to_bytes(1, byteorder='little')) |
| 81 | + |
| 82 | + # append u-boot image to header |
| 83 | + with open(headerised_filename, "ab") as fo: |
| 84 | + with open(uboot_bin_filename,'rb') as fi: |
| 85 | + fo.write(fi.read()) |
| 86 | + |
| 87 | + # calc u-boot headerised image CRC32 (will be used by uboot update |
| 88 | + # command for check) |
| 89 | + headerised_image_crc = "" |
| 90 | + with open(headerised_filename, "rb") as fi: |
| 91 | + headerised_image_crc = hex(zlib.crc32(fi.read()) & 0xffffffff) |
| 92 | + print("image CRC32 = " + headerised_image_crc) |
| 93 | + |
| 94 | + load_addr = 0x81000000 |
| 95 | + load_size = os.path.getsize(headerised_filename) |
| 96 | + |
| 97 | +# print("crc32 " + hex(load_addr) + " " + hex (load_size)) |
| 98 | + |
| 99 | + # make errase size to be allighned by 64K |
| 100 | + if load_size & 0xFFFF == 0: |
| 101 | + errase_size = load_size |
| 102 | + else: |
| 103 | + errase_size = load_size - (load_size & 0xFFFF) + 0x10000 |
| 104 | + |
| 105 | + # u-bood CMD to load u-bood with header to SPI flash |
| 106 | + sf_load_image_cmd = \ |
| 107 | + "fatload mmc 0:1 " + hex(load_addr) + " " + headerised_filename + " && " + \ |
| 108 | + "sf probe 0:0 && " + \ |
| 109 | + "sf protect unlock 0x0 " + hex(errase_size) + " && " + \ |
| 110 | + "sf erase 0x0 " + hex(errase_size) + " && " + \ |
| 111 | + "sf write " + hex(load_addr) + " 0x0 " + hex(errase_size) + " && " + \ |
| 112 | + "sf protect lock 0x0 " + hex(errase_size) |
| 113 | + |
| 114 | + update_uboot_cmd = "update_uboot=" + \ |
| 115 | + sf_load_image_cmd + " && echo \"u-boot update: OK\"" |
| 116 | + |
| 117 | + # append default environment with update commands |
| 118 | + with open(uboot_default_env, "rb") as fi: |
| 119 | + with open(uboot_patched_env, 'wb') as fo: |
| 120 | + fo.write(fi.read()) |
| 121 | + fo.write(update_uboot_cmd.encode('ascii')) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main() |
0 commit comments