123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891 |
- #define INCLUDE_FROM_BOOTLOADER_C
- #include "BootloaderDFU.h"
- static bool IsSecure = SECURE_MODE;
- static bool RunBootloader = true;
- static bool WaitForExit = false;
- static uint8_t DFU_State = dfuIDLE;
- static uint8_t DFU_Status = OK;
- static DFU_Command_t SentCommand;
- static uint8_t ResponseByte;
- static AppPtr_t AppStartPtr = (AppPtr_t)0x0000;
- static uint8_t Flash64KBPage = 0;
- static uint16_t StartAddr = 0x0000;
- static uint16_t EndAddr = 0x0000;
- uint16_t MagicBootKey ATTR_NO_INIT;
- void Application_Jump_Check(void)
- {
- bool JumpToApplication = false;
- #if (BOARD == BOARD_LEONARDO)
-
- PORTC |= (1 << 7);
- Delay_MS(10);
-
- JumpToApplication = ((PINC & (1 << 7)) != 0);
-
- PORTC &= ~(1 << 7);
- #elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
-
- JTAG_DISABLE();
-
- PORTF |= (1 << 4);
- Delay_MS(10);
-
- JumpToApplication = ((PINF & (1 << 4)) != 0);
-
- JTAG_ENABLE();
- #else
-
- if (boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS) & FUSE_BOOTRST)
- {
-
-
-
-
- MCUSR &= ~(1 << EXTRF);
- }
- else
- {
-
-
-
-
- MCUSR &= ~(1 << WDRF);
- }
- #endif
-
- bool ApplicationValid = (pgm_read_word_near(0) != 0xFFFF);
-
- if (JumpToApplication && ApplicationValid)
- {
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- MagicBootKey = 0;
-
- ((void (*)(void))0x0000)();
- }
- }
- int main(void)
- {
-
- SetupHardware();
-
- LEDs_SetAllLEDs(LEDS_LED1 | LEDS_LED2);
-
- GlobalInterruptEnable();
- #if (BOARD == BOARD_QMK)
- uint16_t keypress = 0;
- #endif
-
- while (RunBootloader || WaitForExit) {
- USB_USBTask();
- #if (BOARD == BOARD_QMK)
- bool pressed = (PIN(QMK_ESC_ROW) & NUM(QMK_ESC_ROW));
- if ((DFU_State == dfuIDLE) && (keypress > 5000) && pressed) {
- break;
- }
- if (pressed) {
- keypress++;
- } else {
- keypress = 0;
- }
- #endif
- }
-
- ResetHardware();
-
- AppStartPtr();
- }
- static void SetupHardware(void)
- {
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- clock_prescale_set(clock_div_1);
-
- MCUCR = (1 << IVCE);
- MCUCR = (1 << IVSEL);
- #if (BOARD == BOARD_QMK)
-
- DDR(QMK_ESC_COL) |= NUM(QMK_ESC_COL);
- PORT(QMK_ESC_COL) |= NUM(QMK_ESC_COL);
-
- DDR(QMK_ESC_ROW) |= NUM(QMK_ESC_ROW);
- #endif
-
- USB_Init();
- LEDs_Init();
-
- TIMSK1 = (1 << TOIE1);
- TCCR1B = ((1 << CS11) | (1 << CS10));
- }
- static void ResetHardware(void)
- {
-
- USB_Disable();
- LEDs_Disable();
-
- TIMSK1 = 0;
- TCCR1B = 0;
-
- MCUCR = (1 << IVCE);
- MCUCR = 0;
- #if (BOARD == BOARD_QMK)
- DDR(QMK_ESC_COL) = PORT(QMK_ESC_COL) = DDR(QMK_ESC_ROW) = PORT(QMK_ESC_ROW) = 0;
- #endif
- }
- ISR(TIMER1_OVF_vect, ISR_BLOCK)
- {
- LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2);
- }
- void EVENT_USB_Device_ControlRequest(void)
- {
-
- if ((USB_ControlRequest.bmRequestType & (CONTROL_REQTYPE_TYPE | CONTROL_REQTYPE_RECIPIENT)) !=
- (REQTYPE_CLASS | REQREC_INTERFACE))
- {
- return;
- }
-
- LEDs_ToggleLEDs(LEDS_LED1 | LEDS_LED2);
-
- SentCommand.DataSize = USB_ControlRequest.wLength;
- switch (USB_ControlRequest.bRequest)
- {
- case DFU_REQ_DNLOAD:
- Endpoint_ClearSETUP();
-
- if (WaitForExit)
- {
-
- ProcessBootloaderCommand();
-
- WaitForExit = false;
- }
-
- if (SentCommand.DataSize)
- {
- while (!(Endpoint_IsOUTReceived()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
-
- SentCommand.Command = Endpoint_Read_8();
-
- SentCommand.DataSize--;
-
- for (uint8_t DataByte = 0; (DataByte < sizeof(SentCommand.Data)) &&
- Endpoint_BytesInEndpoint(); DataByte++)
- {
- SentCommand.Data[DataByte] = Endpoint_Read_8();
- SentCommand.DataSize--;
- }
-
- ProcessBootloaderCommand();
- }
-
- if (DFU_State == dfuDNLOAD_IDLE)
- {
- if (!(SentCommand.DataSize))
- {
- DFU_State = dfuIDLE;
- }
- else
- {
-
- DiscardFillerBytes(DFU_FILLER_BYTES_SIZE);
-
- DiscardFillerBytes(StartAddr % FIXED_CONTROL_ENDPOINT_SIZE);
-
- uint16_t BytesRemaining = ((EndAddr - StartAddr) + 1);
- if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00))
- {
-
- uint16_t WordsRemaining = (BytesRemaining >> 1);
- union
- {
- uint16_t Words[2];
- uint32_t Long;
- } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};
- uint32_t CurrFlashPageStartAddress = CurrFlashAddress.Long;
- uint8_t WordsInFlashPage = 0;
- while (WordsRemaining--)
- {
-
- if (!(Endpoint_BytesInEndpoint()))
- {
- Endpoint_ClearOUT();
- while (!(Endpoint_IsOUTReceived()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- }
-
- boot_page_fill(CurrFlashAddress.Long, Endpoint_Read_16_LE());
-
- WordsInFlashPage += 1;
- CurrFlashAddress.Long += 2;
-
- if ((WordsInFlashPage == (SPM_PAGESIZE >> 1)) || !(WordsRemaining))
- {
-
- boot_page_write(CurrFlashPageStartAddress);
- boot_spm_busy_wait();
-
- if (WordsRemaining)
- {
- CurrFlashPageStartAddress = CurrFlashAddress.Long;
- WordsInFlashPage = 0;
-
- boot_page_erase(CurrFlashAddress.Long);
- boot_spm_busy_wait();
- }
- }
- }
-
- StartAddr = EndAddr;
-
- boot_rww_enable();
- }
- else
- {
- while (BytesRemaining--)
- {
-
- if (!(Endpoint_BytesInEndpoint()))
- {
- Endpoint_ClearOUT();
- while (!(Endpoint_IsOUTReceived()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- }
-
- eeprom_update_byte((uint8_t*)StartAddr, Endpoint_Read_8());
-
- StartAddr++;
- }
- }
-
- DiscardFillerBytes(DFU_FILE_SUFFIX_SIZE);
- }
- }
- Endpoint_ClearOUT();
- Endpoint_ClearStatusStage();
- break;
- case DFU_REQ_UPLOAD:
- Endpoint_ClearSETUP();
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- if (DFU_State != dfuUPLOAD_IDLE)
- {
- if ((DFU_State == dfuERROR) && IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))
- {
-
- Endpoint_Write_16_LE(StartAddr);
- }
- else
- {
-
- Endpoint_Write_8(ResponseByte);
- }
- }
- else
- {
-
- uint16_t BytesRemaining = ((EndAddr - StartAddr) + 1);
- if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00))
- {
-
- uint16_t WordsRemaining = (BytesRemaining >> 1);
- union
- {
- uint16_t Words[2];
- uint32_t Long;
- } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};
- while (WordsRemaining--)
- {
-
- if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
- {
- Endpoint_ClearIN();
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- }
-
- #if (FLASHEND > 0xFFFF)
- Endpoint_Write_16_LE(pgm_read_word_far(CurrFlashAddress.Long));
- #else
- Endpoint_Write_16_LE(pgm_read_word(CurrFlashAddress.Long));
- #endif
-
- CurrFlashAddress.Long += 2;
- }
-
- StartAddr = EndAddr;
- }
- else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x02))
- {
- while (BytesRemaining--)
- {
-
- if (Endpoint_BytesInEndpoint() == FIXED_CONTROL_ENDPOINT_SIZE)
- {
- Endpoint_ClearIN();
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- }
-
- Endpoint_Write_8(eeprom_read_byte((uint8_t*)StartAddr));
-
- StartAddr++;
- }
- }
-
- DFU_State = dfuIDLE;
- }
- Endpoint_ClearIN();
- Endpoint_ClearStatusStage();
- break;
- case DFU_REQ_GETSTATUS:
- Endpoint_ClearSETUP();
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
-
- Endpoint_Write_8(DFU_Status);
-
- Endpoint_Write_8(0);
- Endpoint_Write_16_LE(0);
-
- Endpoint_Write_8(DFU_State);
-
- Endpoint_Write_8(0);
- Endpoint_ClearIN();
- Endpoint_ClearStatusStage();
- break;
- case DFU_REQ_CLRSTATUS:
- Endpoint_ClearSETUP();
-
- DFU_Status = OK;
- Endpoint_ClearStatusStage();
- break;
- case DFU_REQ_GETSTATE:
- Endpoint_ClearSETUP();
- while (!(Endpoint_IsINReady()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
-
- Endpoint_Write_8(DFU_State);
- Endpoint_ClearIN();
- Endpoint_ClearStatusStage();
- break;
- case DFU_REQ_ABORT:
- Endpoint_ClearSETUP();
-
- DFU_State = dfuIDLE;
- Endpoint_ClearStatusStage();
- break;
- }
- }
- static void DiscardFillerBytes(uint8_t NumberOfBytes)
- {
- while (NumberOfBytes--)
- {
- if (!(Endpoint_BytesInEndpoint()))
- {
- Endpoint_ClearOUT();
-
- while (!(Endpoint_IsOUTReceived()))
- {
- if (USB_DeviceState == DEVICE_STATE_Unattached)
- return;
- }
- }
- else
- {
- Endpoint_Discard_8();
- }
- }
- }
- static void ProcessBootloaderCommand(void)
- {
-
- if (IsSecure)
- {
-
- if (!(((SentCommand.Command == COMMAND_WRITE) &&
- IS_TWOBYTE_COMMAND(SentCommand.Data, 0x00, 0xFF)) ||
- (SentCommand.Command == COMMAND_READ)))
- {
-
- DFU_State = dfuERROR;
- DFU_Status = errWRITE;
-
- Endpoint_StallTransaction();
-
- return;
- }
- }
-
- switch (SentCommand.Command)
- {
- case COMMAND_PROG_START:
- ProcessMemProgCommand();
- break;
- case COMMAND_DISP_DATA:
- ProcessMemReadCommand();
- break;
- case COMMAND_WRITE:
- ProcessWriteCommand();
- break;
- case COMMAND_READ:
- ProcessReadCommand();
- break;
- case COMMAND_CHANGE_BASE_ADDR:
- if (IS_TWOBYTE_COMMAND(SentCommand.Data, 0x03, 0x00))
- Flash64KBPage = SentCommand.Data[2];
- break;
- }
- }
- static void LoadStartEndAddresses(void)
- {
- union
- {
- uint8_t Bytes[2];
- uint16_t Word;
- } Address[2] = {{.Bytes = {SentCommand.Data[2], SentCommand.Data[1]}},
- {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}}};
-
- StartAddr = Address[0].Word;
- EndAddr = Address[1].Word;
- }
- static void ProcessMemProgCommand(void)
- {
- if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00) ||
- IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))
- {
-
- LoadStartEndAddresses();
-
- if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00))
- {
- union
- {
- uint16_t Words[2];
- uint32_t Long;
- } CurrFlashAddress = {.Words = {StartAddr, Flash64KBPage}};
-
- boot_page_erase(CurrFlashAddress.Long);
- boot_spm_busy_wait();
- }
-
- DFU_State = dfuDNLOAD_IDLE;
- }
- }
- static void ProcessMemReadCommand(void)
- {
- if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00) ||
- IS_ONEBYTE_COMMAND(SentCommand.Data, 0x02))
- {
-
- LoadStartEndAddresses();
-
- DFU_State = dfuUPLOAD_IDLE;
- }
- else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))
- {
- uint32_t CurrFlashAddress = 0;
- while (CurrFlashAddress < (uint32_t)BOOT_START_ADDR)
- {
-
- #if (FLASHEND > 0xFFFF)
- if (pgm_read_byte_far(CurrFlashAddress) != 0xFF)
- #else
- if (pgm_read_byte(CurrFlashAddress) != 0xFF)
- #endif
- {
-
- Flash64KBPage = (CurrFlashAddress >> 16);
- StartAddr = CurrFlashAddress;
-
- DFU_State = dfuERROR;
- DFU_Status = errCHECK_ERASED;
- break;
- }
- CurrFlashAddress++;
- }
- }
- }
- static void ProcessWriteCommand(void)
- {
- if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x03))
- {
-
- WaitForExit = true;
-
- if (SentCommand.DataSize)
- {
- if (SentCommand.Data[1] == 0x01)
- {
- union
- {
- uint8_t Bytes[2];
- AppPtr_t FuncPtr;
- } Address = {.Bytes = {SentCommand.Data[4], SentCommand.Data[3]}};
-
- AppStartPtr = Address.FuncPtr;
- }
- }
- else
- {
- if (SentCommand.Data[1] == 0x00)
- {
-
- MagicBootKey = MAGIC_BOOT_KEY;
-
- wdt_enable(WDTO_250MS);
- }
- else
- {
-
- if (pgm_read_word_near(0) == 0xFFFF)
- RunBootloader = false;
- }
- }
- }
- else if (IS_TWOBYTE_COMMAND(SentCommand.Data, 0x00, 0xFF))
- {
- uint32_t CurrFlashAddress = 0;
-
- while (CurrFlashAddress < (uint32_t)BOOT_START_ADDR)
- {
- boot_page_erase(CurrFlashAddress);
- boot_spm_busy_wait();
- boot_page_write(CurrFlashAddress);
- boot_spm_busy_wait();
- CurrFlashAddress += SPM_PAGESIZE;
- }
-
- boot_rww_enable();
-
- IsSecure = false;
- }
- }
- static void ProcessReadCommand(void)
- {
- const uint8_t BootloaderInfo[3] = {BOOTLOADER_VERSION, BOOTLOADER_ID_BYTE1, BOOTLOADER_ID_BYTE2};
- const uint8_t SignatureInfo[4] = {0x58, AVR_SIGNATURE_1, AVR_SIGNATURE_2, AVR_SIGNATURE_3};
- uint8_t DataIndexToRead = SentCommand.Data[1];
- bool ReadAddressInvalid = false;
- if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x00))
- {
- if (DataIndexToRead < 3)
- ResponseByte = BootloaderInfo[DataIndexToRead];
- else
- ReadAddressInvalid = true;
- }
- else if (IS_ONEBYTE_COMMAND(SentCommand.Data, 0x01))
- {
- switch (DataIndexToRead)
- {
- case 0x30:
- ResponseByte = SignatureInfo[0];
- break;
- case 0x31:
- ResponseByte = SignatureInfo[1];
- break;
- case 0x60:
- ResponseByte = SignatureInfo[2];
- break;
- case 0x61:
- ResponseByte = SignatureInfo[3];
- break;
- default:
- ReadAddressInvalid = true;
- break;
- }
- }
- if (ReadAddressInvalid)
- {
-
- DFU_State = dfuERROR;
- DFU_Status = errADDRESS;
- }
- }
|