update_operation.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "update_operation.h"
  2. #include "update_manifest.h"
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <m-string.h>
  6. #include <loader/loader.h>
  7. #include <lib/toolbox/path.h>
  8. #include <lib/toolbox/crc32_calc.h>
  9. #define UPDATE_ROOT_DIR "/ext" UPDATE_DIR_DEFAULT_REL_PATH
  10. #define UPDATE_PREFIX "/ext" UPDATE_DIR_DEFAULT_REL_PATH "/"
  11. #define UPDATE_SUFFIX "/" UPDATE_MANIFEST_DEFAULT_NAME
  12. static const char* update_prepare_result_descr[] = {
  13. [UpdatePrepareResultOK] = "OK",
  14. [UpdatePrepareResultManifestPathInvalid] = "Invalid manifest name or location",
  15. [UpdatePrepareResultManifestFolderNotFound] = "Update folder not found",
  16. [UpdatePrepareResultManifestInvalid] = "Invalid manifest data",
  17. [UpdatePrepareResultStageMissing] = "Missing Stage2 loader",
  18. [UpdatePrepareResultStageIntegrityError] = "Corrupted Stage2 loader",
  19. [UpdatePrepareResultManifestPointerError] = "Failed to create update pointer file",
  20. [UpdatePrepareResultOutdatedManifestVersion] = "Update package is too old",
  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, string_t out_path) {
  30. const uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
  31. string_set_str(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. string_reset(out_path);
  56. }
  57. return found;
  58. }
  59. #define UPDATE_FILE_POINTER_FN "/ext/" UPDATE_MANIFEST_POINTER_FILE_NAME
  60. #define UPDATE_MANIFEST_MAX_PATH_LEN 256u
  61. bool update_operation_get_current_package_manifest_path(Storage* storage, string_t out_path) {
  62. 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. string_set_str(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. string_t rtcpath;
  87. string_init(rtcpath);
  88. do {
  89. if(!update_operation_get_current_package_path_rtc(storage, rtcpath)) {
  90. break;
  91. }
  92. path_concat(string_get_cstr(rtcpath), UPDATE_MANIFEST_DEFAULT_NAME, out_path);
  93. } while(0);
  94. string_clear(rtcpath);
  95. }
  96. return !string_empty_p(out_path);
  97. }
  98. static bool update_operation_persist_manifest_path(Storage* storage, const char* manifest_path) {
  99. const uint16_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 = UpdatePrepareResultManifestFolderNotFound;
  120. Storage* storage = furi_record_open("storage");
  121. UpdateManifest* manifest = update_manifest_alloc();
  122. File* file = storage_file_alloc(storage);
  123. string_t stage_path;
  124. string_init(stage_path);
  125. do {
  126. if(storage_common_stat(storage, manifest_file_path, NULL) != FSE_OK) {
  127. break;
  128. }
  129. if(!update_manifest_init(manifest, manifest_file_path)) {
  130. result = UpdatePrepareResultManifestInvalid;
  131. break;
  132. }
  133. if(manifest->manifest_version < UPDATE_OPERATION_MIN_MANIFEST_VERSION) {
  134. result = UpdatePrepareResultOutdatedManifestVersion;
  135. break;
  136. }
  137. if(furi_hal_version_get_hw_target() != manifest->target) {
  138. result = UpdatePrepareResultTargetMismatch;
  139. break;
  140. }
  141. path_extract_dirname(manifest_file_path, stage_path);
  142. path_append(stage_path, string_get_cstr(manifest->staged_loader_file));
  143. if(!storage_file_open(file, string_get_cstr(stage_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  144. result = UpdatePrepareResultStageMissing;
  145. break;
  146. }
  147. uint32_t crc = crc32_calc_file(file, NULL, NULL);
  148. if(crc != manifest->staged_loader_crc) {
  149. result = UpdatePrepareResultStageIntegrityError;
  150. break;
  151. }
  152. if(!update_operation_persist_manifest_path(storage, manifest_file_path)) {
  153. result = UpdatePrepareResultManifestPointerError;
  154. break;
  155. }
  156. result = UpdatePrepareResultOK;
  157. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePreUpdate);
  158. } while(false);
  159. string_clear(stage_path);
  160. storage_file_free(file);
  161. update_manifest_free(manifest);
  162. furi_record_close("storage");
  163. return result;
  164. }
  165. bool update_operation_is_armed() {
  166. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  167. const uint32_t rtc_upd_index =
  168. furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
  169. Storage* storage = furi_record_open("storage");
  170. const bool upd_fn_ptr_exists =
  171. (storage_common_stat(storage, UPDATE_FILE_POINTER_FN, NULL) == FSE_OK);
  172. furi_record_close("storage");
  173. return (boot_mode >= FuriHalRtcBootModePreUpdate) &&
  174. (boot_mode <= FuriHalRtcBootModePostUpdate) &&
  175. ((rtc_upd_index != INT_MAX) || upd_fn_ptr_exists);
  176. }
  177. void update_operation_disarm() {
  178. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  179. furi_hal_rtc_set_register(FuriHalRtcRegisterUpdateFolderFSIndex, INT_MAX);
  180. Storage* storage = furi_record_open("storage");
  181. storage_simply_remove(storage, UPDATE_FILE_POINTER_FN);
  182. furi_record_close("storage");
  183. }