summaryrefslogtreecommitdiff
path: root/platforms/avr/bootloaders
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2021-12-27 21:10:07 +1100
committerGitHub <noreply@github.com>2021-12-27 21:10:07 +1100
commit6bc870d899c474bce82457699ec4b753d1538123 (patch)
tree600b8dda41f4b90f98199d4afd3037374a679a74 /platforms/avr/bootloaders
parentcffe143ca20d938c910b59410dcd3d96dd7d433b (diff)
Refactor `bootloader_jump()` implementations (#15450)
* Refactor `bootloader_jump()` implementations * Fix tests? * Rename `atmel-samba` to `md-boot`
Diffstat (limited to 'platforms/avr/bootloaders')
-rw-r--r--platforms/avr/bootloaders/bootloadhid.c34
-rw-r--r--platforms/avr/bootloaders/caterina.c39
-rw-r--r--platforms/avr/bootloaders/dfu.c53
-rw-r--r--platforms/avr/bootloaders/halfkay.c129
-rw-r--r--platforms/avr/bootloaders/none.c20
-rw-r--r--platforms/avr/bootloaders/usbasploader.c57
6 files changed, 332 insertions, 0 deletions
diff --git a/platforms/avr/bootloaders/bootloadhid.c b/platforms/avr/bootloaders/bootloadhid.c
new file mode 100644
index 0000000000..8d18114613
--- /dev/null
+++ b/platforms/avr/bootloaders/bootloadhid.c
@@ -0,0 +1,34 @@
+/* Copyright 2021 QMK
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "bootloader.h"
+
+#include <avr/eeprom.h>
+#include <avr/wdt.h>
+
+__attribute__((weak))
+void bootloader_jump(void) {
+ // force bootloadHID to stay in bootloader mode, so that it waits
+ // for a new firmware to be flashed
+ // NOTE: this byte is part of QMK's "magic number" - changing it causes the EEPROM to be re-initialized
+ // thus every time the device is flashed the EEPROM will be wiped
+ eeprom_write_byte((uint8_t *)1, 0x00);
+
+ // watchdog reset
+ wdt_enable(WDTO_250MS);
+ for (;;)
+ ;
+}
diff --git a/platforms/avr/bootloaders/caterina.c b/platforms/avr/bootloaders/caterina.c
new file mode 100644
index 0000000000..1b43ed4e44
--- /dev/null
+++ b/platforms/avr/bootloaders/caterina.c
@@ -0,0 +1,39 @@
+/* Copyright 2021 QMK
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "bootloader.h"
+
+#include <avr/wdt.h>
+
+__attribute__((weak))
+void bootloader_jump(void) {
+ // this block may be optional
+ // TODO: figure it out
+
+ uint16_t *const bootKeyPtr = (uint16_t *)0x0800;
+
+ // Value used by Caterina bootloader use to determine whether to run the
+ // sketch or the bootloader programmer.
+ uint16_t bootKey = 0x7777;
+
+ *bootKeyPtr = bootKey;
+
+ // setup watchdog timeout
+ wdt_enable(WDTO_60MS);
+
+ // wait for watchdog timer to trigger
+ while (1) { }
+}
diff --git a/platforms/avr/bootloaders/dfu.c b/platforms/avr/bootloaders/dfu.c
new file mode 100644
index 0000000000..bdf5908934
--- /dev/null
+++ b/platforms/avr/bootloaders/dfu.c
@@ -0,0 +1,53 @@
+/* Copyright 2021 QMK
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "bootloader.h"
+
+#include <avr/wdt.h>
+#include <util/delay.h>
+
+#define FLASH_SIZE (FLASHEND + 1L)
+
+/** \brief Entering the Bootloader via Software
+ *
+ * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html
+ */
+#define BOOTLOADER_RESET_KEY 0xB007B007
+uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;")));
+
+__attribute__((weak))
+void bootloader_jump(void) {
+ UDCON = 1;
+ USBCON = (1 << FRZCLK); // disable USB
+ UCSR1B = 0;
+ _delay_ms(5); // 5 seems to work fine
+
+ // watchdog reset
+ reset_key = BOOTLOADER_RESET_KEY;
+ wdt_enable(WDTO_250MS);
+ for (;;)
+ ;
+}
+
+/* this runs before main() */
+void bootloader_jump_after_watchdog_reset(void) __attribute__((used, naked, section(".init3")));
+void bootloader_jump_after_watchdog_reset(void) {
+ if ((MCUSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) {
+ reset_key = 0;
+
+ ((void (*)(void))((FLASH_SIZE - BOOTLOADER_SIZE) >> 1))();
+ }
+}
diff --git a/platforms/avr/bootloaders/halfkay.c b/platforms/avr/bootloaders/halfkay.c
new file mode 100644
index 0000000000..abef86980b
--- /dev/null
+++ b/platforms/avr/bootloaders/halfkay.c
@@ -0,0 +1,129 @@
+/* Copyright 2021 QMK
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "bootloader.h"
+
+#include <avr/interrupt.h>
+#include <util/delay.h>
+
+__attribute__((weak))
+void bootloader_jump(void) {
+ // http://www.pjrc.com/teensy/jump_to_bootloader.html
+
+ cli();
+ // disable watchdog, if enabled (it's not)
+ // disable all peripherals
+ // a shutdown call might make sense here
+ UDCON = 1;
+ USBCON = (1 << FRZCLK); // disable USB
+ UCSR1B = 0;
+ _delay_ms(5);
+
+#if defined(__AVR_AT90USB162__) // Teensy 1.0
+ EIMSK = 0;
+ PCICR = 0;
+ SPCR = 0;
+ ACSR = 0;
+ EECR = 0;
+ TIMSK0 = 0;
+ TIMSK1 = 0;
+ UCSR1B = 0;
+ DDRB = 0;
+ DDRC = 0;
+ DDRD = 0;
+ PORTB = 0;
+ PORTC = 0;
+ PORTD = 0;
+ asm volatile("jmp 0x3E00");
+#elif defined(__AVR_ATmega32U4__) // Teensy 2.0
+ EIMSK = 0;
+ PCICR = 0;
+ SPCR = 0;
+ ACSR = 0;
+ EECR = 0;
+ ADCSRA = 0;
+ TIMSK0 = 0;
+ TIMSK1 = 0;
+ TIMSK3 = 0;
+ TIMSK4 = 0;
+ UCSR1B = 0;
+ TWCR = 0;
+ DDRB = 0;
+ DDRC = 0;
+ DDRD = 0;
+ DDRE = 0;
+ DDRF = 0;
+ TWCR = 0;
+ PORTB = 0;
+ PORTC = 0;
+ PORTD = 0;
+ PORTE = 0;
+ PORTF = 0;
+ asm volatile("jmp 0x7E00");
+#elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
+ EIMSK = 0;
+ PCICR = 0;
+ SPCR = 0;
+ ACSR = 0;
+ EECR = 0;
+ ADCSRA = 0;
+ TIMSK0 = 0;
+ TIMSK1 = 0;
+ TIMSK2 = 0;
+ TIMSK3 = 0;
+ UCSR1B = 0;
+ TWCR = 0;
+ DDRA = 0;
+ DDRB = 0;
+ DDRC = 0;
+ DDRD = 0;
+ DDRE = 0;
+ DDRF = 0;
+ PORTA = 0;
+ PORTB = 0;
+ PORTC = 0;
+ PORTD = 0;
+ PORTE = 0;
+ PORTF = 0;
+ asm volatile("jmp 0xFC00");
+#elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
+ EIMSK = 0;
+ PCICR = 0;
+ SPCR = 0;
+ ACSR = 0;
+ EECR = 0;
+ ADCSRA = 0;
+ TIMSK0 = 0;
+ TIMSK1 = 0;
+ TIMSK2 = 0;
+ TIMSK3 = 0;
+ UCSR1B = 0;
+ TWCR = 0;
+ DDRA = 0;
+ DDRB = 0;
+ DDRC = 0;
+ DDRD = 0;
+ DDRE = 0;
+ DDRF = 0;
+ PORTA = 0;
+ PORTB = 0;
+ PORTC = 0;
+ PORTD = 0;
+ PORTE = 0;
+ PORTF = 0;
+ asm volatile("jmp 0x1FC00");
+#endif
+}
diff --git a/platforms/avr/bootloaders/none.c b/platforms/avr/bootloaders/none.c
new file mode 100644
index 0000000000..8af151c04d
--- /dev/null
+++ b/platforms/avr/bootloaders/none.c
@@ -0,0 +1,20 @@
+/* Copyright 2021 QMK
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "bootloader.h"
+
+__attribute__((weak))
+void bootloader_jump(void) { }
diff --git a/platforms/avr/bootloaders/usbasploader.c b/platforms/avr/bootloaders/usbasploader.c
new file mode 100644
index 0000000000..b3e9b2d72d
--- /dev/null
+++ b/platforms/avr/bootloaders/usbasploader.c
@@ -0,0 +1,57 @@
+/* Copyright 2021 QMK
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "bootloader.h"
+
+#include <avr/wdt.h>
+
+#define FLASH_SIZE (FLASHEND + 1L)
+
+#if !defined(MCUCSR)
+# if defined(MCUSR)
+# define MCUCSR MCUSR
+# endif
+#endif
+
+__attribute__((weak))
+void bootloader_jump(void) {
+ // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c
+
+ wdt_enable(WDTO_15MS);
+ wdt_reset();
+ asm volatile("cli \n\t"
+ "ldi r29 , %[ramendhi] \n\t"
+ "ldi r28 , %[ramendlo] \n\t"
+#if (FLASHEND > 131071)
+ "ldi r18 , %[bootaddrhi] \n\t"
+ "st Y+, r18 \n\t"
+#endif
+ "ldi r18 , %[bootaddrme] \n\t"
+ "st Y+, r18 \n\t"
+ "ldi r18 , %[bootaddrlo] \n\t"
+ "st Y+, r18 \n\t"
+ "out %[mcucsrio], __zero_reg__ \n\t"
+ "bootloader_startup_loop%=: \n\t"
+ "rjmp bootloader_startup_loop%= \n\t"
+ :
+ : [mcucsrio] "I"(_SFR_IO_ADDR(MCUCSR)),
+#if (FLASHEND > 131071)
+ [ramendhi] "M"(((RAMEND - 2) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 2) >> 0) & 0xff), [bootaddrhi] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff),
+#else
+ [ramendhi] "M"(((RAMEND - 1) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 1) >> 0) & 0xff),
+#endif
+ [bootaddrme] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [bootaddrlo] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff));
+}