update.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_init() {
  20. furi_hal_clock_init();
  21. furi_hal_rtc_init();
  22. furi_hal_interrupt_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, "/", 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_manifest_path(string_t out_path) {
  82. FIL file;
  83. FILINFO stat;
  84. uint16_t size_read = 0;
  85. char manifest_name_buf[UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN] = {0};
  86. string_reset(out_path);
  87. CHECK_FRESULT(f_stat(UPDATE_POINTER_FILE_PATH, &stat));
  88. CHECK_FRESULT(f_open(&file, UPDATE_POINTER_FILE_PATH, FA_OPEN_EXISTING | FA_READ));
  89. do {
  90. if(f_read(&file, manifest_name_buf, UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN, &size_read) !=
  91. FR_OK) {
  92. break;
  93. }
  94. if((size_read == 0) || (size_read == UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN)) {
  95. break;
  96. }
  97. string_set_str(out_path, manifest_name_buf);
  98. string_right(out_path, strlen(STORAGE_EXT_PATH_PREFIX));
  99. } while(0);
  100. f_close(&file);
  101. return !string_empty_p(out_path);
  102. }
  103. static UpdateManifest* flipper_update_process_manifest(const string_t manifest_path) {
  104. FIL file;
  105. FILINFO stat;
  106. CHECK_FRESULT(f_stat(string_get_cstr(manifest_path), &stat));
  107. CHECK_FRESULT(f_open(&file, string_get_cstr(manifest_path), FA_OPEN_EXISTING | FA_READ));
  108. uint8_t* manifest_data = malloc(stat.fsize);
  109. uint32_t bytes_read = 0;
  110. const uint16_t MAX_READ = 0xFFFF;
  111. do {
  112. uint16_t size_read = 0;
  113. if(f_read(&file, manifest_data + bytes_read, MAX_READ, &size_read) != FR_OK) {
  114. break;
  115. }
  116. bytes_read += size_read;
  117. } while(bytes_read == MAX_READ);
  118. UpdateManifest* manifest = NULL;
  119. do {
  120. if(bytes_read != stat.fsize) {
  121. break;
  122. }
  123. manifest = update_manifest_alloc();
  124. if(!update_manifest_init_mem(manifest, manifest_data, bytes_read)) {
  125. update_manifest_free(manifest);
  126. manifest = NULL;
  127. }
  128. } while(false);
  129. f_close(&file);
  130. free(manifest_data);
  131. return manifest;
  132. }
  133. void flipper_boot_update_exec() {
  134. if(!flipper_update_init()) {
  135. return;
  136. }
  137. string_t work_dir, manifest_path;
  138. string_init(work_dir);
  139. string_init(manifest_path);
  140. do {
  141. if(!flipper_update_get_manifest_path(manifest_path)) {
  142. break;
  143. }
  144. UpdateManifest* manifest = flipper_update_process_manifest(manifest_path);
  145. if(!manifest) {
  146. break;
  147. }
  148. path_extract_dirname(string_get_cstr(manifest_path), work_dir);
  149. if(!flipper_update_load_stage(work_dir, manifest)) {
  150. update_manifest_free(manifest);
  151. }
  152. } while(false);
  153. string_clear(manifest_path);
  154. string_clear(work_dir);
  155. free(pfs);
  156. }