Clone of official tools

Committer:
The Other Jimmy
Date:
Thu Jun 22 11:12:28 2017 -0500
Revision:
36:96847d42f010
Child:
40:7d3fa6b99b2b
Tools release 5.5.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
The Other Jimmy 36:96847d42f010 1 """
The Other Jimmy 36:96847d42f010 2 mbed REALTEK_RTL8195AM elf2bin script
The Other Jimmy 36:96847d42f010 3 Copyright (c) 2011-2016 Realtek Semiconductor Corp.
The Other Jimmy 36:96847d42f010 4
The Other Jimmy 36:96847d42f010 5 Licensed under the Apache License, Version 2.0 (the "License");
The Other Jimmy 36:96847d42f010 6 you may not use this file except in compliance with the License.
The Other Jimmy 36:96847d42f010 7 You may obtain a copy of the License at
The Other Jimmy 36:96847d42f010 8
The Other Jimmy 36:96847d42f010 9 http://www.apache.org/licenses/LICENSE-2.0
The Other Jimmy 36:96847d42f010 10
The Other Jimmy 36:96847d42f010 11 Unless required by applicable law or agreed to in writing, software
The Other Jimmy 36:96847d42f010 12 distributed under the License is distributed on an "AS IS" BASIS,
The Other Jimmy 36:96847d42f010 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
The Other Jimmy 36:96847d42f010 14 See the License for the specific language governing permissions and
The Other Jimmy 36:96847d42f010 15 limitations under the License.
The Other Jimmy 36:96847d42f010 16
The Other Jimmy 36:96847d42f010 17 LIBRARIES BUILD
The Other Jimmy 36:96847d42f010 18 """
The Other Jimmy 36:96847d42f010 19
The Other Jimmy 36:96847d42f010 20 import sys, array, struct, os, re, subprocess
The Other Jimmy 36:96847d42f010 21 import hashlib
The Other Jimmy 36:96847d42f010 22
The Other Jimmy 36:96847d42f010 23 from tools.paths import TOOLS_BOOTLOADERS
The Other Jimmy 36:96847d42f010 24 from datetime import datetime
The Other Jimmy 36:96847d42f010 25
The Other Jimmy 36:96847d42f010 26 # Constant Variables
The Other Jimmy 36:96847d42f010 27 RAM2_RSVD = 0x3131373835393138
The Other Jimmy 36:96847d42f010 28
The Other Jimmy 36:96847d42f010 29 def write_fixed_width_string(value, width, output):
The Other Jimmy 36:96847d42f010 30 # cut string to list & reverse
The Other Jimmy 36:96847d42f010 31 line = [value[i:i+2] for i in range(0, len(value), 2)]
The Other Jimmy 36:96847d42f010 32 output.write("".join([chr(long(b, 16)) for b in line]))
The Other Jimmy 36:96847d42f010 33
The Other Jimmy 36:96847d42f010 34 def write_fixed_width_value(value, width, output):
The Other Jimmy 36:96847d42f010 35 # convert to string
The Other Jimmy 36:96847d42f010 36 line = format(value, '0%dx' % (width))
The Other Jimmy 36:96847d42f010 37 if len(line) > width:
The Other Jimmy 36:96847d42f010 38 print "[ERROR] value 0x%s cannot fit width %d" % (line, width)
The Other Jimmy 36:96847d42f010 39 sys.exit(-1)
The Other Jimmy 36:96847d42f010 40 # cut string to list & reverse
The Other Jimmy 36:96847d42f010 41 line = [line[i:i+2] for i in range(0, len(line), 2)]
The Other Jimmy 36:96847d42f010 42 line.reverse()
The Other Jimmy 36:96847d42f010 43 # convert to write buffer
The Other Jimmy 36:96847d42f010 44 output.write("".join([chr(long(b, 16)) for b in line]))
The Other Jimmy 36:96847d42f010 45
The Other Jimmy 36:96847d42f010 46 def append_image_file(image, output):
The Other Jimmy 36:96847d42f010 47 input = open(image, "rb")
The Other Jimmy 36:96847d42f010 48 output.write(input.read())
The Other Jimmy 36:96847d42f010 49 input.close()
The Other Jimmy 36:96847d42f010 50
The Other Jimmy 36:96847d42f010 51 def prepend(image, image_prepend, toolchain, info):
The Other Jimmy 36:96847d42f010 52 output = open(image_prepend, "wb")
The Other Jimmy 36:96847d42f010 53 write_fixed_width_value(info['size'], 8, output)
The Other Jimmy 36:96847d42f010 54 write_fixed_width_value(info['addr'], 8, output)
The Other Jimmy 36:96847d42f010 55 write_fixed_width_value(RAM2_RSVD, 16, output)
The Other Jimmy 36:96847d42f010 56 with open(image, "rb") as input:
The Other Jimmy 36:96847d42f010 57 if toolchain == "IAR":
The Other Jimmy 36:96847d42f010 58 input.seek(info['addr'])
The Other Jimmy 36:96847d42f010 59 output.write(input.read(info['size']))
The Other Jimmy 36:96847d42f010 60 output.close()
The Other Jimmy 36:96847d42f010 61
The Other Jimmy 36:96847d42f010 62 def parse_section(toolchain, elf, section):
The Other Jimmy 36:96847d42f010 63 info = {'addr':None, 'size':0};
The Other Jimmy 36:96847d42f010 64 if toolchain not in ["GCC_ARM", "ARM_STD", "ARM", "ARM_MICRO", "IAR"]:
The Other Jimmy 36:96847d42f010 65 print "[ERROR] unsupported toolchain " + toolchain
The Other Jimmy 36:96847d42f010 66 sys.exit(-1)
The Other Jimmy 36:96847d42f010 67
The Other Jimmy 36:96847d42f010 68 mapfile = elf.rsplit(".", 1)[0] + ".map"
The Other Jimmy 36:96847d42f010 69
The Other Jimmy 36:96847d42f010 70 with open(mapfile, 'r') as infile:
The Other Jimmy 36:96847d42f010 71 # Search area to parse
The Other Jimmy 36:96847d42f010 72 for line in infile:
The Other Jimmy 36:96847d42f010 73 if toolchain == "GCC_ARM":
The Other Jimmy 36:96847d42f010 74 # .image2.table 0x[00000000]30000000 0x18
The Other Jimmy 36:96847d42f010 75 # 0x[00000000]30000000 __image2_start__ = .
The Other Jimmy 36:96847d42f010 76 # 0x[00000000]30000000 __image2_entry_func__ = .
The Other Jimmy 36:96847d42f010 77 match = re.match(r'^' + section + \
The Other Jimmy 36:96847d42f010 78 r'\s+0x0{,8}(?P<addr>[0-9A-Fa-f]{8})\s+0x(?P<size>[0-9A-Fa-f]+).*$', line)
The Other Jimmy 36:96847d42f010 79 elif toolchain in ["ARM_STD", "ARM", "ARM_MICRO"]:
The Other Jimmy 36:96847d42f010 80 # Memory Map of the image
The Other Jimmy 36:96847d42f010 81 # Load Region LR_DRAM (Base: 0x30000000, Size: 0x00006a74, Max: 0x00200000, ABSOLUTE)
The Other Jimmy 36:96847d42f010 82 # Execution Region IMAGE2_TABLE (Base: 0x30000000, Size: 0x00000018, Max: 0xffffffff, ABSOLUTE, FIXED)
The Other Jimmy 36:96847d42f010 83 # Base Addr Size Type Attr Idx E Section Name Object
The Other Jimmy 36:96847d42f010 84 # 0x30000000 0x00000004 Data RO 5257 .image2.ram.data rtl8195a_init.o
The Other Jimmy 36:96847d42f010 85 match = re.match(r'^.*Region\s+' + section + \
The Other Jimmy 36:96847d42f010 86 r'\s+\(Base: 0x(?P<addr>[0-9A-Fa-f]{8}),\s+Size: 0x(?P<size>[0-9A-Fa-f]+), .*\)$', line)
The Other Jimmy 36:96847d42f010 87 elif toolchain == "IAR":
The Other Jimmy 36:96847d42f010 88 # Section Kind Address Size Object
The Other Jimmy 36:96847d42f010 89 # ------- ---- ------- ---- ------
The Other Jimmy 36:96847d42f010 90 # "A3": 0x8470
The Other Jimmy 36:96847d42f010 91 # IMAGE2 0x10006000 0x5d18 <Block>
The Other Jimmy 36:96847d42f010 92 # .ram_image2.text 0x10006000 0x5bbc <Block>
The Other Jimmy 36:96847d42f010 93 # .rodata const 0x10006000 0x14 retarget.o [17]
The Other Jimmy 36:96847d42f010 94 match = re.match(r'^\s+' + section + \
The Other Jimmy 36:96847d42f010 95 r'\s+0x(?P<addr>[0-9A-Fa-f]{8})\s+0x(?P<size>[0-9A-Fa-f]+)\s+.*<Block>$', line)
The Other Jimmy 36:96847d42f010 96 if match:
The Other Jimmy 36:96847d42f010 97 info['addr'] = int(match.group("addr"), 16)
The Other Jimmy 36:96847d42f010 98 try:
The Other Jimmy 36:96847d42f010 99 info['size'] = int(match.group("size"), 16)
The Other Jimmy 36:96847d42f010 100 except IndexError:
The Other Jimmy 36:96847d42f010 101 print "[WARNING] cannot find the size of section " + section
The Other Jimmy 36:96847d42f010 102 return info
The Other Jimmy 36:96847d42f010 103
The Other Jimmy 36:96847d42f010 104 print "[ERROR] cannot find the address of section " + section
The Other Jimmy 36:96847d42f010 105 return info
The Other Jimmy 36:96847d42f010 106
The Other Jimmy 36:96847d42f010 107 # ----------------------------
The Other Jimmy 36:96847d42f010 108 # main function
The Other Jimmy 36:96847d42f010 109 # ----------------------------
The Other Jimmy 36:96847d42f010 110 def rtl8195a_elf2bin(toolchain, image_elf, image_bin):
The Other Jimmy 36:96847d42f010 111 if toolchain == "GCC_ARM":
The Other Jimmy 36:96847d42f010 112 img2_sections = [".image2.table", ".text", ".data"]
The Other Jimmy 36:96847d42f010 113 elif toolchain in ["ARM_STD", "ARM", "ARM_MICRO"]:
The Other Jimmy 36:96847d42f010 114 img2_sections = [".image2.table", ".text", ".data"]
The Other Jimmy 36:96847d42f010 115 elif toolchain == "IAR":
The Other Jimmy 36:96847d42f010 116 # actually it's block
The Other Jimmy 36:96847d42f010 117 img2_sections = ["IMAGE2"]
The Other Jimmy 36:96847d42f010 118 else:
The Other Jimmy 36:96847d42f010 119 print("[error] unsupported toolchain") + toolchain
The Other Jimmy 36:96847d42f010 120 return
The Other Jimmy 36:96847d42f010 121 ram2_info = {'addr':None, 'size':0}
The Other Jimmy 36:96847d42f010 122 image_name = os.path.splitext(image_elf)[0]
The Other Jimmy 36:96847d42f010 123
The Other Jimmy 36:96847d42f010 124 ram1_prepend_bin = os.path.join(TOOLS_BOOTLOADERS, "REALTEK_RTL8195AM", "ram_1_prepend.bin")
The Other Jimmy 36:96847d42f010 125 ram2_prepend_bin = image_name + '-ram_2_prepend.bin'
The Other Jimmy 36:96847d42f010 126
The Other Jimmy 36:96847d42f010 127 old_bin = image_name + '.bin'
The Other Jimmy 36:96847d42f010 128 for section in img2_sections:
The Other Jimmy 36:96847d42f010 129 section_info = parse_section(toolchain, image_elf, section)
The Other Jimmy 36:96847d42f010 130 if ram2_info['addr'] is None or ram2_info['addr'] > section_info['addr']:
The Other Jimmy 36:96847d42f010 131 ram2_info['addr'] = section_info['addr']
The Other Jimmy 36:96847d42f010 132 ram2_info['size'] = ram2_info['size'] + section_info['size']
The Other Jimmy 36:96847d42f010 133
The Other Jimmy 36:96847d42f010 134 prepend(old_bin, ram2_prepend_bin, toolchain, ram2_info)
The Other Jimmy 36:96847d42f010 135 # write output file
The Other Jimmy 36:96847d42f010 136 output = open(image_bin, "wb")
The Other Jimmy 36:96847d42f010 137 append_image_file(ram1_prepend_bin, output)
The Other Jimmy 36:96847d42f010 138 append_image_file(ram2_prepend_bin, output)
The Other Jimmy 36:96847d42f010 139 output.close()
The Other Jimmy 36:96847d42f010 140 # post built done
The Other Jimmy 36:96847d42f010 141