From 198c3fb21b3daa0cc21165a511b9b9eb0cc7105b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Redon?= Date: Wed, 11 Dec 2019 16:20:14 +0100 Subject: [PATCH] improve shared bootloader/application memory now both partitions (bootloader and application) use a commonly defined memory location to shared the DFU state (which includes the magic value to know which part to start), instead of using a hard coded value. the bootloader size has now also been restricted to 16 kB. this limitation is enforced so to not be able to create larger images, which could be corrupted when flashing the application. bootloader and application flashing have been successfully tested on qmod st12 and st34. Change-Id: I204bed7e9391602672ed894decec1fc12e879275 --- .../atmel_softpack_libraries/usb/device/dfu/dfu_driver.c | 6 ++++-- .../atmel_softpack_libraries/usb/device/dfu/dfu_runtime.c | 7 ++++++- firmware/libboard/common/resources/sam3s4/dfu.ld | 8 +++++--- firmware/libboard/common/resources/sam3s4/flash.ld | 4 ++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_driver.c b/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_driver.c index 3ffd9b31..e95c67b2 100644 --- a/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_driver.c +++ b/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_driver.c @@ -33,8 +33,7 @@ #include #include -/* FIXME: this was used for a special ELF section which then got called - * by DFU code and Application code, across flash partitions */ +/** specific memory location shared across bootloader and application */ #define __dfudata __attribute__ ((section (".dfudata"))) #define __dfufunc @@ -42,11 +41,14 @@ static USBDDriver usbdDriver; static unsigned char if_altsettings[1]; +/** structure containing the DFU state and magic value to know if DFU or application should be started */ __dfudata struct dfudata _g_dfu = { .state = DFU_STATE_appIDLE, .past_manifest = 0, .total_bytes = 0, }; + +/** variable to structure containing DFU state */ struct dfudata *g_dfu = &_g_dfu; WEAK void dfu_drv_updstatus(void) diff --git a/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_runtime.c b/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_runtime.c index ac4d7dff..4f772be6 100644 --- a/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_runtime.c +++ b/firmware/atmel_softpack_libraries/usb/device/dfu/dfu_runtime.c @@ -36,7 +36,12 @@ #include #include -struct dfudata *g_dfu = (struct dfudata *) IRAM_ADDR; +/** specific memory location shared across bootloader and application */ +#define __dfudata __attribute__ ((section (".dfudata"))) +/** structure containing the magic value to know if DFU or application should be started */ +__dfudata struct dfudata _g_dfu; +/** variable to structure containing the magic value to know if DFU or application should be started */ +struct dfudata *g_dfu = &_g_dfu; /* FIXME: this was used for a special ELF section which then got called * by DFU code and Application code, across flash partitions */ diff --git a/firmware/libboard/common/resources/sam3s4/dfu.ld b/firmware/libboard/common/resources/sam3s4/dfu.ld index e56a435a..a485770f 100644 --- a/firmware/libboard/common/resources/sam3s4/dfu.ld +++ b/firmware/libboard/common/resources/sam3s4/dfu.ld @@ -39,9 +39,9 @@ SEARCH_DIR(.) MEMORY { /* reserve the first 16k (= 0x4000) for the DFU bootloader */ - rom (rx) : ORIGIN = 0x00404000, LENGTH = 0x0003c000 /* flash, 256K */ - /* reserve the first 32 (= 0x20) bytes for the _g_dfu struct */ - ram (rwx) : ORIGIN = 0x20000020, LENGTH = 0x0000bfe0 /* sram, 48K */ + rom (rx) : ORIGIN = 0x00400000 + 16K, LENGTH = 256K - 16K /* flash, 256K */ + /* note: dfudata will be at the start */ + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 48K /* SRAM, 48K */ } /* Section Definitions */ @@ -111,6 +111,8 @@ SECTIONS { . = ALIGN(4); _srelocate = .; + /* we must make sure the .dfudata is linked to start of RAM */ + *(.dfudata .dfudata.*); *(.ramfunc .ramfunc.*); *(.data .data.*); . = ALIGN(4); diff --git a/firmware/libboard/common/resources/sam3s4/flash.ld b/firmware/libboard/common/resources/sam3s4/flash.ld index bdebbdee..f5cdbfda 100644 --- a/firmware/libboard/common/resources/sam3s4/flash.ld +++ b/firmware/libboard/common/resources/sam3s4/flash.ld @@ -38,8 +38,8 @@ SEARCH_DIR(.) /* Memory Spaces Definitions */ MEMORY { - rom (rx) : ORIGIN = 0x00400000, LENGTH = 0x00040000 /* flash, 256K */ - ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x0000c000 /* sram, 48K */ + rom (rx) : ORIGIN = 0x00400000, LENGTH = 16K /* flash, 256K, but only the first 16K should be used for the bootloader */ + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 48K /* SRAM, 48K */ } /* Section Definitions */