From b01e4a6e47a1c6aacfd054042863bda8fd971408 Mon Sep 17 00:00:00 2001 From: Ondrej Stanek Date: Tue, 5 Apr 2022 15:09:47 +0200 Subject: [PATCH] Fix STK_SW_MAJOR and STK_SW_MINOR request responses The major and minor version are stored at the end of program space, but the protocol handler falsely uses the `optiboot_version` address as an address to RAM, although the data is actually allocated in FLASH and should be retrieved by a dedicated `lpm` instructions. CAUTION: this change increase the binary size 22 bytes on avr-gcc 11.2.0 and 26 bytes on avr-gcc 5.4.0. Therefore, the team might want to look at different solutions that do not occupy that much code space. Nonetheless, there is a bug in the current implementation of STK_SW_MAJOR STK_SW_MINOR that needs addressing. --- optiboot/bootloaders/optiboot/optiboot.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/optiboot/bootloaders/optiboot/optiboot.c b/optiboot/bootloaders/optiboot/optiboot.c index dec9c45ed2..d194b54953 100644 --- a/optiboot/bootloaders/optiboot/optiboot.c +++ b/optiboot/bootloaders/optiboot/optiboot.c @@ -344,8 +344,15 @@ #define OPTIBOOT_CUSTOMVER 0 #endif -unsigned const int __attribute__((section(".version"))) -optiboot_version = 256*(OPTIBOOT_MAJVER + OPTIBOOT_CUSTOMVER) + OPTIBOOT_MINVER; +struct OptibootVersion { + unsigned char minor; // minor first, for backward compatible memory layout + unsigned char major; +} __attribute__((packed)); +const struct OptibootVersion __attribute__((section(".version"))) +optiboot_version = { + .major = OPTIBOOT_MAJVER + OPTIBOOT_CUSTOMVER, + .minor = OPTIBOOT_MINVER +}; #define __AVR_LIBC_DEPRECATED_ENABLE__ 1 // don't poison MCUSR on some chips @@ -929,12 +936,11 @@ int main(void) { verifySpace(); /* * Send optiboot version as "SW version" - * Note that the references to memory are optimized away. */ if (which == STK_SW_MINOR) { - putch(optiboot_version & 0xFF); + putch(pgm_read_byte_near(&optiboot_version.minor)); } else if (which == STK_SW_MAJOR) { - putch(optiboot_version >> 8); + putch(pgm_read_byte_near(&optiboot_version.major)); } else { /* * GET PARAMETER returns a generic 0x03 reply for