Skip to content

Commit

Permalink
scripts: generate image info header file
Browse files Browse the repository at this point in the history
This commit adds the `gen_image_info.py` script which supports creation
of a header file with image information from the EFL file.

This version populates the header file with:
- Number of segments in the image
- LMA address of each segment
- VMA address of each segment
- Size of each segment

The header file can be used by a secondary build system which needs this
information.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
  • Loading branch information
tejlmand authored and cfriedt committed Jan 22, 2022
1 parent eb9a972 commit c8ddc34
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,19 @@ if(CONFIG_BUILD_OUTPUT_EXE)
)
endif()

if(CONFIG_BUILD_OUTPUT_INFO_HEADER)
list(APPEND
post_build_commands
COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/gen_image_info.py
--elf-file=${KERNEL_ELF_NAME}
--header-file=${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
)
list(APPEND
post_build_byproducts
${PROJECT_BINARY_DIR}/include/public/zephyr_image_info.h
)
endif()

# Generate and use MCUboot related artifacts as needed.
if(CONFIG_BOOTLOADER_MCUBOOT)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/mcuboot.cmake)
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@
/scripts/pylib/twister/expr_parser.py @nashif
/scripts/schemas/twister/ @nashif
/scripts/gen_app_partitions.py @dcpleung @nashif
scripts/gen_image_info.py @tejlmand
/scripts/get_maintainer.py @nashif
/scripts/dts/ @mbolivar-nordic @galak
/scripts/release/ @nashif
Expand Down
11 changes: 11 additions & 0 deletions Kconfig.zephyr
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,17 @@ config BUILD_OUTPUT_STRIPPED
Build a stripped binary zephyr/zephyr.strip in the build directory.
The name of this file can be customized with CONFIG_KERNEL_BIN_NAME.

config BUILD_OUTPUT_INFO_HEADER
bool "Create a image information header"
help
Create an image information header which will contain image
information from the Zephyr binary.
Example of information contained in the header file:
- Number of segments in the image
- LMA address of each segment
- VMA address of each segment
- Size of each segment

config APPLICATION_DEFINED_SYSCALL
bool "Scan application folder for any syscall definition"
help
Expand Down
79 changes: 79 additions & 0 deletions scripts/gen_image_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
#
# Copyright (c) 2022, Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0

'''
Script to generate image information files.
This script creates a image information header which can be included by a
second build system.
This allows a second stage build system to use image information from a Zephyr
build by including the generated header.
Information included in the image information header:
- Number of segments in the image
- LMA address of each segment
- VMA address of each segment
- Size of each segment
'''

import argparse
from elftools.elf.elffile import ELFFile


def write_header(filename, segments):
content = []

filename_we = filename.split('.h')[0].upper()
content.append(f'#ifndef {filename_we}_H')
content.append(f'#define {filename_we}_H')
content.append(f'')
content.append(f'#define SEGMENT_NUM {len(segments)}')

for idx, segment in enumerate(segments):
segment_header = segment['segment'].header
hex_lma_addr = hex(segment_header.p_paddr)
hex_vma_addr = hex(segment_header.p_vaddr)
hex_size = hex(segment_header.p_filesz)

content.append(f'')
content.append(f'#define SEGMENT_LMA_ADDRESS_{idx} {hex_lma_addr}')
content.append(f'#define SEGMENT_VMA_ADDRESS_{idx} {hex_vma_addr}')
content.append(f'#define SEGMENT_SIZE_{idx} {hex_size}')

content.append(f'')
content.append(f'#endif /* {filename_we}_H */')

with open(filename, 'w') as out_file:
out_file.write('\n'.join(content))


def read_segments(filename):
elffile = ELFFile(open(filename, 'rb'))
segments = list()
for segment_idx in range(elffile.num_segments()):
segments.insert(segment_idx, dict())
segments[segment_idx]['segment'] = elffile.get_segment(segment_idx)
return segments


def main():
parser = argparse.ArgumentParser(description='''
Process ELF file and extract image information.
Create header file with extracted image information which can be included
in other build systems.''')

parser.add_argument('--header-file', required=True,
help="""Header file to write with image data.""")
parser.add_argument('--elf-file', required=True,
help="""ELF File to process.""")
args = parser.parse_args()

segments = read_segments(args.elf_file)
write_header(args.header_file, segments)


if __name__ == "__main__":
main()

0 comments on commit c8ddc34

Please sign in to comment.