InfiniTime.git

commit 9b2e1b3250e12440ca259c8e814c0aac3ec9fd3c

Author: Jean-François Milants <jf@codingfield.com>

Merge branch 'aleexxzeero-proper-mcuboot-app-size' into develop

 gcc_nrf52-mcuboot.ld | 34 ++++++++++++++++++++++++++++++++--
 gcc_nrf52.ld | 39 +++++++++++++++++++++++++++++++++++++--
 src/CMakeLists.txt | 8 ++++----


diff --git a/gcc_nrf52-mcuboot.ld b/gcc_nrf52-mcuboot.ld
index 81b318c56b4e194abbdd3ac968943749d1cfd521..6c3a769b585927f026fcd97e8b9cf2c9a8508647 100644
--- a/gcc_nrf52-mcuboot.ld
+++ b/gcc_nrf52-mcuboot.ld
@@ -1,11 +1,41 @@
-/* Linker script to configure memory regions. */
+/****************************************************************
+ * Memory map configuration for using application with MCU-boot *
+ ****************************************************************/
+/*
+ * Internal nRF52 flash memory:
+ * 0x00000000: MCUBoot(28 kB)
+ * 0x00007000: Reboot logs (4 kB)
+ * 0x00008000: MCUBoot header (32 bytes)
+ * 0x00008020: Application (463+ kB)
+ * 0x0007be50: MCUBoot image trailer (432 bytes)
+ * 0x0007c000: MCUBoot Scratch partition (4 kB)
+ * 0x0007d000: unused (12 kB)
+ *
+ * SPI flash:
+ * 0x00000000: Bootloader Assets, like Boot Graphic (256 kB)
+ * 0x00040000: Application 2 (including MCUBoot header) (464 kB)
+ * 0x000b4000: User files - littlefs (3376 kB)
+ */
 
 SEARCH_DIR(.)
 GROUP(-lgcc -lc -lnosys)
 
+MCUBOOT_SIZE = 0x8000;
+SCRATCH_SIZE = 0x1000;
+TLV_SIZE =  0x28; /* Metadata added by imgtool at the end of the image */
+SCRATCH_OFFSET = 0x7c000;
+MCUBOOT_APP_IMAGE_HEADER_SIZE = 0x20;
+MCUBOOT_APP_IMAGE_TRAILER_SIZE = 0x1b0;
+APP_OFFSET = MCUBOOT_SIZE + MCUBOOT_APP_IMAGE_HEADER_SIZE;
+APP_SIZE = SCRATCH_OFFSET - MCUBOOT_SIZE - MCUBOOT_APP_IMAGE_HEADER_SIZE - MCUBOOT_APP_IMAGE_TRAILER_SIZE - TLV_SIZE;
+
+
 MEMORY
 {
-  FLASH (rx) : ORIGIN = 0x08020, LENGTH = 0x78000
+  /* MCUBOOT (r) : ORIGIN = 0x0, LENGTH = MCUBOOT_SIZE */
+  FLASH (rx) : ORIGIN = APP_OFFSET, LENGTH = APP_SIZE
+  /* SCRATCH (r) : ORIGIN = SCRATCH_OFFSET, LENGTH = SCRATCH_SIZE */
+  SPARE_SPACE (r) : ORIGIN = SCRATCH_OFFSET + SCRATCH_SIZE, LENGTH = 12K
   RAM (rwx) :  ORIGIN = 0x20000000, LENGTH = 64K
 }
 




diff --git a/gcc_nrf52.ld b/gcc_nrf52.ld
index f9bc5b681fe19e68ac24b839d78afd53ceceb701..1bd5e82165222b6c19f71d8110f735b568e67f50 100644
--- a/gcc_nrf52.ld
+++ b/gcc_nrf52.ld
@@ -1,11 +1,46 @@
-/* Linker script to configure memory regions. */
+/****************************************************************
+ * Memory map configuration for using application with MCU-boot *
+ ****************************************************************/
+/*
+ * Internal nRF52 flash memory:
+ * 0x00000000: MCUBoot(28 kB)
+ * 0x00007000: Reboot logs (4 kB)
+ * 0x00008000: MCUBoot header (32 bytes)
+ * 0x00008020: Application (463+ kB)
+ * 0x0007be50: MCUBoot image trailer (432 bytes)
+ * 0x0007c000: MCUBoot Scratch partition (4 kB)
+ * 0x0007d000: unused (12 kB)
+ *
+ * SPI flash:
+ * 0x00000000: Bootloader Assets, like Boot Graphic (256 kB)
+ * 0x00040000: Application 2 (including MCUBoot header) (464 kB)
+ * 0x000b4000: User files - littlefs (3376 kB)
+ *
+ * This linker script sets the origin of the application at 0x00
+ * which means that the application must be linked with a start
+ * address at 0x00 and that it'll overwrite the bootloader.
+ * This is very practical when debugging and testing on
+ * the hardware with a SWD probe.
+ */
 
 SEARCH_DIR(.)
 GROUP(-lgcc -lc -lnosys)
 
