update.c 5.2 KB

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