BlueSCSI_bootloader.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Simple bootloader that loads new firmware from SD card.
  2. #include <BlueSCSI_platform.h>
  3. #include "BlueSCSI_config.h"
  4. #include "BlueSCSI_log.h"
  5. #include <SdFat.h>
  6. #include <string.h>
  7. #ifdef PLATFORM_BOOTLOADER_SIZE
  8. extern SdFs SD;
  9. extern FsFile g_logfile;
  10. bool find_firmware_image(FsFile &file, char name[MAX_FILE_PATH + 1])
  11. {
  12. FsFile root;
  13. root.open("/");
  14. while (file.openNext(&root, O_READ))
  15. {
  16. if (file.isDir()) continue;
  17. int namelen = file.getName(name, MAX_FILE_PATH);
  18. if (namelen >= 11 &&
  19. strncasecmp(name, "bluescsi", 8) == 0 &&
  20. strncasecmp(name + namelen - 3, "bin", 3) == 0)
  21. {
  22. root.close();
  23. log("Found firmware file: ", name);
  24. return true;
  25. }
  26. file.close();
  27. }
  28. root.close();
  29. return false;
  30. }
  31. bool program_firmware(FsFile &file)
  32. {
  33. uint32_t filesize = file.size();
  34. uint32_t fwsize = filesize - PLATFORM_BOOTLOADER_SIZE;
  35. uint32_t num_pages = (fwsize + PLATFORM_FLASH_PAGE_SIZE - 1) / PLATFORM_FLASH_PAGE_SIZE;
  36. // Make sure the buffer is aligned to word boundary
  37. static uint32_t buffer32[PLATFORM_FLASH_PAGE_SIZE / 4];
  38. uint8_t *buffer = (uint8_t*)buffer32;
  39. if (filesize > PLATFORM_FLASH_TOTAL_SIZE)
  40. {
  41. log("Firmware too large: ", (int)filesize, " flash size ", (int)PLATFORM_FLASH_TOTAL_SIZE);
  42. return false;
  43. }
  44. if (!file.seek(PLATFORM_BOOTLOADER_SIZE))
  45. {
  46. log("Seek failed");
  47. return false;
  48. }
  49. for (int i = 0; i < num_pages; i++)
  50. {
  51. if (i % 2)
  52. LED_ON();
  53. else
  54. LED_OFF();
  55. if (file.read(buffer, PLATFORM_FLASH_PAGE_SIZE) <= 0)
  56. {
  57. log("Firmware file read failed on page ", i);
  58. return false;
  59. }
  60. if (!platform_rewrite_flash_page(PLATFORM_BOOTLOADER_SIZE + i * PLATFORM_FLASH_PAGE_SIZE, buffer))
  61. {
  62. log("Flash programming failed on page ", i);
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. static bool mountSDCard()
  69. {
  70. // Check for the common case, FAT filesystem as first partition
  71. if (SD.begin(SD_CONFIG))
  72. return true;
  73. // Do we have any kind of card?
  74. if (!SD.card() || SD.sdErrorCode() != 0)
  75. return false;
  76. // Try to mount the whole card as FAT (without partition table)
  77. if (static_cast<FsVolume*>(&SD)->begin(SD.card(), true, 0))
  78. return true;
  79. // Bootloader cannot do anything without FAT
  80. return false;
  81. }
  82. extern "C"
  83. int bootloader_main(void)
  84. {
  85. platform_init();
  86. g_log_debug = true;
  87. log("Bootloader version: " __DATE__ " " __TIME__ " " PLATFORM_NAME);
  88. if (mountSDCard() || mountSDCard())
  89. {
  90. FsFile fwfile;
  91. char name[MAX_FILE_PATH + 1];
  92. if (find_firmware_image(fwfile, name))
  93. {
  94. if (program_firmware(fwfile))
  95. {
  96. log("Firmware update successful!");
  97. fwfile.close();
  98. if (!SD.remove(name))
  99. {
  100. log("Failed to remove firmware file");
  101. }
  102. }
  103. else
  104. {
  105. log("Firmware update failed!");
  106. platform_emergency_log_save();
  107. }
  108. }
  109. }
  110. else
  111. {
  112. log("Bootloader SD card init failed");
  113. }
  114. log("Bootloader continuing to main firmware");
  115. platform_boot_to_main_firmware();
  116. return 0;
  117. }
  118. #endif