update_operation.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "update_operation.h"
  2. #include "update_manifest.h"
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <loader/loader.h>
  6. #include <lib/toolbox/path.h>
  7. #include <lib/toolbox/crc32_calc.h>
  8. #define UPDATE_ROOT_DIR EXT_PATH("update")
  9. /* Need at least 4 free LFS pages before update */
  10. #define UPDATE_MIN_INT_FREE_SPACE (2 * 4 * 1024)
  11. static const char* update_prepare_result_descr[] = {
  12. [UpdatePrepareResultOK] = "OK",
  13. [UpdatePrepareResultManifestPathInvalid] = "Invalid manifest name or location",
  14. [UpdatePrepareResultManifestFolderNotFound] = "Update folder not found",
  15. [UpdatePrepareResultManifestInvalid] = "Invalid manifest data",
  16. [UpdatePrepareResultStageMissing] = "Missing Stage2 loader",
  17. [UpdatePrepareResultStageIntegrityError] = "Corrupted Stage2 loader",
  18. [UpdatePrepareResultManifestPointerError] = "Failed to create update pointer file",
  19. [UpdatePrepareResultOutdatedManifestVersion] = "Update package is too old",
  20. [UpdatePrepareResultIntFull] = "Need more free space in internal storage",
  21. };
  22. const char* update_operation_describe_preparation_result(const UpdatePrepareResult value) {
  23. if(value >= COUNT_OF(update_prepare_result_descr)) {
  24. return "...";
  25. } else {
  26. return update_prepare_result_descr[value];
  27. }
  28. }
  29. static bool update_operation_get_current_package_path_rtc(Storage* storage, FuriString* out_path) {
  30. const uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
  31. furi_string_set(out_path, UPDATE_ROOT_DIR);
  32. if(update_index == UPDATE_OPERATION_ROOT_DIR_PACKAGE_MAGIC) {
  33. return true;
  34. }
  35. bool found = false;
  36. uint32_t iter_index = 0;
  37. File* dir = storage_file_alloc(storage);
  38. FileInfo fi = {0};
  39. char* name_buffer = malloc(UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN);
  40. do {
  41. if(!storage_dir_open(dir, UPDATE_ROOT_DIR)) {
  42. break;
  43. }
  44. while(storage_dir_read(dir, &fi, name_buffer, UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN)) {
  45. if(++iter_index == update_index) {
  46. found = true;
  47. path_append(out_path, name_buffer);
  48. break;
  49. }
  50. }
  51. } while(false);
  52. free(name_buffer);
  53. storage_file_free(dir);
  54. if(!found) {
  55. furi_string_reset(out_path);
  56. }
  57. return found;
  58. }
  59. #define UPDATE_FILE_POINTER_FN EXT_PATH(UPDATE_MANIFEST_POINTER_FILE_NAME)
  60. #define UPDATE_MANIFEST_MAX_PATH_LEN 256u
  61. bool update_operation_get_current_package_manifest_path(Storage* storage, FuriString* out_path) {
  62. furi_string_reset(out_path);
  63. if(storage_common_stat(storage, UPDATE_FILE_POINTER_FN, NULL) == FSE_OK) {
  64. char* manifest_name_buffer = malloc(UPDATE_MANIFEST_MAX_PATH_LEN);
  65. File* upd_file = NULL;
  66. do {
  67. upd_file = storage_file_alloc(storage);
  68. if(!storage_file_open(
  69. upd_file, UPDATE_FILE_POINTER_FN, FSAM_READ, FSOM_OPEN_EXISTING)) {
  70. break;
  71. }
  72. uint16_t bytes_read =
  73. storage_file_read(upd_file, manifest_name_buffer, UPDATE_MANIFEST_MAX_PATH_LEN);
  74. if((bytes_read == 0) || (bytes_read == UPDATE_MANIFEST_MAX_PATH_LEN)) {
  75. break;
  76. }
  77. if(storage_common_stat(storage, manifest_name_buffer, NULL) != FSE_OK) {
  78. break;
  79. }
  80. furi_string_set(out_path, manifest_name_buffer);
  81. } while(0);
  82. free(manifest_name_buffer);
  83. storage_file_free(upd_file);
  84. } else {
  85. /* legacy, will be deprecated */
  86. FuriString* rtcpath;
  87. rtcpath = furi_string_alloc();
  88. do {
  89. if(!update_operation_get_current_package_path_rtc(storage, rtcpath)) {
  90. break;
  91. }
  92. path_concat(furi_string_get_cstr(rtcpath), UPDATE_MANIFEST_DEFAULT_NAME, out_path);
  93. } while(0);
  94. furi_string_free(rtcpath);
  95. }
  96. return !furi_string_empty(out_path);
  97. }
  98. static bool update_operation_persist_manifest_path(Storage* storage, const char* manifest_path) {
  99. const size_t manifest_path_len = strlen(manifest_path);
  100. furi_check(manifest_path && manifest_path_len);
  101. bool success = false;
  102. File* file = storage_file_alloc(storage);
  103. do {
  104. if(manifest_path_len >= UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN) {
  105. break;
  106. }
  107. if(!storage_file_open(file, UPDATE_FILE_POINTER_FN, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  108. break;
  109. }
  110. if(storage_file_write(file, manifest_path, manifest_path_len) != manifest_path_len) {
  111. break;
  112. }
  113. success = true;
  114. } while(0);
  115. storage_file_free(file);
  116. return success;
  117. }
  118. UpdatePrepareResult update_operation_prepare(const char* manifest_file_path) {
  119. UpdatePrepareResult result = UpdatePrepareResultIntFull;
  120. Storage* storage = furi_record_open(RECORD_STORAGE);
  121. UpdateManifest* manifest = update_manifest_alloc();
  122. File* file = storage_file_alloc(storage);
  123. uint64_t free_int_space;
  124. FuriString* stage_path;
  125. stage_path = furi_string_alloc();
  126. do {
  127. if((storage_common_fs_info(storage, STORAGE_INT_PATH_PREFIX, NULL, &free_int_space) !=
  128. FSE_OK) ||
  129. (free_int_space < UPDATE_MIN_INT_FREE_SPACE)) {
  130. break;
  131. }
  132. if(storage_common_stat(storage, manifest_file_path, NULL) != FSE_OK) {
  133. result = UpdatePrepareResultManifestFolderNotFound;
  134. break;
  135. }
  136. if(!update_manifest_init(manifest, manifest_file_path)) {
  137. result = UpdatePrepareResultManifestInvalid;
  138. break;
  139. }
  140. if(manifest->manifest_version < UPDATE_OPERATION_MIN_MANIFEST_VERSION) {
  141. result = UpdatePrepareResultOutdatedManifestVersion;
  142. break;
  143. }
  144. if(furi_hal_version_get_hw_target() != manifest->target) {
  145. result = UpdatePrepareResultTargetMismatch;
  146. break;
  147. }
  148. path_extract_dirname(manifest_file_path, stage_path);
  149. path_append(stage_path, furi_string_get_cstr(manifest->staged_loader_file));
  150. if(!storage_file_open(
  151. file, furi_string_get_cstr(stage_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  152. result = UpdatePrepareResultStageMissing;
  153. break;
  154. }
  155. uint32_t crc = crc32_calc_file(file, NULL, NULL);
  156. if(crc != manifest->staged_loader_crc) {
  157. result = UpdatePrepareResultStageIntegrityError;
  158. break;
  159. }
  160. if(!update_operation_persist_manifest_path(storage, manifest_file_path)) {
  161. result = UpdatePrepareResultManifestPointerError;
  162. break;
  163. }
  164. result = UpdatePrepareResultOK;
  165. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePreUpdate);
  166. } while(false);
  167. furi_string_free(stage_path);
  168. storage_file_free(file);
  169. update_manifest_free(manifest);
  170. furi_record_close(RECORD_STORAGE);
  171. return result;
  172. }
  173. bool update_operation_is_armed() {
  174. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  175. const uint32_t rtc_upd_index =
  176. furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
  177. Storage* storage = furi_record_open(RECORD_STORAGE);
  178. const bool upd_fn_ptr_exists =
  179. (storage_common_stat(storage, UPDATE_FILE_POINTER_FN, NULL) == FSE_OK);
  180. furi_record_close(RECORD_STORAGE);
  181. return (boot_mode >= FuriHalRtcBootModePreUpdate) &&
  182. (boot_mode <= FuriHalRtcBootModePostUpdate) &&
  183. ((rtc_upd_index != INT_MAX) || upd_fn_ptr_exists);
  184. }
  185. void update_operation_disarm() {
  186. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  187. furi_hal_rtc_set_register(FuriHalRtcRegisterUpdateFolderFSIndex, INT_MAX);
  188. Storage* storage = furi_record_open(RECORD_STORAGE);
  189. storage_simply_remove(storage, UPDATE_FILE_POINTER_FN);
  190. furi_record_close(RECORD_STORAGE);
  191. }