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