summaryrefslogtreecommitdiffstats
path: root/ui/ncurses/console-codes.c
diff options
context:
space:
mode:
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>2017-10-31 10:49:51 +1100
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>2017-11-14 16:33:41 +1100
commit8d1e4f053574d69aae89af19983c96500b4156a4 (patch)
tree190c566638fac6bbcc7964566cbe75e6d05d3fff /ui/ncurses/console-codes.c
parent669083ee9eda63af65d7cfc43968947f09162996 (diff)
downloadtalos-petitboot-8d1e4f053574d69aae89af19983c96500b4156a4.tar.gz
talos-petitboot-8d1e4f053574d69aae89af19983c96500b4156a4.zip
ui/ncurses: Safely handle lost terminal control commandsv1.6.3
Normally terminal control commands are caught and handled before ncurses or Petitboot could see them. However several combinations of broken terminal emulators or console connections can cause these command sequences to be seen by Petitboot, usually resulting in Petitboot exiting due to the ESC character and then the rest printed to the console. Aside from confusing the user this can also cancel autoboot, so it's important we don't let these sequences go unnoticed if possible. In ui/ncurses/console-codes we add a state machine that recognises the syntax of these control/escape sequences and handles the lost characters. We don't try to emulate the functionality of these commands, instead just logging them for reference. Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
Diffstat (limited to 'ui/ncurses/console-codes.c')
-rw-r--r--ui/ncurses/console-codes.c178
1 files changed, 178 insertions, 0 deletions
diff --git a/ui/ncurses/console-codes.c b/ui/ncurses/console-codes.c
new file mode 100644
index 0000000..5c28615
--- /dev/null
+++ b/ui/ncurses/console-codes.c
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2017 IBM Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include "talloc/talloc.h"
+
+#ifndef PETITBOOT_TEST
+#include "log/log.h"
+#include "nc-scr.h"
+#endif
+
+#include "console-codes.h"
+
+#define ESC_CHAR 033
+#define CSI_CHAR '['
+#define INTER_CHAR_START 040
+#define INTER_CHAR_END 057
+#define ESC_SEQUENCE_FINAL_START 060
+#define ESC_SEQUENCE_FINAL_END 0176
+#define CTRL_SEQUENCE_FINAL_START 0100
+#define CTRL_SEQUENCE_FINAL_END 0176
+#define DEC_PARAMETER 077
+
+enum console_sequence_state {
+ CONSOLE_STATE_START,
+ CONSOLE_STATE_ESC_SEQ,
+ CONSOLE_STATE_CTRL_SEQ_START,
+ CONSOLE_STATE_CTRL_SEQ,
+ CONSOLE_STATE_DONE,
+ CONSOLE_STATE_CONFUSED,
+};
+
+static inline bool is_intermediate(signed char c)
+{
+ return c > INTER_CHAR_START && c < INTER_CHAR_END;
+}
+
+static inline bool is_parameter(signed char c)
+{
+ return (c >= 060 && c <= 071) || c == 073;
+}
+
+static inline bool is_escape_final(signed char c)
+{
+ return c >= ESC_SEQUENCE_FINAL_START && c < ESC_SEQUENCE_FINAL_END;
+}
+
+static inline bool is_control_final(signed char c)
+{
+ return c >= CTRL_SEQUENCE_FINAL_START && c <= CTRL_SEQUENCE_FINAL_END;
+}
+
+static char console_sequence_getch(char **sequence)
+{
+ signed char c = getch();
+
+ if (c != ERR)
+ *sequence = talloc_asprintf_append(*sequence, "%c", c);
+ return c;
+}
+
+/*
+ * Catch terminal control sequences that have accidentally been sent to
+ * Petitboot. These are of the form
+ * ESC I .. I F
+ * where I is an Intermediate Character and F is a Final Character, eg:
+ * ESC ^ [ ? 1 ; 0 c
+ * or ESC # 6
+ *
+ * This is based off the definitions provided by
+ * https://vt100.net/docs/vt100-ug/contents.html
+ */
+char *handle_control_sequence(void *ctx, signed char start)
+{
+ enum console_sequence_state state = CONSOLE_STATE_START;
+ bool in_sequence = true;
+ signed char c;
+ char *seq;
+
+ if (start != ESC_CHAR) {
+ pb_log("%s: Called with non-escape character: 0%o\n",
+ __func__, start);
+ return NULL;
+ }
+
+ seq = talloc_asprintf(ctx, "%c", start);
+
+ while (in_sequence) {
+ switch (state) {
+ case CONSOLE_STATE_START:
+ c = console_sequence_getch(&seq);
+ if (c == CSI_CHAR)
+ state = CONSOLE_STATE_CTRL_SEQ_START;
+ else if (is_intermediate(c))
+ state = CONSOLE_STATE_ESC_SEQ;
+ else if (is_escape_final(c))
+ state = CONSOLE_STATE_DONE;
+ else if (c != ERR) {
+ /* wait on c == ERR */
+ pb_debug("Unexpected start: \\x%x\n", c);
+ state = CONSOLE_STATE_CONFUSED;
+ }
+ break;
+ case CONSOLE_STATE_ESC_SEQ:
+ c = console_sequence_getch(&seq);
+ if (is_intermediate(c))
+ state = CONSOLE_STATE_ESC_SEQ;
+ else if (is_escape_final(c))
+ state = CONSOLE_STATE_DONE;
+ else if (c != ERR) {
+ /* wait on c == ERR */
+ pb_debug("Unexpected character after intermediate: \\x%x\n",
+ c);
+ state = CONSOLE_STATE_CONFUSED;
+ }
+ break;
+ case CONSOLE_STATE_CTRL_SEQ_START:
+ c = console_sequence_getch(&seq);
+ if (is_intermediate(c) || is_parameter(c) ||
+ c == DEC_PARAMETER)
+ state = CONSOLE_STATE_CTRL_SEQ;
+ else if (is_control_final(c))
+ state = CONSOLE_STATE_DONE;
+ else if (c != ERR) {
+ /* wait on c == ERR */
+ pb_debug("Unexpected character in param string: \\x%x\n",
+ c);
+ state = CONSOLE_STATE_CONFUSED;
+ }
+ break;
+ case CONSOLE_STATE_CTRL_SEQ:
+ c = console_sequence_getch(&seq);
+ if (is_intermediate(c) || is_parameter(c))
+ state = CONSOLE_STATE_CTRL_SEQ;
+ else if (is_control_final(c))
+ state = CONSOLE_STATE_DONE;
+ else if (c != ERR) {
+ /* wait on c == ERR */
+ pb_debug("Unexpected character in param string: \\x%x\n",
+ c);
+ state = CONSOLE_STATE_CONFUSED;
+ }
+ break;
+ case CONSOLE_STATE_DONE:
+ in_sequence = false;
+ break;
+ case CONSOLE_STATE_CONFUSED:
+ /* fall-through */
+ default:
+ pb_debug("We got lost interpreting a control sequence!\n");
+ seq = talloc_asprintf_append(seq, "...");
+ in_sequence = false;
+ break;
+ };
+ }
+
+ return seq;
+}
OpenPOWER on IntegriCloud