git: 0c1f70f8ee46 - stable/13 - kboot: implement stripped down termios

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:11:40 UTC
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=0c1f70f8ee468ebdbb7d6bb92d706c573a58fcf3

commit 0c1f70f8ee468ebdbb7d6bb92d706c573a58fcf3
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-07-26 23:31:23 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:28 +0000

    kboot: implement stripped down termios
    
    Implement a stripped down termios, obtained from various files in musl
    and HOST_ or host_ prepended to most things and a few unavoidable style
    tweaks. Only implements the bits of termios we need for the boot loader:
    put the terminal into raw mode, restore terminal settings and speed
    stuff.
    
    Sponsored by:           Netflix
    
    (cherry picked from commit 963037786f76a414e91e45d4fbef5e35fc1abc84)
---
 stand/kboot/Makefile                      |   1 +
 stand/kboot/arch/amd64/termios_arch.h     |   1 +
 stand/kboot/arch/powerpc64/termios_arch.h | 184 ++++++++++++++++++++++++++++++
 stand/kboot/termios.c                     |  66 +++++++++++
 stand/kboot/termios.h                     |  24 ++++
 stand/kboot/termios_gen.h                 | 169 +++++++++++++++++++++++++++
 6 files changed, 445 insertions(+)

diff --git a/stand/kboot/Makefile b/stand/kboot/Makefile
index 3a38b8d3d0cf..986e636a8c35 100644
--- a/stand/kboot/Makefile
+++ b/stand/kboot/Makefile
@@ -19,6 +19,7 @@ INSTALLFLAGS=	-b
 
 # Architecture-specific loader code
 SRCS=		crt1.c conf.c vers.c main.c host_syscalls.c hostcons.c hostdisk.c kbootfdt.c gfx_fb_stub.c
+SRCS+=		termios.c
 
 CFLAGS.gfx_fb_stub.c += -I${SRCTOP}/contrib/pnglite -I${SRCTOP}/sys/teken
 
