Skip to contentSkip to conversation

cli: Correct several bugs in cli_getch()

PATCH RESEND In review Simon Glass 3 participants 6 messages Mar 27, 2023 – Mar 30, 2023

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v3 1/2 cli: Correct several bugs in cli_getch()

This function does not behave as expected when unknown escape sequences are sent to it:

- it fails to store (and thus echo) the last character of the invalid sequence
- it fails to set esc_len to 0 when it finishes emitting the invalid sequence, meaning that the following character will appear to be part of a new escape sequence
- it processes the first character of the rejected sequence as a valid character, just starting the sequence all over again

The last two bugs conspire to produce an "impossible condition #876" message which is the main symptom of this behaviour.

Fix these bugs and add a test to verify the behaviour.

---

(no changes since v1)

 common/cli_getch.c   |  5 +++--
 test/common/Makefile |  1 +
 test/common/cread.c  | 48 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 test/common/cread.c
common/cli_getch.c +3 -2
index 87c23edcf4b4..61d4cb261b81 100644
@@ -129,7 +129,7 @@ static int cli_ch_esc(struct cli_ch_state *cch, int ichar,
 
 	*actp = act;
 
-	return act == ESC_CONVERTED ? ichar : 0;
+	return ichar;
 }
 
 int cli_ch_process(struct cli_ch_state *cch, int ichar)
@@ -145,6 +145,7 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar)
 				return cch->esc_save[cch->emit_upto++];
 			cch->emit_upto = 0;
 			cch->emitting = false;
+			cch->esc_len = 0;
 		}
 		return 0;
 	} else if (ichar == -ETIMEDOUT) {
@@ -185,7 +186,7 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar)
 			cch->esc_save[cch->esc_len++] = ichar;
 			ichar = cch->esc_save[cch->emit_upto++];
 			cch->emitting = true;
-			break;
+			return ichar;
 		case ESC_CONVERTED:
 			/* valid escape sequence, return the resulting char */
 			cch->esc_len = 0;
test/common/Makefile +1 -0
index cc918f64e544..a5ab10f6f69b 100644
@@ -3,3 +3,4 @@ obj-y += cmd_ut_common.o
 obj-$(CONFIG_AUTOBOOT) += test_autoboot.o
 obj-$(CONFIG_CYCLIC) += cyclic.o
 obj-$(CONFIG_EVENT) += event.o
+obj-y += cread.o
test/common/cread.c new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.40.0.348.gf938b09366-goog
#1 lore raw

PATCH RESEND v3 2/2 cli: Correct handling of invalid escape sequences in cread_line()

The second call to cli_ch_process() is in the wrong place, meaning that the one of the characters of an invalid escape sequence is swallowed instead of being returned.

Fix the bug and add a test to cover this.

This behaviour matches that of the code before cli_getch() was introduced. This was verified on the commit before b08e9d4b66 i.e.:

7d850f85aad ("sandbox: Enable mmc command and legacy images")

---

Changes in v3:
- Update commit message

Changes in v2:
- Fix a failling test

 common/cli_readline.c |  3 +--
 include/cli.h         |  4 ++--
 test/common/cread.c   | 45 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 4 deletions(-)
common/cli_readline.c +1 -2
index 709e9c3d38b1..e83743e90c92 100644
@@ -284,10 +284,9 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 			}
 
 			ichar = getcmd_getch();
+			ichar = cli_ch_process(cch, ichar);
 		}
 
-		ichar = cli_ch_process(cch, ichar);
-
 		/* ichar=0x0 when error occurs in U-Boot getc */
 		if (!ichar)
 			continue;
include/cli.h +2 -2
index c777c90313f2..094a6602d70e 100644
@@ -98,8 +98,8 @@ int cli_readline(const char *const prompt);
  *
  * @prompt:	Prompt to display
  * @buffer:	Place to put the line that is entered
- * @timeout:	Timeout in milliseconds, 0 if none
- * Return: command line length excluding terminator, or -ve on error: of the
+ * @timeout:	Timeout in seconds, 0 if none
+ * Return: command line length excluding terminator, or -ve on error: if the
  * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
  * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
  * -1 is returned.
