update.c 5.0 KB

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