Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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