From 536e2ccac6d04b983dfaf34f45ca5ec383e76ac3 Mon Sep 17 00:00:00 2001 From: wangjianyu3 Date: Thu, 20 Feb 2025 11:28:10 +0800 Subject: [PATCH] system/fastboot: Add support for fastboot oem shell To support executing custom commands. Usage fastboot oem shell Tests # Configuration "esp32s3-devkit:fastboot" with `SYSTEM_FASTBOOTD_SHELL` enabled $ fastboot --version fastboot version 35.0.2-12147458 $ fastboot oem shell ls /FILE_NOT_EXISTS FAILED (remote: 'error detected 0xff00 4') fastboot: error: Command failed $ fastboot oem shell ps PID GROUP PRI POLICY TYPE NPX STATE EVENT SIGMASK STACK COMMAND 0 0 0 FIFO Kthread - Ready 0000000000000000 0003056 Idle_Task 1 0 224 RR Kthread - Waiting Semaphore 0000000000000000 0001976 hpwork 0x3fc8bd50 0x3fc8bd80 2 2 100 RR Task - Waiting Semaphore 0000000000000000 0004048 nsh_main 3 3 100 RR Task - Ready 0000000000000000 0001992 fastbootd 4 4 100 RR Task - Running 0000000000000000 0001992 popen -c ps OKAY [ 0.010s] Finished. Total time: 0.010s Signed-off-by: wangjianyu3 --- system/fastboot/Kconfig | 10 ++++++++++ system/fastboot/fastboot.c | 41 +++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/system/fastboot/Kconfig b/system/fastboot/Kconfig index 75e464d99..eb5edcfd9 100644 --- a/system/fastboot/Kconfig +++ b/system/fastboot/Kconfig @@ -32,4 +32,14 @@ config SYSTEM_FASTBOOTD_USB_BOARDCTL ---help--- Connect usbdev before running fastboot daemon. +config SYSTEM_FASTBOOTD_SHELL + bool "Execute custom commands" + default n + depends on SCHED_CHILD_STATUS + depends on SYSTEM_POPEN + ---help--- + Enable to support executing custom commands. + e.g. fastboot oem shell ps + e.g. fastboot oem shell "mkrd -m 10 -s 512 640" + endif # SYSTEM_FASTBOOTD diff --git a/system/fastboot/fastboot.c b/system/fastboot/fastboot.c index 72f27e18d..4fa882eee 100644 --- a/system/fastboot/fastboot.c +++ b/system/fastboot/fastboot.c @@ -46,6 +46,7 @@ #include #include #include +#include /**************************************************************************** * Pre-processor Definitions @@ -180,6 +181,10 @@ static void fastboot_memdump(FAR struct fastboot_ctx_s *context, FAR const char *arg); static void fastboot_filedump(FAR struct fastboot_ctx_s *context, FAR const char *arg); +#ifdef CONFIG_SYSTEM_FASTBOOTD_SHELL +static void fastboot_shell(FAR struct fastboot_ctx_s *context, + FAR const char *arg); +#endif /**************************************************************************** * Private Data @@ -200,7 +205,10 @@ static const struct fastboot_cmd_s g_fast_cmd[] = static const struct fastboot_cmd_s g_oem_cmd[] = { { "filedump", fastboot_filedump }, - { "memdump", fastboot_memdump } + { "memdump", fastboot_memdump }, +#ifdef CONFIG_SYSTEM_FASTBOOTD_SHELL + { "shell", fastboot_shell }, +#endif }; /**************************************************************************** @@ -776,6 +784,37 @@ static void fastboot_filedump(FAR struct fastboot_ctx_s *context, fastboot_okay(context, ""); } +#ifdef CONFIG_SYSTEM_FASTBOOTD_SHELL +static void fastboot_shell(FAR struct fastboot_ctx_s *context, + FAR const char *arg) +{ + char response[FASTBOOT_MSG_LEN - 4]; + FILE *fp; + int ret; + + fp = popen(arg, "r"); + if (fp == NULL) + { + fastboot_fail(context, "popen() fails %d", errno); + return; + } + + while (fgets(response, sizeof(response), fp)) + { + fastboot_ack(context, "TEXT", response); + } + + ret = pclose(fp); + if (WIFEXITED(ret) && WEXITSTATUS(ret) == 0) + { + fastboot_okay(context, ""); + return; + } + + fastboot_fail(context, "error detected 0x%x %d", ret, errno); +} +#endif + static void fastboot_upload(FAR struct fastboot_ctx_s *context, FAR const char *arg) {