update_operation.c 6.6 KB

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