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