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.
This commit is contained in:
Scott Allen 2017-11-13 17:41:23 +01:00 committed by Martino Facchin
parent 31223160eb
commit 7d4bca5041
3 changed files with 7 additions and 12 deletions

View File

@ -36,8 +36,6 @@ static volatile int32_t breakValue = -1;
static u8 wdtcsr_save;
bool _updatedLUFAbootloader = false;
#define WEAK __attribute__ ((weak))
extern const CDCDescriptor _cdcInterface PROGMEM;
@ -59,6 +57,11 @@ const CDCDescriptor _cdcInterface =
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0)
};
bool isLUFAbootloader()
{
return pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE;
}
int CDC_GetInterface(u8* interfaceNum)
{
interfaceNum[0] += 2; // uses 2
@ -108,7 +111,7 @@ bool CDC_Setup(USBSetup& setup)
#if MAGIC_KEY_POS != (RAMEND-1)
// For future boards save the key in the inproblematic RAMEND
// Which is reserved for the main() return value (which will never return)
if (_updatedLUFAbootloader) {
if (isLUFAbootloader()) {
// horray, we got a new bootloader!
magic_key_pos = (RAMEND-1);
}

View File

@ -35,7 +35,6 @@ extern const u16 STRING_LANGUAGE[] PROGMEM;
extern const u8 STRING_PRODUCT[] PROGMEM;
extern const u8 STRING_MANUFACTURER[] PROGMEM;
extern const DeviceDescriptor USB_DeviceDescriptorIAD PROGMEM;
extern bool _updatedLUFAbootloader;
const u16 STRING_LANGUAGE[2] = {
(3<<8) | (2+2),
@ -819,12 +818,6 @@ void USBDevice_::attach()
UDIEN = (1<<EORSTE) | (1<<SOFE) | (1<<SUSPE); // Enable interrupts for EOR (End of Reset), SOF (start of frame) and SUSPEND
TX_RX_LED_INIT;
#if MAGIC_KEY_POS != (RAMEND-1)
if (pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE) {
_updatedLUFAbootloader = true;
}
#endif
}
void USBDevice_::detach()

View File

@ -285,8 +285,7 @@ typedef struct
// Old Caterina bootloader places the MAGIC key into unsafe RAM locations (it can be rewritten
// by the running sketch before to actual reboot).
// Newer bootloaders, recognizable by the LUFA "signature" at the end of the flash, can handle both
// the usafe and the safe location. Check once (in USBCore.cpp) if the bootloader in new, then set the global
// _updatedLUFAbootloader variable to true/false and place the magic key consequently
// the usafe and the safe location.
#ifndef MAGIC_KEY
#define MAGIC_KEY 0x7777
#endif