|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2024 Realtek Semiconductor, Inc. |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +import argparse |
| 8 | +import hashlib |
| 9 | +import pathlib |
| 10 | +import sys |
| 11 | + |
| 12 | +PATCH_FLAG = 0x3 |
| 13 | +PATCH_FLAG_OFFSET = 0x0 |
| 14 | +PATCH_FLAG_LEN = 0x4 |
| 15 | +PATCH_IMG_SIZE_OFFSET = 0x4 |
| 16 | +PATCH_IMG_SIZE_LEN = 0x2 |
| 17 | + |
| 18 | +PATCH_CONFIG_LEN = 0x50 |
| 19 | +PATCH_RESERVED_LEN = 0xF70 |
| 20 | +PATCH_SHA_LEN = 0x20 |
| 21 | +PATCH_PADDING_LEN = 0x20 |
| 22 | + |
| 23 | +IMG_HEADER_FLAG = 0x2 |
| 24 | +IMG_HEADER_FLAG_OFFSET = 0x0 |
| 25 | +IMG_HEADER_FLAG_LEN = 0x4 |
| 26 | +IMG_HEADER_VER_OFFSET = 0x4 |
| 27 | +IMG_HEADER_VER_LEN = 0x4 |
| 28 | +IMG_HEADER_RESERVED_LEN = 0xF8 |
| 29 | + |
| 30 | +IMG_SIZE_UNIT = 0x1000 |
| 31 | +IMG_SHA_LEN = 0x20 |
| 32 | +IMG_REAR_PADDING_LEN = 0x60 |
| 33 | + |
| 34 | + |
| 35 | +def create_parser(arg_list): |
| 36 | + """create argument parser according to pre-defined arguments |
| 37 | +
|
| 38 | + :param arg_list: when empty, parses command line arguments, |
| 39 | + else parses the given string |
| 40 | + """ |
| 41 | + parser = argparse.ArgumentParser(conflict_handler='resolve', allow_abbrev=False) |
| 42 | + parser.add_argument("--input", "-i", nargs='?', dest="input") |
| 43 | + parser.add_argument("--output", "-o", nargs='?', dest="output") |
| 44 | + parser.add_argument("--version", "-v", type=lambda x: int(x, 16), dest="version") |
| 45 | + |
| 46 | + args = parser.parse_known_args(arg_list.split()) |
| 47 | + |
| 48 | + if arg_list == "": |
| 49 | + args = parser.parse_known_args() |
| 50 | + |
| 51 | + return args |
| 52 | + |
| 53 | + |
| 54 | +def file_check(input, output=None): |
| 55 | + """check input and output files |
| 56 | + If the input file is not exists or empty, raise error |
| 57 | +
|
| 58 | + :param input: the input file object |
| 59 | + :param output: the output file object |
| 60 | + """ |
| 61 | + if not input.exists(): |
| 62 | + raise RuntimeError(f'Input file ({input}) is not exists') |
| 63 | + elif input.stat().st_size == 0: |
| 64 | + raise RuntimeError(f'Input file ({input}) is empty') |
| 65 | + |
| 66 | + if output is None: |
| 67 | + output = ['out_', input] |
| 68 | + |
| 69 | + if output.exists(): |
| 70 | + if output.samefile(input): |
| 71 | + raise RuntimeError(f'Input file {input} should be different from Output file {output}') |
| 72 | + output.unlink() |
| 73 | + |
| 74 | + output.touch() |
| 75 | + return output |
| 76 | + |
| 77 | + |
| 78 | +def gen_rompatch(img_len): |
| 79 | + """generate rom patch for image |
| 80 | +
|
| 81 | + :param img_len: the length of image |
| 82 | + """ |
| 83 | + patch_config = bytearray([0x0] * PATCH_CONFIG_LEN) |
| 84 | + patch_config[PATCH_FLAG_OFFSET] = PATCH_FLAG |
| 85 | + patch_config[PATCH_IMG_SIZE_OFFSET] = img_len |
| 86 | + patch_reserved = bytearray([0xFF] * PATCH_RESERVED_LEN) |
| 87 | + patch = patch_config + patch_reserved |
| 88 | + patch_sha = hashlib.sha256(patch).digest() |
| 89 | + patch += patch_sha |
| 90 | + patch_padding = bytearray([0xFF] * PATCH_PADDING_LEN) |
| 91 | + patch += patch_padding |
| 92 | + return patch |
| 93 | + |
| 94 | + |
| 95 | +def gen_img_header(img_version=0): |
| 96 | + """generate rom patch for image |
| 97 | +
|
| 98 | + :param img_version: the version of image |
| 99 | + """ |
| 100 | + img_header = IMG_HEADER_FLAG.to_bytes(IMG_HEADER_FLAG_LEN, "little") |
| 101 | + img_header += img_version.to_bytes(IMG_HEADER_VER_LEN, "little") |
| 102 | + img_header += bytearray([0xFF] * IMG_HEADER_RESERVED_LEN) |
| 103 | + return img_header |
| 104 | + |
| 105 | + |
| 106 | +def img_padding(img): |
| 107 | + size = len(img) // IMG_SIZE_UNIT |
| 108 | + rear = len(img) % IMG_SIZE_UNIT |
| 109 | + |
| 110 | + if rear <= IMG_SIZE_UNIT - IMG_SHA_LEN - IMG_REAR_PADDING_LEN: |
| 111 | + size += 1 |
| 112 | + padding_len = IMG_SIZE_UNIT - rear - IMG_SHA_LEN - IMG_REAR_PADDING_LEN |
| 113 | + else: |
| 114 | + size += 2 |
| 115 | + padding_len = 2 * IMG_SIZE_UNIT - rear - IMG_SHA_LEN - IMG_REAR_PADDING_LEN |
| 116 | + |
| 117 | + img_with_padding = img + bytes([0xFF] * padding_len) |
| 118 | + return size, img_with_padding |
| 119 | + |
| 120 | + |
| 121 | +def main(): |
| 122 | + """main of the application""" |
| 123 | + |
| 124 | + if len(sys.argv) < 3: |
| 125 | + sys.exit() |
| 126 | + |
| 127 | + input_file = None |
| 128 | + output_file = None |
| 129 | + fw_version = 0x0 |
| 130 | + |
| 131 | + # parser input arguments |
| 132 | + arguments = create_parser("") |
| 133 | + for arg in vars(arguments[0]): |
| 134 | + if (arg == "input") & (arguments[0].input is not None): |
| 135 | + input_file = arguments[0].input |
| 136 | + elif (arg == "output") & (arguments[0].output is not None): |
| 137 | + output_file = arguments[0].output |
| 138 | + elif (arg == "version") & (arguments[0].version is not None): |
| 139 | + fw_version = arguments[0].version |
| 140 | + |
| 141 | + # check file |
| 142 | + output_file = file_check(pathlib.Path(input_file), pathlib.Path(output_file)) |
| 143 | + |
| 144 | + # generate image header |
| 145 | + header = gen_img_header(fw_version) |
| 146 | + |
| 147 | + with open(input_file, 'rb') as origin_img_file: |
| 148 | + # start handling |
| 149 | + origin_img = origin_img_file.read() |
| 150 | + img_with_header = header + origin_img |
| 151 | + img_size, img_with_padding = img_padding(img_with_header) |
| 152 | + img_sha = hashlib.sha256(img_with_padding).digest() |
| 153 | + img_with_sha = img_with_padding + img_sha |
| 154 | + rompatch = gen_rompatch(img_size) |
| 155 | + final_img = rompatch + img_with_sha + bytes([0xFF] * IMG_REAR_PADDING_LEN) |
| 156 | + origin_img_file.close() |
| 157 | + |
| 158 | + with open(output_file, 'wb') as new_fw: |
| 159 | + new_fw.write(final_img) |
| 160 | + new_fw.close() |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == '__main__': |
| 164 | + main() |
0 commit comments