update.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 string_t work_dir, UpdateManifest* manifest) {
  45. FIL file;
  46. FILINFO stat;
  47. string_t loader_img_path;
  48. string_init_set(loader_img_path, work_dir);
  49. path_append(loader_img_path, string_get_cstr(manifest->staged_loader_file));
  50. if((f_stat(string_get_cstr(loader_img_path), &stat) != FR_OK) ||
  51. (f_open(&file, string_get_cstr(loader_img_path), FA_OPEN_EXISTING | FA_READ) != FR_OK)) {
  52. string_clear(loader_img_path);
  53. return false;
  54. }
  55. string_clear(loader_img_path);
  56. void* img = malloc(stat.fsize);
  57. uint32_t bytes_read = 0;
  58. const uint16_t MAX_READ = 0xFFFF;
  59. uint32_t crc = 0;
  60. do {
  61. uint16_t size_read = 0;
  62. if(f_read(&file, img + bytes_read, MAX_READ, &size_read) != FR_OK) {
  63. break;
  64. }
  65. crc = crc32_calc_buffer(crc, img + bytes_read, size_read);
  66. bytes_read += size_read;
  67. } while(bytes_read == MAX_READ);
  68. do {
  69. if((bytes_read != stat.fsize) || (crc != manifest->staged_loader_crc)) {
  70. break;
  71. }
  72. /* Point of no return. Literally
  73. *
  74. * NB: we MUST disable IRQ, otherwise handlers from flash
  75. * will change global variables (like tick count)
  76. * that are located in .data. And we move staged loader
  77. * to the same memory region. So, IRQ handlers will mess up
  78. * memmove'd .text section and ruin your day.
  79. * We don't want that to happen.
  80. */
  81. __disable_irq();
  82. memmove((void*)(SRAM1_BASE), img, stat.fsize);
  83. LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SRAM);
  84. furi_hal_switch((void*)SRAM1_BASE);
  85. return true;
  86. } while(false);
  87. free(img);
  88. return false;
  89. }
  90. static bool flipper_update_get_manifest_path(string_t out_path) {
  91. FIL file;
  92. FILINFO stat;
  93. uint16_t size_read = 0;
  94. char manifest_name_buf[UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN] = {0};
  95. string_reset(out_path);
  96. CHECK_FRESULT(f_stat(UPDATE_POINTER_FILE_PATH, &stat));
  97. CHECK_FRESULT(f_open(&file, UPDATE_POINTER_FILE_PATH, FA_OPEN_EXISTING | FA_READ));
  98. do {
  99. if(f_read(&file, manifest_name_buf, UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN, &size_read) !=
  100. FR_OK) {
  101. break;
  102. }
  103. if((size_read == 0) || (size_read == UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN)) {
  104. break;
  105. }
  106. string_set_str(out_path, manifest_name_buf);
  107. string_right(out_path, strlen(STORAGE_EXT_PATH_PREFIX));
  108. } while(0);
  109. f_close(&file);
  110. return !string_empty_p(out_path);
  111. }
  112. static UpdateManifest* flipper_update_process_manifest(const string_t manifest_path) {
  113. FIL file;
  114. FILINFO stat;
  115. CHECK_FRESULT(f_stat(string_get_cstr(manifest_path), &stat));
  116. CHECK_FRESULT(f_open(&file, string_get_cstr(manifest_path), FA_OPEN_EXISTING | FA_READ));
  117. uint8_t* manifest_data = malloc(stat.fsize);
  118. uint32_t bytes_read = 0;
  119. const uint16_t MAX_READ = 0xFFFF;
  120. do {
  121. uint16_t size_read = 0;
  122. if(f_read(&file, manifest_data + bytes_read, MAX_READ, &size_read) != FR_OK) {
  123. break;
  124. }
  125. bytes_read += size_read;
  126. } while(bytes_read == MAX_READ);
  127. UpdateManifest* manifest = NULL;
  128. do {
  129. if(bytes_read != stat.fsize) {
  130. break;
  131. }
  132. manifest = update_manifest_alloc();
  133. if(!update_manifest_init_mem(manifest, manifest_data, bytes_read)) {
  134. update_manifest_free(manifest);
  135. manifest = NULL;
  136. }
  137. } while(false);
  138. f_close(&file);
  139. free(manifest_data);
  140. return manifest;
  141. }
  142. void flipper_boot_update_exec() {
  143. if(!flipper_update_init()) {
  144. return;
  145. }
  146. string_t work_dir, manifest_path;
  147. string_init(work_dir);
  148. string_init(manifest_path);
  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(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. string_clear(manifest_path);
  163. string_clear(work_dir);
  164. free(pfs);
  165. }