add new feature: "BOOTUP_CLEARRAM"
[pub/USBaspLoader.git] / firmware / main.c
index ae06b49..91127ef 100644 (file)
@@ -42,8 +42,6 @@
 
 
 
 
 
 
-static void leaveBootloader() __attribute__((__noreturn__));
-
 #include "bootloaderconfig.h"
 #include "usbdrv/usbdrv.c"
 
 #include "bootloaderconfig.h"
 #include "usbdrv/usbdrv.c"
 
@@ -147,7 +145,7 @@ static const uchar  signatureBytes[4] = {
     0x1e, 0x93, 0x0F, 0
 #elif defined (__AVR_ATmega164A__)
     0x1e, 0x94, 0x0f, 0
     0x1e, 0x93, 0x0F, 0
 #elif defined (__AVR_ATmega164A__)
     0x1e, 0x94, 0x0f, 0
-#elif defined (__AVR_ATmega164P__)
+#elif defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
     0x1e, 0x94, 0x0a, 0
 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
     0x1e, 0x94, 0x06, 0
     0x1e, 0x94, 0x0a, 0
 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
     0x1e, 0x94, 0x06, 0
@@ -180,10 +178,63 @@ static const uchar  signatureBytes[4] = {
 
 /* ------------------------------------------------------------------------ */
 
 
 /* ------------------------------------------------------------------------ */
 
+#if (USE_BOOTUP_CLEARRAM)
+/*
+* Under normal circumstances, RESET will not clear contents of RAM.
+* As always, if you want it done - do it yourself...
+*/
+void __attribute__ ((naked)) __attribute__ ((section (".init3"))) __clearram(void);
+void __clearram(void) {
+  extern size_t __bss_end;
+  asm volatile (
+    "__clearram:\n\t"
+    "ldi r29, %[ramendhi]\n\t"
+    "ldi r28, %[ramendlo]\n\t"
+    "__clearramloop%=:\n\t"
+    "st -Y , __zero_reg__\n\t"
+    "cp r28, %A[bssend]\n\t"
+    "cpc r29, %B[bssend]\n\t"
+    "brne __clearramloop%=\n\t"
+    :
+    : [ramendhi] "M" (((RAMEND+1)>>8) & 0xff),
+      [ramendlo] "M" (((RAMEND+1)>>0) & 0xff),
+      [bssend] "r" (&__bss_end)
+    : "memory"
+      );
+}
+#endif
+
+#if (!USE_EXCESSIVE_ASSEMBLER) || (!(defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)))
 static void (*nullVector)(void) __attribute__((__noreturn__));
 static void (*nullVector)(void) __attribute__((__noreturn__));
+#endif
 
 
-static void leaveBootloader()
+static void __attribute__((__noreturn__)) leaveBootloader()
 {
 {
+#if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
+asm  volatile  (
+  "cli\n\t"
+  "clr         r30\n\t"
+  "sbi         %[usbddr],      %[usbminus]\n\t"  
+  "cbi         %[port],        %[bit]\n\t"
+  "out         %[usbintrenab], r30\n\t"
+  "out         %[usbintrcfg],  r30\n\t"
+  "ldi         r31,            %[ivce]\n\t"
+  "out         %[mygicr],      r31\n\t"
+  "out         %[mygicr],      r30\n\t"  
+  "clr         r31\n\t"
+  "icall\n\t"
+  :
+  : [port]        "I" (_SFR_IO_ADDR(PIN_PORT(JUMPER_PORT))),
+    [bit]         "I" (PIN(JUMPER_PORT, JUMPER_BIT)),
+    [usbintrenab] "I" (_SFR_IO_ADDR(USB_INTR_ENABLE)),
+    [usbintrcfg]  "I" (_SFR_IO_ADDR(USB_INTR_CFG)),
+    [usbddr]      "I" (_SFR_IO_ADDR(USBDDR)),
+    [usbminus]    "I" (USBMINUS),
+    [mygicr]      "I" (_SFR_IO_ADDR(GICR)),          
+    [ivce]        "I" (1<<IVCE)
+);
+/* TODO: compiler will put an unnecessary "ret" at this end ! */
+#else
     DBG1(0x01, 0, 0);
     cli();
     usbDeviceDisconnect();
     DBG1(0x01, 0, 0);
     cli();
     usbDeviceDisconnect();
@@ -199,97 +250,106 @@ static void leaveBootloader()
  * handled correctly by the assembler.
  */
     nullVector();
  * handled correctly by the assembler.
  */
     nullVector();
+#endif
 }
 
 /* ------------------------------------------------------------------------ */
 
 }
 
 /* ------------------------------------------------------------------------ */
 
-uchar   usbFunctionSetup(uchar data[8])
-{
-usbRequest_t    *rq = (void *)data;
-uchar           len = 0;
-static uchar    replyBuffer[4];
 
 
-    usbMsgPtr = replyBuffer;
-    if(rq->bRequest == USBASP_FUNC_TRANSMIT){   /* emulate parts of ISP protocol */
-        uchar rval = 0;
-        usbWord_t address;
-        address.bytes[1] = rq->wValue.bytes[1];
-        address.bytes[0] = rq->wIndex.bytes[0];
-        if(rq->wValue.bytes[0] == 0x30){        /* read signature */
-            rval = rq->wIndex.bytes[0] & 3;
-            rval = signatureBytes[rval];
+uchar usbFunctionSetup_USBASP_FUNC_TRANSMIT(usbRequest_t *rq) {
+  uchar rval = 0;
+  usbWord_t address;
+  address.bytes[1] = rq->wValue.bytes[1];
+  address.bytes[0] = rq->wIndex.bytes[0];
+
+  if(rq->wValue.bytes[0] == 0x30){        /* read signature */
+    rval = rq->wIndex.bytes[0] & 3;
+    rval = signatureBytes[rval];
 #if HAVE_READ_LOCK_FUSE
 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega32__)
 #if HAVE_READ_LOCK_FUSE
 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega32__)
-        }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
-            rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
-        }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
-            rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
-        }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){  /* read hfuse bits */
-            rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
+  }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
+      rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
+  }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
+      rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
+  }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){  /* read hfuse bits */
+      rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
 
 #elif defined (__AVR_ATmega48__)   || defined (__AVR_ATmega48A__)   || defined (__AVR_ATmega48P__)   || defined (__AVR_ATmega48PA__)  ||  \
 
 #elif defined (__AVR_ATmega48__)   || defined (__AVR_ATmega48A__)   || defined (__AVR_ATmega48P__)   || defined (__AVR_ATmega48PA__)  ||  \
-      defined (__AVR_ATmega88__)   || defined (__AVR_ATmega88A__)   || defined (__AVR_ATmega88P__)   || defined (__AVR_ATmega88PA__)  ||  \
-      defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__)  ||                                                                       \
-      defined (__AVR_ATmega168__)  || defined (__AVR_ATmega168A__)  || defined (__AVR_ATmega168P__)  || defined (__AVR_ATmega168PA__) ||  \
-      defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__)  ||                                                               \
-      defined (__AVR_ATmega328__)  || defined (__AVR_ATmega328P__)  ||                                                               \
-      defined (__AVR_ATmega644__)  || defined (__AVR_ATmega644A__)  || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)  ||  \
-      defined (__AVR_ATmega128__)  ||                                                                                                        \
-      defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
-        }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
-            rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
-        }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
-            rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
-        }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){  /* read hfuse bits */
-            rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
-        }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x08){  /* read efuse bits */
-            rval = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS );
+defined (__AVR_ATmega88__)   || defined (__AVR_ATmega88A__)   || defined (__AVR_ATmega88P__)   || defined (__AVR_ATmega88PA__)  ||  \
+defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__)  ||                                                                     \
+defined (__AVR_ATmega168__)  || defined (__AVR_ATmega168A__)  || defined (__AVR_ATmega168P__)  || defined (__AVR_ATmega168PA__) ||  \
+defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__)  ||                                                                     \
+defined (__AVR_ATmega328__)  || defined (__AVR_ATmega328P__)  ||                                                                     \
+defined (__AVR_ATmega644__)  || defined (__AVR_ATmega644A__)  || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)  ||  \
+defined (__AVR_ATmega128__)  ||                                                                                                              \
+defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
+  }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
+      rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
+  }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
+      rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
+  }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){  /* read hfuse bits */
+      rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
+  }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x08){  /* read efuse bits */
+      rval = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS );
 #else
 #else
