program_flash.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version. 
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. * GNU General Public License for more details. 
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. /**
  22. * This has been removed from ZuluSCSI_platform.cpp so this code can be
  23. * placed in SRAM while ZuluSCSI_platform.cpp.o can be placed in flash
  24. */
  25. #include "ZuluSCSI_platform.h"
  26. #include "ZuluSCSI_log.h"
  27. #include <hardware/flash.h>
  28. #include <hardware/structs/xip_ctrl.h>
  29. #include <hardware/structs/usb.h>
  30. #ifndef PIO_FRAMEWORK_ARDUINO_NO_USB
  31. #include <SerialUSB.h>
  32. #include <class/cdc/cdc_device.h>
  33. #endif
  34. /*****************************************/
  35. /* Flash reprogramming from bootloader */
  36. /*****************************************/
  37. #ifdef PLATFORM_BOOTLOADER_SIZE
  38. extern uint32_t __real_vectors_start;
  39. extern uint32_t __StackTop;
  40. static volatile void *g_bootloader_exit_req;
  41. __attribute__((section(".time_critical.platform_rewrite_flash_page")))
  42. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE])
  43. {
  44. if (offset == PLATFORM_BOOTLOADER_SIZE)
  45. {
  46. if (buffer[3] != 0x20 || buffer[7] != 0x10)
  47. {
  48. logmsg("Invalid firmware file, starts with: ", bytearray(buffer, 16));
  49. return false;
  50. }
  51. }
  52. if (NVIC_GetEnableIRQ(USBCTRL_IRQ_IRQn))
  53. {
  54. logmsg("Disabling USB during firmware flashing");
  55. NVIC_DisableIRQ(USBCTRL_IRQ_IRQn);
  56. usb_hw->main_ctrl = 0;
  57. }
  58. dbgmsg("Writing flash at offset ", offset, " data ", bytearray(buffer, 4));
  59. assert(offset % PLATFORM_FLASH_PAGE_SIZE == 0);
  60. assert(offset >= PLATFORM_BOOTLOADER_SIZE);
  61. // Avoid any mbed timer interrupts triggering during the flashing.
  62. __disable_irq();
  63. // For some reason any code executed after flashing crashes
  64. // unless we disable the XIP cache.
  65. // Not sure why this happens, as flash_range_program() is flushing
  66. // the cache correctly.
  67. // The cache is now enabled from bootloader start until it starts
  68. // flashing, and again after reset to main firmware.
  69. xip_ctrl_hw->ctrl = 0;
  70. flash_range_erase(offset, PLATFORM_FLASH_PAGE_SIZE);
  71. flash_range_program(offset, buffer, PLATFORM_FLASH_PAGE_SIZE);
  72. uint32_t *buf32 = (uint32_t*)buffer;
  73. uint32_t num_words = PLATFORM_FLASH_PAGE_SIZE / 4;
  74. for (int i = 0; i < num_words; i++)
  75. {
  76. uint32_t expected = buf32[i];
  77. uint32_t actual = *(volatile uint32_t*)(XIP_NOCACHE_BASE + offset + i * 4);
  78. if (actual != expected)
  79. {
  80. logmsg("Flash verify failed at offset ", offset + i * 4, " got ", actual, " expected ", expected);
  81. __enable_irq();
  82. return false;
  83. }
  84. }
  85. __enable_irq();
  86. return true;
  87. }
  88. void platform_boot_to_main_firmware()
  89. {
  90. // To ensure that the system state is reset properly, we perform
  91. // a SYSRESETREQ and jump straight from the reset vector to main application.
  92. g_bootloader_exit_req = &g_bootloader_exit_req;
  93. SCB->AIRCR = 0x05FA0004;
  94. while(1);
  95. }
  96. void btldr_reset_handler()
  97. {
  98. uint32_t* application_base = &__real_vectors_start;
  99. if (g_bootloader_exit_req == &g_bootloader_exit_req)
  100. {
  101. // Boot to main application
  102. application_base = (uint32_t*)(XIP_BASE + PLATFORM_BOOTLOADER_SIZE);
  103. }
  104. SCB->VTOR = (uint32_t)application_base;
  105. __asm__(
  106. "msr msp, %0\n\t"
  107. "bx %1" : : "r" (application_base[0]),
  108. "r" (application_base[1]) : "memory");
  109. }
  110. // Replace the reset handler when building the bootloader
  111. // The rp2040_btldr.ld places real vector table at an offset.
  112. __attribute__((section(".btldr_vectors")))
  113. const void * btldr_vectors[2] = {&__StackTop, (void*)&btldr_reset_handler};
  114. #endif // PLATFORM_BOOTLOADER_SIZE