program_flash.cpp 4.5 KB

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