update_operation.c 8.0 KB

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