Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 #!/usr/bin/env python
marcozecchini 0:9fca2b23d0ba 2 """
marcozecchini 0:9fca2b23d0ba 3 mbed
marcozecchini 0:9fca2b23d0ba 4 Copyright (c) 2017-2017 ARM Limited
marcozecchini 0:9fca2b23d0ba 5
marcozecchini 0:9fca2b23d0ba 6 Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 7 you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 8 You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 9
marcozecchini 0:9fca2b23d0ba 10 http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 11
marcozecchini 0:9fca2b23d0ba 12 Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 13 distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 15 See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 16 limitations under the License.
marcozecchini 0:9fca2b23d0ba 17 """
marcozecchini 0:9fca2b23d0ba 18
marcozecchini 0:9fca2b23d0ba 19 from __future__ import print_function
marcozecchini 0:9fca2b23d0ba 20 import os
marcozecchini 0:9fca2b23d0ba 21 import struct
marcozecchini 0:9fca2b23d0ba 22 import binascii
marcozecchini 0:9fca2b23d0ba 23 import argparse
marcozecchini 0:9fca2b23d0ba 24 import logging
marcozecchini 0:9fca2b23d0ba 25 import StringIO
marcozecchini 0:9fca2b23d0ba 26 import jinja2
marcozecchini 0:9fca2b23d0ba 27 from collections import namedtuple
marcozecchini 0:9fca2b23d0ba 28 from itertools import count
marcozecchini 0:9fca2b23d0ba 29
marcozecchini 0:9fca2b23d0ba 30 from elftools.common.py3compat import bytes2str
marcozecchini 0:9fca2b23d0ba 31 from elftools.elf.elffile import ELFFile
marcozecchini 0:9fca2b23d0ba 32 from elftools.elf.sections import SymbolTableSection
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34 logger = logging.getLogger(__name__)
marcozecchini 0:9fca2b23d0ba 35 logger.addHandler(logging.NullHandler())
marcozecchini 0:9fca2b23d0ba 36
marcozecchini 0:9fca2b23d0ba 37
marcozecchini 0:9fca2b23d0ba 38 def main():
marcozecchini 0:9fca2b23d0ba 39 parser = argparse.ArgumentParser(description="Algo Extracter")
marcozecchini 0:9fca2b23d0ba 40 parser.add_argument("input", help="File to extract flash algo from")
marcozecchini 0:9fca2b23d0ba 41 parser.add_argument("template", default="py_blob.tmpl",
marcozecchini 0:9fca2b23d0ba 42 help="Template to use")
marcozecchini 0:9fca2b23d0ba 43 parser.add_argument("output", help="Output file")
marcozecchini 0:9fca2b23d0ba 44 args = parser.parse_args()
marcozecchini 0:9fca2b23d0ba 45
marcozecchini 0:9fca2b23d0ba 46 with open(args.input, "rb") as file_handle:
marcozecchini 0:9fca2b23d0ba 47 data = file_handle.read()
marcozecchini 0:9fca2b23d0ba 48 algo = PackFlashAlgo(data)
marcozecchini 0:9fca2b23d0ba 49 algo.process_template(args.template, args.output)
marcozecchini 0:9fca2b23d0ba 50
marcozecchini 0:9fca2b23d0ba 51
marcozecchini 0:9fca2b23d0ba 52 class PackFlashAlgo(object):
marcozecchini 0:9fca2b23d0ba 53 """
marcozecchini 0:9fca2b23d0ba 54 Class to wrap a flash algo
marcozecchini 0:9fca2b23d0ba 55
marcozecchini 0:9fca2b23d0ba 56 This class is intended to provide easy access to the information
marcozecchini 0:9fca2b23d0ba 57 provided by a flash algorithm, such as symbols and the flash
marcozecchini 0:9fca2b23d0ba 58 algorithm itself.
marcozecchini 0:9fca2b23d0ba 59 """
marcozecchini 0:9fca2b23d0ba 60
marcozecchini 0:9fca2b23d0ba 61 REQUIRED_SYMBOLS = set([
marcozecchini 0:9fca2b23d0ba 62 "Init",
marcozecchini 0:9fca2b23d0ba 63 "UnInit",
marcozecchini 0:9fca2b23d0ba 64 "EraseSector",
marcozecchini 0:9fca2b23d0ba 65 "ProgramPage",
marcozecchini 0:9fca2b23d0ba 66 ])
marcozecchini 0:9fca2b23d0ba 67
marcozecchini 0:9fca2b23d0ba 68 EXTRA_SYMBOLS = set([
marcozecchini 0:9fca2b23d0ba 69 "BlankCheck",
marcozecchini 0:9fca2b23d0ba 70 "EraseChip",
marcozecchini 0:9fca2b23d0ba 71 "Verify",
marcozecchini 0:9fca2b23d0ba 72 ])
marcozecchini 0:9fca2b23d0ba 73
marcozecchini 0:9fca2b23d0ba 74 def __init__(self, data):
marcozecchini 0:9fca2b23d0ba 75 """Construct a PackFlashAlgorithm from an ElfFileSimple"""
marcozecchini 0:9fca2b23d0ba 76 self.elf = ElfFileSimple(data)
marcozecchini 0:9fca2b23d0ba 77 self.flash_info = PackFlashInfo(self.elf)
marcozecchini 0:9fca2b23d0ba 78
marcozecchini 0:9fca2b23d0ba 79 self.flash_start = self.flash_info.start
marcozecchini 0:9fca2b23d0ba 80 self.flash_size = self.flash_info.size
marcozecchini 0:9fca2b23d0ba 81 self.page_size = self.flash_info.page_size
marcozecchini 0:9fca2b23d0ba 82 self.sector_sizes = self.flash_info.sector_info_list
marcozecchini 0:9fca2b23d0ba 83
marcozecchini 0:9fca2b23d0ba 84 symbols = {}
marcozecchini 0:9fca2b23d0ba 85 symbols.update(_extract_symbols(self.elf, self.REQUIRED_SYMBOLS))
marcozecchini 0:9fca2b23d0ba 86 symbols.update(_extract_symbols(self.elf, self.EXTRA_SYMBOLS,
marcozecchini 0:9fca2b23d0ba 87 default=0xFFFFFFFF))
marcozecchini 0:9fca2b23d0ba 88 self.symbols = symbols
marcozecchini 0:9fca2b23d0ba 89
marcozecchini 0:9fca2b23d0ba 90 sections_to_find = (
marcozecchini 0:9fca2b23d0ba 91 ("PrgCode", "SHT_PROGBITS"),
marcozecchini 0:9fca2b23d0ba 92 ("PrgData", "SHT_PROGBITS"),
marcozecchini 0:9fca2b23d0ba 93 ("PrgData", "SHT_NOBITS"),
marcozecchini 0:9fca2b23d0ba 94 )
marcozecchini 0:9fca2b23d0ba 95
marcozecchini 0:9fca2b23d0ba 96 ro_rw_zi = _find_sections(self.elf, sections_to_find)
marcozecchini 0:9fca2b23d0ba 97 ro_rw_zi = _algo_fill_zi_if_missing(ro_rw_zi)
marcozecchini 0:9fca2b23d0ba 98 error_msg = _algo_check_for_section_problems(ro_rw_zi)
marcozecchini 0:9fca2b23d0ba 99 if error_msg is not None:
marcozecchini 0:9fca2b23d0ba 100 raise Exception(error_msg)
marcozecchini 0:9fca2b23d0ba 101
marcozecchini 0:9fca2b23d0ba 102 sect_ro, sect_rw, sect_zi = ro_rw_zi
marcozecchini 0:9fca2b23d0ba 103 self.ro_start = sect_ro["sh_addr"]
marcozecchini 0:9fca2b23d0ba 104 self.ro_size = sect_ro["sh_size"]
marcozecchini 0:9fca2b23d0ba 105 self.rw_start = sect_rw["sh_addr"]
marcozecchini 0:9fca2b23d0ba 106 self.rw_size = sect_rw["sh_size"]
marcozecchini 0:9fca2b23d0ba 107 self.zi_start = sect_zi["sh_addr"]
marcozecchini 0:9fca2b23d0ba 108 self.zi_size = sect_zi["sh_size"]
marcozecchini 0:9fca2b23d0ba 109
marcozecchini 0:9fca2b23d0ba 110 self.algo_data = _create_algo_bin(ro_rw_zi)
marcozecchini 0:9fca2b23d0ba 111
marcozecchini 0:9fca2b23d0ba 112 def format_algo_data(self, spaces, group_size, fmt):
marcozecchini 0:9fca2b23d0ba 113 """"
marcozecchini 0:9fca2b23d0ba 114 Return a string representing algo_data suitable for use in a template
marcozecchini 0:9fca2b23d0ba 115
marcozecchini 0:9fca2b23d0ba 116 The string is intended for use in a template.
marcozecchini 0:9fca2b23d0ba 117
marcozecchini 0:9fca2b23d0ba 118 :param spaces: The number of leading spaces for each line
marcozecchini 0:9fca2b23d0ba 119 :param group_size: number of elements per line (element type
marcozecchini 0:9fca2b23d0ba 120 depends of format)
marcozecchini 0:9fca2b23d0ba 121 :param fmt: - format to create - can be either "hex" or "c"
marcozecchini 0:9fca2b23d0ba 122 """
marcozecchini 0:9fca2b23d0ba 123 padding = " " * spaces
marcozecchini 0:9fca2b23d0ba 124 if fmt == "hex":
marcozecchini 0:9fca2b23d0ba 125 blob = binascii.b2a_hex(self.algo_data)
marcozecchini 0:9fca2b23d0ba 126 line_list = []
marcozecchini 0:9fca2b23d0ba 127 for i in xrange(0, len(blob), group_size):
marcozecchini 0:9fca2b23d0ba 128 line_list.append('"' + blob[i:i + group_size] + '"')
marcozecchini 0:9fca2b23d0ba 129 return ("\n" + padding).join(line_list)
marcozecchini 0:9fca2b23d0ba 130 elif fmt == "c":
marcozecchini 0:9fca2b23d0ba 131 blob = self.algo_data[:]
marcozecchini 0:9fca2b23d0ba 132 pad_size = 0 if len(blob) % 4 == 0 else 4 - len(blob) % 4
marcozecchini 0:9fca2b23d0ba 133 blob = blob + "\x00" * pad_size
marcozecchini 0:9fca2b23d0ba 134 integer_list = struct.unpack("<" + "L" * (len(blob) / 4), blob)
marcozecchini 0:9fca2b23d0ba 135 line_list = []
marcozecchini 0:9fca2b23d0ba 136 for pos in range(0, len(integer_list), group_size):
marcozecchini 0:9fca2b23d0ba 137 group = ["0x%08x" % value for value in
marcozecchini 0:9fca2b23d0ba 138 integer_list[pos:pos + group_size]]
marcozecchini 0:9fca2b23d0ba 139 line_list.append(", ".join(group))
marcozecchini 0:9fca2b23d0ba 140 return (",\n" + padding).join(line_list)
marcozecchini 0:9fca2b23d0ba 141 else:
marcozecchini 0:9fca2b23d0ba 142 raise Exception("Unsupported format %s" % fmt)
marcozecchini 0:9fca2b23d0ba 143
marcozecchini 0:9fca2b23d0ba 144 def process_template(self, template_path, output_path, data_dict=None):
marcozecchini 0:9fca2b23d0ba 145 """
marcozecchini 0:9fca2b23d0ba 146 Generate output from the supplied template
marcozecchini 0:9fca2b23d0ba 147
marcozecchini 0:9fca2b23d0ba 148 All the public methods and fields of this class can be accessed from
marcozecchini 0:9fca2b23d0ba 149 the template via "algo".
marcozecchini 0:9fca2b23d0ba 150
marcozecchini 0:9fca2b23d0ba 151 :param template_path: Relative or absolute file path to the template
marcozecchini 0:9fca2b23d0ba 152 :param output_path: Relative or absolute file path to create
marcozecchini 0:9fca2b23d0ba 153 :param data_dict: Additional data to use when generating
marcozecchini 0:9fca2b23d0ba 154 """
marcozecchini 0:9fca2b23d0ba 155 if data_dict is None:
marcozecchini 0:9fca2b23d0ba 156 data_dict = {}
marcozecchini 0:9fca2b23d0ba 157 else:
marcozecchini 0:9fca2b23d0ba 158 assert isinstance(data_dict, dict)
marcozecchini 0:9fca2b23d0ba 159 data_dict = dict(data_dict)
marcozecchini 0:9fca2b23d0ba 160 assert "algo" not in data_dict, "algo already set by user data"
marcozecchini 0:9fca2b23d0ba 161 data_dict["algo"] = self
marcozecchini 0:9fca2b23d0ba 162
marcozecchini 0:9fca2b23d0ba 163 with open(template_path) as file_handle:
marcozecchini 0:9fca2b23d0ba 164 template_text = file_handle.read()
marcozecchini 0:9fca2b23d0ba 165
marcozecchini 0:9fca2b23d0ba 166 template = jinja2.Template(template_text)
marcozecchini 0:9fca2b23d0ba 167 target_text = template.render(data_dict)
marcozecchini 0:9fca2b23d0ba 168
marcozecchini 0:9fca2b23d0ba 169 with open(output_path, "wb") as file_handle:
marcozecchini 0:9fca2b23d0ba 170 file_handle.write(target_text)
marcozecchini 0:9fca2b23d0ba 171
marcozecchini 0:9fca2b23d0ba 172
marcozecchini 0:9fca2b23d0ba 173 def _extract_symbols(simple_elf, symbols, default=None):
marcozecchini 0:9fca2b23d0ba 174 """Fill 'symbols' field with required flash algo symbols"""
marcozecchini 0:9fca2b23d0ba 175 to_ret = {}
marcozecchini 0:9fca2b23d0ba 176 for symbol in symbols:
marcozecchini 0:9fca2b23d0ba 177 if symbol not in simple_elf.symbols:
marcozecchini 0:9fca2b23d0ba 178 if default is not None:
marcozecchini 0:9fca2b23d0ba 179 to_ret[symbol] = default
marcozecchini 0:9fca2b23d0ba 180 continue
marcozecchini 0:9fca2b23d0ba 181 raise Exception("Missing symbol %s" % symbol)
marcozecchini 0:9fca2b23d0ba 182 to_ret[symbol] = simple_elf.symbols[symbol].value
marcozecchini 0:9fca2b23d0ba 183 return to_ret
marcozecchini 0:9fca2b23d0ba 184
marcozecchini 0:9fca2b23d0ba 185
marcozecchini 0:9fca2b23d0ba 186 def _find_sections(elf, name_type_pairs):
marcozecchini 0:9fca2b23d0ba 187 """Return a list of sections the same length and order of the input list"""
marcozecchini 0:9fca2b23d0ba 188 sections = [None] * len(name_type_pairs)
marcozecchini 0:9fca2b23d0ba 189 for section in elf.iter_sections():
marcozecchini 0:9fca2b23d0ba 190 section_name = bytes2str(section.name)
marcozecchini 0:9fca2b23d0ba 191 section_type = section["sh_type"]
marcozecchini 0:9fca2b23d0ba 192 for i, name_and_type in enumerate(name_type_pairs):
marcozecchini 0:9fca2b23d0ba 193 if name_and_type != (section_name, section_type):
marcozecchini 0:9fca2b23d0ba 194 continue
marcozecchini 0:9fca2b23d0ba 195 if sections[i] is not None:
marcozecchini 0:9fca2b23d0ba 196 raise Exception("Elf contains duplicate section %s attr %s" %
marcozecchini 0:9fca2b23d0ba 197 (section_name, section_type))
marcozecchini 0:9fca2b23d0ba 198 sections[i] = section
marcozecchini 0:9fca2b23d0ba 199 return sections
marcozecchini 0:9fca2b23d0ba 200
marcozecchini 0:9fca2b23d0ba 201
marcozecchini 0:9fca2b23d0ba 202 def _algo_fill_zi_if_missing(ro_rw_zi):
marcozecchini 0:9fca2b23d0ba 203 """Create an empty zi section if it is missing"""
marcozecchini 0:9fca2b23d0ba 204 s_ro, s_rw, s_zi = ro_rw_zi
marcozecchini 0:9fca2b23d0ba 205 if s_rw is None:
marcozecchini 0:9fca2b23d0ba 206 return ro_rw_zi
marcozecchini 0:9fca2b23d0ba 207 if s_zi is not None:
marcozecchini 0:9fca2b23d0ba 208 return ro_rw_zi
marcozecchini 0:9fca2b23d0ba 209 s_zi = {
marcozecchini 0:9fca2b23d0ba 210 "sh_addr": s_rw["sh_addr"] + s_rw["sh_size"],
marcozecchini 0:9fca2b23d0ba 211 "sh_size": 0
marcozecchini 0:9fca2b23d0ba 212 }
marcozecchini 0:9fca2b23d0ba 213 return s_ro, s_rw, s_zi
marcozecchini 0:9fca2b23d0ba 214
marcozecchini 0:9fca2b23d0ba 215
marcozecchini 0:9fca2b23d0ba 216 def _algo_check_for_section_problems(ro_rw_zi):
marcozecchini 0:9fca2b23d0ba 217 """Return a string describing any errors with the layout or None if good"""
marcozecchini 0:9fca2b23d0ba 218 s_ro, s_rw, s_zi = ro_rw_zi
marcozecchini 0:9fca2b23d0ba 219 if s_ro is None:
marcozecchini 0:9fca2b23d0ba 220 return "RO section is missing"
marcozecchini 0:9fca2b23d0ba 221 if s_rw is None:
marcozecchini 0:9fca2b23d0ba 222 return "RW section is missing"
marcozecchini 0:9fca2b23d0ba 223 if s_zi is None:
marcozecchini 0:9fca2b23d0ba 224 return "ZI section is missing"
marcozecchini 0:9fca2b23d0ba 225 if s_ro["sh_addr"] != 0:
marcozecchini 0:9fca2b23d0ba 226 return "RO section does not start at address 0"
marcozecchini 0:9fca2b23d0ba 227 if s_ro["sh_addr"] + s_ro["sh_size"] != s_rw["sh_addr"]:
marcozecchini 0:9fca2b23d0ba 228 return "RW section does not follow RO section"
marcozecchini 0:9fca2b23d0ba 229 if s_rw["sh_addr"] + s_rw["sh_size"] != s_zi["sh_addr"]:
marcozecchini 0:9fca2b23d0ba 230 return "ZI section does not follow RW section"
marcozecchini 0:9fca2b23d0ba 231 return None
marcozecchini 0:9fca2b23d0ba 232
marcozecchini 0:9fca2b23d0ba 233
marcozecchini 0:9fca2b23d0ba 234 def _create_algo_bin(ro_rw_zi):
marcozecchini 0:9fca2b23d0ba 235 """Create a binary blob of the flash algo which can execute from ram"""
marcozecchini 0:9fca2b23d0ba 236 sect_ro, sect_rw, sect_zi = ro_rw_zi
marcozecchini 0:9fca2b23d0ba 237 algo_size = sect_ro["sh_size"] + sect_rw["sh_size"] + sect_zi["sh_size"]
marcozecchini 0:9fca2b23d0ba 238 algo_data = bytearray(algo_size)
marcozecchini 0:9fca2b23d0ba 239 for section in (sect_ro, sect_rw):
marcozecchini 0:9fca2b23d0ba 240 start = section["sh_addr"]
marcozecchini 0:9fca2b23d0ba 241 size = section["sh_size"]
marcozecchini 0:9fca2b23d0ba 242 data = section.data()
marcozecchini 0:9fca2b23d0ba 243 assert len(data) == size
marcozecchini 0:9fca2b23d0ba 244 algo_data[start:start + size] = data
marcozecchini 0:9fca2b23d0ba 245 return algo_data
marcozecchini 0:9fca2b23d0ba 246
marcozecchini 0:9fca2b23d0ba 247
marcozecchini 0:9fca2b23d0ba 248 class PackFlashInfo(object):
marcozecchini 0:9fca2b23d0ba 249 """Wrapper class for the non-executable information in an FLM file"""
marcozecchini 0:9fca2b23d0ba 250
marcozecchini 0:9fca2b23d0ba 251 FLASH_DEVICE_STRUCT = "<H128sHLLLLBxxxLL"
marcozecchini 0:9fca2b23d0ba 252 FLASH_SECTORS_STRUCT = "<LL"
marcozecchini 0:9fca2b23d0ba 253 FLASH_SECTORS_STRUCT_SIZE = struct.calcsize(FLASH_SECTORS_STRUCT)
marcozecchini 0:9fca2b23d0ba 254 SECTOR_END = 0xFFFFFFFF
marcozecchini 0:9fca2b23d0ba 255
marcozecchini 0:9fca2b23d0ba 256 def __init__(self, elf_simple):
marcozecchini 0:9fca2b23d0ba 257 dev_info = elf_simple.symbols["FlashDevice"]
marcozecchini 0:9fca2b23d0ba 258 info_start = dev_info.value
marcozecchini 0:9fca2b23d0ba 259 info_size = struct.calcsize(self.FLASH_DEVICE_STRUCT)
marcozecchini 0:9fca2b23d0ba 260 data = elf_simple.read(info_start, info_size)
marcozecchini 0:9fca2b23d0ba 261 values = struct.unpack(self.FLASH_DEVICE_STRUCT, data)
marcozecchini 0:9fca2b23d0ba 262
marcozecchini 0:9fca2b23d0ba 263 self.version = values[0]
marcozecchini 0:9fca2b23d0ba 264 self.name = values[1].strip("\x00")
marcozecchini 0:9fca2b23d0ba 265 self.type = values[2]
marcozecchini 0:9fca2b23d0ba 266 self.start = values[3]
marcozecchini 0:9fca2b23d0ba 267 self.size = values[4]
marcozecchini 0:9fca2b23d0ba 268 self.page_size = values[5]
marcozecchini 0:9fca2b23d0ba 269 self.value_empty = values[7]
marcozecchini 0:9fca2b23d0ba 270 self.prog_timeout_ms = values[8]
marcozecchini 0:9fca2b23d0ba 271 self.erase_timeout_ms = values[9]
marcozecchini 0:9fca2b23d0ba 272
marcozecchini 0:9fca2b23d0ba 273 sector_gen = self._sector_and_sz_itr(elf_simple,
marcozecchini 0:9fca2b23d0ba 274 info_start + info_size)
marcozecchini 0:9fca2b23d0ba 275 self.sector_info_list = list(sector_gen)
marcozecchini 0:9fca2b23d0ba 276
marcozecchini 0:9fca2b23d0ba 277 def __str__(self):
marcozecchini 0:9fca2b23d0ba 278 desc = ""
marcozecchini 0:9fca2b23d0ba 279 desc += "Flash Device:" + os.linesep
marcozecchini 0:9fca2b23d0ba 280 desc += " name=%s" % self.name + os.linesep
marcozecchini 0:9fca2b23d0ba 281 desc += " version=0x%x" % self.version + os.linesep
marcozecchini 0:9fca2b23d0ba 282 desc += " type=%i" % self.type + os.linesep
marcozecchini 0:9fca2b23d0ba 283 desc += " start=0x%x" % self.start + os.linesep
marcozecchini 0:9fca2b23d0ba 284 desc += " size=0x%x" % self.size + os.linesep
marcozecchini 0:9fca2b23d0ba 285 desc += " page_size=0x%x" % self.page_size + os.linesep
marcozecchini 0:9fca2b23d0ba 286 desc += " value_empty=0x%x" % self.value_empty + os.linesep
marcozecchini 0:9fca2b23d0ba 287 desc += " prog_timeout_ms=%i" % self.prog_timeout_ms + os.linesep
marcozecchini 0:9fca2b23d0ba 288 desc += " erase_timeout_ms=%i" % self.erase_timeout_ms + os.linesep
marcozecchini 0:9fca2b23d0ba 289 desc += " sectors:" + os.linesep
marcozecchini 0:9fca2b23d0ba 290 for sector_start, sector_size in self.sector_info_list:
marcozecchini 0:9fca2b23d0ba 291 desc += (" start=0x%x, size=0x%x" %
marcozecchini 0:9fca2b23d0ba 292 (sector_start, sector_size) + os.linesep)
marcozecchini 0:9fca2b23d0ba 293 return desc
marcozecchini 0:9fca2b23d0ba 294
marcozecchini 0:9fca2b23d0ba 295 def _sector_and_sz_itr(self, elf_simple, data_start):
marcozecchini 0:9fca2b23d0ba 296 """Iterator which returns starting address and sector size"""
marcozecchini 0:9fca2b23d0ba 297 for entry_start in count(data_start, self.FLASH_SECTORS_STRUCT_SIZE):
marcozecchini 0:9fca2b23d0ba 298 data = elf_simple.read(entry_start, self.FLASH_SECTORS_STRUCT_SIZE)
marcozecchini 0:9fca2b23d0ba 299 size, start = struct.unpack(self.FLASH_SECTORS_STRUCT, data)
marcozecchini 0:9fca2b23d0ba 300 start_and_size = start, size
marcozecchini 0:9fca2b23d0ba 301 if start_and_size == (self.SECTOR_END, self.SECTOR_END):
marcozecchini 0:9fca2b23d0ba 302 return
marcozecchini 0:9fca2b23d0ba 303 yield start_and_size
marcozecchini 0:9fca2b23d0ba 304
marcozecchini 0:9fca2b23d0ba 305
marcozecchini 0:9fca2b23d0ba 306 SymbolSimple = namedtuple("SymbolSimple", "name, value, size")
marcozecchini 0:9fca2b23d0ba 307
marcozecchini 0:9fca2b23d0ba 308
marcozecchini 0:9fca2b23d0ba 309 class ElfFileSimple(ELFFile):
marcozecchini 0:9fca2b23d0ba 310 """Wrapper for elf object which allows easy access to symbols and rom"""
marcozecchini 0:9fca2b23d0ba 311
marcozecchini 0:9fca2b23d0ba 312 def __init__(self, data):
marcozecchini 0:9fca2b23d0ba 313 """Construct a ElfFileSimple from bytes or a bytearray"""
marcozecchini 0:9fca2b23d0ba 314 super(ElfFileSimple, self).__init__(StringIO.StringIO(data))
marcozecchini 0:9fca2b23d0ba 315 self.symbols = self._read_symbol_table()
marcozecchini 0:9fca2b23d0ba 316
marcozecchini 0:9fca2b23d0ba 317 def _read_symbol_table(self):
marcozecchini 0:9fca2b23d0ba 318 """Read the symbol table into the field "symbols" for easy use"""
marcozecchini 0:9fca2b23d0ba 319 section = self.get_section_by_name(b".symtab")
marcozecchini 0:9fca2b23d0ba 320 if not section:
marcozecchini 0:9fca2b23d0ba 321 raise Exception("Missing symbol table")
marcozecchini 0:9fca2b23d0ba 322
marcozecchini 0:9fca2b23d0ba 323 if not isinstance(section, SymbolTableSection):
marcozecchini 0:9fca2b23d0ba 324 raise Exception("Invalid symbol table section")
marcozecchini 0:9fca2b23d0ba 325
marcozecchini 0:9fca2b23d0ba 326 symbols = {}
marcozecchini 0:9fca2b23d0ba 327 for symbol in section.iter_symbols():
marcozecchini 0:9fca2b23d0ba 328 name_str = bytes2str(symbol.name)
marcozecchini 0:9fca2b23d0ba 329 if name_str in symbols:
marcozecchini 0:9fca2b23d0ba 330 logging.debug("Duplicate symbol %s", name_str)
marcozecchini 0:9fca2b23d0ba 331 symbols[name_str] = SymbolSimple(name_str, symbol["st_value"],
marcozecchini 0:9fca2b23d0ba 332 symbol["st_size"])
marcozecchini 0:9fca2b23d0ba 333 return symbols
marcozecchini 0:9fca2b23d0ba 334
marcozecchini 0:9fca2b23d0ba 335 def read(self, addr, size):
marcozecchini 0:9fca2b23d0ba 336 """Read program data from the elf file
marcozecchini 0:9fca2b23d0ba 337
marcozecchini 0:9fca2b23d0ba 338 :param addr: physical address (load address) to read from
marcozecchini 0:9fca2b23d0ba 339 :param size: number of bytes to read
marcozecchini 0:9fca2b23d0ba 340 :return: Requested data or None if address is unmapped
marcozecchini 0:9fca2b23d0ba 341 """
marcozecchini 0:9fca2b23d0ba 342 for segment in self.iter_segments():
marcozecchini 0:9fca2b23d0ba 343 seg_addr = segment["p_paddr"]
marcozecchini 0:9fca2b23d0ba 344 seg_size = min(segment["p_memsz"], segment["p_filesz"])
marcozecchini 0:9fca2b23d0ba 345 if addr >= seg_addr + seg_size:
marcozecchini 0:9fca2b23d0ba 346 continue
marcozecchini 0:9fca2b23d0ba 347 if addr + size <= seg_addr:
marcozecchini 0:9fca2b23d0ba 348 continue
marcozecchini 0:9fca2b23d0ba 349 # There is at least some overlap
marcozecchini 0:9fca2b23d0ba 350
marcozecchini 0:9fca2b23d0ba 351 if addr >= seg_addr and addr + size <= seg_addr + seg_size:
marcozecchini 0:9fca2b23d0ba 352 # Region is fully contained
marcozecchini 0:9fca2b23d0ba 353 data = segment.data()
marcozecchini 0:9fca2b23d0ba 354 start = addr - seg_addr
marcozecchini 0:9fca2b23d0ba 355 return data[start:start + size]
marcozecchini 0:9fca2b23d0ba 356
marcozecchini 0:9fca2b23d0ba 357
marcozecchini 0:9fca2b23d0ba 358 if __name__ == '__main__':
marcozecchini 0:9fca2b23d0ba 359 main()