| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import os |
| 20 |
import argparse |
| 21 |
import colorama |
| 22 |
from datetime import datetime |
| 23 |
import struct |
| 24 |
import binascii |
| 25 |
import jinja2 |
| 26 |
from pyocd.target.pack.flash_algo import PackFlashAlgo |
| 27 |
from pyocd.utility.mask import align_up |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
BLOB_HEADER = '0xe7fdbe00,' |
| 39 |
HEADER_SIZE = 4 |
| 40 |
|
| 41 |
STACK_SIZE = 0x1000 |
| 42 |
|
| 43 |
PYOCD_TEMPLATE = \ |
| 44 |
"""# pyOCD debugger |
| 45 |
# Copyright (c) {{year}} {{copyright_owner}} |
| 46 |
# SPDX-License-Identifier: Apache-2.0 |
| 47 |
# |
| 48 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 49 |
# you may not use this file except in compliance with the License. |
| 50 |
# You may obtain a copy of the License at |
| 51 |
# |
| 52 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 53 |
# |
| 54 |
# Unless required by applicable law or agreed to in writing, software |
| 55 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 56 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 57 |
# See the License for the specific language governing permissions and |
| 58 |
# limitations under the License. |
| 59 |
|
| 60 |
FLASH_ALGO = { |
| 61 |
'load_address' : {{'0x%08x' % entry}}, |
| 62 |
|
| 63 |
# Flash algorithm as a hex string |
| 64 |
'instructions': [ |
| 65 |
{{prog_header}} |
| 66 |
{{algo.format_algo_data(4, 8, "c")}} |
| 67 |
], |
| 68 |
|
| 69 |
# Relative function addresses |
| 70 |
'pc_init': {{'0x%08x' % (algo.symbols['Init'] + header_size + entry)}}, |
| 71 |
'pc_unInit': {{'0x%08x' % (algo.symbols['UnInit'] + header_size + entry)}}, |
| 72 |
'pc_program_page': {{'0x%08x' % (algo.symbols['ProgramPage'] + header_size + entry)}}, |
| 73 |
'pc_erase_sector': {{'0x%08x' % (algo.symbols['EraseSector'] + header_size + entry)}}, |
| 74 |
'pc_eraseAll': {{'0x%08x' % (algo.symbols['EraseChip'] + header_size + entry)}}, |
| 75 |
|
| 76 |
'static_base' : {{'0x%08x' % entry}} + {{'0x%08x' % header_size}} + {{'0x%08x' % algo.rw_start}}, |
| 77 |
'begin_stack' : {{'0x%08x' % stack_pointer}}, |
| 78 |
'end_stack' : {{'0x%08x' % stack_base}}, |
| 79 |
'page_size' : {{'0x%x' % algo.page_size}}, |
| 80 |
'analyzer_supported' : False, |
| 81 |
'analyzer_address' : 0x00000000, |
| 82 |
# Enable double buffering |
| 83 |
'page_buffers' : [ |
| 84 |
{{'0x%08x' % (page_buffers[0])}}, |
| 85 |
{{'0x%08x' % (page_buffers[1])}} |
| 86 |
], |
| 87 |
'min_program_length' : {{'0x%x' % algo.page_size}}, |
| 88 |
|
| 89 |
# Relative region addresses and sizes |
| 90 |
'ro_start': {{'0x%x' % (header_size + algo.ro_start)}}, |
| 91 |
'ro_size': {{'0x%x' % algo.ro_size}}, |
| 92 |
'rw_start': {{'0x%x' % (header_size + algo.rw_start)}}, |
| 93 |
'rw_size': {{'0x%x' % algo.rw_size}}, |
| 94 |
'zi_start': {{'0x%x' % (header_size + algo.zi_start)}}, |
| 95 |
'zi_size': {{'0x%x' % algo.zi_size}}, |
| 96 |
|
| 97 |
# Flash information |
| 98 |
'flash_start': {{'0x%x' % algo.flash_start}}, |
| 99 |
'flash_size': {{'0x%x' % algo.flash_size}}, |
| 100 |
'sector_sizes': ( |
| 101 |
{%- for start, size in algo.sector_sizes %} |
| 102 |
{{ "(0x%x, 0x%x)" % (start, size) }}, |
| 103 |
{%- endfor %} |
| 104 |
) |
| 105 |
} |
| 106 |
|
| 107 |
""" |
| 108 |
|
| 109 |
colorama.init() |
| 110 |
|
| 111 |
def str_to_num(val): |
| 112 |
return int(val, 0) |
| 113 |
|
| 114 |
class PackFlashAlgoGenerator(PackFlashAlgo): |
| 115 |
""" |
| 116 |
Class to wrap a flash algo |
| 117 |
|
| 118 |
This class is intended to provide easy access to the information |
| 119 |
provided by a flash algorithm, such as symbols and the flash |
| 120 |
algorithm itself. |
| 121 |
""" |
| 122 |
|
| 123 |
def format_algo_data(self, spaces, group_size, fmt): |
| 124 |
"""" |
| 125 |
Return a string representing algo_data suitable for use in a template |
| 126 |
|
| 127 |
The string is intended for use in a template. |
| 128 |
|
| 129 |
:param spaces: The number of leading spaces for each line |
| 130 |
:param group_size: number of elements per line (element type |
| 131 |
depends of format) |
| 132 |
:param fmt: - format to create - can be either "hex" or "c" |
| 133 |
""" |
| 134 |
padding = " " * spaces |
| 135 |
if fmt == "hex": |
| 136 |
blob = binascii.b2a_hex(self.algo_data).decode() |
| 137 |
line_list = [] |
| 138 |
for i in range(0, len(blob), group_size): |
| 139 |
line_list.append('"' + blob[i:i + group_size] + '"') |
| 140 |
return ("\n" + padding).join(line_list) |
| 141 |
elif fmt == "c": |
| 142 |
blob = self.algo_data[:] |
| 143 |
pad_size = 0 if len(blob) % 4 == 0 else 4 - len(blob) % 4 |
| 144 |
blob = blob + b"\x00" * pad_size |
| 145 |
integer_list = struct.unpack("<" + "L" * (len(blob) // 4), blob) |
| 146 |
line_list = [] |
| 147 |
for pos in range(0, len(integer_list), group_size): |
| 148 |
group = ["0x%08x" % value for value in |
| 149 |
integer_list[pos:pos + group_size]] |
| 150 |
line_list.append(", ".join(group)) |
| 151 |
return (",\n" + padding).join(line_list) |
| 152 |
else: |
| 153 |
raise ValueError("Unsupported format %s" % fmt) |
| 154 |
|
| 155 |
def process_template(self, template_text, data_dict=None): |
| 156 |
""" |
| 157 |
Generate output from the supplied template |
| 158 |
|
| 159 |
All the public methods and fields of this class can be accessed from |
| 160 |
the template via "algo". |
| 161 |
|
| 162 |
:param template_path: Relative or absolute file path to the template |
| 163 |
:param data_dict: Additional data to use when generating |
| 164 |
""" |
| 165 |
if data_dict is None: |
| 166 |
data_dict = {} |
| 167 |
else: |
| 168 |
assert isinstance(data_dict, dict) |
| 169 |
data_dict = dict(data_dict) |
| 170 |
assert "algo" not in data_dict, "algo already set by user data" |
| 171 |
data_dict["algo"] = self |
| 172 |
|
| 173 |
template = jinja2.Template(template_text) |
| 174 |
return template.render(data_dict) |
| 175 |
|
| 176 |
def main(): |
| 177 |
parser = argparse.ArgumentParser(description="Blob generator") |
| 178 |
parser.add_argument("elf_path", help="Elf, axf, or flm to extract flash algo from") |
| 179 |
parser.add_argument("--blob-start", default=0x20000000, type=str_to_num, help="Starting " |
| 180 |
"address of the flash blob in target RAM. (default 0x20000000)") |
| 181 |
parser.add_argument("--stack-size", default=STACK_SIZE, type=str_to_num, help="Stack size for the algo. " |
| 182 |
f"(default {STACK_SIZE})") |
| 183 |
parser.add_argument("-i", "--info-only", action="store_true", help="Only print information about the flash " |
| 184 |
"algo, do not generate a blob.") |
| 185 |
parser.add_argument("-o", "--output", default="pyocd_blob.py", help="Path of output file " |
| 186 |
"(default 'pyocd_blob.py').") |
| 187 |
parser.add_argument("-t", "--template", help="Path to Jinja template file (default is an internal " |
| 188 |
"template for pyocd).") |
| 189 |
parser.add_argument('-c', '--copyright', help="Set copyright owner.") |
| 190 |
args = parser.parse_args() |
| 191 |
|
| 192 |
if not args.copyright and not args.info_only: |
| 193 |
print(f"{colorama.Fore.YELLOW}Warning! No copyright owner was specified. Defaulting to \"PyOCD Authors\". " |
| 194 |
f"Please set via --copyright, or edit output.{colorama.Style.RESET_ALL}") |
| 195 |
|
| 196 |
if args.template: |
| 197 |
with open(args.template, "r") as tmpl_file: |
| 198 |
tmpl = tmpl_file.read() |
| 199 |
else: |
| 200 |
tmpl = PYOCD_TEMPLATE |
| 201 |
|
| 202 |
with open(args.elf_path, "rb") as file_handle: |
| 203 |
algo = PackFlashAlgoGenerator(file_handle) |
| 204 |
|
| 205 |
print(algo.flash_info) |
| 206 |
|
| 207 |
|
| 208 |
buffer_base = align_up(args.blob_start + HEADER_SIZE |
| 209 |
+ algo.ro_size + algo.rw_size + algo.zi_size, 0x10) |
| 210 |
|
| 211 |
page_buffers = [ |
| 212 |
buffer_base, |
| 213 |
buffer_base + algo.page_size, |
| 214 |
] |
| 215 |
|
| 216 |
|
| 217 |
stack_base = align_up(buffer_base + algo.page_size * 2, 8) |
| 218 |
sp = align_up(stack_base + args.stack_size, 8) |
| 219 |
|
| 220 |
header_end = args.blob_start + HEADER_SIZE |
| 221 |
print(f"load addr: {args.blob_start:#010x}") |
| 222 |
print(f"data: {HEADER_SIZE + len(algo.algo_data):#x} bytes") |
| 223 |
print(f" header: {args.blob_start:#010x} + {HEADER_SIZE:#x} bytes") |
| 224 |
print(f" ro: {header_end + algo.ro_start:#010x} + {algo.ro_size:#x} bytes") |
| 225 |
print(f" rw: {header_end + algo.rw_start:#010x} + {algo.rw_size:#x} bytes") |
| 226 |
print(f" zi: {header_end + algo.zi_start:#010x} + {algo.zi_size:#x} bytes") |
| 227 |
print(f"buffer[0]: {page_buffers[0]:#010x}") |
| 228 |
print(f"buffer[1]: {page_buffers[1]:#010x}") |
| 229 |
print(f"stack: {stack_base:#010x} .. {sp:#010x} ({sp - stack_base:#x} bytes)") |
| 230 |
|
| 231 |
print("\nSymbol offsets:") |
| 232 |
for n, v in algo.symbols.items(): |
| 233 |
print(f"{n}:{' ' * (11 - len(n))} {v:#010x}") |
| 234 |
|
| 235 |
if args.info_only: |
| 236 |
return |
| 237 |
|
| 238 |
if len(algo.sector_sizes) > 1: |
| 239 |
print(f"{colorama.Fore.YELLOW}Warning! Flash has more than one sector size. Remember to create one flash memory region for each sector size range.{colorama.Style.RESET_ALL}") |
| 240 |
|
| 241 |
data_dict = { |
| 242 |
'name': os.path.splitext(os.path.split(args.elf_path)[-1])[0], |
| 243 |
'prog_header': BLOB_HEADER, |
| 244 |
'header_size': HEADER_SIZE, |
| 245 |
'entry': args.blob_start, |
| 246 |
'stack_base': stack_base, |
| 247 |
'stack_pointer': sp, |
| 248 |
'page_buffers': page_buffers, |
| 249 |
'year': datetime.now().year, |
| 250 |
'copyright_owner': args.copyright or "PyOCD Authors", |
| 251 |
} |
| 252 |
|
| 253 |
text = algo.process_template(tmpl, data_dict) |
| 254 |
|
| 255 |
with open(args.output, "w") as file_handle: |
| 256 |
file_handle.write(text) |
| 257 |
|
| 258 |
print(f"Wrote flash algo dict to {args.output}") |
| 259 |
|
| 260 |
if __name__ == '__main__': |
| 261 |
main() |