update.c 5.1 KB

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