update.c 5.2 KB

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