ZuluSCSI_bootloader.cpp 4.6 KB

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