diff --git a/modules/hal_rpi_pico/CMakeLists.txt b/modules/hal_rpi_pico/CMakeLists.txt index 1e5798c7d1f0f4c..516ea938ef32c27 100644 --- a/modules/hal_rpi_pico/CMakeLists.txt +++ b/modules/hal_rpi_pico/CMakeLists.txt @@ -115,4 +115,48 @@ if(CONFIG_HAS_RPI_PICO) COMPILE_FLAGS $ ) + # 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() diff --git a/soc/arm/rpi_pico/rp2/CMakeLists.txt b/soc/arm/rpi_pico/rp2/CMakeLists.txt index 210f5623ae9869d..c8e1cd285af78b4 100644 --- a/soc/arm/rpi_pico/rp2/CMakeLists.txt +++ b/soc/arm/rpi_pico/rp2/CMakeLists.txt @@ -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) diff --git a/soc/arm/rpi_pico/rp2/Kconfig.soc b/soc/arm/rpi_pico/rp2/Kconfig.soc index 601182672db46dd..b5fb41572084058 100644 --- a/soc/arm/rpi_pico/rp2/Kconfig.soc +++ b/soc/arm/rpi_pico/rp2/Kconfig.soc @@ -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" diff --git a/soc/arm/rpi_pico/rp2/binary_info.c b/soc/arm/rpi_pico/rp2/binary_info.c new file mode 100644 index 000000000000000..3cb6293651432ab --- /dev/null +++ b/soc/arm/rpi_pico/rp2/binary_info.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2023 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +#include + +#include + +#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 diff --git a/soc/arm/rpi_pico/rp2/binary_info.ld b/soc/arm/rpi_pico/rp2/binary_info.ld new file mode 100644 index 000000000000000..0ea43f4f5d111e3 --- /dev/null +++ b/soc/arm/rpi_pico/rp2/binary_info.ld @@ -0,0 +1,11 @@ +/* + * Copyright (c) 2023 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +. = ALIGN(4); +__binary_info_start = .; +KEEP(*(.binary_info.keep.*)) +*(.binary_info.*) +__binary_info_end = .; diff --git a/soc/arm/rpi_pico/rp2/binary_info_header.S b/soc/arm/rpi_pico/rp2/binary_info_header.S new file mode 100644 index 000000000000000..b24a8ddf7ae1bb1 --- /dev/null +++ b/soc/arm/rpi_pico/rp2/binary_info_header.S @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 TOKITA Hiroshi + * + * 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 */ diff --git a/soc/arm/rpi_pico/rp2/binary_info_header.ld b/soc/arm/rpi_pico/rp2/binary_info_header.ld new file mode 100644 index 000000000000000..9a571e58c4fe679 --- /dev/null +++ b/soc/arm/rpi_pico/rp2/binary_info_header.ld @@ -0,0 +1,7 @@ +/* + * Copyright (c) 2023 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +KEEP (*(.binary_info_header))