test/common/cread.c +45 -0

48 lines not on this page · raw message

Signature · 1 line
2.40.0.348.gf938b09366-goog
#2 lore raw

Re: [RESEND PATCH v3 2/2] cli: Correct handling of invalid escape sequences in cread_line()

Quoted 6 lines · Simon Glass

The second call to cli_ch_process() is in the wrong place, meaning that the one of the characters of an invalid escape sequence is swallowed instead of being returned.

Fix the bug and add a test to cover this.

Quoted 8 lines

This behaviour matches that of the code before cli_getch() was introduced. This was verified on the commit before b08e9d4b66 i.e.:

7d850f85aad ("sandbox: Enable mmc command and legacy images")

---

Thanks for the fix. Some strange behavior still remains like <SHIFT><CTRL><F4> giving '6S' on the command line. But at least no error code.

Best regards

Heinrich

#3 lore raw

Re: [RESEND PATCH v3 2/2] cli: Correct handling of invalid escape sequences in cread_line()

Hi Heinrich,

Quoted 13 lines · Heinrich Schuchardt
Quoted 11 lines · Simon Glass

The second call to cli_ch_process() is in the wrong place, meaning that the one of the characters of an invalid escape sequence is swallowed instead of being returned.

Fix the bug and add a test to cover this.

This behaviour matches that of the code before cli_getch() was introduced. This was verified on the commit before b08e9d4b66 i.e.:

7d850f85aad ("sandbox: Enable mmc command and legacy images")

Quoted 7 lines
Quoted 3 lines

---

Thanks for the fix. Some strange behavior still remains like <SHIFT><CTRL><F4> giving '6S' on the command line. But at least no error code.

Yes, this matches the previous behaviour, which has existed a long time. We could easily suppress echoing of unknown escape sequence. Should we suppress them being entered also, so that U-Boot ignores them? I'm really not sure. There was a reverted patch[1] which indicates that this is not trivial to figure out.

Regards,
Simon

[1] d2e64d29c44d cli_readline: Only insert printable chars

#4 lore raw

Re: [RESEND PATCH v3 2/2] cli: Correct handling of invalid escape sequences in cread_line()

Am 29. März 2023 22:02:11 MESZ schrieb Simon Glass <[email protected]>:

Quoted 24 lines

Hi Heinrich,

Quoted 13 lines · Heinrich Schuchardt
Quoted 11 lines · Simon Glass

The second call to cli_ch_process() is in the wrong place, meaning that the one of the characters of an invalid escape sequence is swallowed instead of being returned.

Fix the bug and add a test to cover this.

This behaviour matches that of the code before cli_getch() was introduced. This was verified on the commit before b08e9d4b66 i.e.:

7d850f85aad ("sandbox: Enable mmc command and legacy images")

Quoted 7 lines
Quoted 3 lines

---

Thanks for the fix. Some strange behavior still remains like <SHIFT><CTRL><F4> giving '6S' on the command line. But at least no error code.

Quoted 5 lines

Yes, this matches the previous behaviour, which has existed a long time. We could easily suppress echoing of unknown escape sequence. Should we suppress them being entered also, so that U-Boot ignores them? I'm really not sure. There was a reverted patch[1] which indicates that this is not trivial to figure out.

We already have code in lib/efi_loader/efi_condole.c that supports many more escape sequences. We should deduplicate the code.

Best regards

Heinrich

Quoted 5 lines

Regards,
Simon

[1] d2e64d29c44d cli_readline: Only insert printable chars

#5 lore raw
In reply to Simon Glass
Quoted 11 lines · Simon Glass

This function does not behave as expected when unknown escape sequences are sent to it:

- it fails to store (and thus echo) the last character of the invalid sequence
- it fails to set esc_len to 0 when it finishes emitting the invalid sequence, meaning that the following character will appear to be part of a new escape sequence
- it processes the first character of the rejected sequence as a valid character, just starting the sequence all over again

Quoted 1 line

[...]

Applied to u-boot/master, thanks!

Signature · 1 line
Tom