efi_loader: Fix serial console size detection
PATCH 1/1 efi_loader: Fix serial console size detection
From: Matthias Brugger <[email protected]>
Function term_read_reply tries to read from the serial console until the end_char was read. This can hang forever if we are, for some reason, not be able to read the full response (e.g. serial buffer too small, frame error). This patch moves the timeout detection into term_read_reply to assure we will make progress.
--- lib/efi_loader/efi_console.c | 63 +++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 26 deletions(-)
lib/efi_loader/efi_console.c +37 -26
@@ -62,6 +62,16 @@ static struct simple_text_output_mode efi_con_mode = { .cursor_visible = 1, }; +static int term_get_char (char *c) +{ +if (tstc ()) { + *c =getc (); +return 0 ; + } + +return 1 ; +} +/* * Receive and parse a reply from the terminal. *
@@ -74,32 +84,42 @@ static int term_read_reply(int *n, int num, char end_char) {char c;int i =0 ; +u64 timeout; - c =getc (); -if (c != cESC) +/* Allow up to one second for the response */ + timeout =timer_get_us () +1000000 ; +while (!tstc ()) +if (timer_get_us () > timeout) +return -1 ; + +if (term_get_char (&c) || c != cESC)return -1 ; - c =getc (); -if (c !='[' ) + +if (term_get_char (&c) || c !='[' )return -1 ; n[0 ] =0 ;while (1 ) { - c =getc (); -if (c ==';' ) { - i++; -if (i >= num) +if (!term_get_char (&c)) { +if (c ==';' ) { + i++; +if (i >= num) +return -1 ; + n[i] =0 ; +continue ; + }else if (c == end_char) { +break ; + }else if (c >'9' || c <'0' ) {return -1 ; - n[i] =0 ; -continue ; - }else if (c == end_char) { -break ; - }else if (c >'9' || c <'0' ) { -return -1 ; + } + +/* Read one more decimal position */ + n[i] *=10 ; + n[i] += c -'0' ; } -/* Read one more decimal position */ - n[i] *=10 ; - n[i] += c -'0' ; +if (timer_get_us () > timeout) +return -1 ; }if (i != num -1 )return -1 ;
@@ -196,7 +216,6 @@ static int query_console_serial(int *rows, int *cols) {int ret =0 ;int n[2 ]; -u64 timeout;/* Empty input buffer */ while (tstc ())
@@ -216,14 +235,6 @@ static int query_console_serial(int *rows, int *cols)ESC "[999;999H" /* Move to bottom right corner */ ESC "[6n"); /* Query cursor position */ -/* Allow up to one second for a response */ - timeout =timer_get_us () +1000000 ; -while (!tstc ()) -if (timer_get_us () > timeout) { - ret =-1 ; - goto out; - } -/* Read {rows,cols} */ if (term_read_reply (n,2 ,'R' )) { ret =1 ;
Signature · 1 line
2.20.1
PATCH 1/1 efi_loader: Fix serial console size detection
Quoted 10 lines · matthias.bgg at kernel.org
From: Matthias Brugger <[email protected]>
Function
term_read_replytries to read from the serial console until theend_charwas read. This can hang forever if we are, for some reason, not be able to read the full response (e.g. serial buffer too small, frame error). This patch moves the timeout detection intoterm_read_replyto assure we will make progress.Fixes 6bb591f704 ("efi_loader: query serial console size reliably")Signed-off-by Matthias Brugger <[email protected]>
Thanks Matthias for the patch.
The general direction is right. Yet I would prefer if you could move the waiting loop as described below.
Quoted 10 lines
--- lib/efi_loader/efi_console.c | 63 +++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 26 deletions(-)lib/efi_loader/efi_console.c +0
-0@@ -62,6 +62,16 @@ static struct simple_text_output_mode efi_con_mode = { .cursor_visible = 1,
lib/efi_loader/efi_console.c static struct simple_text_output_mode efi_con_mode = { · line 72
Quoted 10 lines
lib/efi_loader/efi_console.c +8
-0}; +static int term_get_char (char *c) +{ +if (tstc ()) { + *c =getc (); +return 0 ; + } + +return 1 ;
The function signals an error if the character to read is not yet in the UART buffer. I think it would be preferable to put the waiting time (100 ms is sufficient at 110 baud and above) into this function instead. This has the following advantages:
- We would need only one waiting loop.
- We could reuse the function in function efi_cin_read_key() where we have another chance to hang waiting for a character.
Quoted 6 lines
lib/efi_loader/efi_console.c +2
-0+} +/* * Receive and parse a reply from the terminal. *
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 90
Quoted 9 lines
lib/efi_loader/efi_console.c +3
-2@@ -74,32 +84,42 @@ static int term_read_reply(int *n, int num, char end_char) {char c;int i =0 ; +u64 timeout; - c =getc (); -if (c != cESC) +/* Allow up to one second for the response */ + timeout =timer_get_us () +1000000 ;
Even at 110 baud a waiting time of 100 ms is sufficient.
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 93
Quoted 3 lines
lib/efi_loader/efi_console.c +3
-0+while (!tstc ()) +if (timer_get_us () > timeout) +return -1 ;
This loop could be moved to term_get_char().
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 98
Quoted 7 lines
lib/efi_loader/efi_console.c +4
-2+ +if (term_get_char (&c) || c != cESC)return -1 ; - c =getc (); -if (c !='[' ) + +if (term_get_char (&c) || c !='[' )
We should allow time for this character to arrive.
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char)
Quoted 8 lines
lib/efi_loader/efi_console.c +0
-4return -1 ; n[0 ] =0 ;while (1 ) { - c =getc (); -if (c ==';' ) { - i++; - if (i >= num)> + if (!term_get_char(&c)) {
On loop entry there is no wait before this term_get_char().
Best regards
Heinrich
Quoted 55 lines
lib/efi_loader/efi_console.c +16
-18+if (c ==';' ) { + i++; +if (i >= num) +return -1 ; + n[i] =0 ; +continue ; + }else if (c == end_char) { +break ; + }else if (c >'9' || c <'0' ) {return -1 ; - n[i] =0 ; -continue ; - }else if (c == end_char) { -break ; - }else if (c >'9' || c <'0' ) { -return -1 ; + } + +/* Read one more decimal position */ + n[i] *=10 ; + n[i] += c -'0' ; } -/* Read one more decimal position */ - n[i] *=10 ; - n[i] += c -'0' ; +if (timer_get_us () > timeout) +return -1 ; }if (i != num -1 )return -1 ;@@ -196,7 +216,6 @@ static int query_console_serial(int *rows, int *cols) {int ret =0 ;int n[2 ]; -u64 timeout;/* Empty input buffer */ while (tstc ())@@ -216,14 +235,6 @@ static int query_console_serial(int *rows, int *cols)ESC "[999;999H" /* Move to bottom right corner */ ESC "[6n"); /* Query cursor position */ -/* Allow up to one second for a response */ - timeout =timer_get_us () +1000000 ; -while (!tstc ()) -if (timer_get_us () > timeout) { - ret =-1 ; - goto out; - } -/* Read {rows,cols} */ if (term_read_reply (n,2 ,'R' )) { ret =1 ;
PATCH 1/1 efi_loader: Fix serial console size detection
Quoted 43 lines · Heinrich Schuchardt
Quoted 10 lines · matthias.bgg at kernel.org
From: Matthias Brugger <[email protected]>
Function
term_read_replytries to read from the serial console until theend_charwas read. This can hang forever if we are, for some reason, not be able to read the full response (e.g. serial buffer too small, frame error). This patch moves the timeout detection intoterm_read_replyto assure we will make progress.Fixes 6bb591f704 ("efi_loader: query serial console size reliably")Signed-off-by Matthias Brugger <[email protected]>Thanks Matthias for the patch.
The general direction is right. Yet I would prefer if you could move the waiting loop as described below.
Quoted 10 lines
--- lib/efi_loader/efi_console.c | 63 +++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 26 deletions(-)lib/efi_loader/efi_console.c +0
-0@@ -62,6 +62,16 @@ static struct simple_text_output_mode efi_con_mode = { .cursor_visible = 1,lib/efi_loader/efi_console.c static struct simple_text_output_mode efi_con_mode = { · line 72
Quoted 10 lines
lib/efi_loader/efi_console.c +8
-0}; +static int term_get_char (char *c) +{ +if (tstc ()) { + *c =getc (); +return 0 ; + } + +return 1 ;The function signals an error if the character to read is not yet in the UART buffer. I think it would be preferable to put the waiting time (100 ms is sufficient at 110 baud and above) into this function instead. This has the following advantages:
Quoted 4 lines
- We would need only one waiting loop.
- We could reuse the function in functionefi_cin_read_key()where we have another chance to hang waiting for a character.
Ok, I'll do that in v2.
Quoted 11 lines
Quoted 11 lines
lib/efi_loader/efi_console.c +3
-0+} +/* * Receive and parse a reply from the terminal. * @@ -74,32 +84,42 @@ static int term_read_reply(int *n, int num, char end_char) {char c;int i =0 ; +u64 timeout;
Quoted 7 lines
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 90
Quoted 4 lines
lib/efi_loader/efi_console.c +2
-2- c =getc (); -if (c != cESC) +/* Allow up to one second for the response */ + timeout =timer_get_us () +1000000 ;Even at 110 baud a waiting time of 100 ms is sufficient.
So we don't have to wait up to one second for the first character to arrive as we did in query_console_serial() before this patch?
Quoted 18 lines
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 93
Quoted 3 lines
lib/efi_loader/efi_console.c +3
-0+while (!tstc ()) +if (timer_get_us () > timeout) +return -1 ;This loop could be moved to
term_get_char().lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 98
Quoted 7 lines
lib/efi_loader/efi_console.c +4
-2+ +if (term_get_char (&c) || c != cESC)return -1 ; - c =getc (); -if (c !='[' ) + +if (term_get_char (&c) || c !='[' )We should allow time for this character to arrive.
Quoted 2 lines
lib/efi_loader/efi_console.c +0
-0return -1 ;
Quoted 8 lines
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char)
Quoted 6 lines
lib/efi_loader/efi_console.c +0
-4n[0 ] =0 ;while (1 ) { - c =getc (); -if (c ==';' ) { - i++; - if (i >= num)> + if (!term_get_char(&c)) {On loop entry there is no wait before this
term_get_char().
I don't understand, if the character is not yet present, we will loop in the while until it arrives or we hit the timeout.
Regards,
Matthias
Quoted 61 lines
Best regards
Heinrich
Quoted 55 lines
lib/efi_loader/efi_console.c +16
-18+if (c ==';' ) { + i++; +if (i >= num) +return -1 ; + n[i] =0 ; +continue ; + }else if (c == end_char) { +break ; + }else if (c >'9' || c <'0' ) {return -1 ; - n[i] =0 ; -continue ; - }else if (c == end_char) { -break ; - }else if (c >'9' || c <'0' ) { -return -1 ; + } + +/* Read one more decimal position */ + n[i] *=10 ; + n[i] += c -'0' ; } -/* Read one more decimal position */ - n[i] *=10 ; - n[i] += c -'0' ; +if (timer_get_us () > timeout) +return -1 ; }if (i != num -1 )return -1 ;@@ -196,7 +216,6 @@ static int query_console_serial(int *rows, int *cols) {int ret =0 ;int n[2 ]; -u64 timeout;/* Empty input buffer */ while (tstc ())@@ -216,14 +235,6 @@ static int query_console_serial(int *rows, int *cols)ESC "[999;999H" /* Move to bottom right corner */ ESC "[6n"); /* Query cursor position */ -/* Allow up to one second for a response */ - timeout =timer_get_us () +1000000 ; -while (!tstc ()) -if (timer_get_us () > timeout) { - ret =-1 ; - goto out; - } -/* Read {rows,cols} */ if (term_read_reply (n,2 ,'R' )) { ret =1 ;
PATCH 1/1 efi_loader: Fix serial console size detection
Quoted 72 lines · Matthias Brugger
Quoted 43 lines · Heinrich Schuchardt
Quoted 10 lines · matthias.bgg at kernel.org
From: Matthias Brugger <[email protected]>
Function
term_read_replytries to read from the serial console until theend_charwas read. This can hang forever if we are, for some reason, not be able to read the full response (e.g. serial buffer too small, frame error). This patch moves the timeout detection intoterm_read_replyto assure we will make progress.Fixes 6bb591f704 ("efi_loader: query serial console size reliably")Signed-off-by Matthias Brugger <[email protected]>Thanks Matthias for the patch.
The general direction is right. Yet I would prefer if you could move the waiting loop as described below.
Quoted 10 lines
--- lib/efi_loader/efi_console.c | 63 +++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 26 deletions(-)lib/efi_loader/efi_console.c +0
-0@@ -62,6 +62,16 @@ static struct simple_text_output_mode efi_con_mode = { .cursor_visible = 1,lib/efi_loader/efi_console.c static struct simple_text_output_mode efi_con_mode = { · line 72
Quoted 10 lines
lib/efi_loader/efi_console.c +8
-0}; +static int term_get_char (char *c) +{ +if (tstc ()) { + *c =getc (); +return 0 ; + } + +return 1 ;The function signals an error if the character to read is not yet in the UART buffer. I think it would be preferable to put the waiting time (100 ms is sufficient at 110 baud and above) into this function instead. This has the following advantages:
Quoted 4 lines
- We would need only one waiting loop.
- We could reuse the function in functionefi_cin_read_key()where we have another chance to hang waiting for a character.Ok, I'll do that in v2.
Quoted 11 lines
Quoted 11 lines
lib/efi_loader/efi_console.c +3
-0+} +/* * Receive and parse a reply from the terminal. * @@ -74,32 +84,42 @@ static int term_read_reply(int *n, int num, char end_char) {char c;int i =0 ; +u64 timeout;Quoted 7 lines
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 90
Quoted 4 lines
lib/efi_loader/efi_console.c +2
-2- c =getc (); -if (c != cESC) +/* Allow up to one second for the response */ + timeout =timer_get_us () +1000000 ;Even at 110 baud a waiting time of 100 ms is sufficient.
Quoted 2 lines
So we don't have to wait up to one second for the first character to arrive as we did in
query_console_serial()before this patch?
Teterminal will respond immediately when we query the size. Even at 110 baud it should be sufficient to wait for a maximum of 100 ms for the first character.
Quoted 28 lines
Quoted 19 lines
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 93
Quoted 4 lines
lib/efi_loader/efi_console.c +3
-0+while (!tstc ()) +if (timer_get_us () > timeout) +return -1 ;This loop could be moved to
term_get_char().lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char) · line 98
Quoted 7 lines
lib/efi_loader/efi_console.c +4
-2+ +if (term_get_char (&c) || c != cESC)return -1 ; - c =getc (); -if (c !='[' ) + +if (term_get_char (&c) || c !='[' )We should allow time for this character to arrive.
Quoted 2 lines
lib/efi_loader/efi_console.c +0
-0return -1 ;Quoted 8 lines
lib/efi_loader/efi_console.c static int term_read_reply(int *n, int num, char end_char)
Quoted 6 lines
lib/efi_loader/efi_console.c +0
-4n[0 ] =0 ;while (1 ) { - c =getc (); -if (c ==';' ) { - i++; - if (i >= num)> + if (!term_get_char(&c)) {On loop entry there is no wait before this
term_get_char().
Quoted 2 lines
I don't understand, if the character is not yet present, we will loop in the while until it arrives or we hit the timeout.
We should not loop forever. If the next character is not received within 100 ms we should error out.
Best regards
Heinrich
Quoted 66 lines
Regards,
MatthiasQuoted 61 lines
Best regards
Heinrich
Quoted 55 lines
lib/efi_loader/efi_console.c +16
-18+if (c ==';' ) { + i++; +if (i >= num) +return -1 ; + n[i] =0 ; +continue ; + }else if (c == end_char) { +break ; + }else if (c >'9' || c <'0' ) {return -1 ; - n[i] =0 ; -continue ; - }else if (c == end_char) { -break ; - }else if (c >'9' || c <'0' ) { -return -1 ; + } + +/* Read one more decimal position */ + n[i] *=10 ; + n[i] += c -'0' ; } -/* Read one more decimal position */ - n[i] *=10 ; - n[i] += c -'0' ; +if (timer_get_us () > timeout) +return -1 ; }if (i != num -1 )return -1 ;@@ -196,7 +216,6 @@ static int query_console_serial(int *rows, int *cols) {int ret =0 ;int n[2 ]; -u64 timeout;/* Empty input buffer */ while (tstc ())@@ -216,14 +235,6 @@ static int query_console_serial(int *rows, int *cols)ESC "[999;999H" /* Move to bottom right corner */ ESC "[6n"); /* Query cursor position */ -/* Allow up to one second for a response */ - timeout =timer_get_us () +1000000 ; -while (!tstc ()) -if (timer_get_us () > timeout) { - ret =-1 ; - goto out; - } -/* Read {rows,cols} */ if (term_read_reply (n,2 ,'R' )) { ret =1 ;