+MCUBOOT_SIZE = 0x8000;
+SCRATCH_SIZE = 0x1000;
+TLV_SIZE =  0x28; /* Metadata added by imgtool at the end of the image */
+SCRATCH_OFFSET = 0x7c000;
+MCUBOOT_APP_IMAGE_HEADER_SIZE = 0x20;
+MCUBOOT_APP_IMAGE_TRAILER_SIZE = 0x1b0;
+APP_OFFSET = 0x00;
+APP_SIZE = SCRATCH_OFFSET - MCUBOOT_SIZE - MCUBOOT_APP_IMAGE_HEADER_SIZE - MCUBOOT_APP_IMAGE_TRAILER_SIZE - TLV_SIZE;
+
 MEMORY
 {
-  FLASH (rx) : ORIGIN = 0x00000, LENGTH = 0x78000
+  /* MCUBOOT (r) : ORIGIN = 0x0, LENGTH = MCUBOOT_SIZE */
+  FLASH (rx) : ORIGIN = APP_OFFSET, LENGTH = APP_SIZE
+  /* SCRATCH (r) : ORIGIN = SCRATCH_OFFSET, LENGTH = SCRATCH_SIZE */
+  SPARE_SPACE (r) : ORIGIN = SCRATCH_OFFSET + SCRATCH_SIZE, LENGTH = 12K
   RAM (rwx) :  ORIGIN = 0x20000000, LENGTH = 64K
 }
 




diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c6031cd751295b0b667617f16dc7ec4c36416d67..95846a9bddba3e9f34d99828b0386c0e361c3107 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -988,8 +988,8 @@         POST_BUILD
         COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE_MCUBOOT_FILE_NAME}.out
         COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE_MCUBOOT_FILE_NAME}.out "${EXECUTABLE_MCUBOOT_FILE_NAME}.bin"
         COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_MCUBOOT_FILE_NAME}.out "${EXECUTABLE_MCUBOOT_FILE_NAME}.hex"
-        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 4 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_MCUBOOT_FILE_NAME}.hex ${IMAGE_MCUBOOT_FILE_NAME_HEX}
-        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 4 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_MCUBOOT_FILE_NAME}.bin ${IMAGE_MCUBOOT_FILE_NAME_BIN}
+        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 1 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_MCUBOOT_FILE_NAME}.hex ${IMAGE_MCUBOOT_FILE_NAME_HEX}
+        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 1 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_MCUBOOT_FILE_NAME}.bin ${IMAGE_MCUBOOT_FILE_NAME_BIN}
         COMMENT "post build steps for ${EXECUTABLE_MCUBOOT_FILE_NAME}"
         )
 
@@ -1063,7 +1063,7 @@         POST_BUILD
         COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}.out
         COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}.out "${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}.bin"
         COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}.out "${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}.hex"
-        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 4 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}.hex ${IMAGE_RECOVERY_MCUBOOT_FILE_NAME_HEX}
+        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 1 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}.hex ${IMAGE_RECOVERY_MCUBOOT_FILE_NAME_HEX}
         COMMAND ${CMAKE_OBJCOPY} -I ihex -O binary ${IMAGE_RECOVERY_MCUBOOT_FILE_NAME_HEX} "${IMAGE_RECOVERY_MCUBOOT_FILE_NAME}.bin"
         COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/bin2c.py ${IMAGE_RECOVERY_MCUBOOT_FILE_NAME}.bin recoveryImage > recoveryImage.h
         COMMENT "post build steps for ${EXECUTABLE_RECOVERY_MCUBOOT_FILE_NAME}"
@@ -1141,7 +1141,7 @@         POST_BUILD
         COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.out
         COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.out "${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.bin"
         COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.out "${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.hex"
-        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 4 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.hex ${IMAGE_MCUBOOT_RECOVERYLOADER_FILE_NAME_HEX}
+        COMMAND ${CMAKE_SOURCE_DIR}/tools/mcuboot/imgtool.py create --align 1 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header ${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.hex ${IMAGE_MCUBOOT_RECOVERYLOADER_FILE_NAME_HEX}
         COMMAND ${CMAKE_OBJCOPY} -I ihex -O binary ${IMAGE_MCUBOOT_RECOVERYLOADER_FILE_NAME_HEX} "${IMAGE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.bin"
         COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/bin2c.py ${IMAGE_MCUBOOT_RECOVERYLOADER_FILE_NAME}.bin recoveryLoaderImage > recoveryLoaderImage.h
         COMMENT "post build steps for ${EXECUTABLE_MCUBOOT_RECOVERYLOADER_FILE_NAME}"