bootloader.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "bootloader.h"
  2. #include <ch.h>
  3. #include <hal.h>
  4. #include "wait.h"
  5. /* This code should be checked whether it runs correctly on platforms */
  6. #define SYMVAL(sym) (uint32_t)(((uint8_t *)&(sym)) - ((uint8_t *)0))
  7. #define BOOTLOADER_MAGIC 0xDEADBEEF
  8. #define MAGIC_ADDR (unsigned long *)(SYMVAL(__ram0_end__) - 4)
  9. #ifndef STM32_BOOTLOADER_DUAL_BANK
  10. # define STM32_BOOTLOADER_DUAL_BANK FALSE
  11. #endif
  12. #ifdef BOOTLOADER_TINYUF2
  13. # define DBL_TAP_MAGIC 0xf01669ef // From tinyuf2's board_api.h
  14. // defined by linker script
  15. extern uint32_t _board_dfu_dbl_tap[];
  16. # define DBL_TAP_REG _board_dfu_dbl_tap[0]
  17. void bootloader_jump(void) {
  18. DBL_TAP_REG = DBL_TAP_MAGIC;
  19. NVIC_SystemReset();
  20. }
  21. void enter_bootloader_mode_if_requested(void) { /* not needed, no two-stage reset */
  22. }
  23. #elif STM32_BOOTLOADER_DUAL_BANK
  24. // Need pin definitions
  25. # include "config_common.h"
  26. # ifndef STM32_BOOTLOADER_DUAL_BANK_GPIO
  27. # error "No STM32_BOOTLOADER_DUAL_BANK_GPIO defined, don't know which pin to toggle"
  28. # endif
  29. # ifndef STM32_BOOTLOADER_DUAL_BANK_POLARITY
  30. # define STM32_BOOTLOADER_DUAL_BANK_POLARITY 0
  31. # endif
  32. # ifndef STM32_BOOTLOADER_DUAL_BANK_DELAY
  33. # define STM32_BOOTLOADER_DUAL_BANK_DELAY 100000
  34. # endif
  35. extern uint32_t __ram0_end__;
  36. __attribute__((weak)) void bootloader_jump(void) {
  37. // For STM32 MCUs with dual-bank flash, and we're incapable of jumping to the bootloader. The first valid flash
  38. // bank is executed unconditionally after a reset, so it doesn't enter DFU unless BOOT0 is high. Instead, we do
  39. // it with hardware...in this case, we pull a GPIO high/low depending on the configuration, connects 3.3V to
  40. // BOOT0's RC charging circuit, lets it charge the capacitor, and issue a system reset. See the QMK discord
  41. // #hardware channel pins for an example circuit.
  42. palSetPadMode(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_MODE_OUTPUT_PUSHPULL);
  43. # if STM32_BOOTLOADER_DUAL_BANK_POLARITY
  44. palSetPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
  45. # else
  46. palClearPad(PAL_PORT(STM32_BOOTLOADER_DUAL_BANK_GPIO), PAL_PAD(STM32_BOOTLOADER_DUAL_BANK_GPIO));
  47. # endif
  48. // Wait for a while for the capacitor to charge
  49. wait_ms(100);
  50. // Issue a system reset to get the ROM bootloader to execute, with BOOT0 high
  51. NVIC_SystemReset();
  52. }
  53. void enter_bootloader_mode_if_requested(void) {} // not needed at all, but if anybody attempts to invoke it....
  54. #elif defined(STM32_BOOTLOADER_ADDRESS) // STM32_BOOTLOADER_DUAL_BANK
  55. extern uint32_t __ram0_end__;
  56. __attribute__((weak)) void bootloader_jump(void) {
  57. *MAGIC_ADDR = BOOTLOADER_MAGIC; // set magic flag => reset handler will jump into boot loader
  58. NVIC_SystemReset();
  59. }
  60. void enter_bootloader_mode_if_requested(void) {
  61. unsigned long *check = MAGIC_ADDR;
  62. if (*check == BOOTLOADER_MAGIC) {
  63. *check = 0;
  64. __set_CONTROL(0);
  65. __set_MSP(*(__IO uint32_t *)STM32_BOOTLOADER_ADDRESS);
  66. __enable_irq();
  67. typedef void (*BootJump_t)(void);
  68. BootJump_t boot_jump = *(BootJump_t *)(STM32_BOOTLOADER_ADDRESS + 4);
  69. boot_jump();
  70. while (1)
  71. ;
  72. }
  73. }
  74. #elif defined(GD32VF103)
  75. # define DBGMCU_KEY_UNLOCK 0x4B5A6978
  76. # define DBGMCU_CMD_RESET 0x1
  77. __IO uint32_t *DBGMCU_KEY = (uint32_t *)DBGMCU_BASE + 0x0CU;
  78. __IO uint32_t *DBGMCU_CMD = (uint32_t *)DBGMCU_BASE + 0x08U;
  79. __attribute__((weak)) void bootloader_jump(void) {
  80. /* The MTIMER unit of the GD32VF103 doesn't have the MSFRST
  81. * register to generate a software reset request.
  82. * BUT instead two undocumented registers in the debug peripheral
  83. * that allow issueing a software reset. WHO would need the MSFRST
  84. * register anyway? Source:
  85. * https://github.com/esmil/gd32vf103inator/blob/master/include/gd32vf103/dbg.h */
  86. *DBGMCU_KEY = DBGMCU_KEY_UNLOCK;
  87. *DBGMCU_CMD = DBGMCU_CMD_RESET;
  88. }
  89. void enter_bootloader_mode_if_requested(void) { /* Jumping to bootloader is not possible from user code. */
  90. }
  91. #elif defined(KL2x) || defined(K20x) || defined(MK66F18) || defined(MIMXRT1062) // STM32_BOOTLOADER_DUAL_BANK // STM32_BOOTLOADER_ADDRESS
  92. /* Kinetis */
  93. # if defined(BOOTLOADER_KIIBOHD)
  94. /* Kiibohd Bootloader (MCHCK and Infinity KB) */
  95. # define SCB_AIRCR_VECTKEY_WRITEMAGIC 0x05FA0000
  96. const uint8_t sys_reset_to_loader_magic[] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  97. __attribute__((weak)) void bootloader_jump(void) {
  98. void *volatile vbat = (void *)VBAT;
  99. __builtin_memcpy(vbat, (const void *)sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic));
  100. // request reset
  101. SCB->AIRCR = SCB_AIRCR_VECTKEY_WRITEMAGIC | SCB_AIRCR_SYSRESETREQ_Msk;
  102. }
  103. # else /* defined(BOOTLOADER_KIIBOHD) */
  104. /* Default for Kinetis - expecting an ARM Teensy */
  105. # include "wait.h"
  106. __attribute__((weak)) void bootloader_jump(void) {
  107. wait_ms(100);
  108. __BKPT(0);
  109. }
  110. # endif /* defined(BOOTLOADER_KIIBOHD) */
  111. #else /* neither STM32 nor KINETIS */
  112. __attribute__((weak)) void bootloader_jump(void) {}
  113. #endif