-       #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
+  #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
 #endif
 #endif
 #if HAVE_FLASH_BYTE_READACCESS
 #endif
 #endif
 #if HAVE_FLASH_BYTE_READACCESS
-        }else if(rq->wValue.bytes[0] == 0x20){  /* read FLASH low  byte */
+  }else if(rq->wValue.bytes[0] == 0x20){  /* read FLASH low  byte */
 #if ((FLASHEND) > 65535)
 #if ((FLASHEND) > 65535)
-            rval = pgm_read_byte_far((((addr_t)address.word)<<1)+0);
+      rval = pgm_read_byte_far((((addr_t)address.word)<<1)+0);
 #else
 #else
-            rval = pgm_read_byte((((addr_t)address.word)<<1)+0);
+      rval = pgm_read_byte((((addr_t)address.word)<<1)+0);
 #endif
 #endif
-        }else if(rq->wValue.bytes[0] == 0x28){  /* read FLASH high byte */
+  }else if(rq->wValue.bytes[0] == 0x28){  /* read FLASH high byte */
 #if ((FLASHEND) > 65535)
 #if ((FLASHEND) > 65535)
-            rval = pgm_read_byte_far((((addr_t)address.word)<<1)+1);
+      rval = pgm_read_byte_far((((addr_t)address.word)<<1)+1);
 #else
 #else
-            rval = pgm_read_byte((((addr_t)address.word)<<1)+1);
+      rval = pgm_read_byte((((addr_t)address.word)<<1)+1);
 #endif
 #endif
 #if HAVE_EEPROM_BYTE_ACCESS
 #endif
 #endif
 #if HAVE_EEPROM_BYTE_ACCESS
-        }else if(rq->wValue.bytes[0] == 0xa0){  /* read EEPROM byte */
-            rval = eeprom_read_byte((void *)address.word);
-        }else if(rq->wValue.bytes[0] == 0xc0){  /* write EEPROM byte */
-            eeprom_write_byte((void *)address.word, rq->wIndex.bytes[1]);
+  }else if(rq->wValue.bytes[0] == 0xa0){  /* read EEPROM byte */
+      rval = eeprom_read_byte((void *)address.word);
+  }else if(rq->wValue.bytes[0] == 0xc0){  /* write EEPROM byte */
+      eeprom_write_byte((void *)address.word, rq->wIndex.bytes[1]);
 #endif
 #if HAVE_CHIP_ERASE
 #endif
 #if HAVE_CHIP_ERASE
-        }else if(rq->wValue.bytes[0] == 0xac && rq->wValue.bytes[1] == 0x80){  /* chip erase */
-            addr_t addr;
+  }else if(rq->wValue.bytes[0] == 0xac && rq->wValue.bytes[1] == 0x80){  /* chip erase */
+      addr_t addr;
 #if HAVE_BLB11_SOFTW_LOCKBIT
 #if HAVE_BLB11_SOFTW_LOCKBIT
-            for(addr = 0; addr < (addr_t)(BOOTLOADER_PAGEADDR) ; addr += SPM_PAGESIZE) {
+      for(addr = 0; addr < (addr_t)(BOOTLOADER_PAGEADDR) ; addr += SPM_PAGESIZE) {
 #else
 #else
-            for(addr = 0; addr <= (addr_t)(FLASHEND) ; addr += SPM_PAGESIZE) {
+      for(addr = 0; addr <= (addr_t)(FLASHEND) ; addr += SPM_PAGESIZE) {
 #endif
 #endif
-                /* wait and erase page */
-                DBG1(0x33, 0, 0);
+         /* wait and erase page */
+         DBG1(0x33, 0, 0);
 #   ifndef NO_FLASH_WRITE
 #   ifndef NO_FLASH_WRITE
-                boot_spm_busy_wait();
-                cli();
-                boot_page_erase(addr);
-                sei();
+         boot_spm_busy_wait();
+         cli();
+         boot_page_erase(addr);
+         sei();
 #   endif
 #   endif
-            }
+      }
 #endif
 #endif
-        }else{
-            /* ignore all others, return default value == 0 */
-        }
-        replyBuffer[3] = rval;
+  }else{
+      /* ignore all others, return default value == 0 */
+  }
+        
+  return rval;
+}
+
+
+uchar   usbFunctionSetup(uchar data[8])
+{
+usbRequest_t    *rq = (void *)data;
+uchar           len = 0;
+static uchar    replyBuffer[4];
+
+    usbMsgPtr = replyBuffer;
+    if(rq->bRequest == USBASP_FUNC_TRANSMIT){   /* emulate parts of ISP protocol */
+        replyBuffer[3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT(rq);
         len = 4;
     }else if((rq->bRequest == USBASP_FUNC_ENABLEPROG) || (rq->bRequest == USBASP_FUNC_SETISPSCK)){
         /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
         len = 4;
     }else if((rq->bRequest == USBASP_FUNC_ENABLEPROG) || (rq->bRequest == USBASP_FUNC_SETISPSCK)){
         /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
@@ -324,6 +384,119 @@ static uchar    replyBuffer[4];
     return len;
 }
 
     return len;
 }
 
+#if (USE_EXCESSIVE_ASSEMBLER) && ((!HAVE_CHIP_ERASE) || (HAVE_ONDEMAND_PAGEERASE)) && (SPM_PAGESIZE <= 256) && (((BOOTLOADER_PAGEADDR>>0)&0xff) == 0)
+uchar usbFunctionWrite(uchar *data, uchar len)
+{
+uchar   isLast;
+
+    DBG1(0x31, (void *)&currentAddress.l, 4);
+    if(len > bytesRemaining)
+        len = bytesRemaining;
+    bytesRemaining -= len;
+    isLast = bytesRemaining == 0;
+    if(currentRequest >= USBASP_FUNC_READEEPROM){
+        uchar i;
+        for(i = 0; i < len; i++){
+            eeprom_write_byte((void *)(currentAddress.w[0]++), *data++);
+        }
+    }else{
+       asm  volatile  (
+         "sbrc         %[len], 0\n\t"
+         "inc          %[len]\n\t"
+"usbFunctionWrite_flashloop:\n\t"
+         "subi         %[len], 2\n\t"
+         "brlo         usbFunctionWrite_finished\n\t"
+         
+#if HAVE_BLB11_SOFTW_LOCKBIT
+         "cpi          r31, %[blsaddrhi]\n\t"                  /* accelerated BLB11_SOFTW_LOCKBIT check */
+         "brsh         usbFunctionWrite_finished\n\t"
+//       "brlo         usbFunctionWrite_addrunlock_ok\n\t"
+//       "brne         usbFunctionWrite_finished\n\t"
+//       "cpi          r30, %[blsaddrlo]\n\t"
+//       "brlo         usbFunctionWrite_addrunlock_ok\n\t"
+//       "rjmp         usbFunctionWrite_finished\n\t"
+// "usbFunctionWrite_addrunlock_ok:\n\t"
+#endif
+         "rcall        usbFunctionWrite_waitA\n\t"
+         "cli\n\t"                                             /* r0 or r1 may be __zero_reg__ and may become dangerous nonzero within interrupts */
+         "ld           r0,             X+\n\t"
+         "ld           r1,             X+\n\t"
+
+         "ldi          r18,            %[pagfillval]\n\t"
+         "rcall        usbFunctionWrite_saveflash\n\t" /* page fill */
+
+         "mov          r18,            r30\n\t"
+         "subi         r18,            0xfe\n\t"               /* add with 2 */
+         "andi         r18,            %[pagemask]\n\t"
+         "breq         usbFunctionWrite_pageisfull\n\t"
+         "tst          %[islast]\n\t"
+         "breq         usbFunctionWrite_skippageisfull\n\t"
+         "tst          %[isLastPage]\n\t"
+         "breq         usbFunctionWrite_skippageisfull\n\t"
+         "cpi          %[len],         0\n\t"
+         "brne         usbFunctionWrite_skippageisfull\n\t"
+
+"usbFunctionWrite_pageisfull:\n\t"                             /* start writing the page */
+         "ldi          r18,            %[pageraseval]\n\t"
+         "rcall        usbFunctionWrite_saveflash\n\t" /* page erase */
+         "rcall        usbFunctionWrite_waitA\n\t"
+
+         "ldi          r18,            %[pagwriteval]\n\t"
+         "rcall        usbFunctionWrite_saveflash\n\t" /* page write */
+         "rcall        usbFunctionWrite_waitA\n\t"
+
+         "in           __tmp_reg__,    %[spmcr]\n\t"
+         "sbrs         __tmp_reg__,    %[rwwsbbit]\n\t"
+         "rjmp         usbFunctionWrite_skippageisfull\n\t"
+         "ldi          r18,            %[rwwenrval]\n\t"
+         "rcall        usbFunctionWrite_saveflash\n\t" /* reenable rww*/
+//       "rcall        usbFunctionWrite_waitA\n\t"
+
+
+"usbFunctionWrite_skippageisfull:\n\t"   
+         "adiw         r30,            0x2\n\t"
+         "rjmp         usbFunctionWrite_flashloop\n\t"
+
+"usbFunctionWrite_saveflash:\n\t"
+         "cli\n\t"
+         "out          %[spmcr],       r18\n\t"
+         "spm\n\t"
+         "clr          __zero_reg__\n\t"                       /* if r0 or r1 is __zero_reg__ it may have become inconsisten while page-fill */
+         "sei\n\t"
+         "ret\n\t"
+
+"usbFunctionWrite_waitA:\n\t"
+         "in           __tmp_reg__,    %[spmcr]\n\t"
+         "sbrc         __tmp_reg__,    %[spmenbit]\n\t"
+         "rjmp         usbFunctionWrite_waitA\n\t"
+         "ret\n\t"
+
+"usbFunctionWrite_finished:\n\t"
+         : [addr]         "+z" (currentAddress.l)
+
+         : [spmenbit]    "I" (SPMEN),
+           [rwwsbbit]    "I" (RWWSB),
+           [spmcr]       "I" (_SFR_IO_ADDR(__SPM_REG)),
+           [pagfillval]  "M" ((1<<SPMEN)),
+           [pageraseval] "M" ((1<<PGERS) | (1<<SPMEN)),
+           [pagwriteval] "M" ((1<<PGWRT) | (1<<SPMEN)),
+           [rwwenrval]   "M" ((1<<RWWSRE) | (1<<SPMEN)),
+           [pagemask]    "M" (SPM_PAGESIZE-1),
+#if HAVE_BLB11_SOFTW_LOCKBIT
+           [blsaddrhi]    "M" ((uint8_t)((BOOTLOADER_PAGEADDR>>8)&0xff)),
+//         [blsaddrlo]    "M" ((uint8_t)((BOOTLOADER_PAGEADDR>>0)&0xff)),
+#endif
+           [islast]      "r"  (isLast),
+           [isLastPage]  "r"  (isLastPage),
+           [len]          "d" (len),
+           [dataptr]      "x" (data)
+
+           : "r0", "r1", "r18"
+       );
+    }
+    return isLast;
+}
+#else
 uchar usbFunctionWrite(uchar *data, uchar len)
 {
 uchar   i,isLast;
 uchar usbFunctionWrite(uchar *data, uchar len)
 {
 uchar   i,isLast;
@@ -377,6 +550,7 @@ uchar   i,isLast;
     }
     return isLast;
 }
     }
     return isLast;
 }
