[coreboot] [PATCH 2/2] libpayload: support color over serial

Ulf Jordan jordan at chalmers.se
Wed Sep 3 21:24:29 CEST 2008


Here is an updated version of the serial color patch.

Compared to the previous patch:

* serial_set_color takes explicit foreground and background color numbers.

* The need to output color escape sequences is determined from the 
color pair number, not the color content of a given pair. This eliminates 
the possibbility of obtaining incorrect colors after \e[m (e.g. bold to 
non-bold transition) on terminals where the default color is not white on 
black.

* doxygen comment added to serial_set_color

* Foreground and background colors of a color pair are extracted with 
pair_content(), instead of relying on explicit bitmasking and shifting. 
Improves readability and removes some code duplication.

Build and runtime tested with coreinfo+libpayload+coreboot-v3 under QEMU.


/ulf
-------------- next part --------------
Add support for curses color output over serial.

Note that the sequence \e[m for turning off bold resets all attributes,
including color.

Signed-off-by: Ulf Jordan <jordan at chalmers.se>

Index: libpayload/curses/tinycurses.c
===================================================================
--- libpayload/curses/tinycurses.c.orig	2008-08-19 20:04:18.000000000 +0200
+++ libpayload/curses/tinycurses.c	2008-09-02 22:12:14.000000000 +0200
@@ -669,10 +669,12 @@
 	// FIXME.
 	int serial_is_bold = 0;
 	int serial_is_altcharset = 0;
+	int serial_cur_pair = 0;
 
 	int x, y;
 	chtype ch;
 	int need_altcharset;
+	short fg, bg;
 
 	serial_end_bold();
 	serial_end_altcharset();
@@ -703,6 +705,7 @@
 					if (serial_is_bold) {
 						serial_end_bold();
 						serial_is_bold = 0;
+						serial_cur_pair = 0;
 					}
 				}
 
@@ -723,6 +726,13 @@
 					serial_is_altcharset = 0;
 				}
 
+				if (serial_cur_pair != PAIR_NUMBER(attr)) {
+					pair_content(PAIR_NUMBER(attr),
+						     &fg, &bg);
+					serial_set_color(fg, bg);
+					serial_cur_pair = PAIR_NUMBER(attr);
+				}
+
 				serial_putchar(ch);
 			}
 
Index: libpayload/drivers/serial.c
===================================================================
--- libpayload/drivers/serial.c.orig	2008-08-19 20:04:18.000000000 +0200
+++ libpayload/drivers/serial.c	2008-09-02 22:38:19.000000000 +0200
@@ -110,6 +110,8 @@
    enacs=\E(B\E)0, smacs=^N, rmacs=^O.  */
 #define VT100_SMACS       "\e(0"
 #define VT100_RMACS       "\e(B"
+/* A vt100 doesn't do color, setaf/setab below are from xterm-color. */
+#define VT100_SET_COLOR   "\e[3%d;4%dm"
 
 static void serial_putcmd(char *str)
 {
@@ -142,6 +144,19 @@
 	serial_putcmd(VT100_RMACS);
 }
 
+/**
+ * Set the foreground and background colors on the serial console.
+ *
+ * @param fg Foreground color number.
+ * @param bg Background color number.
+ */
+void serial_set_color(short fg, short bg)
+{
+	char buffer[32];
+	snprintf(buffer, sizeof(buffer), VT100_SET_COLOR, fg, bg);
+	serial_putcmd(buffer);
+}
+
 void serial_set_cursor(int y, int x)
 {
 	char buffer[32];
Index: libpayload/include/libpayload.h
===================================================================
--- libpayload/include/libpayload.h.orig	2008-09-02 21:25:39.000000000 +0200
+++ libpayload/include/libpayload.h	2008-09-02 22:07:45.000000000 +0200
@@ -139,6 +139,7 @@
 void serial_end_bold(void);
 void serial_start_altcharset(void);
 void serial_end_altcharset(void);
+void serial_set_color(short fg, short bg);
 void serial_set_cursor(int y, int x);
 /** @} */
 


More information about the coreboot mailing list