Merge branch 'master_sh/gen4/mmcfix' of https://source.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / common / cli.c
index a433ef2..3916a7b 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2000
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
@@ -5,18 +6,23 @@
  * Add to readline cmdline-editing by
  * (C) Copyright 2005
  * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
+#define pr_fmt(fmt) "cli: %s: " fmt, __func__
+
 #include <common.h>
+#include <bootstage.h>
 #include <cli.h>
 #include <cli_hush.h>
+#include <command.h>
 #include <console.h>
+#include <env.h>
 #include <fdtdec.h>
+#include <hang.h>
 #include <malloc.h>
-
-DECLARE_GLOBAL_DATA_PTR;
+#include <asm/global_data.h>
+#include <dm/ofnode.h>
+#include <linux/errno.h>
 
 #ifdef CONFIG_CMDLINE
 /*
@@ -24,11 +30,11 @@ DECLARE_GLOBAL_DATA_PTR;
  *
  * @param cmd  Command to run
  * @param flag Execution flags (CMD_FLAG_...)
- * @return 0 on success, or != 0 on error.
+ * Return: 0 on success, or != 0 on error.
  */
 int run_command(const char *cmd, int flag)
 {
-#ifndef CONFIG_HUSH_PARSER
+#if !IS_ENABLED(CONFIG_HUSH_PARSER)
        /*
         * cli_run_command can return 0 or 1 for success, so clean up
         * its result.
@@ -51,7 +57,7 @@ int run_command(const char *cmd, int flag)
  *
  * @param cmd  Command to run
  * @param flag Execution flags (CMD_FLAG_...)
- * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
+ * Return: 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
  */
 int run_command_repeatable(const char *cmd, int flag)
 {
@@ -69,6 +75,13 @@ int run_command_repeatable(const char *cmd, int flag)
        return 0;
 #endif
 }
+#else
+__weak int board_run_command(const char *cmdline)
+{
+       printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
+
+       return 1;
+}
 #endif /* CONFIG_CMDLINE */
 
 int run_command_list(const char *cmd, int len, int flag)
@@ -116,12 +129,37 @@ int run_command_list(const char *cmd, int len, int flag)
        return rcode;
 }
 
+int run_commandf(const char *fmt, ...)
+{
+       va_list args;
+       int nbytes;
+
+       va_start(args, fmt);
+       /*
+        * Limit the console_buffer space being used to CONFIG_SYS_CBSIZE,
+        * because its last byte is used to fit the replacement of \0 by \n\0
+        * in underlying hush parser
+        */
+       nbytes = vsnprintf(console_buffer, CONFIG_SYS_CBSIZE, fmt, args);
+       va_end(args);
+
+       if (nbytes < 0) {
+               pr_debug("I/O internal error occurred.\n");
+               return -EIO;
+       } else if (nbytes >= CONFIG_SYS_CBSIZE) {
+               pr_debug("'fmt' size:%d exceeds the limit(%d)\n",
+                        nbytes, CONFIG_SYS_CBSIZE);
+               return -ENOSPC;
+       }
+       return run_command(console_buffer, 0);
+}
+
 /****************************************************************************/
 
 #if defined(CONFIG_CMD_RUN)
-int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+int do_run(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-       int i;
+       int i, ret;
 
        if (argc < 2)
                return CMD_RET_USAGE;
@@ -129,14 +167,15 @@ int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        for (i = 1; i < argc; ++i) {
                char *arg;
 
-               arg = getenv(argv[i]);
+               arg = env_get(argv[i]);
                if (arg == NULL) {
                        printf("## Error: \"%s\" not defined\n", argv[i]);
                        return 1;
                }
 
-               if (run_command(arg, flag | CMD_FLAG_ENV) != 0)
-                       return 1;
+               ret = run_command(arg, flag | CMD_FLAG_ENV);
+               if (ret)
+                       return ret;
        }
        return 0;
 }
@@ -146,7 +185,7 @@ int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 bool cli_process_fdt(const char **cmdp)
 {
        /* Allow the fdt to override the boot command */
-       char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
+       const char *env = ofnode_conf_read_str("bootcmd");
        if (env)
                *cmdp = env;
        /*
@@ -154,7 +193,7 @@ bool cli_process_fdt(const char **cmdp)
         * Always use 'env' in this case, since bootsecure requres that the
         * bootcmd was specified in the FDT too.
         */
-       return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0;
+       return ofnode_conf_read_int("bootsecure", 0);
 }
 
 /*
@@ -173,7 +212,7 @@ bool cli_process_fdt(const char **cmdp)
 void cli_secure_boot_cmd(const char *cmd)
 {
 #ifdef CONFIG_CMDLINE
-       cmd_tbl_t *cmdtp;
+       struct cmd_tbl *cmdtp;
 #endif
        int rc;
 
@@ -214,6 +253,7 @@ err:
 
 void cli_loop(void)
 {
+       bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
 #ifdef CONFIG_HUSH_PARSER
        parse_file_outer();
        /* This point is never reached */