blob: 86c6471aa4d226ed4a80a063010669e358de3626 [file] [log] [blame]
Francis Laniel6bb39f52023-12-22 22:02:31 +01001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
4#include <cli.h>
5#include <command.h>
6#include <string.h>
7#include <asm/global_data.h>
8
9DECLARE_GLOBAL_DATA_PTR;
10
11static const char *gd_flags_to_parser_name(void)
12{
13 if (gd->flags & GD_FLG_HUSH_OLD_PARSER)
14 return "old";
15 return NULL;
16}
17
18static int do_cli_get(struct cmd_tbl *cmdtp, int flag, int argc,
19 char *const argv[])
20{
21 const char *current = gd_flags_to_parser_name();
22
23 if (!current) {
24 printf("current cli value is not valid, this should not happen!\n");
25 return CMD_RET_FAILURE;
26 }
27
28 printf("%s\n", current);
29
30 return CMD_RET_SUCCESS;
31}
32
33static int parser_string_to_gd_flags(const char *parser)
34{
35 if (!strcmp(parser, "old"))
36 return GD_FLG_HUSH_OLD_PARSER;
37 return -1;
38}
39
40static void reset_parser_gd_flags(void)
41{
42 gd->flags &= ~GD_FLG_HUSH_OLD_PARSER;
43}
44
45static int do_cli_set(struct cmd_tbl *cmdtp, int flag, int argc,
46 char *const argv[])
47{
48 char *parser_name;
49 int parser_flag;
50
51 if (argc < 2)
52 return CMD_RET_USAGE;
53
54 parser_name = argv[1];
55
56 parser_flag = parser_string_to_gd_flags(parser_name);
57 if (parser_flag == -1) {
58 printf("Bad value for parser name: %s\n", parser_name);
59 return CMD_RET_USAGE;
60 }
61
62 if (parser_flag == GD_FLG_HUSH_OLD_PARSER &&
63 !CONFIG_IS_ENABLED(HUSH_OLD_PARSER)) {
64 printf("Want to set current parser to old, but its code was not compiled!\n");
65 return CMD_RET_FAILURE;
66 }
67
68 reset_parser_gd_flags();
69 gd->flags |= parser_flag;
70
71 cli_init();
72 cli_loop();
73
74 /* cli_loop() should never return. */
75 return CMD_RET_FAILURE;
76}
77
78static struct cmd_tbl parser_sub[] = {
79 U_BOOT_CMD_MKENT(get, 1, 1, do_cli_get, "", ""),
80 U_BOOT_CMD_MKENT(set, 2, 1, do_cli_set, "", ""),
81};
82
83static int do_cli(struct cmd_tbl *cmdtp, int flag, int argc,
84 char *const argv[])
85{
86 struct cmd_tbl *cp;
87
88 if (argc < 2)
89 return CMD_RET_USAGE;
90
91 /* drop initial "parser" arg */
92 argc--;
93 argv++;
94
95 cp = find_cmd_tbl(argv[0], parser_sub, ARRAY_SIZE(parser_sub));
96 if (cp)
97 return cp->cmd(cmdtp, flag, argc, argv);
98
99 return CMD_RET_USAGE;
100}
101
102#if CONFIG_IS_ENABLED(SYS_LONGHELP)
103static char cli_help_text[] =
104 "get - print current cli\n"
105 "set - set the current cli, possible value is: old"
106 ;
107#endif
108
109U_BOOT_CMD(cli, 3, 1, do_cli,
110 "cli",
111#if CONFIG_IS_ENABLED(SYS_LONGHELP)
112 cli_help_text
113#endif
114);