diff --git a/doc/BOOT-SEQUENCE.md b/doc/BOOT-SEQUENCE.md index 0874e2b..3478e73 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* 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. @@ -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 | @@ -51,6 +53,8 @@ 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 | diff --git a/examples/riscv-blink/Makefile b/examples/riscv-blink/Makefile index af44183..f4e5dda 100644 --- a/examples/riscv-blink/Makefile +++ b/examples/riscv-blink/Makefile @@ -26,19 +26,33 @@ 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 ifdef AUTOBOOT_TIMEOUT ADD_CFLAGS += -DAUTOBOOT_TIMEOUT=$(AUTOBOOT_TIMEOUT) 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 := -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 \ @@ -46,8 +60,10 @@ 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=$(LDREGIONS) \ -Wl,--script=$(LDSCRIPT) \ -Wl,--build-id=none @@ -68,7 +84,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) @@ -80,7 +96,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 $@ @@ -110,7 +126,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))" @@ -120,4 +136,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/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 87c6d51..fa459d4 100644 --- a/examples/riscv-blink/src/crt0-vexriscv.S +++ b/examples/riscv-blink/src/crt0-vexriscv.S @@ -10,8 +10,14 @@ _start: .word 0x4a6de3ac .word AUTOBOOT_TIMEOUT #endif +#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 7983f6d..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 @@ -76,7 +89,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 +101,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 +131,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 +141,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/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