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 <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_PATH("update")
  10. /* Need at least 4 free LFS pages before update */
  11. #define UPDATE_MIN_INT_FREE_SPACE 2 * 4 * 1024
  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. [UpdatePrepareResultIntFull] = "Need more free space in internal storage",
  22. };
  23. const char* update_operation_describe_preparation_result(const UpdatePrepareResult value) {
  24. if(value >= COUNT_OF(update_prepare_result_descr)) {
  25. return "...";
  26. } else {
  27. return update_prepare_result_descr[value];
  28. }
  29. }
  30. static bool update_operation_get_current_package_path_rtc(Storage* storage, string_t out_path) {
  31. const uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
  32. string_set_str(out_path, UPDATE_ROOT_DIR);
  33. if(update_index == UPDATE_OPERATION_ROOT_DIR_PACKAGE_MAGIC) {
  34. return true;
  35. }
  36. bool found = false;
  37. uint32_t iter_index = 0;
  38. File* dir = storage_file_alloc(storage);
  39. FileInfo fi = {0};
  40. char* name_buffer = malloc(UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN);
  41. do {
  42. if(!storage_dir_open(dir, UPDATE_ROOT_DIR)) {
  43. break;
  44. }
  45. while(storage_dir_read(dir, &fi, name_buffer, UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN)) {
  46. if(++iter_index == update_index) {
  47. found = true;
  48. path_append(out_path, name_buffer);
  49. break;
  50. }
  51. }
  52. } while(false);
  53. free(name_buffer);
  54. storage_file_free(dir);
  55. if(!found) {
  56. string_reset(out_path);
  57. }
  58. return found;
  59. }
  60. #define UPDATE_FILE_POINTER_FN EXT_PATH(UPDATE_MANIFEST_POINTER_FILE_NAME)
  61. #define UPDATE_MANIFEST_MAX_PATH_LEN 256u
  62. bool update_operation_get_current_package_manifest_path(Storage* storage, string_t out_path) {
  63. string_reset(out_path);
  64. if(storage_common_stat(storage, UPDATE_FILE_POINTER_FN, NULL) == FSE_OK) {
  65. char* manifest_name_buffer = malloc(UPDATE_MANIFEST_MAX_PATH_LEN);
  66. File* upd_file = NULL;
  67. do {
  68. upd_file = storage_file_alloc(storage);
  69. if(!storage_file_open(
  70. upd_file, UPDATE_FILE_POINTER_FN, FSAM_READ, FSOM_OPEN_EXISTING)) {
  71. break;
  72. }
  73. uint16_t bytes_read =
  74. storage_file_read(upd_file, manifest_name_buffer, UPDATE_MANIFEST_MAX_PATH_LEN);
  75. if((bytes_read == 0) || (bytes_read == UPDATE_MANIFEST_MAX_PATH_LEN)) {
  76. break;
  77. }
  78. if(storage_common_stat(storage, manifest_name_buffer, NULL) != FSE_OK) {
  79. break;
  80. }
  81. string_set_str(out_path, manifest_name_buffer);
  82. } while(0);
  83. free(manifest_name_buffer);
  84. storage_file_free(upd_file);
  85. } else {
  86. /* legacy, will be deprecated */
  87. string_t rtcpath;
  88. string_init(rtcpath);
  89. do {
  90. if(!update_operation_get_current_package_path_rtc(storage, rtcpath)) {
  91. break;
  92. }
  93. path_concat(string_get_cstr(rtcpath), UPDATE_MANIFEST_DEFAULT_NAME, out_path);
  94. } while(0);
  95. string_clear(rtcpath);
  96. }
  97. return !string_empty_p(out_path);
  98. }
  99. static bool update_operation_persist_manifest_path(Storage* storage, const char* manifest_path) {
  100. const uint16_t manifest_path_len = strlen(manifest_path);
  101. furi_check(manifest_path && manifest_path_len);
  102. bool success = false;
  103. File* file = storage_file_alloc(storage);
  104. do {
  105. if(manifest_path_len >= UPDATE_OPERATION_MAX_MANIFEST_PATH_LEN) {
  106. break;
  107. }
  108. if(!storage_file_open(file, UPDATE_FILE_POINTER_FN, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  109. break;
  110. }
  111. if(storage_file_write(file, manifest_path, manifest_path_len) != manifest_path_len) {
  112. break;
  113. }
  114. success = true;
  115. } while(0);
  116. storage_file_free(file);
  117. return success;
  118. }
  119. UpdatePrepareResult update_operation_prepare(const char* manifest_file_path) {
  120. UpdatePrepareResult result = UpdatePrepareResultIntFull;
  121. Storage* storage = furi_record_open(RECORD_STORAGE);
  122. UpdateManifest* manifest = update_manifest_alloc();
  123. File* file = storage_file_alloc(storage);
  124. uint64_t free_int_space;
  125. string_t stage_path;
  126. string_init(stage_path);
  127. do {
  128. if((storage_common_fs_info(storage, STORAGE_INT_PATH_PREFIX, NULL, &free_int_space) !=
  129. FSE_OK) ||
  130. (free_int_space < UPDATE_MIN_INT_FREE_SPACE)) {
  131. break;
  132. }
  133. if(storage_common_stat(storage, manifest_file_path, NULL) != FSE_OK) {
  134. result = UpdatePrepareResultManifestFolderNotFound;
  135. break;
  136. }
  137. if(!update_manifest_init(manifest, manifest_file_path)) {
  138. result = UpdatePrepareResultManifestInvalid;
  139. break;
  140. }
  141. if(manifest->manifest_version < UPDATE_OPERATION_MIN_MANIFEST_VERSION) {
  142. result = UpdatePrepareResultOutdatedManifestVersion;
  143. break;
  144. }
  145. if(furi_hal_version_get_hw_target() != manifest->target) {
  146. result = UpdatePrepareResultTargetMismatch;
  147. break;
  148. }
  149. path_extract_dirname(manifest_file_path, stage_path);
  150. path_append(stage_path, string_get_cstr(manifest->staged_loader_file));
  151. if(!storage_file_open(file, 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. string_clear(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. }