updater_cli.c 4.4 KB

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