update.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <flipper.h>
  4. #include <alt_boot.h>
  5. #include <fatfs.h>
  6. #include <flipper_format/flipper_format.h>
  7. #include <update_util/update_manifest.h>
  8. #include <lib/toolbox/path.h>
  9. static FATFS* pfs = NULL;
  10. static const char FS_ROOT_PATH[] = "/";
  11. #define CHECK_FRESULT(result) \
  12. { \
  13. if((result) != FR_OK) { \
  14. return false; \
  15. } \
  16. }
  17. static bool flipper_update_init() {
  18. furi_hal_clock_init();
  19. furi_hal_rtc_init();
  20. furi_hal_interrupt_init();
  21. furi_hal_delay_init();
  22. furi_hal_spi_init();
  23. furi_hal_crc_init(false);
  24. MX_FATFS_Init();
  25. if(!hal_sd_detect()) {
  26. return false;
  27. }
  28. if(BSP_SD_Init(true)) {
  29. return false;
  30. }
  31. pfs = malloc(sizeof(FATFS));
  32. CHECK_FRESULT(f_mount(pfs, FS_ROOT_PATH, 1));
  33. return true;
  34. }
  35. static bool flipper_update_load_stage(const string_t work_dir, UpdateManifest* manifest) {
  36. FIL file;
  37. FILINFO stat;
  38. string_t loader_img_path;
  39. string_init_set(loader_img_path, work_dir);
  40. path_append(loader_img_path, string_get_cstr(manifest->staged_loader_file));
  41. if((f_stat(string_get_cstr(loader_img_path), &stat) != FR_OK) ||
  42. (f_open(&file, string_get_cstr(loader_img_path), FA_OPEN_EXISTING | FA_READ) != FR_OK)) {
  43. string_clear(loader_img_path);
  44. return false;
  45. }
  46. string_clear(loader_img_path);
  47. void* img = malloc(stat.fsize);
  48. uint32_t bytes_read = 0;
  49. const uint16_t MAX_READ = 0xFFFF;
  50. furi_hal_crc_reset();
  51. uint32_t crc = 0;
  52. do {
  53. uint16_t size_read = 0;
  54. if(f_read(&file, img + bytes_read, MAX_READ, &size_read) != FR_OK) {
  55. break;
  56. }
  57. crc = furi_hal_crc_feed(img + bytes_read, size_read);
  58. bytes_read += size_read;
  59. } while(bytes_read == MAX_READ);
  60. furi_hal_crc_reset();
  61. do {
  62. if((bytes_read != stat.fsize) || (crc != manifest->staged_loader_crc)) {
  63. break;
  64. }
  65. /* Point of no return. Literally
  66. *
  67. * NB: we MUST disable IRQ, otherwise handlers from flash
  68. * will change global variables (like tick count)
  69. * that are located in .data. And we move staged loader
  70. * to the same memory region. So, IRQ handlers will mess up
  71. * memmove'd .text section and ruin your day.
  72. * We don't want that to happen.
  73. */
  74. __disable_irq();
  75. memmove((void*)(SRAM1_BASE), img, stat.fsize);
  76. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SRAM);
  77. furi_hal_switch((void*)SRAM1_BASE);
  78. return true;
  79. } while(false);
  80. free(img);
  81. return false;
  82. }
  83. static bool flipper_update_get_work_directory(string_t out_dir) {
  84. const uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
  85. if(update_index == 0) {
  86. string_set(out_dir, UPDATE_DIR_DEFAULT_REL_PATH);
  87. return true;
  88. }
  89. DIR dir;
  90. UINT entry_idx = 0;
  91. FILINFO fno;
  92. CHECK_FRESULT(f_opendir(&dir, UPDATE_DIR_DEFAULT_REL_PATH));
  93. string_set(out_dir, UPDATE_DIR_DEFAULT_REL_PATH);
  94. while(f_readdir(&dir, &fno) == FR_OK) {
  95. entry_idx++;
  96. if(fno.fname[0] == '\0') {
  97. return false;
  98. }
  99. if(entry_idx == update_index) {
  100. path_append(out_dir, fno.fname);
  101. return true;
  102. }
  103. }
  104. string_reset(out_dir);
  105. return false;
  106. }
  107. static UpdateManifest* flipper_update_process_manifest(const string_t work_dir) {
  108. FIL file;
  109. FILINFO stat;
  110. string_t manifest_path;
  111. string_init_set(manifest_path, work_dir);
  112. path_append(manifest_path, UPDATE_MANIFEST_DEFAULT_NAME);
  113. CHECK_FRESULT(f_stat(string_get_cstr(manifest_path), &stat));
  114. CHECK_FRESULT(f_open(&file, string_get_cstr(manifest_path), FA_OPEN_EXISTING | FA_READ));
  115. uint8_t* manifest_data = malloc(stat.fsize);
  116. uint32_t bytes_read = 0;
  117. const uint16_t MAX_READ = 0xFFFF;
  118. do {
  119. uint16_t size_read = 0;
  120. if(f_read(&file, manifest_data + bytes_read, MAX_READ, &size_read) != FR_OK) {
  121. break;
  122. }
  123. bytes_read += size_read;
  124. } while(bytes_read == MAX_READ);
  125. UpdateManifest* manifest = NULL;
  126. do {
  127. if(bytes_read != stat.fsize) {
  128. break;
  129. }
  130. manifest = update_manifest_alloc();
  131. if(!update_manifest_init_mem(manifest, manifest_data, bytes_read)) {
  132. update_manifest_free(manifest);
  133. manifest = NULL;
  134. }
  135. } while(false);
  136. string_clear(manifest_path);
  137. free(manifest_data);
  138. return manifest;
  139. }
  140. void flipper_boot_update_exec() {
  141. if(!flipper_update_init()) {
  142. return;
  143. }
  144. string_t work_dir;
  145. string_init(work_dir);
  146. do {
  147. if(!flipper_update_get_work_directory(work_dir)) {
  148. break;
  149. }
  150. UpdateManifest* manifest = flipper_update_process_manifest(work_dir);
  151. if(!manifest) {
  152. break;
  153. }
  154. if(!flipper_update_load_stage(work_dir, manifest)) {
  155. update_manifest_free(manifest);
  156. }
  157. } while(false);
  158. string_clear(work_dir);
  159. free(pfs);
  160. }