Clone of official tools
targets/REALTEK_RTL8195AM.py@40:7d3fa6b99b2b, 2017-10-10 (annotated)
- Committer:
- theotherjimmy
- Date:
- Tue Oct 10 16:56:30 2017 -0500
- Revision:
- 40:7d3fa6b99b2b
- Parent:
- 36:96847d42f010
- Child:
- 41:2a77626a4c21
Update to tools release 5.6.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
The Other Jimmy |
36:96847d42f010 | 1 | """ |
theotherjimmy |
40:7d3fa6b99b2b | 2 | Realtek Semiconductor Corp. |
The Other Jimmy |
36:96847d42f010 | 3 | |
theotherjimmy |
40:7d3fa6b99b2b | 4 | RTL8195A elf2bin script |
The Other Jimmy |
36:96847d42f010 | 5 | """ |
The Other Jimmy |
36:96847d42f010 | 6 | |
The Other Jimmy |
36:96847d42f010 | 7 | import sys, array, struct, os, re, subprocess |
The Other Jimmy |
36:96847d42f010 | 8 | import hashlib |
theotherjimmy |
40:7d3fa6b99b2b | 9 | import shutil |
The Other Jimmy |
36:96847d42f010 | 10 | |
The Other Jimmy |
36:96847d42f010 | 11 | from tools.paths import TOOLS_BOOTLOADERS |
The Other Jimmy |
36:96847d42f010 | 12 | from datetime import datetime |
The Other Jimmy |
36:96847d42f010 | 13 | |
The Other Jimmy |
36:96847d42f010 | 14 | # Constant Variables |
theotherjimmy |
40:7d3fa6b99b2b | 15 | RAM2_RSVD = 0x00000000 |
theotherjimmy |
40:7d3fa6b99b2b | 16 | RAM2_VER = 0x8195FFFF00000000 |
theotherjimmy |
40:7d3fa6b99b2b | 17 | RAM2_TAG = 0x81950001 |
theotherjimmy |
40:7d3fa6b99b2b | 18 | RAM2_SHA = '0' |
The Other Jimmy |
36:96847d42f010 | 19 | |
The Other Jimmy |
36:96847d42f010 | 20 | def write_fixed_width_string(value, width, output): |
theotherjimmy |
40:7d3fa6b99b2b | 21 | # cut string to list & reverse |
theotherjimmy |
40:7d3fa6b99b2b | 22 | line = [value[i:i+2] for i in range(0, len(value), 2)] |
theotherjimmy |
40:7d3fa6b99b2b | 23 | output.write("".join([chr(long(b, 16)) for b in line])) |
The Other Jimmy |
36:96847d42f010 | 24 | |
The Other Jimmy |
36:96847d42f010 | 25 | def write_fixed_width_value(value, width, output): |
theotherjimmy |
40:7d3fa6b99b2b | 26 | # convert to string |
theotherjimmy |
40:7d3fa6b99b2b | 27 | line = format(value, '0%dx' % (width)) |
theotherjimmy |
40:7d3fa6b99b2b | 28 | if len(line) > width: |
theotherjimmy |
40:7d3fa6b99b2b | 29 | print "[ERROR] value 0x%s cannot fit width %d" % (line, width) |
theotherjimmy |
40:7d3fa6b99b2b | 30 | sys.exit(-1) |
theotherjimmy |
40:7d3fa6b99b2b | 31 | # cut string to list & reverse |
theotherjimmy |
40:7d3fa6b99b2b | 32 | line = [line[i:i+2] for i in range(0, len(line), 2)] |
theotherjimmy |
40:7d3fa6b99b2b | 33 | line.reverse() |
theotherjimmy |
40:7d3fa6b99b2b | 34 | # convert to write buffer |
theotherjimmy |
40:7d3fa6b99b2b | 35 | output.write("".join([chr(long(b, 16)) for b in line])) |
The Other Jimmy |
36:96847d42f010 | 36 | |
The Other Jimmy |
36:96847d42f010 | 37 | def append_image_file(image, output): |
The Other Jimmy |
36:96847d42f010 | 38 | input = open(image, "rb") |
The Other Jimmy |
36:96847d42f010 | 39 | output.write(input.read()) |
The Other Jimmy |
36:96847d42f010 | 40 | input.close() |
The Other Jimmy |
36:96847d42f010 | 41 | |
theotherjimmy |
40:7d3fa6b99b2b | 42 | def write_padding_bytes(output_name, size): |
theotherjimmy |
40:7d3fa6b99b2b | 43 | current_size = os.stat(output_name).st_size |
theotherjimmy |
40:7d3fa6b99b2b | 44 | padcount = size - current_size |
theotherjimmy |
40:7d3fa6b99b2b | 45 | if padcount < 0: |
theotherjimmy |
40:7d3fa6b99b2b | 46 | print "[ERROR] image is larger than expected size" |
theotherjimmy |
40:7d3fa6b99b2b | 47 | sys.exit(-1) |
theotherjimmy |
40:7d3fa6b99b2b | 48 | output = open(output_name, "ab") |
theotherjimmy |
40:7d3fa6b99b2b | 49 | output.write('\377' * padcount) |
The Other Jimmy |
36:96847d42f010 | 50 | output.close() |
The Other Jimmy |
36:96847d42f010 | 51 | |
theotherjimmy |
40:7d3fa6b99b2b | 52 | def sha256_checksum(filename, block_size=65536): |
theotherjimmy |
40:7d3fa6b99b2b | 53 | sha256 = hashlib.sha256() |
theotherjimmy |
40:7d3fa6b99b2b | 54 | with open(filename, 'rb') as f: |
theotherjimmy |
40:7d3fa6b99b2b | 55 | for block in iter(lambda: f.read(block_size), b''): |
theotherjimmy |
40:7d3fa6b99b2b | 56 | sha256.update(block) |
theotherjimmy |
40:7d3fa6b99b2b | 57 | return sha256.hexdigest() |
The Other Jimmy |
36:96847d42f010 | 58 | |
theotherjimmy |
40:7d3fa6b99b2b | 59 | def get_version_by_time(): |
theotherjimmy |
40:7d3fa6b99b2b | 60 | secs = int((datetime.now()-datetime(2016,11,1)).total_seconds()) |
theotherjimmy |
40:7d3fa6b99b2b | 61 | return RAM2_VER + secs |
The Other Jimmy |
36:96847d42f010 | 62 | |
The Other Jimmy |
36:96847d42f010 | 63 | # ---------------------------- |
The Other Jimmy |
36:96847d42f010 | 64 | # main function |
The Other Jimmy |
36:96847d42f010 | 65 | # ---------------------------- |
theotherjimmy |
40:7d3fa6b99b2b | 66 | def prepend(image, entry, segment, image_ram2, image_ota): |
theotherjimmy |
40:7d3fa6b99b2b | 67 | |
theotherjimmy |
40:7d3fa6b99b2b | 68 | # parse input arguments |
theotherjimmy |
40:7d3fa6b99b2b | 69 | output = open(image_ram2, "wb") |
theotherjimmy |
40:7d3fa6b99b2b | 70 | |
theotherjimmy |
40:7d3fa6b99b2b | 71 | write_fixed_width_value(os.stat(image).st_size, 8, output) |
theotherjimmy |
40:7d3fa6b99b2b | 72 | write_fixed_width_value(int(entry), 8, output) |
theotherjimmy |
40:7d3fa6b99b2b | 73 | write_fixed_width_value(int(segment), 8, output) |
theotherjimmy |
40:7d3fa6b99b2b | 74 | |
theotherjimmy |
40:7d3fa6b99b2b | 75 | RAM2_SHA = sha256_checksum(image) |
theotherjimmy |
40:7d3fa6b99b2b | 76 | write_fixed_width_value(RAM2_TAG, 8, output) |
theotherjimmy |
40:7d3fa6b99b2b | 77 | write_fixed_width_value(get_version_by_time(), 16, output) |
theotherjimmy |
40:7d3fa6b99b2b | 78 | write_fixed_width_string(RAM2_SHA, 64, output) |
theotherjimmy |
40:7d3fa6b99b2b | 79 | write_fixed_width_value(RAM2_RSVD, 8, output) |
theotherjimmy |
40:7d3fa6b99b2b | 80 | |
theotherjimmy |
40:7d3fa6b99b2b | 81 | append_image_file(image, output) |
theotherjimmy |
40:7d3fa6b99b2b | 82 | output.close() |
theotherjimmy |
40:7d3fa6b99b2b | 83 | |
theotherjimmy |
40:7d3fa6b99b2b | 84 | ota = open(image_ota, "wb") |
theotherjimmy |
40:7d3fa6b99b2b | 85 | write_fixed_width_value(os.stat(image).st_size, 8, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 86 | write_fixed_width_value(int(entry), 8, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 87 | write_fixed_width_value(int(segment), 8, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 88 | write_fixed_width_value(0xFFFFFFFF, 8, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 89 | write_fixed_width_value(get_version_by_time(), 16, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 90 | write_fixed_width_string(RAM2_SHA, 64, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 91 | write_fixed_width_value(RAM2_RSVD, 8, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 92 | |
theotherjimmy |
40:7d3fa6b99b2b | 93 | append_image_file(image, ota) |
theotherjimmy |
40:7d3fa6b99b2b | 94 | ota.close() |
theotherjimmy |
40:7d3fa6b99b2b | 95 | |
theotherjimmy |
40:7d3fa6b99b2b | 96 | def find_symbol(toolchain, mapfile, symbol): |
theotherjimmy |
40:7d3fa6b99b2b | 97 | ret = None |
theotherjimmy |
40:7d3fa6b99b2b | 98 | |
theotherjimmy |
40:7d3fa6b99b2b | 99 | HEX = '0x0{,8}(?P<addr>[0-9A-Fa-f]{8})' |
The Other Jimmy |
36:96847d42f010 | 100 | if toolchain == "GCC_ARM": |
theotherjimmy |
40:7d3fa6b99b2b | 101 | SYM = re.compile(r'^\s+' + HEX + r'\s+' + symbol + '\r?$') |
The Other Jimmy |
36:96847d42f010 | 102 | elif toolchain in ["ARM_STD", "ARM", "ARM_MICRO"]: |
theotherjimmy |
40:7d3fa6b99b2b | 103 | SYM = re.compile(r'^\s+' + HEX + r'\s+0x[0-9A-Fa-f]{8}\s+Code.*\s+i\.' + symbol + r'\s+.*$') |
The Other Jimmy |
36:96847d42f010 | 104 | elif toolchain == "IAR": |
theotherjimmy |
40:7d3fa6b99b2b | 105 | SYM = re.compile(r'^' + symbol + r'\s+' + HEX + '\s+.*$') |
theotherjimmy |
40:7d3fa6b99b2b | 106 | |
theotherjimmy |
40:7d3fa6b99b2b | 107 | with open(mapfile, 'r') as infile: |
theotherjimmy |
40:7d3fa6b99b2b | 108 | for line in infile: |
theotherjimmy |
40:7d3fa6b99b2b | 109 | match = re.match(SYM, line) |
theotherjimmy |
40:7d3fa6b99b2b | 110 | if match: |
theotherjimmy |
40:7d3fa6b99b2b | 111 | ret = match.group("addr") |
theotherjimmy |
40:7d3fa6b99b2b | 112 | |
theotherjimmy |
40:7d3fa6b99b2b | 113 | if not ret: |
theotherjimmy |
40:7d3fa6b99b2b | 114 | print "[ERROR] cannot find the address of symbol " + symbol |
theotherjimmy |
40:7d3fa6b99b2b | 115 | return 0 |
theotherjimmy |
40:7d3fa6b99b2b | 116 | |
theotherjimmy |
40:7d3fa6b99b2b | 117 | return int(ret,16) | 1 |
theotherjimmy |
40:7d3fa6b99b2b | 118 | |
theotherjimmy |
40:7d3fa6b99b2b | 119 | def parse_load_segment_gcc(image_elf): |
theotherjimmy |
40:7d3fa6b99b2b | 120 | # Program Headers: |
theotherjimmy |
40:7d3fa6b99b2b | 121 | # Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align |
theotherjimmy |
40:7d3fa6b99b2b | 122 | # LOAD 0x000034 0x10006000 0x10006000 0x026bc 0x026bc RW 0x8 |
theotherjimmy |
40:7d3fa6b99b2b | 123 | # LOAD 0x0026f0 0x30000000 0x30000000 0x06338 0x06338 RWE 0x4 |
theotherjimmy |
40:7d3fa6b99b2b | 124 | segment_list = [] |
theotherjimmy |
40:7d3fa6b99b2b | 125 | cmd = 'arm-none-eabi-readelf -l ' + image_elf |
theotherjimmy |
40:7d3fa6b99b2b | 126 | for line in subprocess.check_output(cmd, shell=True, universal_newlines=True).split("\n"): |
theotherjimmy |
40:7d3fa6b99b2b | 127 | if not line.startswith(" LOAD"): |
theotherjimmy |
40:7d3fa6b99b2b | 128 | continue |
theotherjimmy |
40:7d3fa6b99b2b | 129 | segment = line.split() |
theotherjimmy |
40:7d3fa6b99b2b | 130 | if len(segment) != 8: |
theotherjimmy |
40:7d3fa6b99b2b | 131 | continue |
theotherjimmy |
40:7d3fa6b99b2b | 132 | offset = int(segment[1][2:], 16) |
theotherjimmy |
40:7d3fa6b99b2b | 133 | addr = int(segment[2][2:], 16) |
theotherjimmy |
40:7d3fa6b99b2b | 134 | size = int(segment[4][2:], 16) |
theotherjimmy |
40:7d3fa6b99b2b | 135 | if addr != 0 and size != 0: |
theotherjimmy |
40:7d3fa6b99b2b | 136 | segment_list.append((offset, addr, size)) |
theotherjimmy |
40:7d3fa6b99b2b | 137 | return segment_list |
theotherjimmy |
40:7d3fa6b99b2b | 138 | |
theotherjimmy |
40:7d3fa6b99b2b | 139 | def parse_load_segment_armcc(image_elf): |
theotherjimmy |
40:7d3fa6b99b2b | 140 | # ==================================== |
theotherjimmy |
40:7d3fa6b99b2b | 141 | # |
theotherjimmy |
40:7d3fa6b99b2b | 142 | # ** Program header #2 |
theotherjimmy |
40:7d3fa6b99b2b | 143 | # |
theotherjimmy |
40:7d3fa6b99b2b | 144 | # Type : PT_LOAD (1) |
theotherjimmy |
40:7d3fa6b99b2b | 145 | # File Offset : 52 (0x34) |
theotherjimmy |
40:7d3fa6b99b2b | 146 | # Virtual Addr : 0x30000000 |
theotherjimmy |
40:7d3fa6b99b2b | 147 | # Physical Addr : 0x30000000 |
theotherjimmy |
40:7d3fa6b99b2b | 148 | # Size in file : 27260 bytes (0x6a7c) |
theotherjimmy |
40:7d3fa6b99b2b | 149 | # Size in memory: 42168 bytes (0xa4b8) |
theotherjimmy |
40:7d3fa6b99b2b | 150 | # Flags : PF_X + PF_W + PF_R + PF_ARM_ENTRY (0x80000007) |
theotherjimmy |
40:7d3fa6b99b2b | 151 | # Alignment : 8 |
theotherjimmy |
40:7d3fa6b99b2b | 152 | # |
theotherjimmy |
40:7d3fa6b99b2b | 153 | (offset, addr, size) = (0, 0, 0) |
theotherjimmy |
40:7d3fa6b99b2b | 154 | segment_list = [] |
theotherjimmy |
40:7d3fa6b99b2b | 155 | in_segment = False |
theotherjimmy |
40:7d3fa6b99b2b | 156 | cmd = 'fromelf --text -v --only=none ' + image_elf |
theotherjimmy |
40:7d3fa6b99b2b | 157 | for line in subprocess.check_output(cmd, shell=True, universal_newlines=True).split("\n"): |
theotherjimmy |
40:7d3fa6b99b2b | 158 | if line == "": |
theotherjimmy |
40:7d3fa6b99b2b | 159 | pass |
theotherjimmy |
40:7d3fa6b99b2b | 160 | elif line.startswith("** Program header"): |
theotherjimmy |
40:7d3fa6b99b2b | 161 | in_segment = True |
theotherjimmy |
40:7d3fa6b99b2b | 162 | elif in_segment == False: |
theotherjimmy |
40:7d3fa6b99b2b | 163 | pass |
theotherjimmy |
40:7d3fa6b99b2b | 164 | elif line.startswith("============"): |
theotherjimmy |
40:7d3fa6b99b2b | 165 | if addr != 0 and size != 0: |
theotherjimmy |
40:7d3fa6b99b2b | 166 | segment_list.append((offset, addr, size)) |
theotherjimmy |
40:7d3fa6b99b2b | 167 | in_segment = False |
theotherjimmy |
40:7d3fa6b99b2b | 168 | (offset, addr, size) = (0, 0, 0) |
theotherjimmy |
40:7d3fa6b99b2b | 169 | elif line.startswith(" Type"): |
theotherjimmy |
40:7d3fa6b99b2b | 170 | if not re.match(r'\s+Type\s+:\s+PT_LOAD\s.*$', line): |
theotherjimmy |
40:7d3fa6b99b2b | 171 | in_segment = False |
theotherjimmy |
40:7d3fa6b99b2b | 172 | elif line.startswith(" File Offset"): |
theotherjimmy |
40:7d3fa6b99b2b | 173 | match = re.match(r'^\s+File Offset\s+:\s+(?P<offset>\d+).*$', line) |
theotherjimmy |
40:7d3fa6b99b2b | 174 | if match: |
theotherjimmy |
40:7d3fa6b99b2b | 175 | offset = int(match.group("offset")) |
theotherjimmy |
40:7d3fa6b99b2b | 176 | elif line.startswith(" Virtual Addr"): |
theotherjimmy |
40:7d3fa6b99b2b | 177 | match = re.match(r'^\s+Virtual Addr\s+:\s+0x(?P<addr>[0-9a-f]+).*$', line) |
theotherjimmy |
40:7d3fa6b99b2b | 178 | if match: |
theotherjimmy |
40:7d3fa6b99b2b | 179 | addr = int(match.group("addr"), 16) |
theotherjimmy |
40:7d3fa6b99b2b | 180 | elif line.startswith(" Size in file"): |
theotherjimmy |
40:7d3fa6b99b2b | 181 | match = re.match(r'^\s+Size in file\s+:.*\(0x(?P<size>[0-9a-f]+)\).*$', line) |
theotherjimmy |
40:7d3fa6b99b2b | 182 | if match: |
theotherjimmy |
40:7d3fa6b99b2b | 183 | size = int(match.group("size"), 16) |
theotherjimmy |
40:7d3fa6b99b2b | 184 | return segment_list |
theotherjimmy |
40:7d3fa6b99b2b | 185 | |
theotherjimmy |
40:7d3fa6b99b2b | 186 | |
theotherjimmy |
40:7d3fa6b99b2b | 187 | def parse_load_segment_iar(image_elf): |
theotherjimmy |
40:7d3fa6b99b2b | 188 | # SEGMENTS: |
theotherjimmy |
40:7d3fa6b99b2b | 189 | # |
theotherjimmy |
40:7d3fa6b99b2b | 190 | # Type Offset Virtual Physical File Sz Mem Sz Flags Align |
theotherjimmy |
40:7d3fa6b99b2b | 191 | # ---- ------ ------- -------- ------- ------ ----- ----- |
theotherjimmy |
40:7d3fa6b99b2b | 192 | # 0: load 0x34 0x10006000 0x10006000 0x26bc 0x26bc 0x6 WR 0x8 |
theotherjimmy |
40:7d3fa6b99b2b | 193 | # 1: load 0x26f0 0x30000000 0x30000000 0x6338 0x6338 0x7 XWR 0x4 |
theotherjimmy |
40:7d3fa6b99b2b | 194 | # |
theotherjimmy |
40:7d3fa6b99b2b | 195 | # SECTIONS: |
theotherjimmy |
40:7d3fa6b99b2b | 196 | # |
theotherjimmy |
40:7d3fa6b99b2b | 197 | # Name Type Addr Offset Size Aln Lnk Inf ESz Flags |
theotherjimmy |
40:7d3fa6b99b2b | 198 | # ---- ---- ---- ------ ---- --- --- --- --- ----- |
theotherjimmy |
40:7d3fa6b99b2b | 199 | # 1: .shstrtab strtab 0xfc4d8 0x60 0x4 |
theotherjimmy |
40:7d3fa6b99b2b | 200 | # 2: .strtab strtab 0xfc538 0xbb3f 0x4 |
theotherjimmy |
40:7d3fa6b99b2b | 201 | |
theotherjimmy |
40:7d3fa6b99b2b | 202 | segment_list = [] |
theotherjimmy |
40:7d3fa6b99b2b | 203 | in_segment = False |
theotherjimmy |
40:7d3fa6b99b2b | 204 | cmd = 'ielfdumparm ' + image_elf |
theotherjimmy |
40:7d3fa6b99b2b | 205 | for line in subprocess.check_output(cmd, shell=True, universal_newlines=True).split("\n"): |
theotherjimmy |
40:7d3fa6b99b2b | 206 | if line.startswith(" SEGMENTS:"): |
theotherjimmy |
40:7d3fa6b99b2b | 207 | in_segment = True |
theotherjimmy |
40:7d3fa6b99b2b | 208 | elif in_segment == False: |
theotherjimmy |
40:7d3fa6b99b2b | 209 | pass |
theotherjimmy |
40:7d3fa6b99b2b | 210 | elif line.startswith(" SECTIONS:"): |
theotherjimmy |
40:7d3fa6b99b2b | 211 | break |
theotherjimmy |
40:7d3fa6b99b2b | 212 | elif re.match(r'^\s+\w+:\s+load\s+.*$', line): |
theotherjimmy |
40:7d3fa6b99b2b | 213 | segment = line.split() |
theotherjimmy |
40:7d3fa6b99b2b | 214 | offset = int(segment[2][2:], 16) |
theotherjimmy |
40:7d3fa6b99b2b | 215 | addr = int(segment[3][2:], 16) |
theotherjimmy |
40:7d3fa6b99b2b | 216 | size = int(segment[5][2:], 16) |
theotherjimmy |
40:7d3fa6b99b2b | 217 | if addr < 0x10007000: |
theotherjimmy |
40:7d3fa6b99b2b | 218 | continue |
theotherjimmy |
40:7d3fa6b99b2b | 219 | if addr != 0 and size != 0: |
theotherjimmy |
40:7d3fa6b99b2b | 220 | segment_list.append((offset, addr, size)) |
theotherjimmy |
40:7d3fa6b99b2b | 221 | return segment_list |
theotherjimmy |
40:7d3fa6b99b2b | 222 | |
theotherjimmy |
40:7d3fa6b99b2b | 223 | def parse_load_segment(toolchain, image_elf): |
theotherjimmy |
40:7d3fa6b99b2b | 224 | if toolchain == "GCC_ARM": |
theotherjimmy |
40:7d3fa6b99b2b | 225 | return parse_load_segment_gcc(image_elf) |
theotherjimmy |
40:7d3fa6b99b2b | 226 | elif toolchain in ["ARM_STD", "ARM", "ARM_MICRO"]: |
theotherjimmy |
40:7d3fa6b99b2b | 227 | return parse_load_segment_armcc(image_elf) |
theotherjimmy |
40:7d3fa6b99b2b | 228 | elif toolchain == "IAR": |
theotherjimmy |
40:7d3fa6b99b2b | 229 | return parse_load_segment_iar(image_elf) |
The Other Jimmy |
36:96847d42f010 | 230 | else: |
theotherjimmy |
40:7d3fa6b99b2b | 231 | return [] |
theotherjimmy |
40:7d3fa6b99b2b | 232 | |
theotherjimmy |
40:7d3fa6b99b2b | 233 | def write_load_segment(image_elf, image_bin, segment): |
theotherjimmy |
40:7d3fa6b99b2b | 234 | file_elf = open(image_elf, "rb") |
theotherjimmy |
40:7d3fa6b99b2b | 235 | file_bin = open(image_bin, "wb") |
theotherjimmy |
40:7d3fa6b99b2b | 236 | for (offset, addr, size) in segment: |
theotherjimmy |
40:7d3fa6b99b2b | 237 | file_elf.seek(offset) |
theotherjimmy |
40:7d3fa6b99b2b | 238 | # write image header - size & addr |
theotherjimmy |
40:7d3fa6b99b2b | 239 | write_fixed_width_value(addr, 8, file_bin) |
theotherjimmy |
40:7d3fa6b99b2b | 240 | write_fixed_width_value(size, 8, file_bin) |
theotherjimmy |
40:7d3fa6b99b2b | 241 | # write load segment |
theotherjimmy |
40:7d3fa6b99b2b | 242 | file_bin.write(file_elf.read(size)) |
theotherjimmy |
40:7d3fa6b99b2b | 243 | delta = size % 4 |
theotherjimmy |
40:7d3fa6b99b2b | 244 | if delta != 0: |
theotherjimmy |
40:7d3fa6b99b2b | 245 | padding = 4 - delta |
theotherjimmy |
40:7d3fa6b99b2b | 246 | write_fixed_width_value(0x0, padding * 2, file_bin) |
theotherjimmy |
40:7d3fa6b99b2b | 247 | file_bin.close() |
theotherjimmy |
40:7d3fa6b99b2b | 248 | file_elf.close() |
theotherjimmy |
40:7d3fa6b99b2b | 249 | |
theotherjimmy |
40:7d3fa6b99b2b | 250 | # ---------------------------- |
theotherjimmy |
40:7d3fa6b99b2b | 251 | # main function |
theotherjimmy |
40:7d3fa6b99b2b | 252 | # ---------------------------- |
theotherjimmy |
40:7d3fa6b99b2b | 253 | def rtl8195a_elf2bin(t_self, image_elf, image_bin): |
theotherjimmy |
40:7d3fa6b99b2b | 254 | # remove target binary file/path |
theotherjimmy |
40:7d3fa6b99b2b | 255 | if os.path.isfile(image_bin): |
theotherjimmy |
40:7d3fa6b99b2b | 256 | os.remove(image_bin) |
theotherjimmy |
40:7d3fa6b99b2b | 257 | else: |
theotherjimmy |
40:7d3fa6b99b2b | 258 | shutil.rmtree(image_bin) |
theotherjimmy |
40:7d3fa6b99b2b | 259 | |
theotherjimmy |
40:7d3fa6b99b2b | 260 | segment = parse_load_segment(t_self.name, image_elf) |
theotherjimmy |
40:7d3fa6b99b2b | 261 | write_load_segment(image_elf, image_bin, segment) |
theotherjimmy |
40:7d3fa6b99b2b | 262 | |
The Other Jimmy |
36:96847d42f010 | 263 | image_name = os.path.splitext(image_elf)[0] |
theotherjimmy |
40:7d3fa6b99b2b | 264 | image_map = image_name + '.map' |
The Other Jimmy |
36:96847d42f010 | 265 | |
theotherjimmy |
40:7d3fa6b99b2b | 266 | ram2_ent = find_symbol(t_self.name, image_map, "PLAT_Start") |
theotherjimmy |
40:7d3fa6b99b2b | 267 | ram1_bin = os.path.join(TOOLS_BOOTLOADERS, "REALTEK_RTL8195AM", "ram_1.bin") |
theotherjimmy |
40:7d3fa6b99b2b | 268 | ram2_bin = image_name + '-ram_2.bin' |
theotherjimmy |
40:7d3fa6b99b2b | 269 | ota_bin = image_name + '-ota.bin' |
theotherjimmy |
40:7d3fa6b99b2b | 270 | prepend(image_bin, ram2_ent, len(segment), ram2_bin, ota_bin) |
The Other Jimmy |
36:96847d42f010 | 271 | |
The Other Jimmy |
36:96847d42f010 | 272 | # write output file |
The Other Jimmy |
36:96847d42f010 | 273 | output = open(image_bin, "wb") |
theotherjimmy |
40:7d3fa6b99b2b | 274 | append_image_file(ram1_bin, output) |
theotherjimmy |
40:7d3fa6b99b2b | 275 | append_image_file(ram2_bin, output) |
The Other Jimmy |
36:96847d42f010 | 276 | output.close() |