From 832c3e5206173e3eff1f1ee8d0906274dabd3ae7 Mon Sep 17 00:00:00 2001 From: Sven Willner Date: Sat, 1 Aug 2020 17:25:50 +0200 Subject: [PATCH 1/4] Add load target and unify makefiles --- examples/riscv-blink/Makefile | 13 ++++++++++--- sw/Makefile | 10 +++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/examples/riscv-blink/Makefile b/examples/riscv-blink/Makefile index 7e2a4fb..b0b07c0 100644 --- a/examples/riscv-blink/Makefile +++ b/examples/riscv-blink/Makefile @@ -34,7 +34,9 @@ THIRD_PARTY := $(BASE_DIR)/third_party DBG_CFLAGS := -ggdb -g -DDEBUG -Wall DBG_LFLAGS := -ggdb -g -Wall CFLAGS := $(ADD_CFLAGS) \ + -D__vexriscv__ -march=rv32i -mabi=ilp32 \ -Wall -Wextra \ + -flto \ -ffunction-sections -fdata-sections -fno-common \ -fomit-frame-pointer -Os \ -march=rv32i \ @@ -42,6 +44,7 @@ CFLAGS := $(ADD_CFLAGS) \ CXXFLAGS := $(CFLAGS) -std=c++11 -fno-rtti -fno-exceptions LFLAGS := $(CFLAGS) $(ADD_LFLAGS) -L$(LD_DIR) \ -nostartfiles \ + -nostdlib \ -Wl,--gc-sections \ -Wl,--no-warn-mismatch \ -Wl,--script=$(LDSCRIPT) \ @@ -64,7 +67,7 @@ ALL := all TARGET := $(PACKAGE).elf CLEAN := clean -$(ALL): $(TARGET) $(PACKAGE).bin $(PACKAGE).ihex +$(ALL): $(TARGET) $(PACKAGE).bin $(PACKAGE).ihex $(PACKAGE).dfu $(OBJECTS): | $(OBJ_DIR) @@ -76,7 +79,7 @@ $(PACKAGE).bin: $(TARGET) $(QUIET) echo " OBJCOPY $@" $(QUIET) $(OBJCOPY) -O binary $(TARGET) $@ -$(PACKAGE).dfu: $(TARGET) +$(PACKAGE).dfu: $(PACKAGE).bin $(QUIET) echo " DFU $@" $(QUIET) $(COPY) $(PACKAGE).bin $@ $(QUIET) dfu-suffix -v 1209 -p 70b1 -a $@ @@ -106,7 +109,7 @@ $(OBJ_DIR)/%.o: %.S $(QUIET) echo " AS $< $(notdir $@)" $(QUIET) $(CC) -x assembler-with-cpp -c $< $(CFLAGS) -o $@ -MMD -.PHONY: clean +.PHONY: clean load clean: $(QUIET) echo " RM $(subst /,$(PATH_SEP),$(wildcard $(OBJ_DIR)/*.d))" @@ -116,4 +119,8 @@ clean: $(QUIET) echo " RM $(TARGET) $(PACKAGE).bin $(PACKAGE).symbol $(PACKAGE).ihex $(PACKAGE).dfu" -$(QUIET) $(RM) $(TARGET) $(PACKAGE).bin $(PACKAGE).symbol $(PACKAGE).ihex $(PACKAGE).dfu +load: $(PACKAGE).dfu + $(QUIET) dfu-util -D $< + include $(wildcard $(OBJ_DIR)/*.d) + diff --git a/sw/Makefile b/sw/Makefile index 7983f6d..28de782 100644 --- a/sw/Makefile +++ b/sw/Makefile @@ -76,7 +76,7 @@ ALL := all TARGET := $(PACKAGE).elf CLEAN := clean -$(ALL): $(TARGET) $(PACKAGE).bin $(PACKAGE).ihex +$(ALL): $(TARGET) $(PACKAGE).bin $(PACKAGE).ihex $(PACKAGE).dfu $(OBJECTS): | $(OBJ_DIR) @@ -88,7 +88,7 @@ $(PACKAGE).bin: $(TARGET) $(QUIET) echo " OBJCOPY $@" $(QUIET) $(OBJCOPY) -O binary $(TARGET) $@ -$(PACKAGE).dfu: $(TARGET) +$(PACKAGE).dfu: $(PACKAGE).bin $(QUIET) echo " DFU $@" $(QUIET) $(COPY) $(PACKAGE).bin $@ $(QUIET) dfu-suffix -v 1209 -p 70b1 -a $@ @@ -118,7 +118,7 @@ $(OBJ_DIR)/%.o: %.S $(QUIET) echo " AS $< $(notdir $@)" $(QUIET) $(CC) -x assembler-with-cpp -c $< $(CFLAGS) -o $@ -MMD -.PHONY: clean +.PHONY: clean load clean: $(QUIET) echo " RM $(subst /,$(PATH_SEP),$(wildcard $(OBJ_DIR)/*.d))" @@ -128,4 +128,8 @@ clean: $(QUIET) echo " RM $(TARGET) $(PACKAGE).bin $(PACKAGE).symbol $(PACKAGE).ihex $(PACKAGE).dfu" -$(QUIET) $(RM) $(TARGET) $(PACKAGE).bin $(PACKAGE).symbol $(PACKAGE).ihex $(PACKAGE).dfu +load: $(PACKAGE).dfu + $(QUIET) dfu-util -D $< + include $(wildcard $(OBJ_DIR)/*.d) + From 538a82dc9cdbca66247fb321a33ed56ee9fd324a Mon Sep 17 00:00:00 2001 From: Sven Willner Date: Sat, 1 Aug 2020 17:28:24 +0200 Subject: [PATCH 2/4] Add options for ram boot compilation (LOAD_RAM_ADDR and LOAD_BOOT_CONFIG) --- examples/riscv-blink/Makefile | 17 +++++++++++++++-- examples/riscv-blink/ld/linker.ld | 2 -- examples/riscv-blink/ld/regions_ram.ld | 5 +++++ examples/riscv-blink/src/crt0-vexriscv.S | 8 +++++++- sw/Makefile | 15 ++++++++++++++- sw/ld/linker.ld | 2 -- sw/ld/regions_ram.ld | 5 +++++ sw/third_party/libbase/crt0-vexriscv.S | 10 ++++++++++ 8 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 examples/riscv-blink/ld/regions_ram.ld create mode 100644 sw/ld/regions_ram.ld diff --git a/examples/riscv-blink/Makefile b/examples/riscv-blink/Makefile index b0b07c0..961b365 100644 --- a/examples/riscv-blink/Makefile +++ b/examples/riscv-blink/Makefile @@ -26,9 +26,21 @@ LD_DIR := $(BASE_DIR)/ld LDSCRIPT := $(BASE_DIR)/ld/linker.ld ADD_CFLAGS := -I$(BASE_DIR)/include -D__vexriscv__ -march=rv32i -mabi=ilp32 ADD_LFLAGS := -PACKAGE := example +PACKAGE := riscv-blink -LDSCRIPTS := $(LDSCRIPT) $(LD_DIR)/output_format.ld $(LD_DIR)/regions.ld +ifdef LOAD_RAM_ADDR +LDREGIONS := $(LD_DIR)/regions_ram.ld +ADD_CFLAGS += -DLOAD_RAM_ADDR=$(LOAD_RAM_ADDR) +ADD_LFLAGS += -Wl,--defsym=LOAD_RAM_ADDR=$(LOAD_RAM_ADDR) +else +LDREGIONS := $(LD_DIR)/regions.ld +endif + +ifdef LOAD_BOOT_CONFIG +ADD_CFLAGS += -DLOAD_BOOT_CONFIG=$(LOAD_BOOT_CONFIG) +endif + +LDSCRIPTS := $(LDSCRIPT) $(LDREGIONS) $(LD_DIR)/output_format.ld SRC_DIR := $(BASE_DIR)/src THIRD_PARTY := $(BASE_DIR)/third_party DBG_CFLAGS := -ggdb -g -DDEBUG -Wall @@ -47,6 +59,7 @@ LFLAGS := $(CFLAGS) $(ADD_LFLAGS) -L$(LD_DIR) \ -nostdlib \ -Wl,--gc-sections \ -Wl,--no-warn-mismatch \ + -Wl,--script=$(LDREGIONS) \ -Wl,--script=$(LDSCRIPT) \ -Wl,--build-id=none diff --git a/examples/riscv-blink/ld/linker.ld b/examples/riscv-blink/ld/linker.ld index 5796f16..aa2c483 100644 --- a/examples/riscv-blink/ld/linker.ld +++ b/examples/riscv-blink/ld/linker.ld @@ -3,8 +3,6 @@ ENTRY(_start) __DYNAMIC = 0; -INCLUDE regions.ld - SECTIONS { .text : diff --git a/examples/riscv-blink/ld/regions_ram.ld b/examples/riscv-blink/ld/regions_ram.ld new file mode 100644 index 0000000..3c7c9f1 --- /dev/null +++ b/examples/riscv-blink/ld/regions_ram.ld @@ -0,0 +1,5 @@ +MEMORY { + ram : ORIGIN = LOAD_RAM_ADDR, LENGTH = 0x00010000 +} +REGION_ALIAS("rom", ram) +REGION_ALIAS("sram", ram) diff --git a/examples/riscv-blink/src/crt0-vexriscv.S b/examples/riscv-blink/src/crt0-vexriscv.S index 631b6a2..144bafe 100644 --- a/examples/riscv-blink/src/crt0-vexriscv.S +++ b/examples/riscv-blink/src/crt0-vexriscv.S @@ -6,8 +6,14 @@ _start: j crt_init +#ifdef LOAD_RAM_ADDR + .word 0x17ab0f23 + .word LOAD_RAM_ADDR +#endif +#ifdef LOAD_BOOT_CONFIG .word 0xb469075a - .word 0x00000020 + .word LOAD_BOOT_CONFIG +#endif .section .text .global trap_entry diff --git a/sw/Makefile b/sw/Makefile index 28de782..2c9a840 100644 --- a/sw/Makefile +++ b/sw/Makefile @@ -37,7 +37,19 @@ ADD_LFLAGS := PACKAGE := foboot endif -LDSCRIPTS := $(LDSCRIPT) $(LD_DIR)/output_format.ld $(LD_DIR)/regions.ld +ifdef LOAD_RAM_ADDR +LDREGIONS := $(LD_DIR)/regions_ram.ld +ADD_CFLAGS += -DLOAD_RAM_ADDR=$(LOAD_RAM_ADDR) +ADD_LFLAGS += -Wl,--defsym=LOAD_RAM_ADDR=$(LOAD_RAM_ADDR) +else +LDREGIONS := $(LD_DIR)/regions.ld +endif + +ifdef LOAD_BOOT_CONFIG +ADD_CFLAGS += -DLOAD_BOOT_CONFIG=$(LOAD_BOOT_CONFIG) +endif + +LDSCRIPTS := $(LDSCRIPT) $(LDREGIONS) $(LD_DIR)/output_format.ld SRC_DIR := $(BASE_DIR)/src THIRD_PARTY := $(BASE_DIR)/third_party DBG_CFLAGS := -ggdb3 -DDEBUG -Wall @@ -56,6 +68,7 @@ LFLAGS := $(CFLAGS) $(ADD_LFLAGS) -L$(LD_DIR) \ -nostdlib \ -Wl,--gc-sections \ -Wl,--no-warn-mismatch \ + -Wl,--script=$(LDREGIONS) \ -Wl,--script=$(LDSCRIPT) \ -Wl,--build-id=none diff --git a/sw/ld/linker.ld b/sw/ld/linker.ld index 5796f16..aa2c483 100644 --- a/sw/ld/linker.ld +++ b/sw/ld/linker.ld @@ -3,8 +3,6 @@ ENTRY(_start) __DYNAMIC = 0; -INCLUDE regions.ld - SECTIONS { .text : diff --git a/sw/ld/regions_ram.ld b/sw/ld/regions_ram.ld new file mode 100644 index 0000000..3c7c9f1 --- /dev/null +++ b/sw/ld/regions_ram.ld @@ -0,0 +1,5 @@ +MEMORY { + ram : ORIGIN = LOAD_RAM_ADDR, LENGTH = 0x00010000 +} +REGION_ALIAS("rom", ram) +REGION_ALIAS("sram", ram) diff --git a/sw/third_party/libbase/crt0-vexriscv.S b/sw/third_party/libbase/crt0-vexriscv.S index 931d50a..6b48f5d 100644 --- a/sw/third_party/libbase/crt0-vexriscv.S +++ b/sw/third_party/libbase/crt0-vexriscv.S @@ -7,10 +7,20 @@ _start: j crt_init nop +#ifdef LOAD_RAM_ADDR + .word 0x17ab0f23 + .word LOAD_RAM_ADDR +#else nop nop +#endif +#ifdef LOAD_BOOT_CONFIG + .word 0xb469075a + .word LOAD_BOOT_CONFIG +#else nop nop +#endif nop nop From 0bd7791aad829ce7cb9ce7226b1c7077554f67fc Mon Sep 17 00:00:00 2001 From: Sven Willner Date: Sat, 1 Aug 2020 17:38:23 +0200 Subject: [PATCH 3/4] Add documentation for ram boot and boot config make options --- doc/BOOT-SEQUENCE.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/doc/BOOT-SEQUENCE.md b/doc/BOOT-SEQUENCE.md index 654b74c..bdbf3ce 100644 --- a/doc/BOOT-SEQUENCE.md +++ b/doc/BOOT-SEQUENCE.md @@ -14,7 +14,7 @@ At start, **FBU** is loaded by the hardware. **FBU** will look for a special *F ## Boot Flags -A series of *Boot Fags* determine how **FBU** and **FBM** hand over execution to the subsequent program. +A series of *Boot Flags* determine hows **FBU** and **FBM** hand over execution to the subsequent program. *Boot Flags* are indicated by the magic number `0xb469075a`, followed by a 32-bit field, in the first 128 bytes of the binary. @@ -28,6 +28,8 @@ struct { } ``` +These can be provided when making the program (when using a Makefile as in `examples/riscv-blink`), e.g. `make LOAD_BOOT_CONFIG=0x00000020`. + | Bit Flag | Short Name | Description | | -------- | ------------ | ------------------------------------------------ | | 00000001 | QPI_EN | Enable QPI mode on the SPI flash | @@ -45,10 +47,12 @@ If the DFU bootloader encounters the magic number `0x17ab0f23` within the first Note that the value following the magic number indicates the offset where the program will be loaded to. This should be somewhere in RAM. `0x10001000` is a good value, and is guaranteed to not interfere with Foboot itself. +When using a Makefile as in `examples/riscv-blink` you can provide the offset as in `make LOAD_RAM_ADDR=0x10001000`. This makes sure that the linker links the binary as one block. + ## Magic Numbers | Value | Description | | ------------ | ------------------------------------------- | | `0xb469075a` | Denotes the start of *boot flags* bitfield | | `0x17ab0f23` | Loads the program into RAM instead of flash | -| `0x032bd37d` | Indicates a valid **FBM** image | \ No newline at end of file +| `0x032bd37d` | Indicates a valid **FBM** image | From 714814b7350fe01668e2c7cfd84a5cb41a4ddb42 Mon Sep 17 00:00:00 2001 From: Sven Willner Date: Sat, 1 Aug 2020 19:35:00 +0200 Subject: [PATCH 4/4] Fix spelling --- doc/BOOT-SEQUENCE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/BOOT-SEQUENCE.md b/doc/BOOT-SEQUENCE.md index bdbf3ce..29d12b1 100644 --- a/doc/BOOT-SEQUENCE.md +++ b/doc/BOOT-SEQUENCE.md @@ -14,7 +14,7 @@ At start, **FBU** is loaded by the hardware. **FBU** will look for a special *F ## Boot Flags -A series of *Boot Flags* determine hows **FBU** and **FBM** hand over execution to the subsequent program. +A series of *Boot Flags* determines how **FBU** and **FBM** hand over execution to the subsequent program. *Boot Flags* are indicated by the magic number `0xb469075a`, followed by a 32-bit field, in the first 128 bytes of the binary.