From 399708910e9d332897da6d008513266a6c172527 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Thu, 18 Mar 2021 17:46:37 -0700 Subject: [PATCH] Add target for bootloader update --- blup/blup.h | 3 ++ blup/blupmain.c | 35 ++++++++++++++++++++++++ build.mk | 65 ++++++++++++++++++++++++++++++++++---------- scripts/bin2c.js | 25 +++++++++++++++++ scripts/patch-bin.js | 2 +- 5 files changed, 115 insertions(+), 15 deletions(-) create mode 100644 blup/blup.h create mode 100644 blup/blupmain.c create mode 100644 scripts/bin2c.js diff --git a/blup/blup.h b/blup/blup.h new file mode 100644 index 0000000..308f278 --- /dev/null +++ b/blup/blup.h @@ -0,0 +1,3 @@ +#pragma once + +#include "blutil.h" diff --git a/blup/blupmain.c b/blup/blupmain.c new file mode 100644 index 0000000..ecea50a --- /dev/null +++ b/blup/blupmain.c @@ -0,0 +1,35 @@ +#include "blup.h" + +extern const unsigned char bootloader[]; + +int main(void) { + __disable_irq(); + + led_init(); + + const uint32_t *sp = (const uint32_t *)bootloader; + if (sp[0] != 0x2fd56055) + jd_panic(); // bad magic + uint32_t len = sp[1]; + sp += 4; + + uint8_t *flashend = (void *)(0x08000000 + JD_FLASH_SIZE); + uint8_t *bladdr = (void *)(0x08000000 + JD_FLASH_SIZE - BL_SIZE); + + uint8_t *erase_ptr = bladdr; + while (erase_ptr < flashend) { + flash_erase(erase_ptr); + erase_ptr += FLASH_PAGE_SIZE; + } + + len = (len + 7) & ~7; + flash_program(bladdr, sp, len); + + // self-destruct + uint32_t addr = (uint32_t)&app_handlers->app_reset_handler; + addr &= ~7; + uint64_t zero = 0; + flash_program((void *)addr, &zero, sizeof(zero)); + + target_reset(); +} diff --git a/build.mk b/build.mk index 463a363..af54d7e 100644 --- a/build.mk +++ b/build.mk @@ -39,11 +39,25 @@ CONFIG_DEPS = \ $(wildcard targets/$(TARGET)/*.h) \ targets/$(TARGET)/config.mk -ifeq ($(BL),) -PREF = app -else +ifneq ($(BL),) PREF = bl +override APP = +override BLUP = endif + +ifneq ($(BLUP),) +PREF = blup +override APP = +override BL = +endif + +ifeq ($(BL)$(BLUP),) +PREF = app +override APP = 1 +override BL = +override BLUP = +endif + BUILT_BIN = built/$(TARGET) BUILT = $(BUILT_BIN)/$(PREF) @@ -52,7 +66,7 @@ BASE_TARGET ?= $(TARGET) PROFILES = $(patsubst targets/$(TARGET)/profile/%.c,%,$(wildcard targets/$(TARGET)/profile/*.c)) -ifeq ($(BL),) +ifneq ($(APP),) C_SRC += $(wildcard $(JD_CORE)/source/*.c) C_SRC += $(wildcard $(JD_CORE)/services/*.c) C_SRC += $(wildcard $(JD_CORE)/drivers/*.c) @@ -67,19 +81,32 @@ endif C_SRC += $(wildcard $(JD_STM)/src/*.c) C_SRC += $(wildcard $(PLATFORM)/*.c) C_SRC += $(HALSRC) -else -DEFINES += -DDEVICE_DMESG_BUFFER_SIZE=0 -DBL -CPPFLAGS += -Ibl -C_SRC += $(wildcard $(JD_STM)/bl/*.c) +endif + +ifneq ($(BL)$(BLUP),) C_SRC += $(PLATFORM)/pins.c C_SRC += $(PLATFORM)/init.c C_SRC += $(PLATFORM)/flash.c C_SRC += $(PLATFORM)/adc.c C_SRC += $(JD_STM)/src/dmesg.c C_SRC += $(JD_CORE)/source/jd_util.c +endif + +ifneq ($(BL),) +DEFINES += -DDEVICE_DMESG_BUFFER_SIZE=0 -DBL +C_SRC += $(wildcard $(JD_STM)/bl/*.c) AS_SRC += $(JD_STM)/bl/boothandler.s endif +ifneq ($(BLUP),) +DEFINES += -DBLUP +CPPFLAGS += -I$(JD_STM)/bl +C_SRC += $(JD_STM)/bl/blutils.c +C_SRC += $(JD_STM)/bl/blpwm.c +C_SRC += $(JD_STM)/bl/blled.c +C_SRC += $(wildcard $(JD_STM)/blup/*.c) +endif + ELF = $(BUILT_BIN)/$(PREF)-$(PROF).elf ifneq ($(BMP),) @@ -108,10 +135,10 @@ LDFLAGS = -specs=nosys.specs -specs=nano.specs \ all: refresh-version ifeq ($(BL)$(NOBL),) $(V)node $(SCRIPTS)/check-fw-id.js targets + $(MAKE) $(MAKE_FLAGS) BL=1 build endif $(MAKE) $(MAKE_FLAGS) build ifeq ($(BL)$(NOBL),) - $(MAKE) $(MAKE_FLAGS) BL=1 build $(MAKE) combine endif $(V)$(PREFIX)size $(BUILT_BIN)/*.elf @@ -172,10 +199,13 @@ $(BUILT)/%.o: %.s @echo AS $< $(V)$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $< -%.hex: %.elf $(SCRIPTS)/bin2uf2.js +%.hex: %.elf $(SCRIPTS)/bin2uf2.js $(SCRIPTS)/bin2c.js @echo BIN/HEX $< $(V)$(PREFIX)objcopy -O binary $< $(@:.hex=.bin) $(V)$(PREFIX)objcopy -O ihex $< $@ +ifneq ($(BL),) + $(V)node $(SCRIPTS)/bin2c.js $(@:.hex=.bin) +endif ifeq ($(BL)$(NOBL),) @echo UF2 $< $(V)node $(SCRIPTS)/bin2uf2.js $(@:.hex=.bin) @@ -207,11 +237,16 @@ refresh-version: @mkdir -p $(BUILT_BIN) echo 'const char app_fw_version[] = "$(FW_VERSION)";' > $(BUILT_BIN)/version.c -$(BUILT_BIN)/version.o: $(BUILT_BIN)/version.c +$(BUILT_BIN)/%.o: $(BUILT_BIN)/%.c $(V)$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $< +ifneq ($(BLUP),) +PROF_DEP = $(BUILT_BIN)/bl +else +PROF_DEP = $(BUILT)/jd/prof +endif -$(BUILT_BIN)/$(PREF)-%.elf: $(BUILT)/jd/prof-%.o $(OBJ) Makefile $(LD_SCRIPT) $(SCRIPTS)/patch-bin.js $(FORCE) +$(BUILT_BIN)/$(PREF)-%.elf: $(PROF_DEP)-%.o $(OBJ) Makefile $(LD_SCRIPT) $(SCRIPTS)/patch-bin.js $(FORCE) @echo LD $@ $(V)$(CC) $(CFLAGS) $(LDFLAGS) -Wl,-Map=$@.map -o $@ $(OBJ) $< -lm ifeq ($(NOBL),) @@ -240,10 +275,12 @@ force: targ-%: $(MAKE) TARGET=$(subst targ-,,$@) + $(MAKE) TARGET=$(subst targ-,,$@) BLUP=1 drop: $(addprefix targ-,$(DROP_TARGETS)) - cd built; cat $(addsuffix /*.uf2,$(DROP_TARGETS)) > drop.uf2 - @ls -l built/drop.uf2 + cd built; cat $(addsuffix /app-*.uf2,$(DROP_TARGETS)) > drop.uf2 + cd built; cat $(addsuffix /blup-*.uf2,$(DROP_TARGETS)) > bootloader-update.uf2 + @ls -l built/drop.uf2 built/bootloader-update.uf2 ff: full-flash diff --git a/scripts/bin2c.js b/scripts/bin2c.js new file mode 100644 index 0000000..639869f --- /dev/null +++ b/scripts/bin2c.js @@ -0,0 +1,25 @@ +#!/usr/bin/env node +"use strict"; + +const fs = require("fs") +const fn = process.argv[2] +if (!fn) + throw "USAGE: node bin2uf2.js file.bin" + + +const buf0 = fs.readFileSync(fn) + +let hex = "extern const unsigned char bootloader[];\n" +hex += "const unsigned char bootloader[] __attribute__((aligned(8))) = {\n" +const bufpref = Buffer.alloc(16) +bufpref.writeUInt32LE(0x2fd56055, 0) +bufpref.writeUInt32LE(buf0.length, 4) +const bufsuff = Buffer.alloc(16) +const buf = Buffer.concat([bufpref, buf0, bufsuff]) +for (let i = 0; i < buf.length; ++i) { + hex += "0x" + ("0" + buf[i].toString(16)).slice(-2) + "," + if (i % 16 == 15) hex += "\n" + else hex += " " +} +hex += "};\n" +fs.writeFileSync(fn.replace(".bin", ".c"), hex) diff --git a/scripts/patch-bin.js b/scripts/patch-bin.js index 000cc42..223c46f 100644 --- a/scripts/patch-bin.js +++ b/scripts/patch-bin.js @@ -60,7 +60,7 @@ if ((w0 & 0xff000000) == 0x20000000) { throw "folder names cannot start with _" // figure out device class - const profile_name = basename.replace(/.*\/app-/, "") + const profile_name = basename.replace(/.*\/(app|blup)-/, "") const profile_fn = profiles_path + "/" + profile_name + ".c" const src = fs.readFileSync(profile_fn, "utf8") const m = /FIRMWARE_IDENTIFIER\((0x3[0-9a-f]+),\s*"([^"]+)"\)/.exec(src)