bootloader.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (C) 2016 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "bootloader.h"
  18. #ifdef STM32F2xx
  19. #include "stm32f2xx_hal.h"
  20. #endif
  21. #ifdef STM32F4xx
  22. #include "stm32f4xx_hal.h"
  23. #endif
  24. #define MAGIC 0xDEADBEEF
  25. // "System Memory" address of the bootloader. Specific to stm32f2xxxx
  26. #define SYSMEM_RESET_VECTOR 0x1fff0000
  27. extern void OrigSystemInit(void);
  28. // This symbol is in a section of ram that isn't initialised by the
  29. // Reset_Handler that calls SystemInit.
  30. static uint32_t resetMagic __attribute__ ((section("bootloaderMagic")));
  31. // Override STM32CubeMX supplied SystemInit method.
  32. void SystemInit(void)
  33. {
  34. if (resetMagic == MAGIC)
  35. {
  36. void (*bootloader)(void) = (void (*)(void)) (*(uint32_t*)(SYSMEM_RESET_VECTOR + 4));
  37. resetMagic = 0;
  38. __set_MSP(* ((uint32_t*)SYSMEM_RESET_VECTOR));
  39. bootloader();
  40. while (1) {}
  41. }
  42. else
  43. {
  44. OrigSystemInit();
  45. }
  46. }
  47. void s2s_enterBootloader()
  48. {
  49. resetMagic = MAGIC;
  50. NVIC_SystemReset();
  51. }