program_flash.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <hardware/structs/nvic.h>
  31. #include <hardware/structs/scb.h>
  32. #include <hardware/sync.h>
  33. #ifndef PIO_FRAMEWORK_ARDUINO_NO_USB
  34. #include <SerialUSB.h>
  35. #include <class/cdc/cdc_device.h>
  36. #endif
  37. /*****************************************/
  38. /* Flash reprogramming from bootloader */
  39. /*****************************************/
  40. #ifdef PLATFORM_BOOTLOADER_SIZE
  41. extern uint32_t __real_vectors_start;
  42. extern uint32_t __StackTop;
  43. static volatile void *g_bootloader_exit_req;
  44. __attribute__((section(".time_critical.platform_rewrite_flash_page")))
  45. bool platform_rewrite_flash_page(uint32_t offset, uint8_t buffer[PLATFORM_FLASH_PAGE_SIZE])
  46. {
  47. if (offset == PLATFORM_BOOTLOADER_SIZE)
  48. {
  49. if (buffer[3] != 0x20 || buffer[7] != 0x10)
  50. {
  51. logmsg("Invalid firmware file, starts with: ", bytearray(buffer, 16));
  52. return false;
  53. }
  54. }
  55. //if (NVIC_GetEnableIRQ(USBCTRL_IRQ_IRQn))
  56. if (nvic_hw->iser[0] & 1 << 14)
  57. {
  58. logmsg("Disabling USB during firmware flashing");
  59. //NVIC_DisableIRQ(USBCTRL_IRQ_IRQn);
  60. nvic_hw->icer[0] = 1 << 14;
  61. usb_hw->main_ctrl = 0;
  62. }
  63. dbgmsg("Writing flash at offset ", offset, " data ", bytearray(buffer, 4));
  64. assert(offset % PLATFORM_FLASH_PAGE_SIZE == 0);
  65. assert(offset >= PLATFORM_BOOTLOADER_SIZE);
  66. // Avoid any mbed timer interrupts triggering during the flashing.
  67. uint32_t saved_irq = save_and_disable_interrupts();
  68. // For some reason any code executed after flashing crashes
  69. // unless we disable the XIP cache.
  70. // Not sure why this happens, as flash_range_program() is flushing
  71. // the cache correctly.
  72. // The cache is now enabled from bootloader start until it starts
  73. // flashing, and again after reset to main firmware.
  74. xip_ctrl_hw->ctrl = 0;
  75. flash_range_erase(offset, PLATFORM_FLASH_PAGE_SIZE);
  76. flash_range_program(offset, buffer, PLATFORM_FLASH_PAGE_SIZE);
  77. uint32_t *buf32 = (uint32_t*)buffer;
  78. uint32_t num_words = PLATFORM_FLASH_PAGE_SIZE / 4;
  79. for (int i = 0; i < num_words; i++)
  80. {
  81. uint32_t expected = buf32[i];
  82. uint32_t actual = *(volatile uint32_t*)(XIP_NOCACHE_NOALLOC_BASE + offset + i * 4);
  83. if (actual != expected)
  84. {
  85. logmsg("Flash verify failed at offset ", offset + i * 4, " got ", actual, " expected ", expected);
  86. restore_interrupts(saved_irq);
  87. return false;
  88. }
  89. }
  90. restore_interrupts(saved_irq);
  91. return true;
  92. }
  93. void platform_boot_to_main_firmware()
  94. {
  95. // To ensure that the system state is reset properly, we perform
  96. // a SYSRESETREQ and jump straight from the reset vector to main application.
  97. g_bootloader_exit_req = &g_bootloader_exit_req;
  98. scb_hw->aircr = 0x05FA0004;
  99. while(1);
  100. }
  101. void btldr_reset_handler()
  102. {
  103. uint32_t* application_base = &__real_vectors_start;
  104. if (g_bootloader_exit_req == &g_bootloader_exit_req)
  105. {
  106. // Boot to main application
  107. application_base = (uint32_t*)(XIP_BASE + PLATFORM_BOOTLOADER_SIZE);
  108. }
  109. scb_hw->vtor = (uint32_t)application_base;
  110. __asm__(
  111. "msr msp, %0\n\t"
  112. "bx %1" : : "r" (application_base[0]),
  113. "r" (application_base[1]) : "memory");
  114. }
  115. // Replace the reset handler when building the bootloader
  116. // The rp2040_btldr.ld places real vector table at an offset.
  117. __attribute__((section(".btldr_vectors")))
  118. const void * btldr_vectors[2] = {&__StackTop, (void*)&btldr_reset_handler};
  119. #endif // PLATFORM_BOOTLOADER_SIZE