Skip to content

Commit

Permalink
Update the way ELF metadata sections are added.
Browse files Browse the repository at this point in the history
Update metadata to keep the exact same values
  • Loading branch information
agrojean-ledger authored and Xavier Chapron committed Dec 1, 2023
1 parent 3786673 commit 536c84d
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 23 deletions.
13 changes: 10 additions & 3 deletions Makefile.defines
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ endif
ifeq ($(SDK_HASH),)
SDK_HASH := "None"
endif
# Expose API_LEVEL, SDK_VERSION and SDK_HASH to the app.

# APPNAME exposed to the app as a CFLAG because it might contain spaces
CFLAGS += -DAPPNAME=\"$(APPNAME)\"

# API_LEVEL exposed to the app as an integer
DEFINES += API_LEVEL=$(API_LEVEL)
DEFINES += SDK_VERSION=\"$(SDK_VERSION)\"
DEFINES += SDK_HASH=\"$(SDK_HASH)\"

# Define list of other items to be exposed to the app as strings
# TARGET_ID is not in this list: it is already defined in bolos_target.h.
APP_METADATA_LIST := TARGET TARGET_NAME APPVERSION SDK_NAME SDK_VERSION SDK_HASH
DEFINES += $(foreach item,$(APP_METADATA_LIST), $(item)=\"$($(item))\")

# extra load parameters for loadApp script
ifneq ($(SCP_PRIVKEY),)
Expand Down
17 changes: 0 additions & 17 deletions Makefile.rules_generic
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,6 @@ $(BIN_DIR)/app.elf: $(LINK_DEPENDENCIES)
$(L)$(call link_cmdline,$(OBJECT_FILES) $(LDLIBS),$(BIN_DIR)/app.elf)
$(L)$(GCCPATH)arm-none-eabi-objcopy -O ihex -S $(BIN_DIR)/app.elf $(BIN_DIR)/app.hex
$(L)$(GCCPATH)arm-none-eabi-objdump -S -d $(BIN_DIR)/app.elf > $(DBG_DIR)/app.asm
$(L)$(call objcopy_add_section_cmdline,$(TARGET), ledger.target)
$(L)$(call objcopy_add_section_cmdline,$(TARGET_NAME), ledger.target_name)
$(L)$(call objcopy_add_section_cmdline,$(TARGET_ID), ledger.target_id)
$(L)$(call objcopy_add_section_cmdline,$(APPNAME), ledger.app_name)
$(L)$(call objcopy_add_section_cmdline,$(APPVERSION), ledger.app_version)
$(L)$(call objcopy_add_section_cmdline,$(API_LEVEL), ledger.api_level)
$(L)$(call objcopy_add_section_cmdline,$(SDK_NAME), ledger.sdk_name)
$(L)$(call objcopy_add_section_cmdline,$(SDK_VERSION), ledger.sdk_version)
$(L)$(call objcopy_add_section_cmdline,$(SDK_HASH), ledger.sdk_hash)

# This targets are generated along $(OBJ_DIR)/app.elf but we can't make them co-target
# otherwise building with `make -j` fails due to multiple threads running simultaneously
Expand Down Expand Up @@ -172,14 +163,6 @@ cc_cmdline = $(CC) -c $(CFLAGS) -MMD -MT $(OBJ_DIR)/$(basename $(notdir $(4))).o

as_cmdline = $(AS) -c $(AFLAGS) $(addprefix -D,$(2)) $(addprefix -I,$(1)) -o $(4) $(3)

# objcopy_add_section_cmdline(data,section_name)
TMPFILE := $(shell mktemp)
objcopy_add_section_cmdline = echo $(1) > $(TMPFILE) && \
$(GCCPATH)arm-none-eabi-objcopy --add-section $(2)="$(TMPFILE)" \
--set-section-flags $(2)=noload,readonly \
$(BIN_DIR)/app.elf $(BIN_DIR)/app.elf && \
rm $(TMPFILE)

### END GCC COMPILER RULES

# Run Clang Static Analyzer
Expand Down
3 changes: 0 additions & 3 deletions Makefile.standard_app
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ endif
# STANDARD DEFINES #
#####################################################################
DEFINES += $(DEFINES_LIB)
# Added directly as a CFLAG because it might contain spaces
CFLAGS += -DAPPNAME=\"$(APPNAME)\"
DEFINES += APPVERSION=\"$(APPVERSION)\"
DEFINES += MAJOR_VERSION=$(APPVERSION_M) MINOR_VERSION=$(APPVERSION_N) PATCH_VERSION=$(APPVERSION_P)
DEFINES += IO_HID_EP_LENGTH=64

Expand Down
71 changes: 71 additions & 0 deletions src/app_metadata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*******************************************************************************
* Ledger - Secure firmware
* (c) 2023 Ledger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#if !defined(HAVE_BOLOS)

#include <stdint.h>

#include "bolos_target.h"

#define STR_IMPL_(x) #x
#define STRINGIFY(x) STR_IMPL_(x)

#define CREATE_METADATA_STRING_ITEM(ITEM_NAME, section_name) \
__attribute__((section("ledger." #section_name))) \
const char section_name[sizeof(ITEM_NAME) - 1] \
= ITEM_NAME;

#define CREATE_METADATA_STRING_ITEM_FROM_INT(ITEM_NAME, section_name) \
CREATE_METADATA_STRING_ITEM(STRINGIFY(ITEM_NAME), section_name)

#if defined(TARGET)
CREATE_METADATA_STRING_ITEM(TARGET, target)
#endif

#if defined(TARGET_NAME)
CREATE_METADATA_STRING_ITEM(TARGET_NAME, target_name)
#endif

#if defined(TARGET_ID)
CREATE_METADATA_STRING_ITEM_FROM_INT(TARGET_ID, target_id)
#endif

#if defined(APPNAME)
CREATE_METADATA_STRING_ITEM(APPNAME, app_name)
#endif

#if defined(APPVERSION)
CREATE_METADATA_STRING_ITEM(APPVERSION, app_version)
#endif

#if defined(API_LEVEL)
CREATE_METADATA_STRING_ITEM_FROM_INT(API_LEVEL, api_level)
#endif

#if defined(SDK_NAME)
CREATE_METADATA_STRING_ITEM(SDK_NAME, sdk_name)
#endif

#if defined(SDK_VERSION)
CREATE_METADATA_STRING_ITEM(SDK_VERSION, sdk_version)
#endif

#if defined(SDK_HASH)
CREATE_METADATA_STRING_ITEM(SDK_HASH, sdk_hash)
#endif

#endif
10 changes: 10 additions & 0 deletions target/nanos/script.ld
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ SECTIONS
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }

ledger.target (INFO): { KEEP(*(ledger.target)) }
ledger.target_name (INFO): { KEEP(*(ledger.target_name)) }
ledger.target_id (INFO): { KEEP(*(ledger.target_id)) }
ledger.app_name (INFO): { KEEP(*(ledger.app_name)) }
ledger.app_version (INFO): { KEEP(*(ledger.app_version)) }
ledger.api_level (INFO): { KEEP(*(ledger.api_level)) }
ledger.sdk_name (INFO): { KEEP(*(ledger.sdk_name)) }
ledger.sdk_version (INFO): { KEEP(*(ledger.sdk_version)) }
ledger.sdk_hash (INFO): { KEEP(*(ledger.sdk_hash)) }
}

PROVIDE(_nvram = ABSOLUTE(_nvram_start));
Expand Down
10 changes: 10 additions & 0 deletions target/nanos2/script.ld
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ SECTIONS
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }

ledger.target (INFO): { KEEP(*(ledger.target)) }
ledger.target_name (INFO): { KEEP(*(ledger.target_name)) }
ledger.target_id (INFO): { KEEP(*(ledger.target_id)) }
ledger.app_name (INFO): { KEEP(*(ledger.app_name)) }
ledger.app_version (INFO): { KEEP(*(ledger.app_version)) }
ledger.api_level (INFO): { KEEP(*(ledger.api_level)) }
ledger.sdk_name (INFO): { KEEP(*(ledger.sdk_name)) }
ledger.sdk_version (INFO): { KEEP(*(ledger.sdk_version)) }
ledger.sdk_hash (INFO): { KEEP(*(ledger.sdk_hash)) }
}

