From b08e9d4b6632a72b91306690d552c125b071441e Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:20 -0600 Subject: cli: Move readline character-processing to a state machine The current cread_line() function is very long. It handles the escape processing inline. The menu command does similar processing but at the character level, so there is some duplication. Split the character processing into a new function cli_ch_process() which processes individual characters and returns the resulting input character, taking account of escape sequences. It requires the caller to set up and maintain its state. Update cread_line() to use this new function. The only intended functional change is that an invalid escape sequence does not add invalid/control characters into the input buffer, but instead discards these. Signed-off-by: Simon Glass --- common/Makefile | 6 +- common/cli_getch.c | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++ common/cli_readline.c | 150 ++++++------------------------------- 3 files changed, 229 insertions(+), 131 deletions(-) create mode 100644 common/cli_getch.c (limited to 'common') diff --git a/common/Makefile b/common/Makefile index 7789aab484f..252e9656dfd 100644 --- a/common/Makefile +++ b/common/Makefile @@ -39,7 +39,7 @@ obj-$(CONFIG_SPLASH_SOURCE) += splash_source.o obj-$(CONFIG_MENU) += menu.o obj-$(CONFIG_UPDATE_COMMON) += update.o obj-$(CONFIG_USB_KEYBOARD) += usb_kbd.o -obj-$(CONFIG_CMDLINE) += cli_readline.o cli_simple.o +obj-$(CONFIG_CMDLINE) += cli_getch.o cli_readline.o cli_simple.o endif # !CONFIG_SPL_BUILD @@ -94,8 +94,8 @@ obj-y += eeprom/eeprom_field.o eeprom/eeprom_layout.o endif obj-y += cli.o -obj-$(CONFIG_FSL_DDR_INTERACTIVE) += cli_simple.o cli_readline.o -obj-$(CONFIG_STM32MP1_DDR_INTERACTIVE) += cli_simple.o cli_readline.o +obj-$(CONFIG_FSL_DDR_INTERACTIVE) += cli_getch.o cli_simple.o cli_readline.o +obj-$(CONFIG_STM32MP1_DDR_INTERACTIVE) += cli_getch.o cli_simple.o cli_readline.o obj-$(CONFIG_DFU_OVER_USB) += dfu.o obj-y += command.o obj-$(CONFIG_$(SPL_TPL_)LOG) += log.o diff --git a/common/cli_getch.c b/common/cli_getch.c new file mode 100644 index 00000000000..9eeea7fef29 --- /dev/null +++ b/common/cli_getch.c @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2000 + * Wolfgang Denk, DENX Software Engineering, wd@denx.de. + * + * Copyright 2022 Google LLC + */ + +#include +#include + +/** + * enum cli_esc_state_t - indicates what to do with an escape character + * + * @ESC_REJECT: Invalid escape sequence, so the esc_save[] characters are + * returned from each subsequent call to cli_ch_esc() + * @ESC_SAVE: Character should be saved in esc_save until we have another one + * @ESC_CONVERTED: Escape sequence has been completed and the resulting + * character is available + */ +enum cli_esc_state_t { + ESC_REJECT, + ESC_SAVE, + ESC_CONVERTED +}; + +void cli_ch_init(struct cli_ch_state *cch) +{ + memset(cch, '\0', sizeof(*cch)); +} + +/** + * cli_ch_esc() - Process a character in an ongoing escape sequence + * + * @cch: State information + * @ichar: Character to process + * @actp: Returns the action to take + * Returns: Output character if *actp is ESC_CONVERTED, else 0 + */ +static int cli_ch_esc(struct cli_ch_state *cch, int ichar, + enum cli_esc_state_t *actp) +{ + enum cli_esc_state_t act = ESC_REJECT; + + switch (cch->esc_len) { + case 1: + if (ichar == '[' || ichar == 'O') + act = ESC_SAVE; + break; + case 2: + switch (ichar) { + case 'D': /* <- key */ + ichar = CTL_CH('b'); + act = ESC_CONVERTED; + break; /* pass off to ^B handler */ + case 'C': /* -> key */ + ichar = CTL_CH('f'); + act = ESC_CONVERTED; + break; /* pass off to ^F handler */ + case 'H': /* Home key */ + ichar = CTL_CH('a'); + act = ESC_CONVERTED; + break; /* pass off to ^A handler */ + case 'F': /* End key */ + ichar = CTL_CH('e'); + act = ESC_CONVERTED; + break; /* pass off to ^E handler */ + case 'A': /* up arrow */ + ichar = CTL_CH('p'); + act = ESC_CONVERTED; + break; /* pass off to ^P handler */ + case 'B': /* down arrow */ + ichar = CTL_CH('n'); + act = ESC_CONVERTED; + break; /* pass off to ^N handler */ + case '1': + case '2': + case '3': + case '4': + case '7': + case '8': + if (cch->esc_save[1] == '[') { + /* see if next character is ~ */ + act = ESC_SAVE; + } + break; + } + break; + case 3: + switch (ichar) { + case '~': + switch (cch->esc_save[2]) { + case '3': /* Delete key */ + ichar = CTL_CH('d'); + act = ESC_CONVERTED; + break; /* pass to ^D handler */ + case '1': /* Home key */ + case '7': + ichar = CTL_CH('a'); + act = ESC_CONVERTED; + break; /* pass to ^A handler */ + case '4': /* End key */ + case '8': + ichar = CTL_CH('e'); + act = ESC_CONVERTED; + break; /* pass to ^E handler */ + } + break; + case '0': + if (cch->esc_save[2] == '2') + act = ESC_SAVE; + break; + } + break; + case 4: + switch (ichar) { + case '0': + case '1': + act = ESC_SAVE; + break; /* bracketed paste */ + } + break; + case 5: + if (ichar == '~') { /* bracketed paste */ + ichar = 0; + act = ESC_CONVERTED; + } + } + + *actp = act; + + return act == ESC_CONVERTED ? ichar : 0; +} + +int cli_ch_process(struct cli_ch_state *cch, int ichar) +{ + /* + * ichar=0x0 when error occurs in U-Boot getchar() or when the caller + * wants to check if there are more characters saved in the escape + * sequence + */ + if (!ichar) { + if (cch->emit_upto) { + if (cch->emit_upto < cch->esc_len) + return cch->esc_save[cch->emit_upto++]; + cch->emit_upto = 0; + } + return 0; + } else if (ichar == -ETIMEDOUT) { + /* + * If we are in an escape sequence but nothing has followed the + * Escape character, then the user probably just pressed the + * Escape key. Return it and clear the sequence. + */ + if (cch->esc_len) { + cch->esc_len = 0; + return '\e'; + } + + /* Otherwise there is nothing to return */ + return 0; + } + + if (ichar == '\n' || ichar == '\r') + return '\n'; + + /* handle standard linux xterm esc sequences for arrow key, etc. */ + if (cch->esc_len != 0) { + enum cli_esc_state_t act; + + ichar = cli_ch_esc(cch, ichar, &act); + + switch (act) { + case ESC_SAVE: + /* save this character and return nothing */ + cch->esc_save[cch->esc_len++] = ichar; + return 0; + case ESC_REJECT: + /* + * invalid escape sequence, start returning the + * characters in it + */ + cch->esc_save[cch->esc_len++] = ichar; + return cch->esc_save[cch->emit_upto++]; + case ESC_CONVERTED: + /* valid escape sequence, return the resulting char */ + cch->esc_len = 0; + return ichar; + } + } + + if (ichar == '\e') { + if (!cch->esc_len) { + cch->esc_save[cch->esc_len] = ichar; + cch->esc_len = 1; + } else { + puts("impossible condition #876\n"); + cch->esc_len = 0; + } + return 0; + } + + return ichar; +} diff --git a/common/cli_readline.c b/common/cli_readline.c index d6444f5fc1d..709e9c3d38b 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -62,7 +62,6 @@ static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen) #define putnstr(str, n) printf("%.*s", (int)n, str) -#define CTL_CH(c) ((c) - 'a' + 1) #define CTL_BACKSPACE ('\b') #define DEL ((char)255) #define DEL7 ((char)127) @@ -252,156 +251,53 @@ static void cread_add_str(char *str, int strsize, int insert, static int cread_line(const char *const prompt, char *buf, unsigned int *len, int timeout) { + struct cli_ch_state s_cch, *cch = &s_cch; unsigned long num = 0; unsigned long eol_num = 0; unsigned long wlen; char ichar; int insert = 1; - int esc_len = 0; - char esc_save[8]; int init_len = strlen(buf); int first = 1; + cli_ch_init(cch); + if (init_len) cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); while (1) { - if (bootretry_tstc_timeout()) - return -2; /* timed out */ - if (first && timeout) { - uint64_t etime = endtick(timeout); - - while (!tstc()) { /* while no incoming data */ - if (get_ticks() >= etime) - return -2; /* timed out */ - schedule(); + /* Check for saved characters */ + ichar = cli_ch_process(cch, 0); + + if (!ichar) { + if (bootretry_tstc_timeout()) + return -2; /* timed out */ + if (first && timeout) { + u64 etime = endtick(timeout); + + while (!tstc()) { /* while no incoming data */ + if (get_ticks() >= etime) + return -2; /* timed out */ + schedule(); + } + first = 0; } - first = 0; + + ichar = getcmd_getch(); } - ichar = getcmd_getch(); + ichar = cli_ch_process(cch, ichar); /* ichar=0x0 when error occurs in U-Boot getc */ if (!ichar) continue; - if ((ichar == '\n') || (ichar == '\r')) { + if (ichar == '\n') { putc('\n'); break; } - /* - * handle standard linux xterm esc sequences for arrow key, etc. - */ - if (esc_len != 0) { - enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT; - - if (esc_len == 1) { - if (ichar == '[' || ichar == 'O') - act = ESC_SAVE; - } else if (esc_len == 2) { - switch (ichar) { - case 'D': /* <- key */ - ichar = CTL_CH('b'); - act = ESC_CONVERTED; - break; /* pass off to ^B handler */ - case 'C': /* -> key */ - ichar = CTL_CH('f'); - act = ESC_CONVERTED; - break; /* pass off to ^F handler */ - case 'H': /* Home key */ - ichar = CTL_CH('a'); - act = ESC_CONVERTED; - break; /* pass off to ^A handler */ - case 'F': /* End key */ - ichar = CTL_CH('e'); - act = ESC_CONVERTED; - break; /* pass off to ^E handler */ - case 'A': /* up arrow */ - ichar = CTL_CH('p'); - act = ESC_CONVERTED; - break; /* pass off to ^P handler */ - case 'B': /* down arrow */ - ichar = CTL_CH('n'); - act = ESC_CONVERTED; - break; /* pass off to ^N handler */ - case '1': - case '2': - case '3': - case '4': - case '7': - case '8': - if (esc_save[1] == '[') { - /* see if next character is ~ */ - act = ESC_SAVE; - } - break; - } - } else if (esc_len == 3) { - switch (ichar) { - case '~': - switch (esc_save[2]) { - case '3': /* Delete key */ - ichar = CTL_CH('d'); - act = ESC_CONVERTED; - break; /* pass to ^D handler */ - case '1': /* Home key */ - case '7': - ichar = CTL_CH('a'); - act = ESC_CONVERTED; - break; /* pass to ^A handler */ - case '4': /* End key */ - case '8': - ichar = CTL_CH('e'); - act = ESC_CONVERTED; - break; /* pass to ^E handler */ - } - break; - case '0': - if (esc_save[2] == '2') - act = ESC_SAVE; - break; - } - } else if (esc_len == 4) { - switch (ichar) { - case '0': - case '1': - act = ESC_SAVE; - break; /* bracketed paste */ - } - } else if (esc_len == 5) { - if (ichar == '~') { /* bracketed paste */ - ichar = 0; - act = ESC_CONVERTED; - } - } - switch (act) { - case ESC_SAVE: - esc_save[esc_len++] = ichar; - continue; - case ESC_REJECT: - esc_save[esc_len++] = ichar; - cread_add_str(esc_save, esc_len, insert, - &num, &eol_num, buf, *len); - esc_len = 0; - continue; - case ESC_CONVERTED: - esc_len = 0; - break; - } - } - switch (ichar) { - case 0x1b: - if (esc_len == 0) { - esc_save[esc_len] = ichar; - esc_len = 1; - } else { - puts("impossible condition #876\n"); - esc_len = 0; - } - break; - case CTL_CH('a'): BEGINNING_OF_LINE(); break; @@ -470,8 +366,6 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len, { char *hline; - esc_len = 0; - if (ichar == CTL_CH('p')) hline = hist_prev(); else -- cgit v1.2.3 From 2da4a15e7e947d2d304ec1ba392556c3e0393e13 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:22 -0600 Subject: menu: Rename KEY_... to BKEY_... This enum values conflict with linux/input.h so rename them. Signed-off-by: Simon Glass --- common/menu.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'common') diff --git a/common/menu.c b/common/menu.c index 8fe00965c0c..a245c5a9c6c 100644 --- a/common/menu.c +++ b/common/menu.c @@ -446,16 +446,16 @@ void bootmenu_autoboot_loop(struct bootmenu_data *menu, switch (c) { case '\e': *esc = 1; - *key = KEY_NONE; + *key = BKEY_NONE; break; case '\r': - *key = KEY_SELECT; + *key = BKEY_SELECT; break; case 0x3: /* ^C */ - *key = KEY_QUIT; + *key = BKEY_QUIT; break; default: - *key = KEY_NONE; + *key = BKEY_NONE; break; } @@ -471,7 +471,7 @@ void bootmenu_autoboot_loop(struct bootmenu_data *menu, printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); if (menu->delay == 0) - *key = KEY_SELECT; + *key = BKEY_SELECT; } void bootmenu_loop(struct bootmenu_data *menu, @@ -503,17 +503,17 @@ void bootmenu_loop(struct bootmenu_data *menu, /* First char of ANSI escape sequence '\e' */ if (c == '\e') { *esc = 1; - *key = KEY_NONE; + *key = BKEY_NONE; } break; case 1: /* Second char of ANSI '[' */ if (c == '[') { *esc = 2; - *key = KEY_NONE; + *key = BKEY_NONE; } else { /* Alone ESC key was pressed */ - *key = KEY_QUIT; + *key = BKEY_QUIT; *esc = (c == '\e') ? 1 : 0; } break; @@ -522,7 +522,7 @@ void bootmenu_loop(struct bootmenu_data *menu, /* Third char of ANSI (number '1') - optional */ if (*esc == 2 && c == '1') { *esc = 3; - *key = KEY_NONE; + *key = BKEY_NONE; break; } @@ -530,31 +530,31 @@ void bootmenu_loop(struct bootmenu_data *menu, /* ANSI 'A' - key up was pressed */ if (c == 'A') - *key = KEY_UP; + *key = BKEY_UP; /* ANSI 'B' - key down was pressed */ else if (c == 'B') - *key = KEY_DOWN; + *key = BKEY_DOWN; /* other key was pressed */ else - *key = KEY_NONE; + *key = BKEY_NONE; break; } /* enter key was pressed */ if (c == '\r') - *key = KEY_SELECT; + *key = BKEY_SELECT; /* ^C was pressed */ if (c == 0x3) - *key = KEY_QUIT; + *key = BKEY_QUIT; if (c == '+') - *key = KEY_PLUS; + *key = BKEY_PLUS; if (c == '-') - *key = KEY_MINUS; + *key = BKEY_MINUS; if (c == ' ') - *key = KEY_SPACE; + *key = BKEY_SPACE; } -- cgit v1.2.3 From 5712976b26f7865f348aba51c9fa367c456e1795 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:23 -0600 Subject: menu: Update bootmenu_autoboot_loop() to return the code Use the return value to save having to pass around a pointer. This also resolves any ambiguity about what *key contains when the function is called. Signed-off-by: Simon Glass --- common/menu.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'common') diff --git a/common/menu.c b/common/menu.c index a245c5a9c6c..bafc8470d7d 100644 --- a/common/menu.c +++ b/common/menu.c @@ -425,9 +425,9 @@ int menu_destroy(struct menu *m) return 1; } -void bootmenu_autoboot_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) +enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) { + enum bootmenu_key key = BKEY_NONE; int i, c; while (menu->delay > 0) { @@ -446,16 +446,16 @@ void bootmenu_autoboot_loop(struct bootmenu_data *menu, switch (c) { case '\e': *esc = 1; - *key = BKEY_NONE; + key = BKEY_NONE; break; case '\r': - *key = BKEY_SELECT; + key = BKEY_SELECT; break; case 0x3: /* ^C */ - *key = BKEY_QUIT; + key = BKEY_QUIT; break; default: - *key = BKEY_NONE; + key = BKEY_NONE; break; } @@ -471,7 +471,9 @@ void bootmenu_autoboot_loop(struct bootmenu_data *menu, printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); if (menu->delay == 0) - *key = BKEY_SELECT; + key = BKEY_SELECT; + + return key; } void bootmenu_loop(struct bootmenu_data *menu, -- cgit v1.2.3 From d0ca98dbd99c2534c9e96e4c226e52ab80f2248f Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:24 -0600 Subject: menu: Update bootmenu_loop() to return the code Use the return value to save having to pass around a pointer. This also resolves any ambiguity about what *key contains when the function is called. Signed-off-by: Simon Glass --- common/menu.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'common') diff --git a/common/menu.c b/common/menu.c index bafc8470d7d..6842f5409d6 100644 --- a/common/menu.c +++ b/common/menu.c @@ -476,9 +476,9 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) return key; } -void bootmenu_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) +enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc) { + enum bootmenu_key key = BKEY_NONE; int c; if (*esc == 1) { @@ -505,17 +505,17 @@ void bootmenu_loop(struct bootmenu_data *menu, /* First char of ANSI escape sequence '\e' */ if (c == '\e') { *esc = 1; - *key = BKEY_NONE; + key = BKEY_NONE; } break; case 1: /* Second char of ANSI '[' */ if (c == '[') { *esc = 2; - *key = BKEY_NONE; + key = BKEY_NONE; } else { /* Alone ESC key was pressed */ - *key = BKEY_QUIT; + key = BKEY_QUIT; *esc = (c == '\e') ? 1 : 0; } break; @@ -524,7 +524,7 @@ void bootmenu_loop(struct bootmenu_data *menu, /* Third char of ANSI (number '1') - optional */ if (*esc == 2 && c == '1') { *esc = 3; - *key = BKEY_NONE; + key = BKEY_NONE; break; } @@ -532,31 +532,33 @@ void bootmenu_loop(struct bootmenu_data *menu, /* ANSI 'A' - key up was pressed */ if (c == 'A') - *key = BKEY_UP; + key = BKEY_UP; /* ANSI 'B' - key down was pressed */ else if (c == 'B') - *key = BKEY_DOWN; + key = BKEY_DOWN; /* other key was pressed */ else - *key = BKEY_NONE; + key = BKEY_NONE; break; } /* enter key was pressed */ if (c == '\r') - *key = BKEY_SELECT; + key = BKEY_SELECT; /* ^C was pressed */ if (c == 0x3) - *key = BKEY_QUIT; + key = BKEY_QUIT; if (c == '+') - *key = BKEY_PLUS; + key = BKEY_PLUS; if (c == '-') - *key = BKEY_MINUS; + key = BKEY_MINUS; if (c == ' ') - *key = BKEY_SPACE; + key = BKEY_SPACE; + + return key; } -- cgit v1.2.3 From 86cc3c5215fc6e3c2cb77ee162c22ad91dbfaff5 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:25 -0600 Subject: menu: Use a switch statement Convert the long line of if() statements to a switch() since this makes better use of the C language. Signed-off-by: Simon Glass --- common/menu.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'common') diff --git a/common/menu.c b/common/menu.c index 6842f5409d6..7db98942a61 100644 --- a/common/menu.c +++ b/common/menu.c @@ -543,22 +543,31 @@ enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc) break; } - /* enter key was pressed */ - if (c == '\r') + switch (c) { + case '\r': + /* enter key was pressed */ key = BKEY_SELECT; - - /* ^C was pressed */ - if (c == 0x3) + break; + case CTL_CH('c'): + /* ^C was pressed */ key = BKEY_QUIT; - - if (c == '+') + break; + case CTL_CH('p'): + key = BKEY_UP; + break; + case CTL_CH('n'): + key = BKEY_DOWN; + break; + case '+': key = BKEY_PLUS; - - if (c == '-') + break; + case '-': key = BKEY_MINUS; - - if (c == ' ') + break; + case ' ': key = BKEY_SPACE; + break; + } return key; } -- cgit v1.2.3 From 32bab0eae51b55898d1e2804e6614d9143840581 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:26 -0600 Subject: menu: Make use of CLI character processing Avoid duplicating some of the escape-sequence processing here and use the CLI function instead. Signed-off-by: Simon Glass --- common/cli_getch.c | 12 ++++--- common/menu.c | 92 +++++++++++++++--------------------------------------- 2 files changed, 34 insertions(+), 70 deletions(-) (limited to 'common') diff --git a/common/cli_getch.c b/common/cli_getch.c index 9eeea7fef29..87c23edcf4b 100644 --- a/common/cli_getch.c +++ b/common/cli_getch.c @@ -140,10 +140,11 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar) * sequence */ if (!ichar) { - if (cch->emit_upto) { + if (cch->emitting) { if (cch->emit_upto < cch->esc_len) return cch->esc_save[cch->emit_upto++]; cch->emit_upto = 0; + cch->emitting = false; } return 0; } else if (ichar == -ETIMEDOUT) { @@ -174,18 +175,21 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar) case ESC_SAVE: /* save this character and return nothing */ cch->esc_save[cch->esc_len++] = ichar; - return 0; + ichar = 0; + break; case ESC_REJECT: /* * invalid escape sequence, start returning the * characters in it */ cch->esc_save[cch->esc_len++] = ichar; - return cch->esc_save[cch->emit_upto++]; + ichar = cch->esc_save[cch->emit_upto++]; + cch->emitting = true; + break; case ESC_CONVERTED: /* valid escape sequence, return the resulting char */ cch->esc_len = 0; - return ichar; + break; } } diff --git a/common/menu.c b/common/menu.c index 7db98942a61..45f36ae3ede 100644 --- a/common/menu.c +++ b/common/menu.c @@ -15,6 +15,8 @@ #include "menu.h" +#define ansi 0 + /* * Internally, each item in a menu is represented by a struct menu_item. * @@ -425,15 +427,19 @@ int menu_destroy(struct menu *m) return 1; } -enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) +enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, + struct cli_ch_state *cch) { enum bootmenu_key key = BKEY_NONE; int i, c; while (menu->delay > 0) { - printf(ANSI_CURSOR_POSITION, menu->count + 5, 3); + if (ansi) + printf(ANSI_CURSOR_POSITION, menu->count + 5, 3); printf("Hit any key to stop autoboot: %d ", menu->delay); for (i = 0; i < 100; ++i) { + int ichar; + if (!tstc()) { schedule(); mdelay(10); @@ -443,12 +449,13 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) menu->delay = -1; c = getchar(); - switch (c) { - case '\e': - *esc = 1; + ichar = cli_ch_process(cch, c); + + switch (ichar) { + case '\0': key = BKEY_NONE; break; - case '\r': + case '\n': key = BKEY_SELECT; break; case 0x3: /* ^C */ @@ -458,7 +465,6 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) key = BKEY_NONE; break; } - break; } @@ -468,7 +474,8 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) --menu->delay; } - printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); + if (ansi) + printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); if (menu->delay == 0) key = BKEY_SELECT; @@ -476,79 +483,32 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) return key; } -enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc) +enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, + struct cli_ch_state *cch) { enum bootmenu_key key = BKEY_NONE; int c; - if (*esc == 1) { - if (tstc()) { - c = getchar(); - } else { + c = cli_ch_process(cch, 0); + if (!c) { + while (!c && !tstc()) { schedule(); mdelay(10); - if (tstc()) - c = getchar(); - else - c = '\e'; + c = cli_ch_process(cch, -ETIMEDOUT); } - } else { - while (!tstc()) { - schedule(); - mdelay(10); - } - c = getchar(); - } - - switch (*esc) { - case 0: - /* First char of ANSI escape sequence '\e' */ - if (c == '\e') { - *esc = 1; - key = BKEY_NONE; - } - break; - case 1: - /* Second char of ANSI '[' */ - if (c == '[') { - *esc = 2; - key = BKEY_NONE; - } else { - /* Alone ESC key was pressed */ - key = BKEY_QUIT; - *esc = (c == '\e') ? 1 : 0; - } - break; - case 2: - case 3: - /* Third char of ANSI (number '1') - optional */ - if (*esc == 2 && c == '1') { - *esc = 3; - key = BKEY_NONE; - break; + if (!c) { + c = getchar(); + c = cli_ch_process(cch, c); } - - *esc = 0; - - /* ANSI 'A' - key up was pressed */ - if (c == 'A') - key = BKEY_UP; - /* ANSI 'B' - key down was pressed */ - else if (c == 'B') - key = BKEY_DOWN; - /* other key was pressed */ - else - key = BKEY_NONE; - - break; } switch (c) { - case '\r': + case '\n': /* enter key was pressed */ key = BKEY_SELECT; break; case CTL_CH('c'): + case '\e': /* ^C was pressed */ key = BKEY_QUIT; break; -- cgit v1.2.3 From 30f3333d8860fd97e131e24ad33a80f4d46e98b1 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:28 -0600 Subject: image: Move common image code to image_board and command We should use the cmd/ directory for commands rather than for common code used elsewhere in U-Boot. Move the common 'source' code into image-board.c to achieve this. The image_source_script() function needs to call run_command_list() so seems to belong better in the command library. Move and rename it. Signed-off-by: Simon Glass --- common/command.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'common') diff --git a/common/command.c b/common/command.c index 41c91c6d8c8..7a86bd76a4c 100644 --- a/common/command.c +++ b/common/command.c @@ -13,7 +13,9 @@ #include #include #include +#include #include +#include #include #include @@ -654,3 +656,20 @@ int cmd_process_error(struct cmd_tbl *cmdtp, int err) return CMD_RET_SUCCESS; } + +int cmd_source_script(ulong addr, const char *fit_uname, const char *confname) +{ + char *data; + void *buf; + uint len; + int ret; + + buf = map_sysmem(addr, 0); + ret = image_locate_script(buf, 0, fit_uname, confname, &data, &len); + unmap_sysmem(buf); + if (ret) + return CMD_RET_FAILURE; + + debug("** Script length: %d\n", len); + return run_command_list(data, len, 0); +} -- cgit v1.2.3 From 9e7ac0b0be5cb663e539716554d66f8f0890ca83 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:35 -0600 Subject: menu: Factor out menu-keypress decoding Move this code into a separate function so that it can be used in the new VBE menu. Signed-off-by: Simon Glass --- common/menu.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'common') diff --git a/common/menu.c b/common/menu.c index 45f36ae3ede..cdcdbb2a185 100644 --- a/common/menu.c +++ b/common/menu.c @@ -483,26 +483,11 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, return key; } -enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, - struct cli_ch_state *cch) +enum bootmenu_key bootmenu_conv_key(int ichar) { - enum bootmenu_key key = BKEY_NONE; - int c; - - c = cli_ch_process(cch, 0); - if (!c) { - while (!c && !tstc()) { - schedule(); - mdelay(10); - c = cli_ch_process(cch, -ETIMEDOUT); - } - if (!c) { - c = getchar(); - c = cli_ch_process(cch, c); - } - } + enum bootmenu_key key; - switch (c) { + switch (ichar) { case '\n': /* enter key was pressed */ key = BKEY_SELECT; @@ -527,7 +512,34 @@ enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, case ' ': key = BKEY_SPACE; break; + default: + key = BKEY_NONE; + break; + } + + return key; +} + +enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, + struct cli_ch_state *cch) +{ + enum bootmenu_key key; + int c; + + c = cli_ch_process(cch, 0); + if (!c) { + while (!c && !tstc()) { + schedule(); + mdelay(10); + c = cli_ch_process(cch, -ETIMEDOUT); + } + if (!c) { + c = getchar(); + c = cli_ch_process(cch, c); + } } + key = bootmenu_conv_key(c); + return key; } -- cgit v1.2.3