BlueSCSI_bootloader.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 fwsize = file.size() - PLATFORM_BOOTLOADER_SIZE;
  34. uint32_t num_pages = (fwsize + PLATFORM_FLASH_PAGE_SIZE - 1) / PLATFORM_FLASH_PAGE_SIZE;
  35. // Make sure the buffer is aligned to word boundary
  36. static uint32_t buffer32[PLATFORM_FLASH_PAGE_SIZE / 4];
  37. uint8_t *buffer = (uint8_t*)buffer32;
  38. if (fwsize > PLATFORM_FLASH_TOTAL_SIZE)
  39. {
  40. log("Firmware too large: ", (int)fwsize, " flash size ", (int)PLATFORM_FLASH_TOTAL_SIZE);
  41. return false;
  42. }
  43. if (!file.seek(PLATFORM_BOOTLOADER_SIZE))
  44. {
  45. log("Seek failed");
  46. return false;
  47. }
  48. for (int i = 0; i < num_pages; i++)
  49. {
  50. if (i % 2)
  51. LED_ON();
  52. else
  53. LED_OFF();
  54. if (file.read(buffer, PLATFORM_FLASH_PAGE_SIZE) <= 0)
  55. {
  56. log("Firmware file read failed on page ", i);
  57. return false;
  58. }
  59. if (!platform_rewrite_flash_page(PLATFORM_BOOTLOADER_SIZE + i * PLATFORM_FLASH_PAGE_SIZE, buffer))
  60. {
  61. log("Flash programming failed on page ", i);
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. static bool mountSDCard()
  68. {
  69. // Check for the common case, FAT filesystem as first partition
  70. if (SD.begin(SD_CONFIG))
  71. return true;
  72. // Do we have any kind of card?
  73. if (!SD.card() || SD.sdErrorCode() != 0)
  74. return false;
  75. // Try to mount the whole card as FAT (without partition table)
  76. if (static_cast<FsVolume*>(&SD)->begin(SD.card(), true, 0))
  77. return true;
  78. // Bootloader cannot do anything without FAT
  79. return false;
  80. }
  81. extern "C"
  82. int bootloader_main(void)
  83. {
  84. platform_init();
  85. g_log_debug = true;
  86. log("Bootloader version: " __DATE__ " " __TIME__ " " PLATFORM_NAME);
  87. if (mountSDCard() || mountSDCard())
  88. {
  89. FsFile fwfile;
  90. char name[MAX_FILE_PATH + 1];
  91. if (find_firmware_image(fwfile, name))
  92. {
  93. if (program_firmware(fwfile))
  94. {
  95. log("Firmware update successful!");
  96. fwfile.close();
  97. if (!SD.remove(name))
  98. {
  99. log("Failed to remove firmware file");
  100. }
  101. }
  102. else
  103. {
  104. log("Firmware update failed!");
  105. platform_emergency_log_save();
  106. }
  107. }
  108. }
  109. else
  110. {
  111. log("Bootloader SD card init failed");
  112. }
  113. log("Bootloader continuing to main firmware");
  114. platform_boot_to_main_firmware();
  115. return 0;
  116. }
  117. #endif