xref: /aosp_15_r20/external/coreboot/payloads/libpayload/drivers/serial/serial.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /*
2  *
3  * Copyright (C) 2008 Advanced Micro Devices, Inc.
4  * Copyright (C) 2008 Ulf Jordan <[email protected]>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <libpayload-config.h>
31 #include <libpayload.h>
32 
33 /*  These are thinly veiled vt100 functions used by curses */
34 
35 #define VT100_CLEAR       "\e[H\e[J"
36 /* These defines will fail if you use bold and reverse at the same time.
37  * Switching off one of them will switch off both. tinycurses knows about
38  * this and does the right thing.
39  */
40 #define VT100_SBOLD       "\e[1m"
41 #define VT100_EBOLD       "\e[m"
42 #define VT100_SREVERSE    "\e[7m"
43 #define VT100_EREVERSE    "\e[m"
44 #define VT100_CURSOR_ADDR "\e[%d;%dH"
45 #define VT100_CURSOR_ON   "\e[?25l"
46 #define VT100_CURSOR_OFF  "\e[?25h"
47 /* The following smacs/rmacs are actually for xterm; a real vt100 has
48    enacs=\E(B\E)0, smacs=^N, rmacs=^O.  */
49 #define VT100_SMACS       "\e(0"
50 #define VT100_RMACS       "\e(B"
51 /* A vt100 doesn't do color, setaf/setab below are from xterm-color. */
52 #define VT100_SET_COLOR   "\e[3%d;4%dm"
53 
serial_putcmd(const char * str)54 static void serial_putcmd(const char *str)
55 {
56 	while (*str)
57 		serial_putchar(*(str++));
58 }
59 
serial_clear(void)60 void serial_clear(void)
61 {
62 	serial_putcmd(VT100_CLEAR);
63 }
64 
serial_start_bold(void)65 void serial_start_bold(void)
66 {
67 	serial_putcmd(VT100_SBOLD);
68 }
69 
serial_end_bold(void)70 void serial_end_bold(void)
71 {
72 	serial_putcmd(VT100_EBOLD);
73 }
74 
serial_start_reverse(void)75 void serial_start_reverse(void)
76 {
77 	serial_putcmd(VT100_SREVERSE);
78 }
79 
serial_end_reverse(void)80 void serial_end_reverse(void)
81 {
82 	serial_putcmd(VT100_EREVERSE);
83 }
84 
serial_start_altcharset(void)85 void serial_start_altcharset(void)
86 {
87 	serial_putcmd(VT100_SMACS);
88 }
89 
serial_end_altcharset(void)90 void serial_end_altcharset(void)
91 {
92 	serial_putcmd(VT100_RMACS);
93 }
94 
95 /**
96  * Set the foreground and background colors on the serial console.
97  *
98  * @param fg Foreground color number.
99  * @param bg Background color number.
100  */
serial_set_color(short fg,short bg)101 void serial_set_color(short fg, short bg)
102 {
103 	char buffer[32];
104 
105 	snprintf(buffer, sizeof(buffer), VT100_SET_COLOR, fg, bg);
106 	serial_putcmd(buffer);
107 }
108 
serial_set_cursor(int y,int x)109 void serial_set_cursor(int y, int x)
110 {
111 	char buffer[32];
112 
113 	snprintf(buffer, sizeof(buffer), VT100_CURSOR_ADDR, y + 1, x + 1);
114 	serial_putcmd(buffer);
115 }
116 
serial_cursor_enable(int state)117 void serial_cursor_enable(int state)
118 {
119 	if (state)
120 		serial_putcmd(VT100_CURSOR_ON);
121 	else
122 		serial_putcmd(VT100_CURSOR_OFF);
123 }
124