Merge branch '2024-11-22-assorted-fixes'
[u-boot.git] / cmd / cli.c
blobe0ddd0a43d0bfc344ad62d6d4a30b6cdf3d35f16
1 // SPDX-License-Identifier: GPL-2.0+
3 #include <cli.h>
4 #include <command.h>
5 #include <string.h>
6 #include <asm/global_data.h>
8 DECLARE_GLOBAL_DATA_PTR;
10 static const char *gd_flags_to_parser_name(void)
12 if (gd->flags & GD_FLG_HUSH_OLD_PARSER)
13 return "old";
14 if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
15 return "modern";
16 return NULL;
19 static int do_cli_get(struct cmd_tbl *cmdtp, int flag, int argc,
20 char *const argv[])
22 const char *current = gd_flags_to_parser_name();
24 if (!current) {
25 printf("current cli value is not valid, this should not happen!\n");
26 return CMD_RET_FAILURE;
29 printf("%s\n", current);
31 return CMD_RET_SUCCESS;
34 static int parser_string_to_gd_flags(const char *parser)
36 if (!strcmp(parser, "old"))
37 return GD_FLG_HUSH_OLD_PARSER;
38 if (!strcmp(parser, "modern"))
39 return GD_FLG_HUSH_MODERN_PARSER;
40 return -1;
43 static int gd_flags_to_parser_config(int flag)
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);
49 return -1;
52 static void reset_parser_gd_flags(void)
54 gd->flags &= ~GD_FLG_HUSH_OLD_PARSER;
55 gd->flags &= ~GD_FLG_HUSH_MODERN_PARSER;
58 static int do_cli_set(struct cmd_tbl *cmdtp, int flag, int argc,
59 char *const argv[])
61 char *parser_name;
62 int parser_config;
63 int parser_flag;
65 if (argc < 2)
66 return CMD_RET_USAGE;
68 parser_name = argv[1];
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;
76 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);
84 return CMD_RET_FAILURE;
87 reset_parser_gd_flags();
88 gd->flags |= parser_flag;
90 cli_init();
91 cli_loop();
93 /* cli_loop() should never return. */
94 return CMD_RET_FAILURE;
97 static 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, "", ""),
102 static int do_cli(struct cmd_tbl *cmdtp, int flag, int argc,
103 char *const argv[])
105 struct cmd_tbl *cp;
107 if (argc < 2)
108 return CMD_RET_USAGE;
110 /* drop initial "parser" arg */
111 argc--;
112 argv++;
114 cp = find_cmd_tbl(argv[0], parser_sub, ARRAY_SIZE(parser_sub));
115 if (cp)
116 return cp->cmd(cmdtp, flag, argc, argv);
118 return CMD_RET_USAGE;
121 U_BOOT_LONGHELP(cli,
122 "get - print current cli\n"
123 "set - set the current cli, possible value are: old, modern\n");
125 U_BOOT_CMD(cli, 3, 1, do_cli,
126 "cli",
127 cli_help_text