diff --git a/stand/kboot/arch/amd64/termios_arch.h b/stand/kboot/arch/amd64/termios_arch.h
new file mode 100644
index 000000000000..0d5ae3718498
--- /dev/null
+++ b/stand/kboot/arch/amd64/termios_arch.h
@@ -0,0 +1 @@
+#include "termios_gen.h"
diff --git a/stand/kboot/arch/powerpc64/termios_arch.h b/stand/kboot/arch/powerpc64/termios_arch.h
new file mode 100644
index 000000000000..62b801ff16da
--- /dev/null
+++ b/stand/kboot/arch/powerpc64/termios_arch.h
@@ -0,0 +1,184 @@
+#undef HOST_NCCS
+#define HOST_NCCS 19
+struct host_termios {
+	host_tcflag_t c_iflag;
+	host_tcflag_t c_oflag;
+	host_tcflag_t c_cflag;
+	host_tcflag_t c_lflag;
+	host_cc_t c_cc[HOST_NCCS];
+	host_cc_t c_line;
+	host_speed_t __c_ispeed;
+	host_speed_t __c_ospeed;
+};
+
+#define HOST_VINTR     0
+#define HOST_VQUIT     1
+#define HOST_VERASE    2
+#define HOST_VKILL     3
+#define HOST_VEOF      4
+#define HOST_VMIN      5
+#define HOST_VEOL      6
+#define HOST_VTIME     7
+#define HOST_VEOL2     8
+#define HOST_VSWTC     9
+#define HOST_VWERASE  10
+#define HOST_VREPRINT 11
+#define HOST_VSUSP    12
+#define HOST_VSTART   13
+#define HOST_VSTOP    14
+#define HOST_VLNEXT   15
+#define HOST_VDISCARD 16
+
+#define HOST_IGNBRK  0000001
+#define HOST_BRKINT  0000002
+#define HOST_IGNPAR  0000004
+#define HOST_PARMRK  0000010
+#define HOST_INPCK   0000020
+#define HOST_ISTRIP  0000040
+#define HOST_INLCR   0000100
+#define HOST_IGNCR   0000200
+#define HOST_ICRNL   0000400
+#define HOST_IXON    0001000
+#define HOST_IXOFF   0002000
+#define HOST_IXANY   0004000
+#define HOST_IUCLC   0010000
+#define HOST_IMAXBEL 0020000
+#define HOST_IUTF8   0040000
+
+#define HOST_OPOST  0000001
+#define HOST_ONLCR  0000002
+#define HOST_OLCUC  0000004
+#define HOST_OCRNL  0000010
+#define HOST_ONOCR  0000020
+#define HOST_ONLRET 0000040
+#define HOST_OFILL  0000100
+#define HOST_OFDEL  0000200
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_XOPEN_SOURCE)
+#define HOST_NLDLY  0001400
+#define HOST_NL0    0000000
+#define HOST_NL1    0000400
+#define HOST_NL2    0001000
+#define HOST_NL3    0001400
+#define HOST_TABDLY 0006000
+#define HOST_TAB0   0000000
+#define HOST_TAB1   0002000
+#define HOST_TAB2   0004000
+#define HOST_TAB3   0006000
+#define HOST_CRDLY  0030000
+#define HOST_CR0    0000000
+#define HOST_CR1    0010000
+#define HOST_CR2    0020000
+#define HOST_CR3    0030000
+#define HOST_FFDLY  0040000
+#define HOST_FF0    0000000
+#define HOST_FF1    0040000
+#define HOST_BSDLY  0100000
+#define HOST_BS0    0000000
+#define HOST_BS1    0100000
+#endif
+
+#define HOST_VTDLY  0200000
+#define HOST_VT0    0000000
+#define HOST_VT1    0200000
+
+#define HOST_B0       0000000
+#define HOST_B50      0000001
+#define HOST_B75      0000002
+#define HOST_B110     0000003
+#define HOST_B134     0000004
+#define HOST_B150     0000005
+#define HOST_B200     0000006
+#define HOST_B300     0000007
+#define HOST_B600     0000010
+#define HOST_B1200    0000011
+#define HOST_B1800    0000012
+#define HOST_B2400    0000013
+#define HOST_B4800    0000014
+#define HOST_B9600    0000015
+#define HOST_B19200   0000016
+#define HOST_B38400   0000017
+
+#define HOST_B57600   00020
+#define HOST_B115200  00021
+#define HOST_B230400  00022
+#define HOST_B460800  00023
+#define HOST_B500000  00024
+#define HOST_B576000  00025
+#define HOST_B921600  00026
+#define HOST_B1000000 00027
+#define HOST_B1152000 00030
+#define HOST_B1500000 00031
+#define HOST_B2000000 00032
+#define HOST_B2500000 00033
+#define HOST_B3000000 00034
+#define HOST_B3500000 00035
+#define HOST_B4000000 00036
+
+#define HOST_CSIZE  00001400
+#define HOST_CS5    00000000
+#define HOST_CS6    00000400
+#define HOST_CS7    00001000
+#define HOST_CS8    00001400
+#define HOST_CSTOPB 00002000
+#define HOST_CREAD  00004000
+#define HOST_PARENB 00010000
+#define HOST_PARODD 00020000
+#define HOST_HUPCL  00040000
+#define HOST_CLOCAL 00100000
+
+#define HOST_ECHOE   0x00000002
+#define HOST_ECHOK   0x00000004
+#define HOST_ECHO    0x00000008
+#define HOST_ECHONL  0x00000010
+#define HOST_ISIG    0x00000080
+#define HOST_ICANON  0x00000100
+#define HOST_IEXTEN  0x00000400
+#define HOST_TOSTOP  0x00400000
+#define HOST_NOFLSH  0x80000000
+
+#define HOST_TCOOFF 0
+#define HOST_TCOON  1
+#define HOST_TCIOFF 2
+#define HOST_TCION  3
+
+#define HOST_TCIFLUSH  0
+#define HOST_TCOFLUSH  1
+#define HOST_TCIOFLUSH 2
+
+#define HOST_TCSANOW   0
+#define HOST_TCSADRAIN 1
+#define HOST_TCSAFLUSH 2
+
+#define HOST_EXTA    0000016
+#define HOST_EXTB    0000017
+#define HOST_CBAUD   00377
+#define HOST_CBAUDEX 0000020
+#define HOST_CIBAUD  077600000
+#define HOST_CMSPAR  010000000000
+#define HOST_CRTSCTS 020000000000
+
+#define HOST_XCASE   0x00004000
+#define HOST_ECHOCTL 0x00000040
+#define HOST_ECHOPRT 0x00000020
+#define HOST_ECHOKE  0x00000001
+#define HOST_FLUSHO  0x00800000
+#define HOST_PENDIN  0x20000000
+#define HOST_EXTPROC 0x10000000
+
+#define HOST_XTABS   00006000
+#define HOST_TIOCSER_TEMT 1
+
+#define _IOC(a,b,c,d) ( ((a)<<29) | ((b)<<8) | (c) | ((d)<<16) )
+#define _IOC_NONE  1U
+#define _IOC_WRITE 4U
+#define _IOC_READ  2U
+
+#define _IO(a,b) _IOC(_IOC_NONE,(a),(b),0)
+#define _IOW(a,b,c) _IOC(_IOC_WRITE,(a),(b),sizeof(c))
+#define _IOR(a,b,c) _IOC(_IOC_READ,(a),(b),sizeof(c))
+#define _IOWR(a,b,c) _IOC(_IOC_READ|_IOC_WRITE,(a),(b),sizeof(c))
+
+#define HOST_TCGETS	_IOR('t', 19, char[44])
+#define HOST_TCSETS	_IOW('t', 20, char[44])
+#define HOST_TCSETSW	_IOW('t', 21, char[44])
+#define HOST_TCSETSF	_IOW('t', 22, char[44])
diff --git a/stand/kboot/termios.c b/stand/kboot/termios.c
new file mode 100644
index 000000000000..ec62170a44c9
--- /dev/null
+++ b/stand/kboot/termios.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2005-2020 Rich Felker, et al.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Note: From the musl project, stripped down and repackaged with HOST_/host_ prepended
+ */
+
+#include <sys/types.h>
+#include "termios.h"
+#include "host_syscall.h"
+
+int
+host_tcgetattr(int fd, struct host_termios *tio)
+{
+	if (host_ioctl(fd, HOST_TCGETS, (uintptr_t)tio))
+		return -1;
+	return 0;
+}
+
+int
+host_tcsetattr(int fd, int act, const struct host_termios *tio)
+{
+	if (act < 0 || act > 2) {
+//		errno = EINVAL;	/* XXX ?? */
+		return -1;
+	}
+	return host_ioctl(fd, HOST_TCSETS+act, (uintptr_t)tio);
+}
+
+void
+host_cfmakeraw(struct host_termios *t)
+{
+	t->c_iflag &= ~(HOST_IGNBRK | HOST_BRKINT | HOST_PARMRK | HOST_ISTRIP |
+	    HOST_INLCR | HOST_IGNCR | HOST_ICRNL | HOST_IXON);
+	t->c_oflag &= ~HOST_OPOST;
+	t->c_lflag &= ~(HOST_ECHO | HOST_ECHONL | HOST_ICANON | HOST_ISIG |
+	    HOST_IEXTEN);
+	t->c_cflag &= ~(HOST_CSIZE | HOST_PARENB);
+	t->c_cflag |= HOST_CS8;
+	t->c_cc[HOST_VMIN] = 1;
+	t->c_cc[HOST_VTIME] = 0;
+}
+
+int host_cfsetospeed(struct host_termios *tio, host_speed_t speed)
+{
+	if (speed & ~HOST_CBAUD) {
+//		errno = EINVAL; /* XXX ? */
+		return -1;
+	}
+	tio->c_cflag &= ~HOST_CBAUD;
+	tio->c_cflag |= speed;
+	return 0;
+}
+
+int host_cfsetispeed(struct host_termios *tio, host_speed_t speed)
+{
+	return speed ? host_cfsetospeed(tio, speed) : 0;
+}
+
+int
+host_cfsetspeed(struct host_termios *tio, host_speed_t speed)
+{
+	return host_cfsetospeed(tio, speed);	/* weak alias in musl */
+}
+
diff --git a/stand/kboot/termios.h b/stand/kboot/termios.h
new file mode 100644
index 000000000000..f5763292e7d5
--- /dev/null
+++ b/stand/kboot/termios.h
@@ -0,0 +1,24 @@
+#ifndef	_TERMIOS_H
+#define	_TERMIOS_H
+
+typedef unsigned char host_cc_t;
+typedef unsigned int host_speed_t;
+typedef unsigned int host_tcflag_t;
+
+#define HOST_NCCS 32
+
+#include "termios_arch.h"
+
+#define HOST_TCSANOW		0
+#define HOST_TCSADRAIN		1
+#define HOST_TCSAFLUSH		2
+
+int host_tcgetattr (int, struct host_termios *);
+int host_tcsetattr (int, int, const struct host_termios *);
+
+void host_cfmakeraw(struct host_termios *);
+int host_cfsetispeed(struct host_termios *, host_speed_t);
+int host_cfsetospeed(struct host_termios *, host_speed_t);
+int host_cfsetspeed(struct host_termios *, host_speed_t);
+
+#endif
diff --git a/stand/kboot/termios_gen.h b/stand/kboot/termios_gen.h
new file mode 100644
index 000000000000..041205e6dd0a
--- /dev/null
+++ b/stand/kboot/termios_gen.h
@@ -0,0 +1,169 @@
+struct host_termios {
+	host_tcflag_t c_iflag;
+	host_tcflag_t c_oflag;
+	host_tcflag_t c_cflag;
+	host_tcflag_t c_lflag;
+	host_cc_t c_line;
+	host_cc_t c_cc[HOST_NCCS];
+	host_speed_t __c_ispeed;
+	host_speed_t __c_ospeed;
+};
+
+#define HOST_VINTR     0
+#define HOST_VQUIT     1
+#define HOST_VERASE    2
+#define HOST_VKILL     3
+#define HOST_VEOF      4
+#define HOST_VTIME     5
+#define HOST_VMIN      6
+#define HOST_VSWTC     7
+#define HOST_VSTART    8
+#define HOST_VSTOP     9
+#define HOST_VSUSP    10
+#define HOST_VEOL     11
+#define HOST_VREPRINT 12
+#define HOST_VDISCARD 13
+#define HOST_VWERASE  14
+#define HOST_VLNEXT   15
+#define HOST_VEOL2    16
+
+#define HOST_IGNBRK  0000001
+#define HOST_BRKINT  0000002
+#define HOST_IGNPAR  0000004
+#define HOST_PARMRK  0000010
+#define HOST_INPCK   0000020
+#define HOST_ISTRIP  0000040
+#define HOST_INLCR   0000100
+#define HOST_IGNCR   0000200
+#define HOST_ICRNL   0000400
+#define HOST_IUCLC   0001000
+#define HOST_IXON    0002000
+#define HOST_IXANY   0004000
+#define HOST_IXOFF   0010000
+#define HOST_IMAXBEL 0020000
+#define HOST_IUTF8   0040000
+
+#define HOST_OPOST  0000001
+#define HOST_OLCUC  0000002
+#define HOST_ONLCR  0000004
+#define HOST_OCRNL  0000010
+#define HOST_ONOCR  0000020
+#define HOST_ONLRET 0000040
+#define HOST_OFILL  0000100
+#define HOST_OFDEL  0000200
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_XOPEN_SOURCE)
+#define HOST_NLDLY  0000400
+#define HOST_NL0    0000000
+#define HOST_NL1    0000400
+#define HOST_CRDLY  0003000
+#define HOST_CR0    0000000
+#define HOST_CR1    0001000
+#define HOST_CR2    0002000
+#define HOST_CR3    0003000
+#define HOST_TABDLY 0014000
+#define HOST_TAB0   0000000
+#define HOST_TAB1   0004000
+#define HOST_TAB2   0010000
+#define HOST_TAB3   0014000
+#define HOST_BSDLY  0020000
+#define HOST_BS0    0000000
+#define HOST_BS1    0020000
+#define HOST_FFDLY  0100000
+#define HOST_FF0    0000000
+#define HOST_FF1    0100000
+#endif
+
+#define HOST_VTDLY  0040000
+#define HOST_VT0    0000000
+#define HOST_VT1    0040000
+
+#define HOST_B0       0000000
+#define HOST_B50      0000001
+#define HOST_B75      0000002
+#define HOST_B110     0000003
+#define HOST_B134     0000004
+#define HOST_B150     0000005
+#define HOST_B200     0000006
+#define HOST_B300     0000007
+#define HOST_B600     0000010
+#define HOST_B1200    0000011
+#define HOST_B1800    0000012
+#define HOST_B2400    0000013
+#define HOST_B4800    0000014
+#define HOST_B9600    0000015
+#define HOST_B19200   0000016
+#define HOST_B38400   0000017
+
+#define HOST_B57600   0010001
+#define HOST_B115200  0010002
+#define HOST_B230400  0010003
+#define HOST_B460800  0010004
+#define HOST_B500000  0010005
+#define HOST_B576000  0010006
+#define HOST_B921600  0010007
+#define HOST_B1000000 0010010
+#define HOST_B1152000 0010011
+#define HOST_B1500000 0010012
+#define HOST_B2000000 0010013
+#define HOST_B2500000 0010014
+#define HOST_B3000000 0010015
+#define HOST_B3500000 0010016
+#define HOST_B4000000 0010017
+
+#define HOST_CSIZE  0000060
+#define HOST_CS5    0000000
+#define HOST_CS6    0000020
+#define HOST_CS7    0000040
+#define HOST_CS8    0000060
+#define HOST_CSTOPB 0000100
+#define HOST_CREAD  0000200
+#define HOST_PARENB 0000400
+#define HOST_PARODD 0001000
+#define HOST_HUPCL  0002000
+#define HOST_CLOCAL 0004000
+
+#define HOST_ISIG   0000001
+#define HOST_ICANON 0000002
+#define HOST_ECHO   0000010
+#define HOST_ECHOE  0000020
+#define HOST_ECHOK  0000040
+#define HOST_ECHONL 0000100
+#define HOST_NOFLSH 0000200
+#define HOST_TOSTOP 0000400
+#define HOST_IEXTEN 0100000
+
+#define HOST_TCOOFF 0
+#define HOST_TCOON  1
+#define HOST_TCIOFF 2
+#define HOST_TCION  3
+
+#define HOST_TCIFLUSH  0
+#define HOST_TCOFLUSH  1
+#define HOST_TCIOFLUSH 2
+
+#define HOST_TCSANOW   0
+#define HOST_TCSADRAIN 1
+#define HOST_TCSAFLUSH 2
+
+#define HOST_EXTA    0000016
+#define HOST_EXTB    0000017
+#define HOST_CBAUD   0010017
+#define HOST_CBAUDEX 0010000
+#define HOST_CIBAUD  002003600000
+#define HOST_CMSPAR  010000000000
+#define HOST_CRTSCTS 020000000000
+
+#define HOST_XCASE   0000004
+#define HOST_ECHOCTL 0001000
+#define HOST_ECHOPRT 0002000
+#define HOST_ECHOKE  0004000
+#define HOST_FLUSHO  0010000
+#define HOST_PENDIN  0040000
+#define HOST_EXTPROC 0200000
+
+#define HOST_XTABS  0014000
+
+#define HOST_TCGETS		0x5401
+#define HOST_TCSETS		0x5402
+#define HOST_TCSETSW		0x5403
+#define HOST_TCSETSF		0x5404