|
16 | 16 | import errno |
17 | 17 | import logging |
18 | 18 | import os |
| 19 | +import pathlib |
| 20 | +import pickle |
19 | 21 | import platform |
20 | 22 | import re |
21 | 23 | import selectors |
@@ -715,13 +717,45 @@ def get_flash_address(args: argparse.Namespace, |
715 | 717 | else: |
716 | 718 | return default |
717 | 719 |
|
| 720 | + @staticmethod |
| 721 | + def get_chosen_code_partition_offset(build_dir: str): |
| 722 | + '''Get the offset corresponding to the zephyr,code-partition.''' |
| 723 | + b = pathlib.Path(build_dir) |
| 724 | + edt_pickle = b / 'zephyr' / 'edt.pickle' |
| 725 | + if not edt_pickle.is_file(): |
| 726 | + raise RuntimeError('cannot load devicetree; expected to find:' + |
| 727 | + str(edt_pickle)) |
| 728 | + |
| 729 | + # Load the devicetree. |
| 730 | + try: |
| 731 | + with open(edt_pickle, 'rb') as f: |
| 732 | + edt = pickle.load(f) |
| 733 | + except ModuleNotFoundError as err: |
| 734 | + raise RuntimeError('could not load devicetree, something may be' |
| 735 | + 'wrong with the python environment') from err |
| 736 | + |
| 737 | + # Find the zephyr,code-partition node. |
| 738 | + node = edt.chosen_node('zephyr,code-partition') |
| 739 | + if node is not None: |
| 740 | + return node.regs[0].addr |
| 741 | + |
| 742 | + return None |
| 743 | + |
718 | 744 | @staticmethod |
719 | 745 | def flash_address_from_build_conf(build_conf: BuildConfiguration): |
720 | | - '''If CONFIG_HAS_FLASH_LOAD_OFFSET is n in build_conf, |
| 746 | + '''If CONFIG_USE_DT_CODE_PARTITION return zephyr,code-partition |
| 747 | + offset + CONFIG_FLASH_BASE_ADDRESS. |
| 748 | + If CONFIG_HAS_FLASH_LOAD_OFFSET is n in build_conf, |
721 | 749 | return the CONFIG_FLASH_BASE_ADDRESS value. Otherwise, return |
722 | 750 | CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_LOAD_OFFSET. |
723 | 751 | ''' |
724 | | - if build_conf.getboolean('CONFIG_HAS_FLASH_LOAD_OFFSET'): |
| 752 | + if build_conf.getboolean('CONFIG_USE_DT_CODE_PARTITION'): |
| 753 | + offset = ZephyrBinaryRunner.get_chosen_code_partition_offset(build_conf.build_dir) |
| 754 | + if offset is None: |
| 755 | + raise RuntimeError('The device tree zephyr,code-partition chosen' |
| 756 | + ' node must be defined.') |
| 757 | + return build_conf['CONFIG_FLASH_BASE_ADDRESS'] + offset |
| 758 | + elif build_conf.getboolean('CONFIG_HAS_FLASH_LOAD_OFFSET'): |
725 | 759 | return (build_conf['CONFIG_FLASH_BASE_ADDRESS'] + |
726 | 760 | build_conf['CONFIG_FLASH_LOAD_OFFSET']) |
727 | 761 | else: |
|
0 commit comments