BlueSCSI_bootloader.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 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. // Simple bootloader that loads new firmware from SD card.
  22. #include <BlueSCSI_platform.h>
  23. #include "BlueSCSI_config.h"
  24. #include "BlueSCSI_log.h"
  25. #include <SdFat.h>
  26. #ifdef PLATFORM_BOOTLOADER_SIZE
  27. extern SdFs SD;
  28. extern FsFile g_logfile;
  29. bool find_firmware_image(FsFile &file, char name[MAX_FILE_PATH + 1])
  30. {
  31. FsFile root;
  32. root.open("/");
  33. while (file.openNext(&root, O_READ))
  34. {
  35. if (file.isDir()) continue;
  36. int namelen = file.getName(name, MAX_FILE_PATH);
  37. const char* board_name = PLATFORM_PID;
  38. if (namelen >= sizeof(FIRMWARE_PREFIX) + 3 &&
  39. strncasecmp(name, FIRMWARE_PREFIX, sizeof(FIRMWARE_PREFIX) - 1) == 0 &&
  40. strstr(name, board_name) != NULL &&
  41. strncasecmp(name + namelen - 3, "bin", 3) == 0)
  42. {
  43. root.close();
  44. logmsg("Found firmware file: ", name);
  45. return true;
  46. }
  47. file.close();
  48. }
  49. root.close();
  50. return false;
  51. }
  52. #ifndef PLATFORM_FLASH_SECTOR_ERASE
  53. bool program_firmware(FsFile &file)
  54. {
  55. uint32_t filesize = file.size();
  56. uint32_t fwsize = filesize - PLATFORM_BOOTLOADER_SIZE;
  57. uint32_t num_pages = (fwsize + PLATFORM_FLASH_PAGE_SIZE - 1) / PLATFORM_FLASH_PAGE_SIZE;
  58. // Make sure the buffer is aligned to word boundary
  59. static uint32_t buffer32[PLATFORM_FLASH_PAGE_SIZE / 4];
  60. uint8_t *buffer = (uint8_t*)buffer32;
  61. if (filesize > PLATFORM_FLASH_TOTAL_SIZE)
  62. {
  63. // logmsg("Firmware too large: ", (int)filesize, " flash size ", (int)PLATFORM_FLASH_TOTAL_SIZE);
  64. return false;
  65. }
  66. if (!file.seek(PLATFORM_BOOTLOADER_SIZE))
  67. {
  68. // logmsg("Seek failed");
  69. return false;
  70. }
  71. for (int i = 0; i < num_pages; i++)
  72. {
  73. if (i % 2)
  74. LED_ON();
  75. else
  76. LED_OFF();
  77. if (file.read(buffer, PLATFORM_FLASH_PAGE_SIZE) <= 0)
  78. {
  79. // logmsg("Firmware file read failed on page ", i);
  80. return false;
  81. }
  82. if (!platform_rewrite_flash_page(PLATFORM_BOOTLOADER_SIZE + i * PLATFORM_FLASH_PAGE_SIZE, buffer))
  83. {
  84. // logmsg("Flash programming failed on page ", i);
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. #else // PLATFORM_FLASH_SECTOR_ERASE
  91. bool program_firmware(FsFile &file)
  92. {
  93. if (!platform_firmware_erase(file))
  94. {
  95. return false;
  96. }
  97. if (!platform_firmware_program(file))
  98. {
  99. return false;
  100. }
  101. return true;
  102. }
  103. #endif // PLATFORM_FLASH_SECTOR_ERASE
  104. static bool mountSDCard()
  105. {
  106. // Check for the common case, FAT filesystem as first partition
  107. if (SD.begin(SD_CONFIG))
  108. return true;
  109. // Do we have any kind of card?
  110. if (!SD.card() || SD.sdErrorCode() != 0)
  111. return false;
  112. // Try to mount the whole card as FAT (without partition table)
  113. if (static_cast<FsVolume*>(&SD)->begin(SD.card(), true, 0))
  114. return true;
  115. // Bootloader cannot do anything without FAT
  116. return false;
  117. }
  118. extern "C"
  119. int bootloader_main(void)
  120. {
  121. platform_init();
  122. g_log_debug = true;
  123. // logmsg("Bootloader version: " __DATE__ " " __TIME__ " " PLATFORM_NAME);
  124. if (mountSDCard() || mountSDCard())
  125. {
  126. FsFile fwfile;
  127. char name[MAX_FILE_PATH + 1];
  128. if (find_firmware_image(fwfile, name))
  129. {
  130. if (program_firmware(fwfile))
  131. {
  132. // logmsg("Firmware update successful!");
  133. fwfile.close();
  134. if (!SD.remove(name))
  135. {
  136. // logmsg("Failed to remove firmware file");
  137. }
  138. }
  139. else
  140. {
  141. // logmsg("Firmware update failed!");
  142. platform_emergency_log_save();
  143. }
  144. }
  145. }
  146. else
  147. {
  148. // logmsg("Bootloader SD card init failed");
  149. }
  150. // logmsg("Bootloader continuing to main firmware");
  151. platform_boot_to_main_firmware();
  152. return 0;
  153. }
  154. #endif