Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 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"; |
Francis Laniel | 3b66e57 | 2023-12-22 22:02:32 +0100 | [diff] [blame] | 14 | if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) |
| 15 | return "modern"; |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 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; |
Francis Laniel | 3b66e57 | 2023-12-22 22:02:32 +0100 | [diff] [blame] | 38 | if (!strcmp(parser, "modern")) |
| 39 | return GD_FLG_HUSH_MODERN_PARSER; |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 40 | return -1; |
| 41 | } |
| 42 | |
Francis Laniel | 3b66e57 | 2023-12-22 22:02:32 +0100 | [diff] [blame] | 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); |
| 49 | return -1; |
| 50 | } |
| 51 | |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 52 | static void reset_parser_gd_flags(void) |
| 53 | { |
| 54 | gd->flags &= ~GD_FLG_HUSH_OLD_PARSER; |
Francis Laniel | 3b66e57 | 2023-12-22 22:02:32 +0100 | [diff] [blame] | 55 | gd->flags &= ~GD_FLG_HUSH_MODERN_PARSER; |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 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; |
Francis Laniel | 3b66e57 | 2023-12-22 22:02:32 +0100 | [diff] [blame] | 62 | int parser_config; |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 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 | |
Francis Laniel | 3b66e57 | 2023-12-22 22:02:32 +0100 | [diff] [blame] | 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); |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 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 | |
| 121 | #if CONFIG_IS_ENABLED(SYS_LONGHELP) |
| 122 | static char cli_help_text[] = |
| 123 | "get - print current cli\n" |
Francis Laniel | 3b66e57 | 2023-12-22 22:02:32 +0100 | [diff] [blame] | 124 | "set - set the current cli, possible value are: old, modern" |
Francis Laniel | efb5800 | 2023-12-22 22:02:31 +0100 | [diff] [blame] | 125 | ; |
| 126 | #endif |
| 127 | |
| 128 | U_BOOT_CMD(cli, 3, 1, do_cli, |
| 129 | "cli", |
| 130 | #if CONFIG_IS_ENABLED(SYS_LONGHELP) |
| 131 | cli_help_text |
| 132 | #endif |
| 133 | ); |