BUGfix: "HAVE_BOOTLOADER_HIDDENEXITCOMMAND" depends on "BOOTLOADER_CAN_EXIT"
[pub/USBaspLoader.git] / firmware / main.c
index 19c7756..8eb785b 100644 (file)
@@ -113,7 +113,13 @@ typedef union longConverter{
 
 
 #if BOOTLOADER_CAN_EXIT
-static volatile unsigned char  stayinloader = 0xfe;
+#      if ((HAVE_UNPRECISEWAIT))
+/* here we have to assume we need to optimize for every byte */
+#define __REGISTER_stayinloader_initialValue 0xfe
+volatile register uint8_t stayinloader __asm__("r17");
+#      else
+static volatile uint8_t stayinloader = 0xfe;
+#      endif
 #endif
 
 static longConverter_t         currentAddress; /* in bytes */
@@ -196,6 +202,56 @@ static const uchar  signatureBytes[4] = {
 
 /* ------------------------------------------------------------------------ */
 
+#if (__REGISTER_stayinloader_initialValue)
+/* need to put it after libc init - otherwise it fucks up the register */
+void __attribute__ ((section(".init8"),naked,used,no_instrument_function)) __REGISTER_stayinloader_initialValue_INITIALIZATION(void);
+void __REGISTER_stayinloader_initialValue_INITIALIZATION(void) {
+  asm volatile (
+    "ldi       %[silreg]       ,       %[silval]\n\t"
+    : [silreg]         "=a" (stayinloader)
+    : [silval]         "M"  (__REGISTER_stayinloader_initialValue)
+  );
+}
+#endif
+
+#if (HAVE_BOOTLOADERENTRY_FROMSOFTWARE)
+void __attribute__ ((section(".init3"),naked,used,no_instrument_function)) __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND(void);
+void __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND(void) {
+  asm volatile (
+    "in                %[mcucsrval]    ,       %[mcucsrio]\n\t"
+    "ldi       r29             ,       %[ramendhi]\n\t"
+    "ldi       r28             ,       %[ramendlo]\n\t"
+#if (FLASHEND>131071)
+    "ld                %[result]       ,       Y+\n\t"
+    "cpi       %[result]       ,       %[bootaddrhi]\n\t"
+    "brne      __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_mismatch%=\n\t"
+#endif
+    "ld                %[result]       ,       Y+\n\t"
+    "cpi       %[result]       ,       %[bootaddrme]\n\t"
+    "ld                %[result]       ,       Y+\n\t"
+    "breq      __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_done%=\n\t"
+
+    "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_mismatch%=:\n\t"
+    "ldi       %[result]       ,       0xff\n\t"
+
+    "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_done%=:\n\t"
+    : [result]         "=a" (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch),
+      [mcucsrval]      "=a" (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR)
+    : [mcucsrio]       "I"  (_SFR_IO_ADDR(MCUCSR)),
+#if (FLASHEND>131071)
+      [ramendhi]       "M" (((RAMEND - 2) >> 8) & 0xff),
+      [ramendlo]       "M" (((RAMEND - 2) >> 0) & 0xff),
+      [bootaddrhi]     "M" (((__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS) >>16) & 0xff),
+#else
+      [ramendhi]       "M" (((RAMEND - 1) >> 8) & 0xff),
+      [ramendlo]       "M" (((RAMEND - 1) >> 0) & 0xff),
+#endif
+      [bootaddrme]     "M" (((__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS) >> 8) & 0xff)
+    
+  );
+}
+#endif
+
 #if (USE_BOOTUP_CLEARRAM)
 /*
 * Under normal circumstances, RESET will not clear contents of RAM.
@@ -206,8 +262,10 @@ void __func_clearram(void) {
   extern size_t __bss_end;
   asm volatile (
     "__clearram:\n\t"
+#if (!(HAVE_BOOTLOADERENTRY_FROMSOFTWARE))
     "ldi r29, %[ramendhi]\n\t"
     "ldi r28, %[ramendlo]\n\t"
+#endif
     "__clearramloop%=:\n\t"
     "st -Y , __zero_reg__\n\t"
     "cp r28, %A[bssend]\n\t"
@@ -367,6 +425,23 @@ defined (__AVR_ATmega2561__)
 #   endif
       }
 #endif
+#if ((HAVE_BOOTLOADER_HIDDENEXITCOMMAND) && (BOOTLOADER_CAN_EXIT))
+#      if ((HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xac) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x20) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x28) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x40) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x48) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x4c) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xa0) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xc0) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x58) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x5c) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x30) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xac) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x50) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x58) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x38))
+  }else if(rq->wValue.bytes[0] == (HAVE_BOOTLOADER_HIDDENEXITCOMMAND)){  /* cause a bootLoaderExit at disconnect */
+      stayinloader = 0xf1;  /* we need to be connected - so assume it */
+#      endif
+#endif
   }else{
       /* ignore all others, return default value == 0 */
   }
@@ -667,6 +742,7 @@ int __attribute__((__noreturn__)) main(void)
 #      endif
        wdt_disable();    /* main app may have enabled watchdog */
 #endif
+       MCUCSR = 0;       /* clear all reset flags for next time */
         initForUsbConnectivity();
         do{
             usbPoll();
@@ -692,11 +768,11 @@ asm  volatile  (
 );
 #else
        if (stayinloader >= 0x10) {
-         if (!bootLoaderCondition()) {
+         if (!bootLoaderConditionSimple()) {
            stayinloader-=0x10;
          } 
        } else {
-         if (bootLoaderCondition()) {
+         if (bootLoaderConditionSimple()) {
            if (stayinloader > 1) stayinloader-=2;
          }
        }