Skip to content

Commit

Permalink
soc: arm: rpi_pico: Add basic support for binary info feature
Browse files Browse the repository at this point in the history
Enable embedding binary info to flash.
As a default, information collects from the build configuration.

CMake's arguments or Kconfig configuration allow overriding of the values.
It can override by command line argument to CMake, such as

```
west build -b rpi_pico -- -DPICO_PROGRAM_NAME="my program" ...
```

Or also can override by Kconfig definitions.

```
CONFIG_RP2_BINARY_INFO_PROGRAM_NAME_OVERRIDE=y
CONFIG_RP2_BINARY_INFO_PROGRAM_NAME="my program override"
```

Signed-off-by: TOKITA Hiroshi <[email protected]>
  • Loading branch information
soburi committed Feb 1, 2023
1 parent ca2d807 commit 25a1cc4
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 0 deletions.
44 changes: 44 additions & 0 deletions modules/hal_rpi_pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,48 @@ if(CONFIG_HAS_RPI_PICO)
COMPILE_FLAGS $<TARGET_PROPERTY:compiler,warning_no_pointer_arithmetic>
)

# binary info settings

zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_NO_BINARY_INFO=0)

get_filename_component(APPLICATION_SOURCE_DIR_BASE ${APPLICATION_SOURCE_DIR} NAME)
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO APPLICATION_SOURCE_DIR_BASE="${APPLICATION_SOURCE_DIR_BASE}")

if (NOT "${flash_type}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_BOOT_STAGE2_CHOOSE_${flash_type}=1)
endif()

if ("${PICO_SDK_VERSION_STRING}" STREQUAL "")
include(${ZEPHYR_HAL_RPI_PICO_MODULE_DIR}/pico_sdk_version.cmake)
endif()
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_SDK_VERSION_STRING="${PICO_SDK_VERSION_STRING}")

