|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2022, Nordic Semiconductor ASA |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +''' |
| 8 | +Script to generate image information files. |
| 9 | +
|
| 10 | +This script creates a image information header which can be included by a |
| 11 | +second build system. |
| 12 | +This allows a second stage build system to use image information from a Zephyr |
| 13 | +build by including the generated header. |
| 14 | +
|
| 15 | +Information included in the image information header: |
| 16 | +- Number of segments in the image |
| 17 | +- LMA address of each segment |
| 18 | +- VMA address of each segment |
| 19 | +- Size of each segment |
| 20 | +''' |
| 21 | + |
| 22 | +import argparse |
| 23 | +from elftools.elf.elffile import ELFFile |
| 24 | + |
| 25 | + |
| 26 | +def write_header(filename, segments): |
| 27 | + content = [] |
| 28 | + |
| 29 | + filename_we = filename.split('.h')[0].upper() |
| 30 | + content.append(f'#ifndef {filename_we}_H') |
| 31 | + content.append(f'#define {filename_we}_H') |
| 32 | + content.append(f'') |
| 33 | + content.append(f'#define SEGMENT_NUM {len(segments)}') |
| 34 | + |
| 35 | + for idx, segment in enumerate(segments): |
| 36 | + segment_header = segment['segment'].header |
| 37 | + hex_lma_addr = hex(segment_header.p_paddr) |
| 38 | + hex_vma_addr = hex(segment_header.p_vaddr) |
| 39 | + hex_size = hex(segment_header.p_filesz) |
| 40 | + |
| 41 | + content.append(f'') |
| 42 | + content.append(f'#define SEGMENT_LMA_ADDRESS_{idx} {hex_lma_addr}') |
| 43 | + content.append(f'#define SEGMENT_VMA_ADDRESS_{idx} {hex_vma_addr}') |
| 44 | + content.append(f'#define SEGMENT_SIZE_{idx} {hex_size}') |
| 45 | + |
| 46 | + content.append(f'') |
| 47 | + content.append(f'#endif /* {filename_we}_H */') |
| 48 | + |
| 49 | + with open(filename, 'w') as out_file: |
| 50 | + out_file.write('\n'.join(content)) |
| 51 | + |
| 52 | + |
| 53 | +def read_segments(filename): |
| 54 | + elffile = ELFFile(open(filename, 'rb')) |
| 55 | + segments = list() |
| 56 | + for segment_idx in range(elffile.num_segments()): |
| 57 | + segments.insert(segment_idx, dict()) |
| 58 | + segments[segment_idx]['segment'] = elffile.get_segment(segment_idx) |
| 59 | + return segments |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + parser = argparse.ArgumentParser(description=''' |
| 64 | + Process ELF file and extract image information. |
| 65 | + Create header file with extracted image information which can be included |
| 66 | + in other build systems.''') |
| 67 | + |
| 68 | + parser.add_argument('--header-file', required=True, |
| 69 | + help="""Header file to write with image data.""") |
| 70 | + parser.add_argument('--elf-file', required=True, |
| 71 | + help="""ELF File to process.""") |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + segments = read_segments(args.elf_file) |
| 75 | + write_header(args.header_file, segments) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments