[feat] prepare for boot2

This commit is contained in:
zhji 2025-04-13 17:29:34 +08:00
parent 821307d4c2
commit e25e3b95a3
8 changed files with 129 additions and 2 deletions

View File

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.10)
include(proj.conf)
project(${EXAMPLE_NAME} VERSION 0.1)
add_executable(${EXAMPLE_NAME}.elf main.c)
enable_language(ASM)
target_sources(${EXAMPLE_NAME}.elf PUBLIC boot2_pre.S)
add_subdirectory(${SDK_BASE_DIR} sdk)
target_link_libraries(${EXAMPLE_NAME}.elf sdk)

20
example/boot2/Makefile Normal file
View File

@ -0,0 +1,20 @@
EXAMPLE_BASE_DIR ?= $(shell realpath .)
EXAMPLE_NAME := $(notdir $(patsubst %/,%,$(CURDIR)))
SDK_BASE_DIR ?= $(shell realpath ./../..)
export SDK_BASE_DIR
export EXAMPLE_NAME
export EXAMPLE_BASE_DIR
GCC_PATH := $(shell which arm-none-eabi-gcc)
CROSS_COMPILE := $(patsubst %gcc,%,$(GCC_PATH))
ifeq ($(GCC_PATH),)
$(error arm-none-eabi-gcc not found in PATH. Please install the ARM toolchain.)
endif
# add custom cmake definition
#cmake_definition+=-Dxxx=sss
LDFLAGS += -T$(EXAMPLE_BASE_DIR)/flash.ld
include $(SDK_BASE_DIR)/project.build

21
example/boot2/boot2_pre.S Normal file
View File

@ -0,0 +1,21 @@
.syntax unified
.cpu cortex-m0plus
.thumb
.global boot2_pre
.section .text.boot2_pre
.type boot2_pre, %function
boot2_pre:
b boot2_copy_self
.word 0x11223344
.word 0xabcdef58
boot2_copy_self:
bl main
.size .boot2_pre, .-boot2_pre

62
example/boot2/flash.ld Normal file
View File

@ -0,0 +1,62 @@
_start = ORIGIN(FLASH);
ENTRY(_start)
MEMORY
{
FLASH (xrx) :ORIGIN = 0x10000000, LENGTH = 256K
RAM (xrw) :ORIGIN = 0x20000000, LENGTH = 256K
STACK (rw) :ORIGIN = 0x20040000, LENGTH = 8K - 256
AUTO(xrx) :ORIGIN = 0x20041F00, LENGTH = 256
}
_stack_top = ORIGIN(STACK) + LENGTH(STACK);
PHDRS
{
boot2_pre PT_LOAD FLAGS(5); /* R + X */
text PT_LOAD FLAGS(5); /* R + X */
rodata PT_LOAD FLAGS(5); /* R + W */
data PT_LOAD FLAGS(6); /* R + W */
bss PT_LOAD FLAGS(6); /* R + W */
}
SECTIONS
{
.boot2_pre :
{
KEEP(*(.text.boot2_pre))
_boot2_pre_size = . - ADDR(.boot2_pre);
ASSERT(_boot2_pre_size <= 252, "Error: .boot2_pre size exceeds 252 bytes!");
. += 256 - _boot2_pre_size;
} >AUTO AT > FLASH :boot2_pre
.text :
{
. = ALIGN(4);
*(.text*)
. = ALIGN(4);
} >RAM AT > FLASH :text
.rodata :
{
. = ALIGN(4);
*(.rodata*)
. = ALIGN(4);
} >RAM AT > FLASH :rodata
.data :
{
. = ALIGN(4);
*(.data*)
. = ALIGN(4);
} >RAM AT > FLASH :data
.bss (NOLOAD) :
{
. = ALIGN(4);
_bss_start = .;
*(.bss*)
. = ALIGN(4);
_bss_end = .;
} >RAM :bss
}

4
example/boot2/main.c Normal file
View File

@ -0,0 +1,4 @@
int main(void)
{
return 0;
}

1
example/boot2/proj.conf Normal file
View File

@ -0,0 +1 @@
# set(CONFIG_COMPONENT1 1)

View File

@ -11,6 +11,9 @@ CFLAGS += -ffunction-sections
CFLAGS += -fdata-sections
CFLAGS += -Wpointer-arith
ifneq ($(findstring -T, $(LDFLAGS)), -T)
LDFLAGS += -T$(SDK_BASE_DIR)/ram.ld
endif
LDFLAGS += -mcpu=cortex-m0plus
LDFLAGS += -mthumb
LDFLAGS += -g3
@ -18,11 +21,11 @@ LDFLAGS += -O2
LDFLAGS += -Wall
LDFLAGS += -Wl,--gc-sections
LDFLAGS += -nostartfiles
LDFLAGS += -T$(SDK_BASE_DIR)/ram.ld
LDFLAGS += -Wl,-Map=$(EXAMPLE_NAME).map
LDFLAGS += -Wl,--print-memory-usage
CMAKE := cmake
ELF2UF2 := $(SDK_BASE_DIR)/tools/elf2uf2
# The command to remove a file.
RM = $(CMAKE) -E remove_directory
@ -55,8 +58,10 @@ build:Makefile
$(MAKE) -C build -j
$(CROSS_COMPILE)objcopy -O binary $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).bin
$(CROSS_COMPILE)objdump -d -S $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).asm
$(CROSS_COMPILE)nm $(FINAL_NAME_PRE).elf > $(FINAL_NAME_PRE).nm
$(ELF2UF2) $(FINAL_NAME_PRE).elf $(FINAL_NAME_PRE).uf2
size $(FINAL_NAME_PRE).elf
sha256sum $(FINAL_NAME_PRE).elf
sha256sum $(FINAL_NAME_PRE).bin
clean::
$(RM) build

BIN
tools/elf2uf2 Executable file

Binary file not shown.