]>
Commit | Line | Data |
---|---|---|
6bb39f5d FL |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | ||
6bb39f5d FL |
3 | #include <cli.h> |
4 | #include <command.h> | |
5 | #include <string.h> | |
6 | #include <asm/global_data.h> | |
7 | ||
8 | DECLARE_GLOBAL_DATA_PTR; | |
9 | ||
10 | static const char *gd_flags_to_parser_name(void) | |
11 | { | |
12 | if (gd->flags & GD_FLG_HUSH_OLD_PARSER) | |
13 | return "old"; | |
9a068377 FL |
14 | if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) |
15 | return "modern"; | |
6bb39f5d FL |
16 | return NULL; |
17 | } | |
18 | ||
19 | static 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 | ||
34 | static int parser_string_to_gd_flags(const char *parser) | |
35 | { | |
36 | if (!strcmp(parser, "old")) | |
37 | return GD_FLG_HUSH_OLD_PARSER; | |
9a068377 FL |
38 | if (!strcmp(parser, "modern")) |
39 | return GD_FLG_HUSH_MODERN_PARSER; | |
40 | return -1; | |
41 | } | |
42 | ||
43 | static 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); | |
6bb39f5d FL |
49 | return -1; |
50 | } | |
51 | ||
52 | static void reset_parser_gd_flags(void) | |
53 | { | |
54 | gd->flags &= ~GD_FLG_HUSH_OLD_PARSER; | |
9a068377 | 55 | gd->flags &= ~GD_FLG_HUSH_MODERN_PARSER; |
6bb39f5d FL |
56 | } |
57 | ||
58 | static int do_cli_set(struct cmd_tbl *cmdtp, int flag, int argc, | |
59 | char *const argv[]) | |
60 | { | |
61 | char *parser_name; | |
9a068377 | 62 | int parser_config; |
6bb39f5d FL |
63 | 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 | ||
9a068377 FL |
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); | |
6bb39f5d FL |
84 | 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 | ||
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, "", ""), | |
100 | }; | |
101 | ||
102 | static 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 | ||
b85ecb27 | 121 | U_BOOT_LONGHELP(cli, |
6bb39f5d | 122 | "get - print current cli\n" |
b85ecb27 | 123 | "set - set the current cli, possible value are: old, modern\n"); |
6bb39f5d FL |
124 | |
125 | U_BOOT_CMD(cli, 3, 1, do_cli, | |
126 | "cli", | |
6bb39f5d | 127 | cli_help_text |
6bb39f5d | 128 | ); |