Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d4bca5

Browse files
MLXXXpfacchinm
authored andcommitted
Always read key to check for new LUFA bootloader
Instead of checking for the NEW_LUFA_SIGNATURE once in program memory and then setting a flag which is used for further checks, a function is used that always checks program memory directly. If a flag is used, there's a slight chance that its location in RAM could fall on MAGIC_KEY_POS. In this case, an aborted USB auto-reset sequence may fail.
1 parent 3122316 commit 7d4bca5

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

cores/arduino/CDC.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ static volatile int32_t breakValue = -1;
3636

3737
static u8 wdtcsr_save;
3838

39-
bool _updatedLUFAbootloader = false;
40-
4139
#define WEAK __attribute__ ((weak))
4240

4341
extern const CDCDescriptor _cdcInterface PROGMEM;
@@ -59,6 +57,11 @@ const CDCDescriptor _cdcInterface =
5957
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0)
6058
};
6159

60+
bool isLUFAbootloader()
61+
{
62+
return pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE;
63+
}
64+
6265
int CDC_GetInterface(u8* interfaceNum)
6366
{
6467
interfaceNum[0] += 2; // uses 2
@@ -108,7 +111,7 @@ bool CDC_Setup(USBSetup& setup)
108111
#if MAGIC_KEY_POS != (RAMEND-1)
109112
// For future boards save the key in the inproblematic RAMEND
110113
// Which is reserved for the main() return value (which will never return)
111-
if (_updatedLUFAbootloader) {
114+
if (isLUFAbootloader()) {
112115
// horray, we got a new bootloader!
113116
magic_key_pos = (RAMEND-1);
114117
}

cores/arduino/USBCore.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ extern const u16 STRING_LANGUAGE[] PROGMEM;
3535
extern const u8 STRING_PRODUCT[] PROGMEM;
3636
extern const u8 STRING_MANUFACTURER[] PROGMEM;
3737
extern const DeviceDescriptor USB_DeviceDescriptorIAD PROGMEM;
38-
extern bool _updatedLUFAbootloader;
3938

4039
const u16 STRING_LANGUAGE[2] = {
4140
(3<<8) | (2+2),
@@ -819,12 +818,6 @@ void USBDevice_::attach()
819818
UDIEN = (1<<EORSTE) | (1<<SOFE) | (1<<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND
820819

821820
TX_RX_LED_INIT;
822-
823-
#if MAGIC_KEY_POS != (RAMEND-1)
824-
if (pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE) {
825-
_updatedLUFAbootloader = true;
826-
}
827-
#endif
828821
}
829822

830823
void USBDevice_::detach()

cores/arduino/USBCore.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ typedef struct
285285
// Old Caterina bootloader places the MAGIC key into unsafe RAM locations (it can be rewritten
286286
// by the running sketch before to actual reboot).
287287
// Newer bootloaders, recognizable by the LUFA "signature" at the end of the flash, can handle both
288-
// the usafe and the safe location. Check once (in USBCore.cpp) if the bootloader in new, then set the global
289-
// _updatedLUFAbootloader variable to true/false and place the magic key consequently
288+
// the usafe and the safe location.
290289
#ifndef MAGIC_KEY
291290
#define MAGIC_KEY 0x7777
292291
#endif

0 commit comments

Comments
 (0)