Clone of official tools
flash_algo/extract.py@47:21ae3e5a7128, 2021-02-04 (annotated)
- Committer:
- Anders Blomdell
- Date:
- Thu Feb 04 17:17:13 2021 +0100
- Revision:
- 47:21ae3e5a7128
- Parent:
- 44:bad0b339f97d
Add a few normpath calls
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
theotherjimmy |
44:bad0b339f97d | 1 | #!/usr/bin/env python |
theotherjimmy |
44:bad0b339f97d | 2 | """ |
theotherjimmy |
44:bad0b339f97d | 3 | mbed |
theotherjimmy |
44:bad0b339f97d | 4 | Copyright (c) 2017-2017 ARM Limited |
theotherjimmy |
44:bad0b339f97d | 5 | |
theotherjimmy |
44:bad0b339f97d | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
theotherjimmy |
44:bad0b339f97d | 7 | you may not use this file except in compliance with the License. |
theotherjimmy |
44:bad0b339f97d | 8 | You may obtain a copy of the License at |
theotherjimmy |
44:bad0b339f97d | 9 | |
theotherjimmy |
44:bad0b339f97d | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
theotherjimmy |
44:bad0b339f97d | 11 | |
theotherjimmy |
44:bad0b339f97d | 12 | Unless required by applicable law or agreed to in writing, software |
theotherjimmy |
44:bad0b339f97d | 13 | distributed under the License is distributed on an "AS IS" BASIS, |
theotherjimmy |
44:bad0b339f97d | 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
theotherjimmy |
44:bad0b339f97d | 15 | See the License for the specific language governing permissions and |
theotherjimmy |
44:bad0b339f97d | 16 | limitations under the License. |
theotherjimmy |
44:bad0b339f97d | 17 | """ |
theotherjimmy |
44:bad0b339f97d | 18 | |
theotherjimmy |
44:bad0b339f97d | 19 | from __future__ import print_function |
theotherjimmy |
44:bad0b339f97d | 20 | import sys |
theotherjimmy |
44:bad0b339f97d | 21 | import os |
theotherjimmy |
44:bad0b339f97d | 22 | import argparse |
theotherjimmy |
44:bad0b339f97d | 23 | from os.path import join, abspath, dirname |
theotherjimmy |
44:bad0b339f97d | 24 | from tools.flash_algo import PackFlashAlgo |
theotherjimmy |
44:bad0b339f97d | 25 | |
theotherjimmy |
44:bad0b339f97d | 26 | # Be sure that the tools directory is in the search path |
theotherjimmy |
44:bad0b339f97d | 27 | ROOT = abspath(join(dirname(__file__), "..", "..")) |
theotherjimmy |
44:bad0b339f97d | 28 | sys.path.insert(0, ROOT) |
theotherjimmy |
44:bad0b339f97d | 29 | |
theotherjimmy |
44:bad0b339f97d | 30 | from tools.targets import TARGETS |
theotherjimmy |
44:bad0b339f97d | 31 | from tools.arm_pack_manager import Cache |
theotherjimmy |
44:bad0b339f97d | 32 | |
theotherjimmy |
44:bad0b339f97d | 33 | TEMPLATE_PATH = "c_blob_mbed.tmpl" |
theotherjimmy |
44:bad0b339f97d | 34 | |
theotherjimmy |
44:bad0b339f97d | 35 | |
theotherjimmy |
44:bad0b339f97d | 36 | def main(): |
theotherjimmy |
44:bad0b339f97d | 37 | """Generate flash algorithms""" |
theotherjimmy |
44:bad0b339f97d | 38 | parser = argparse.ArgumentParser(description='Flash generator') |
theotherjimmy |
44:bad0b339f97d | 39 | parser.add_argument("--rebuild_all", action="store_true", |
theotherjimmy |
44:bad0b339f97d | 40 | help="Rebuild entire cache") |
theotherjimmy |
44:bad0b339f97d | 41 | parser.add_argument("--rebuild_descriptors", action="store_true", |
theotherjimmy |
44:bad0b339f97d | 42 | help="Rebuild descriptors") |
theotherjimmy |
44:bad0b339f97d | 43 | parser.add_argument("--target", default=None, |
theotherjimmy |
44:bad0b339f97d | 44 | help="Name of target to generate algo for") |
theotherjimmy |
44:bad0b339f97d | 45 | parser.add_argument("--all", action="store_true", |
theotherjimmy |
44:bad0b339f97d | 46 | help="Build all flash algos for devcies") |
theotherjimmy |
44:bad0b339f97d | 47 | args = parser.parse_args() |
theotherjimmy |
44:bad0b339f97d | 48 | |
theotherjimmy |
44:bad0b339f97d | 49 | cache = Cache(True, True) |
theotherjimmy |
44:bad0b339f97d | 50 | if args.rebuild_all: |
theotherjimmy |
44:bad0b339f97d | 51 | cache.cache_everything() |
theotherjimmy |
44:bad0b339f97d | 52 | print("Cache rebuilt") |
theotherjimmy |
44:bad0b339f97d | 53 | return |
theotherjimmy |
44:bad0b339f97d | 54 | |
theotherjimmy |
44:bad0b339f97d | 55 | if args.rebuild_descriptors: |
theotherjimmy |
44:bad0b339f97d | 56 | cache.cache_descriptors() |
theotherjimmy |
44:bad0b339f97d | 57 | print("Descriptors rebuilt") |
theotherjimmy |
44:bad0b339f97d | 58 | return |
theotherjimmy |
44:bad0b339f97d | 59 | |
theotherjimmy |
44:bad0b339f97d | 60 | if args.target is None: |
theotherjimmy |
44:bad0b339f97d | 61 | device_and_filenames = [(target.device_name, target.name) for target |
theotherjimmy |
44:bad0b339f97d | 62 | in TARGETS if hasattr(target, "device_name")] |
theotherjimmy |
44:bad0b339f97d | 63 | else: |
theotherjimmy |
44:bad0b339f97d | 64 | device_and_filenames = [(args.target, args.target.replace("/", "-"))] |
theotherjimmy |
44:bad0b339f97d | 65 | |
theotherjimmy |
44:bad0b339f97d | 66 | try: |
theotherjimmy |
44:bad0b339f97d | 67 | os.mkdir("output") |
theotherjimmy |
44:bad0b339f97d | 68 | except OSError: |
theotherjimmy |
44:bad0b339f97d | 69 | # Directory already exists |
theotherjimmy |
44:bad0b339f97d | 70 | pass |
theotherjimmy |
44:bad0b339f97d | 71 | |
theotherjimmy |
44:bad0b339f97d | 72 | for device, filename in device_and_filenames: |
theotherjimmy |
44:bad0b339f97d | 73 | dev = cache.index[device] |
theotherjimmy |
44:bad0b339f97d | 74 | binaries = cache.get_flash_algorthim_binary(device, all=True) |
theotherjimmy |
44:bad0b339f97d | 75 | algos = [PackFlashAlgo(binary.read()) for binary in binaries] |
theotherjimmy |
44:bad0b339f97d | 76 | filtered_algos = algos if args.all else filter_algos(dev, algos) |
theotherjimmy |
44:bad0b339f97d | 77 | for idx, algo in enumerate(filtered_algos): |
theotherjimmy |
44:bad0b339f97d | 78 | file_name = ("%s_%i.c" % (filename, idx) |
theotherjimmy |
44:bad0b339f97d | 79 | if args.all or len(filtered_algos) != 1 |
theotherjimmy |
44:bad0b339f97d | 80 | else "%s.c" % filename) |
theotherjimmy |
44:bad0b339f97d | 81 | output_path = join("output", file_name) |
theotherjimmy |
44:bad0b339f97d | 82 | algo.process_template(TEMPLATE_PATH, output_path) |
theotherjimmy |
44:bad0b339f97d | 83 | print("%s: %s \r" % (device, filename)) |
theotherjimmy |
44:bad0b339f97d | 84 | |
theotherjimmy |
44:bad0b339f97d | 85 | |
theotherjimmy |
44:bad0b339f97d | 86 | def filter_algos(dev, algos): |
theotherjimmy |
44:bad0b339f97d | 87 | if "memory" not in dev: |
theotherjimmy |
44:bad0b339f97d | 88 | return algos |
theotherjimmy |
44:bad0b339f97d | 89 | if "IROM1" not in dev["memory"]: |
theotherjimmy |
44:bad0b339f97d | 90 | return algos |
theotherjimmy |
44:bad0b339f97d | 91 | if "IROM2" in dev["memory"]: |
theotherjimmy |
44:bad0b339f97d | 92 | return algos |
theotherjimmy |
44:bad0b339f97d | 93 | |
theotherjimmy |
44:bad0b339f97d | 94 | rom_rgn = dev["memory"]["IROM1"] |
theotherjimmy |
44:bad0b339f97d | 95 | try: |
theotherjimmy |
44:bad0b339f97d | 96 | start = int(rom_rgn["start"], 0) |
theotherjimmy |
44:bad0b339f97d | 97 | size = int(rom_rgn["size"], 0) |
theotherjimmy |
44:bad0b339f97d | 98 | except ValueError: |
theotherjimmy |
44:bad0b339f97d | 99 | return algos |
theotherjimmy |
44:bad0b339f97d | 100 | |
theotherjimmy |
44:bad0b339f97d | 101 | matching_algos = [algo for algo in algos if |
theotherjimmy |
44:bad0b339f97d | 102 | algo.flash_start == start and algo.flash_size == size] |
theotherjimmy |
44:bad0b339f97d | 103 | return matching_algos if len(matching_algos) == 1 else algos |
theotherjimmy |
44:bad0b339f97d | 104 | |
theotherjimmy |
44:bad0b339f97d | 105 | |
theotherjimmy |
44:bad0b339f97d | 106 | if __name__ == '__main__': |
theotherjimmy |
44:bad0b339f97d | 107 | main() |