updater_cli.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <cli/cli.h>
  4. #include <storage/storage.h>
  5. #include <loader/loader.h>
  6. #include <toolbox/path.h>
  7. #include <toolbox/tar/tar_archive.h>
  8. #include <toolbox/args.h>
  9. #include <update_util/update_manifest.h>
  10. #include <update_util/lfs_backup.h>
  11. #include <update_util/update_operation.h>
  12. typedef void (*cmd_handler)(FuriString* args);
  13. typedef struct {
  14. const char* command;
  15. const cmd_handler handler;
  16. } CliSubcommand;
  17. static void updater_cli_install(FuriString* manifest_path) {
  18. printf("Verifying update package at '%s'\r\n", furi_string_get_cstr(manifest_path));
  19. UpdatePrepareResult result = update_operation_prepare(furi_string_get_cstr(manifest_path));
  20. if(result != UpdatePrepareResultOK) {
  21. printf(
  22. "Error: %s. Stopping update.\r\n",
  23. update_operation_describe_preparation_result(result));
  24. return;
  25. }
  26. printf("OK.\r\nRestarting to apply update. BRB\r\n");
  27. furi_delay_ms(100);
  28. furi_hal_power_reset();
  29. }
  30. static void updater_cli_backup(FuriString* args) {
  31. printf("Backup /int to '%s'\r\n", furi_string_get_cstr(args));
  32. Storage* storage = furi_record_open(RECORD_STORAGE);
  33. bool success = lfs_backup_create(storage, furi_string_get_cstr(args));
  34. furi_record_close(RECORD_STORAGE);
  35. printf("Result: %s\r\n", success ? "OK" : "FAIL");
  36. }
  37. static void updater_cli_restore(FuriString* args) {
  38. printf("Restore /int from '%s'\r\n", furi_string_get_cstr(args));
  39. Storage* storage = furi_record_open(RECORD_STORAGE);
  40. bool success = lfs_backup_unpack(storage, furi_string_get_cstr(args));
  41. furi_record_close(RECORD_STORAGE);
  42. printf("Result: %s\r\n", success ? "OK" : "FAIL");
  43. }
  44. static void updater_cli_help(FuriString* args) {
  45. UNUSED(args);
  46. printf("Commands:\r\n"
  47. "\tinstall /ext/path/to/update.fuf - verify & apply update package\r\n"
  48. "\tbackup /ext/path/to/backup.tar - create internal storage backup\r\n"
  49. "\trestore /ext/path/to/backup.tar - restore internal storage backup\r\n");
  50. }
  51. static const CliSubcommand update_cli_subcommands[] = {
  52. {.command = "install", .handler = updater_cli_install},
  53. {.command = "backup", .handler = updater_cli_backup},
  54. {.command = "restore", .handler = updater_cli_restore},
  55. {.command = "help", .handler = updater_cli_help},
  56. };
  57. static void updater_cli_ep(Cli* cli, FuriString* args, void* context) {
  58. UNUSED(cli);
  59. UNUSED(context);
  60. FuriString* subcommand;
  61. subcommand = furi_string_alloc();
  62. if(!args_read_string_and_trim(args, subcommand) || furi_string_empty(args)) {
  63. updater_cli_help(args);
  64. furi_string_free(subcommand);
  65. return;
  66. }
  67. for(size_t idx = 0; idx < COUNT_OF(update_cli_subcommands); ++idx) {
  68. const CliSubcommand* subcmd_def = &update_cli_subcommands[idx];
  69. if(furi_string_cmp_str(subcommand, subcmd_def->command) == 0) {
  70. furi_string_free(subcommand);
  71. subcmd_def->handler(args);
  72. return;
  73. }
  74. }
  75. furi_string_free(subcommand);
  76. updater_cli_help(args);
  77. }
  78. static int32_t updater_spawner_thread_worker(void* arg) {
  79. UNUSED(arg);
  80. Loader* loader = furi_record_open(RECORD_LOADER);
  81. loader_start(loader, "UpdaterApp", NULL);
  82. furi_record_close(RECORD_LOADER);
  83. return 0;
  84. }
  85. static void updater_spawner_thread_cleanup(FuriThreadState state, void* context) {
  86. FuriThread* thread = context;
  87. if(state == FuriThreadStateStopped) {
  88. furi_thread_free(thread);
  89. }
  90. }
  91. static void updater_start_app() {
  92. FuriHalRtcBootMode mode = furi_hal_rtc_get_boot_mode();
  93. if((mode != FuriHalRtcBootModePreUpdate) && (mode != FuriHalRtcBootModePostUpdate)) {
  94. return;
  95. }
  96. /* We need to spawn a separate thread, because these callbacks are executed
  97. * inside loader process, at startup.
  98. * So, accessing its record would cause a deadlock
  99. */
  100. FuriThread* thread = furi_thread_alloc();
  101. furi_thread_set_name(thread, "UpdateAppSpawner");
  102. furi_thread_set_stack_size(thread, 768);
  103. furi_thread_set_callback(thread, updater_spawner_thread_worker);
  104. furi_thread_set_state_callback(thread, updater_spawner_thread_cleanup);
  105. furi_thread_set_state_context(thread, thread);
  106. furi_thread_start(thread);
  107. }
  108. void updater_on_system_start() {
  109. #ifdef SRV_CLI
  110. Cli* cli = (Cli*)furi_record_open(RECORD_CLI);
  111. cli_add_command(cli, "update", CliCommandFlagDefault, updater_cli_ep, NULL);
  112. furi_record_close(RECORD_CLI);
  113. #else
  114. UNUSED(updater_cli_ep);
  115. #endif
  116. #ifndef FURI_RAM_EXEC
  117. updater_start_app();
  118. #else
  119. UNUSED(updater_start_app);
  120. #endif
  121. }