updater_cli.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/update/PACKAGE/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. string_t subcommand;
  60. string_init(subcommand);
  61. if(!args_read_string_and_trim(args, subcommand) || string_empty_p(args)) {
  62. updater_cli_help(args);
  63. string_clear(subcommand);
  64. return;
  65. }
  66. for(size_t idx = 0; idx < COUNT_OF(update_cli_subcommands); ++idx) {
  67. const CliSubcommand* subcmd_def = &update_cli_subcommands[idx];
  68. if(string_cmp_str(subcommand, subcmd_def->command) == 0) {
  69. string_clear(subcommand);
  70. subcmd_def->handler(args);
  71. return;
  72. }
  73. }
  74. string_clear(subcommand);
  75. updater_cli_help(args);
  76. }
  77. static int32_t updater_spawner_thread_worker(void* arg) {
  78. Loader* loader = furi_record_open("loader");
  79. loader_start(loader, "UpdaterApp", NULL);
  80. furi_record_close("loader");
  81. return 0;
  82. }
  83. static void updater_spawner_thread_cleanup(FuriThreadState state, void* context) {
  84. FuriThread* thread = context;
  85. if(state == FuriThreadStateStopped) {
  86. furi_thread_free(thread);
  87. }
  88. }
  89. static void updater_start_app() {
  90. FuriHalRtcBootMode mode = furi_hal_rtc_get_boot_mode();
  91. if((mode != FuriHalRtcBootModePreUpdate) && (mode != FuriHalRtcBootModePostUpdate)) {
  92. return;
  93. }
  94. /* We need to spawn a separate thread, because these callbacks are executed
  95. * inside loader process, at startup.
  96. * So, accessing its record would cause a deadlock
  97. */
  98. FuriThread* thread = furi_thread_alloc();
  99. furi_thread_set_name(thread, "UpdateAppSpawner");
  100. furi_thread_set_stack_size(thread, 768);
  101. furi_thread_set_callback(thread, updater_spawner_thread_worker);
  102. furi_thread_set_state_callback(thread, updater_spawner_thread_cleanup);
  103. furi_thread_set_state_context(thread, thread);
  104. furi_thread_start(thread);
  105. }
  106. void updater_on_system_start() {
  107. #ifdef SRV_CLI
  108. Cli* cli = (Cli*)furi_record_open("cli");
  109. cli_add_command(cli, "update", CliCommandFlagDefault, updater_cli_ep, NULL);
  110. furi_record_close("cli");
  111. #else
  112. UNUSED(updater_cli_ep);
  113. #endif
  114. #ifndef FURI_RAM_EXEC
  115. updater_start_app();
  116. #else
  117. UNUSED(updater_start_app);
  118. #endif
  119. }