update.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <update_util/update_operation.h>
  9. #include <toolbox/path.h>
  10. #include <toolbox/crc32_calc.h>
  11. #define UPDATE_POINTER_FILE_PATH "/" UPDATE_MANIFEST_POINTER_FILE_NAME
  12. static FATFS* pfs = NULL;
  13. #define CHECK_FRESULT(result) \
  14. { \
  15. if((result) != FR_OK) { \
  16. return false; \
  17. } \
  18. }
  19. static bool flipper_update_mount_sd() {
  20. for(int i = 0; i < BSP_SD_MaxMountRetryCount(); ++i) {
  21. if(BSP_SD_Init((i % 2) == 0) != MSD_OK) {
  22. /* Next attempt will be without card reset, let it settle */
  23. furi_delay_ms(1000);
  24. continue;
  25. }
  26. if(f_mount(pfs, "/", 1) == FR_OK) {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. static bool flipper_update_init() {
  33. furi_hal_clock_init();
  34. furi_hal_rtc_init();
  35. furi_hal_interrupt_init();
  36. furi_hal_spi_init();
  37. MX_FATFS_Init();
  38. if(!hal_sd_detect()) {
  39. return false;
  40. }
  41. pfs = malloc(sizeof(FATFS));
  42. return flipper_update_mount_sd();
  43. }
  44. static bool flipper_update_load_stage(const FuriString* work_dir, UpdateManifest* manifest) {
  45. FIL file;
  46. FILINFO stat;
  47. FuriString* loader_img_path;
  48. loader_img_path = furi_string_alloc_set(work_dir);
  49. path_append(loader_img_path, furi_string_get_cstr(manifest->staged_loader_file));
  50. if((f_stat(furi_string_get_cstr(loader_img_path), &stat) != FR_OK) ||
  51. (f_open(&file, furi_string_get_cstr(loader_img_path), FA_OPEN_EXISTING | FA_READ) !=
  52. FR_OK)) {
  53. furi_string_free(loader_img_path);
  54. return false;
  55. }
  56. furi_string_free(loader_img_path);
  57. void* img = malloc(stat.fsize);
  58. uint32_t bytes_read = 0;
  59. const uint16_t MAX_READ = 0xFFFF;
  60. uint32_t crc = 0;
  61. do {
  62. uint16_t size_read = 0;
  63. if(f_read(&file, img + bytes_read, MAX_READ, &size_read) != FR_OK) {
  64. break;
  65. }
  66. crc = crc32_calc_buffer(crc, img + bytes_read, size_read);
  67. bytes_read += size_read;
  68. } while(bytes_read == MAX_READ);
  69. do {
  70. if((bytes_read != stat.fsize) || (crc != manifest->staged_loader_crc)) {
  71. break;
  72. }
  73. /* Point of no return. Literally
  74. *
  75. * NB: we MUST disable IRQ, otherwise handlers from flash
  76. * will change global variables (like tick count)
  77. * that are located in .data. And we move staged loader
  78. * to the same memory region. So, IRQ handlers will mess up
  79. * memmove'd .text section and ruin your day.
  80. * We don't want that to happen.
  81. */
  82. __disable_irq();
  83. memmove((void*)(SRAM1_BASE), img, stat.fsize);
  84. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SRAM);
  85. furi_hal_switch((void*)SRAM1_BASE);
  86. return true;
  87. } while(false);
  88. free(img);
  89. return false;
  90. }
  91. static bool flipper_update_get_manifest_path(FuriString* out_path) {
  92. FIL file;
  93. FILINFO stat;
  94. uint16_t size_read = 0;
  95. char manifest_name_buf[UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN] = {0};
  96. furi_string_reset(out_path);
  97. CHECK_FRESULT(f_stat(UPDATE_POINTER_FILE_PATH, &stat));
  98. CHECK_FRESULT(f_open(&file, UPDATE_POINTER_FILE_PATH, FA_OPEN_EXISTING | FA_READ));
  99. do {
  100. if(f_read(&file, manifest_name_buf, UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN, &size_read) !=
  101. FR_OK) {
  102. break;
  103. }
  104. if((size_read == 0) || (size_read == UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN)) {
  105. break;
  106. }
  107. furi_string_set(out_path, manifest_name_buf);
  108. furi_string_right(out_path, strlen(STORAGE_EXT_PATH_PREFIX));
  109. } while(0);
  110. f_close(&file);
  111. return !furi_string_empty(out_path);
  112. }
  113. static UpdateManifest* flipper_update_process_manifest(const FuriString* manifest_path) {
  114. FIL file;
  115. FILINFO stat;
  116. CHECK_FRESULT(f_stat(furi_string_get_cstr(manifest_path), &stat));
  117. CHECK_FRESULT(f_open(&file, furi_string_get_cstr(manifest_path), FA_OPEN_EXISTING | FA_READ));
  118. uint8_t* manifest_data = malloc(stat.fsize);
  119. uint32_t bytes_read = 0;
  120. const uint16_t MAX_READ = 0xFFFF;
  121. do {
  122. uint16_t size_read = 0;
  123. if(f_read(&file, manifest_data + bytes_read, MAX_READ, &size_read) != FR_OK) {
  124. break;
  125. }
  126. bytes_read += size_read;
  127. } while(bytes_read == MAX_READ);
  128. UpdateManifest* manifest = NULL;
  129. do {
  130. if(bytes_read != stat.fsize) {
  131. break;
  132. }
  133. manifest = update_manifest_alloc();
  134. if(!update_manifest_init_mem(manifest, manifest_data, bytes_read)) {
  135. update_manifest_free(manifest);
  136. manifest = NULL;
  137. }
  138. } while(false);
  139. f_close(&file);
  140. free(manifest_data);
  141. return manifest;
  142. }
  143. void flipper_boot_update_exec() {
  144. if(!flipper_update_init()) {
  145. return;
  146. }
  147. FuriString* work_dir = furi_string_alloc();
  148. FuriString* manifest_path = furi_string_alloc();
  149. do {
  150. if(!flipper_update_get_manifest_path(manifest_path)) {
  151. break;
  152. }
  153. UpdateManifest* manifest = flipper_update_process_manifest(manifest_path);
  154. if(!manifest) {
  155. break;
  156. }
  157. path_extract_dirname(furi_string_get_cstr(manifest_path), work_dir);
  158. if(!flipper_update_load_stage(work_dir, manifest)) {
  159. update_manifest_free(manifest);
  160. }
  161. } while(false);
  162. furi_string_free(manifest_path);
  163. furi_string_free(work_dir);
  164. free(pfs);
  165. }