update_operation.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #define MAX_DIR_NAME_LEN 250
  13. static const char* update_prepare_result_descr[] = {
  14. [UpdatePrepareResultOK] = "OK",
  15. [UpdatePrepareResultManifestPathInvalid] = "Invalid manifest name or location",
  16. [UpdatePrepareResultManifestFolderNotFound] = "Update folder not found",
  17. [UpdatePrepareResultManifestInvalid] = "Invalid manifest data",
  18. [UpdatePrepareResultStageMissing] = "Missing Stage2 loader",
  19. [UpdatePrepareResultStageIntegrityError] = "Corrupted Stage2 loader",
  20. };
  21. const char* update_operation_describe_preparation_result(const UpdatePrepareResult value) {
  22. if(value >= COUNT_OF(update_prepare_result_descr)) {
  23. return "...";
  24. } else {
  25. return update_prepare_result_descr[value];
  26. }
  27. }
  28. bool update_operation_get_package_dir_name(const char* full_path, string_t out_manifest_dir) {
  29. bool path_ok = false;
  30. string_t full_path_str;
  31. string_init_set(full_path_str, full_path);
  32. string_reset(out_manifest_dir);
  33. bool start_end_ok = string_start_with_str_p(full_path_str, UPDATE_PREFIX) &&
  34. string_end_with_str_p(full_path_str, UPDATE_SUFFIX);
  35. int16_t dir_name_len =
  36. strlen(full_path) - strlen(UPDATE_PREFIX) - strlen(UPDATE_MANIFEST_DEFAULT_NAME) - 1;
  37. if(dir_name_len == -1) {
  38. path_ok = true;
  39. } else if(start_end_ok && (dir_name_len > 0)) {
  40. string_set_n(out_manifest_dir, full_path_str, strlen(UPDATE_PREFIX), dir_name_len);
  41. path_ok = true;
  42. if(string_search_char(out_manifest_dir, '/') != STRING_FAILURE) {
  43. string_reset(out_manifest_dir);
  44. path_ok = false;
  45. }
  46. }
  47. string_clear(full_path_str);
  48. return path_ok;
  49. }
  50. int32_t update_operation_get_package_index(Storage* storage, const char* update_package_dir) {
  51. furi_assert(storage);
  52. furi_assert(update_package_dir);
  53. if(strlen(update_package_dir) == 0) {
  54. return UPDATE_OPERATION_ROOT_DIR_PACKAGE_MAGIC;
  55. }
  56. bool found = false;
  57. int32_t index = 0;
  58. File* dir = storage_file_alloc(storage);
  59. FileInfo fi = {0};
  60. char* name_buffer = malloc(MAX_DIR_NAME_LEN);
  61. do {
  62. if(!storage_dir_open(dir, UPDATE_ROOT_DIR)) {
  63. break;
  64. }
  65. while(storage_dir_read(dir, &fi, name_buffer, MAX_DIR_NAME_LEN)) {
  66. index++;
  67. if(strcmp(name_buffer, update_package_dir)) {
  68. continue;
  69. } else {
  70. found = true;
  71. break;
  72. }
  73. }
  74. } while(false);
  75. free(name_buffer);
  76. storage_file_free(dir);
  77. return found ? index : -1;
  78. }
  79. bool update_operation_get_current_package_path(Storage* storage, string_t out_path) {
  80. const uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
  81. string_set_str(out_path, UPDATE_ROOT_DIR);
  82. if(update_index == UPDATE_OPERATION_ROOT_DIR_PACKAGE_MAGIC) {
  83. return true;
  84. }
  85. bool found = false;
  86. uint32_t iter_index = 0;
  87. File* dir = storage_file_alloc(storage);
  88. FileInfo fi = {0};
  89. char* name_buffer = malloc(MAX_DIR_NAME_LEN);
  90. do {
  91. if(!storage_dir_open(dir, UPDATE_ROOT_DIR)) {
  92. break;
  93. }
  94. while(storage_dir_read(dir, &fi, name_buffer, MAX_DIR_NAME_LEN)) {
  95. if(++iter_index == update_index) {
  96. found = true;
  97. path_append(out_path, name_buffer);
  98. break;
  99. }
  100. }
  101. } while(false);
  102. free(name_buffer);
  103. storage_file_free(dir);
  104. if(!found) {
  105. string_reset(out_path);
  106. }
  107. return found;
  108. }
  109. UpdatePrepareResult update_operation_prepare(const char* manifest_file_path) {
  110. string_t update_folder;
  111. string_init(update_folder);
  112. if(!update_operation_get_package_dir_name(manifest_file_path, update_folder)) {
  113. string_clear(update_folder);
  114. return UpdatePrepareResultManifestPathInvalid;
  115. }
  116. Storage* storage = furi_record_open("storage");
  117. int32_t update_index =
  118. update_operation_get_package_index(storage, string_get_cstr(update_folder));
  119. string_clear(update_folder);
  120. if(update_index < 0) {
  121. furi_record_close("storage");
  122. return UpdatePrepareResultManifestFolderNotFound;
  123. }
  124. string_t update_dir_path;
  125. string_init(update_dir_path);
  126. path_extract_dirname(manifest_file_path, update_dir_path);
  127. UpdatePrepareResult result = UpdatePrepareResultManifestInvalid;
  128. UpdateManifest* manifest = update_manifest_alloc();
  129. if(update_manifest_init(manifest, manifest_file_path)) {
  130. result = UpdatePrepareResultStageMissing;
  131. File* file = storage_file_alloc(storage);
  132. string_t stage_path;
  133. string_init(stage_path);
  134. path_extract_dirname(manifest_file_path, stage_path);
  135. path_append(stage_path, string_get_cstr(manifest->staged_loader_file));
  136. uint32_t crc = 0;
  137. do {
  138. if(!storage_file_open(
  139. file, string_get_cstr(stage_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  140. break;
  141. }
  142. result = UpdatePrepareResultStageIntegrityError;
  143. crc = crc32_calc_file(file, NULL, NULL);
  144. } while(false);
  145. string_clear(stage_path);
  146. storage_file_free(file);
  147. if(crc == manifest->staged_loader_crc) {
  148. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePreUpdate);
  149. update_operation_persist_package_index(update_index);
  150. result = UpdatePrepareResultOK;
  151. }
  152. }
  153. furi_record_close("storage");
  154. update_manifest_free(manifest);
  155. return result;
  156. }
  157. bool update_operation_is_armed() {
  158. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  159. return (boot_mode >= FuriHalRtcBootModePreUpdate) &&
  160. (boot_mode <= FuriHalRtcBootModePostUpdate) &&
  161. (furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex) > 0);
  162. }
  163. void update_operation_disarm() {
  164. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  165. furi_hal_rtc_set_register(
  166. FuriHalRtcRegisterUpdateFolderFSIndex, INT_MAX);
  167. }
  168. void update_operation_persist_package_index(int32_t index) {
  169. furi_check(index >= 0);
  170. furi_hal_rtc_set_register(FuriHalRtcRegisterUpdateFolderFSIndex, index);
  171. }