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