PROVIDE(_nvram = ABSOLUTE(_nvram_start));
Expand Down
10 changes: 10 additions & 0 deletions target/nanox/script.ld
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ SECTIONS
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }

ledger.target (INFO): { KEEP(*(ledger.target)) }
ledger.target_name (INFO): { KEEP(*(ledger.target_name)) }
ledger.target_id (INFO): { KEEP(*(ledger.target_id)) }
ledger.app_name (INFO): { KEEP(*(ledger.app_name)) }
ledger.app_version (INFO): { KEEP(*(ledger.app_version)) }
ledger.api_level (INFO): { KEEP(*(ledger.api_level)) }
ledger.sdk_name (INFO): { KEEP(*(ledger.sdk_name)) }
ledger.sdk_version (INFO): { KEEP(*(ledger.sdk_version)) }
ledger.sdk_hash (INFO): { KEEP(*(ledger.sdk_hash)) }
}

PROVIDE(_nvram = ABSOLUTE(_nvram_start));
Expand Down
10 changes: 10 additions & 0 deletions target/stax/script.ld
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ SECTIONS
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }

ledger.target (INFO): { KEEP(*(ledger.target)) }
ledger.target_name (INFO): { KEEP(*(ledger.target_name)) }
ledger.target_id (INFO): { KEEP(*(ledger.target_id)) }
ledger.app_name (INFO): { KEEP(*(ledger.app_name)) }
ledger.app_version (INFO): { KEEP(*(ledger.app_version)) }
ledger.api_level (INFO): { KEEP(*(ledger.api_level)) }
ledger.sdk_name (INFO): { KEEP(*(ledger.sdk_name)) }
ledger.sdk_version (INFO): { KEEP(*(ledger.sdk_version)) }
ledger.sdk_hash (INFO): { KEEP(*(ledger.sdk_hash)) }
}

PROVIDE(_nvram = ABSOLUTE(_nvram_start));
Expand Down

0 comments on commit 536c84d

Please sign in to comment.