From patchwork Tue Mar 5 11:50:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Brugger X-Patchwork-Id: 1051711 X-Patchwork-Delegate: xypron.glpk@gmx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="vV2oehLV"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 44DFZy0mPcz9s4Y for ; Tue, 5 Mar 2019 22:50:49 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id ADEFAC21E02; Tue, 5 Mar 2019 11:50:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 39934C21D8E; Tue, 5 Mar 2019 11:50:30 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 08730C21C29; Tue, 5 Mar 2019 11:50:28 +0000 (UTC) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by lists.denx.de (Postfix) with ESMTPS id 710D0C21C29 for ; Tue, 5 Mar 2019 11:50:28 +0000 (UTC) Received: from ziggy.de (unknown [37.223.147.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CD35020848; Tue, 5 Mar 2019 11:50:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551786625; bh=6NNWuSvRf08pvtJ6DSZwNfeH4mKuSgUpiJy65Pg5d8E=; h=From:To:Cc:Subject:Date:From; b=vV2oehLVxmiNOj1Lb4N0ZSBcnAhYbSzzO29lrW4ex6lcD12j97HF67iJA3Y18x81e fCBwnxL2+O6inmb0lX6BtElYjiMSM7kGwm/3vLpVveXKASw1D5gumzppJOc7ADy8kN GugjJYjHdfinaout0kVwt30Qn03Mj9fhttLrXmOg= From: matthias.bgg@kernel.org To: u-boot@lists.denx.de Date: Tue, 5 Mar 2019 12:50:18 +0100 Message-Id: <20190305115019.3581-1-matthias.bgg@kernel.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Cc: Heinrich Schuchardt , Matthias Brugger , Alexander Graf Subject: [U-Boot] [PATCH v2 1/2] efi_loader: Fix serial console size detection X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Matthias Brugger 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. Fixes: 6bb591f704 ("efi_loader: query serial console size reliably") Signed-off-by: Matthias Brugger Reviewed-by: Heinrich Schuchardt --- Changes in v2: - move timeout into term_get_char lib/efi_loader/efi_console.c | 60 ++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/efi_loader/efi_console.c b/lib/efi_loader/efi_console.c index 66c33a551d..1133674faf 100644 --- a/lib/efi_loader/efi_console.c +++ b/lib/efi_loader/efi_console.c @@ -62,6 +62,21 @@ static struct simple_text_output_mode efi_con_mode = { .cursor_visible = 1, }; +static int term_get_char(char *c) +{ + u64 timeout; + + /* Wait up to 100 ms for a character */ + timeout = timer_get_us() + 100000; + + while (!tstc()) + if (timer_get_us() > timeout) + return 1; + + *c = getc(); + return 0; +} + /* * Receive and parse a reply from the terminal. * @@ -75,31 +90,31 @@ static int term_read_reply(int *n, int num, char end_char) char c; int i = 0; - c = getc(); - if (c != cESC) + 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 (i != num - 1) return -1; @@ -196,7 +211,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 +230,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; From patchwork Tue Mar 5 11:50:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Brugger X-Patchwork-Id: 1051710 X-Patchwork-Delegate: xypron.glpk@gmx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="j8UXCaiY"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 44DFZn5Hvkz9s4Y for ; Tue, 5 Mar 2019 22:50:39 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 39C61C21DEC; Tue, 5 Mar 2019 11:50:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id D0D6CC21C38; Tue, 5 Mar 2019 11:50:29 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id EE422C21C3F; Tue, 5 Mar 2019 11:50:28 +0000 (UTC) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by lists.denx.de (Postfix) with ESMTPS id 7122DC21C38 for ; Tue, 5 Mar 2019 11:50:28 +0000 (UTC) Received: from ziggy.de (unknown [37.223.147.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 567F12087C; Tue, 5 Mar 2019 11:50:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551786627; bh=X0s5TyIaYnbeJkjMLwxnH6N/qn6QmNu5PrcSHycxyus=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j8UXCaiYDGSkzoyNG+s8Q0L9cxg83pI4v1NxQ86KtewSGvRZG2S3UH0nDf4cHgmAq cwLPG6hxy622jKoLQ+jcz2yvGGVwjxeBKeClW3sitSsDkCBJwmaN3Nov0CS+WABZbV 170IVLfST0dFG4Z1zAir0CpAVrNwC/FdU6I7BNaY= From: matthias.bgg@kernel.org To: u-boot@lists.denx.de Date: Tue, 5 Mar 2019 12:50:19 +0100 Message-Id: <20190305115019.3581-2-matthias.bgg@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190305115019.3581-1-matthias.bgg@kernel.org> References: <20190305115019.3581-1-matthias.bgg@kernel.org> MIME-Version: 1.0 Cc: Heinrich Schuchardt , Matthias Brugger , Alexander Graf Subject: [U-Boot] [PATCH v2 2/2] efi_loader: Fix possible starving in efi_cin_read_key X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Matthias Brugger The function efi_cin_read_key can be affected by a loss of a character which will make u-boot to wait forever. Fix this by calling term_get_char instead. Signed-off-by: Matthias Brugger --- Changes in v2: None lib/efi_loader/efi_console.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/efi_loader/efi_console.c b/lib/efi_loader/efi_console.c index 1133674faf..558aaed109 100644 --- a/lib/efi_loader/efi_console.c +++ b/lib/efi_loader/efi_console.c @@ -493,13 +493,14 @@ static int analyze_modifiers(struct efi_key_state *key_state) { int c, mod = 0, ret = 0; - c = getc(); + if (!term_get_char(&c)) + goto out; if (c != ';') { ret = c; if (c == '~') goto out; - c = getc(); + term_get_char(&c); } for (;;) { switch (c) { @@ -508,7 +509,7 @@ static int analyze_modifiers(struct efi_key_state *key_state) mod += c - '0'; /* fall through */ case ';': - c = getc(); + term_get_char(&c); break; default: goto out; @@ -551,7 +552,9 @@ static efi_status_t efi_cin_read_key(struct efi_key_data *key) * Xterm Control Sequences * https://www.xfree86.org/4.8.0/ctlseqs.html */ - ch = getc(); + if (!term_get_char(&ch)) + return EFI_NOT_READY; + switch (ch) { case cESC: /* ESC */ pressed_key.scan_code = 23; @@ -561,12 +564,15 @@ static efi_status_t efi_cin_read_key(struct efi_key_data *key) /* consider modifiers */ if (ch < 'P') { set_shift_mask(ch - '0', &key->key_state); - ch = getc(); + if (!term_get_char(&ch)) + return EFI_NOT_READY; } pressed_key.scan_code = ch - 'P' + 11; break; case '[': - ch = getc(); + if (!term_get_char(&ch)) + return EFI_NOT_READY; + switch (ch) { case 'A'...'D': /* up, down right, left */ pressed_key.scan_code = ch - 'A' + 1;