if (NOT "${PICO_PROGRAM_NAME}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_PROGRAM_NAME="${PICO_PROGRAM_NAME}")
endif()

if (NOT "${PICO_BOARD}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_BOARD="${PICO_BOARD}")
endif()

if (NOT "${PICO_PROGRAM_VERSION_STRING}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_PROGRAM_VERSION_STRING="${PICO_PROGRAM_VERSION_STRING}")
endif()

if (NOT "${PICO_PROGRAM_DESCRIPTION}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_PROGRAM_DESCRIPTION="${PICO_PROGRAM_DESCRIPTION}")
endif()

if (NOT "${PICO_PROGRAM_URL}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_PROGRAM_URL="${PICO_PROGRAM_URL}")
endif()

if (NOT "${PICO_PROGRAM_BUILD_DATE}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_PROGRAM_BUILD_DATE="${PICO_PROGRAM_BUILD_DATE}")
endif()

if (NOT "${PICO_BOOT_STAGE2_NAME}" STREQUAL "")
zephyr_compile_definitions_ifdef(CONFIG_RP2_BINARY_INFO PICO_BOOT_STAGE2_NAME="${PICO_BOOT_STAGE2_NAME}")
endif()

endif()
5 changes: 5 additions & 0 deletions soc/arm/rpi_pico/rp2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
zephyr_library()

zephyr_library_sources(soc.c)
zephyr_library_sources(binary_info.c)
zephyr_library_sources(binary_info_header.S)

zephyr_linker_sources(ROM_START SORT_KEY 0x0binary_info_header binary_info_header.ld)
zephyr_linker_sources(RODATA binary_info.ld)
54 changes: 54 additions & 0 deletions soc/arm/rpi_pico/rp2/Kconfig.soc
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,57 @@ config RP2_FLASH_AT25SF128A
help
Configure RP2 to use a AT25SF128A flash chip, or similar. Should be selected
by the board definition, not the user.

config RP2_BINARY_INFO
bool "Generate RaspberryPi Pico binary info"
default y
help
Binary info is able to embed machine readable information with the binary in FLASH.
It can read with picotool(https://github.com/raspberrypi/picotool).

config RP2_BINARY_INFO_PROGRAM_BUILD_DATE_OVERRIDE
bool "Override build date in binary info"

config RP2_BINARY_INFO_PROGRAM_BUILD_DATE
string "Override string for build date in binary info"
depends on RP2_BINARY_INFO_PROGRAM_BUILD_DATE_OVERRIDE

config RP2_BINARY_INFO_PROGRAM_NAME_OVERRIDE
bool "Override program name in binary info"

config RP2_BINARY_INFO_PROGRAM_NAME
string "Override string for program name in binary info"
depends on RP2_BINARY_INFO_PROGRAM_NAME_OVERRIDE

config RP2_BINARY_INFO_BOARD_OVERRIDE
bool "Override board in binary info"

config RP2_BINARY_INFO_BOARD
string "Override string for board in binary info"
depends on RP2_BINARY_INFO_BOARD_OVERRIDE

config RP2_BINARY_INFO_SDK_VERSION_STRING_OVERRIDE
bool "Override sdk version in binary info"

config RP2_BINARY_INFO_SDK_VERSION_STRING
string "Override string for sdk version in binary info"
depends on RP2_BINARY_INFO_SDK_VERSION_STRING_OVERRIDE

config RP2_BINARY_INFO_PROGRAM_VERSION_STRING_OVERRIDE
bool "Override program version in binary info"

config RP2_BINARY_INFO_PROGRAM_VERSION_STRING
string "Override string for program version in binary info"
depends on RP2_BINARY_INFO_PROGRAM_VERSION_STRING_OVERRIDE

config RP2_BINARY_INFO_PROGRAM_DESCRIPTION_OVERRIDE
bool "Override program descrpition in binary info"

config RP2_BINARY_INFO_PROGRAM_DESCRIPTION
string "String for program description in binary info"

config RP2_BINARY_INFO_PROGRAM_URL_OVERRIDE
bool "Override program url in binary info"

config RP2_BINARY_INFO_PROGRAM_URL
string "String for program description in binary info"
119 changes: 119 additions & 0 deletions soc/arm/rpi_pico/rp2/binary_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <soc.h>

#include <pico/binary_info.h>
#include <boot_stage2/config.h>

#include <version.h>

#if CONFIG_RP2_BINARY_INFO_PROGRAM_NAME_OVERRIDE
#undef PICO_PROGRAM_NAME
#define PICO_PROGRAM_NAME CONFIG_RP2_BINARY_INFO_PROGRAM_NAME
#else
#ifndef PICO_PROGRAM_NAME
#define PICO_PROGRAM_NAME APPLICATION_SOURCE_DIR_BASE
#endif
#endif

#if CONFIG_RP2_BINARY_INFO_BOARD_OVERRIDE
#undef PICO_BOARD
#define PICO_BOARD CONFIG_RP2_BINARY_INFO_BOARD
#else
#ifndef PICO_BOARD
#define PICO_BOARD CONFIG_BOARD
#endif
#endif

#if CONFIG_RP2_BINARY_INFO_SDK_VERSION_STRING_OVERRIDE
#undef PICO_SDK_VERSION_STRING
#define PICO_SDK_VERSION_STRING CONFIG_RP2_BINARY_INFO_SDK_VERSION_STRING
#else
#ifndef PICO_SDK_VERSION_STRING
#define PICO_SDK_VERSION_STRING RPI_PICO_SDK_VERSION_STRING
#endif
#endif

#if CONFIG_RP2_BINARY_INFO_PROGRAM_VERSION_STRING_OVERRIDE
#undef PICO_PROGRAM_VERSION_STRING
#define PICO_PROGRAM_VERSION_STRING CONFIG_RP2_BINARY_INFO_PROGRAM_VERSION_STRING
#else
#ifndef PICO_PROGRAM_VERSION_STRING
#define PICO_PROGRAM_VERSION_STRING STRINGIFY(BUILD_VERSION)
#endif
#endif

#if CONFIG_RP2_BINARY_INFO_PROGRAM_BUILD_DATE_OVERRIDE
#undef PICO_PROGRAM_BUILD_DATE
#define PICO_PROGRAM_BUILD_DATE CONFIG_RP2_BINARY_INFO_PROGRAM_BUILD_DATE
#ifndef PICO_PROGRAM_BUILD_DATE
#define PICO_PROGRAM_BUILD_DATE __DATE__
#endif
#endif

#if CONFIG_RP2_BINARY_INFO_PROGRAM_DESCRIPTION_OVERRIDE
#undef PICO_PROGRAM_DESCRIPTION
#define PICO_PROGRAM_DESCRIPTION CONFIG_RP2_BINARY_INFO_PROGRAM_DESCRIPTION
#else
#ifndef PICO_PROGRAM_DESCRIPTION
#define PICO_PROGRAM_DESCRIPTION CONFIG_RP2_BINARY_INFO_PROGRAM_DESCRIPTION
#endif
#endif

#if CONFIG_RP2_BINARY_INFO_PROGRAM_URL_OVERRIDE
#undef PICO_PROGRAM_URL
#define PICO_PROGRAM_URL CONFIG_RP2_BINARY_INFO_PROGRAM_URL
#else
#ifndef PICO_PROGRAM_URL
#define PICO_PROGRAM_URL CONFIG_RP2_BINARY_INFO_PROGRAM_URL
#endif
#endif

extern uint32_t __rom_region_end;
bi_decl(bi_binary_end((intptr_t)&__rom_region_end));

#ifdef PICO_PROGRAM_NAME
bi_decl(bi_program_name((uint32_t)PICO_PROGRAM_NAME));
#endif

#ifdef PICO_BOARD
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_PICO_BOARD,
(uint32_t)PICO_BOARD));
#endif

#ifdef PICO_SDK_VERSION_STRING
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_SDK_VERSION,
(uint32_t)PICO_SDK_VERSION_STRING));
#endif

#ifdef PICO_PROGRAM_VERSION_STRING
bi_decl(bi_program_version_string((uint32_t)PICO_PROGRAM_VERSION_STRING));
#endif

#ifdef PICO_PROGRAM_DESCRIPTION
bi_decl(bi_program_description((uint32_t)PICO_PROGRAM_DESCRIPTION));
#endif

#ifdef PICO_PROGRAM_URL
bi_decl(bi_program_url((uint32_t)PICO_PROGRAM_URL));
#endif

#ifdef PICO_PROGRAM_BUILD_DATE
bi_decl(bi_program_build_date_string((uint32_t)PICO_PROGRAM_BUILD_DATE));
#endif

#ifdef PICO_BOOT_STAGE2_NAME
bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_BOOT2_NAME,
(uint32_t)PICO_BOOT_STAGE2_NAME));
#endif

#ifndef NDEBUG
bi_decl(bi_program_build_attribute((uint32_t) "Debug"));
#else
bi_decl(bi_program_build_attribute((uint32_t) "Release"));
#endif
11 changes: 11 additions & 0 deletions soc/arm/rpi_pico/rp2/binary_info.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

. = ALIGN(4);
__binary_info_start = .;
KEEP(*(.binary_info.keep.*))
*(.binary_info.*)
__binary_info_end = .;
23 changes: 23 additions & 0 deletions soc/arm/rpi_pico/rp2/binary_info_header.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "pico/binary_info/defs.h"

.section .binary_info_header

/* binary_info_header */
binary_info_header:
.word BINARY_INFO_MARKER_START
.word __binary_info_start
.word __binary_info_end
.word data_cpy_table // we may need to decode pointers that are in RAM at runtime.
.word BINARY_INFO_MARKER_END

.align 2

/* data_cpy_table */
data_cpy_table:
.word 0 /* centinel */
7 changes: 7 additions & 0 deletions soc/arm/rpi_pico/rp2/binary_info_header.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright (c) 2023 TOKITA Hiroshi <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

KEEP (*(.binary_info_header))

0 comments on commit 25a1cc4

Please sign in to comment.