+#endif
 
 uchar usbFunctionRead(uchar *data, uchar len)
 {
 
 uchar usbFunctionRead(uchar *data, uchar len)
 {
@@ -436,7 +610,7 @@ static void initForUsbConnectivity(void)
     sei();
 }
 
     sei();
 }
 
-int __attribute__((noreturn)) main(void)
+int __attribute__((__noreturn__)) main(void)
 {
     /* initialize  */
     bootLoaderInit();
 {
     /* initialize  */
     bootLoaderInit();
@@ -454,6 +628,26 @@ int __attribute__((noreturn)) main(void)
         do{
             usbPoll();
 #if BOOTLOADER_CAN_EXIT
         do{
             usbPoll();
 #if BOOTLOADER_CAN_EXIT
+#if USE_EXCESSIVE_ASSEMBLER
+asm  volatile  (
+  "cpi         %[sil],         0x10\n\t"
+  "brlo                main_stayinloader_smaller\n\t"
+  "sbic                %[pin],         %[bit]\n\t"
+  "subi                %[sil],         0x10\n\t"
+  "rjmp                main_stayinloader_finished\n\t"
+  
+  "main_stayinloader_smaller:\n\t"
+  "cpi         %[sil],         0x2\n\t"
+  "brlo                main_stayinloader_finished\n\t"
+  "sbis                %[pin],         %[bit]\n\t"
+  "subi                %[sil],         0x2\n\t"
+
+  "main_stayinloader_finished:\n\t"
+  : [sil]        "+d" (stayinloader)
+  : [pin]         "I" (_SFR_IO_ADDR(PIN_PIN(JUMPER_PORT))),
+    [bit]         "I" (PIN(JUMPER_PORT, JUMPER_BIT))
+);
+#else
        if (stayinloader >= 0x10) {
          if (!bootLoaderCondition()) {
            stayinloader-=0x10;
        if (stayinloader >= 0x10) {
          if (!bootLoaderCondition()) {
            stayinloader-=0x10;
@@ -464,6 +658,7 @@ int __attribute__((noreturn)) main(void)
          }
        }
 #endif
          }
        }
 #endif
+#endif
 
 #if BOOTLOADER_CAN_EXIT
         }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/
 
 #if BOOTLOADER_CAN_EXIT
         }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/