2021-03-13 17:33:53 +00:00
|
|
|
#include <errno.h>
|
2019-03-12 23:31:16 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define COM1 0x3f8
|
|
|
|
#define COM2 0x2f8
|
|
|
|
#define COM3 0x3e8
|
|
|
|
#define COM4 0x2e8
|
|
|
|
|
|
|
|
#define SPEEDNOOP 0
|
|
|
|
#define SPEED9600 3
|
|
|
|
#define SPEED19200 4
|
|
|
|
#define SPEED57600 6
|
|
|
|
#define SPEED115200 7
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
struct termios tty;
|
2021-03-13 17:33:53 +00:00
|
|
|
struct termios tty2;
|
|
|
|
struct winsize ws;
|
|
|
|
unsigned width, height;
|
2019-03-12 23:31:16 +00:00
|
|
|
int ttyf;
|
2021-03-13 17:33:53 +00:00
|
|
|
int tmpi;
|
2019-03-12 23:31:16 +00:00
|
|
|
int currspeed;
|
2021-03-13 17:33:53 +00:00
|
|
|
int flags;
|
2019-03-12 23:31:16 +00:00
|
|
|
speed_t cspeed;
|
|
|
|
char buff[128];
|
2021-03-13 17:33:53 +00:00
|
|
|
int bufflen;
|
|
|
|
fd_set set;
|
|
|
|
struct timeval timeout;
|
2020-04-15 20:50:33 +00:00
|
|
|
char* offset;
|
2019-03-12 23:31:16 +00:00
|
|
|
uint64_t address;
|
2021-03-13 17:33:53 +00:00
|
|
|
bufflen = 0;
|
|
|
|
tmpi = open("/sys/firmware/acpi/tables/SPCR", O_RDONLY);
|
|
|
|
if (tmpi < 0) {
|
2019-03-12 23:31:16 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2021-03-13 17:33:53 +00:00
|
|
|
if (read(tmpi, buff, 80) < 80) {
|
2019-03-12 23:31:16 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2021-03-13 17:33:53 +00:00
|
|
|
close(tmpi);
|
2019-03-12 23:31:16 +00:00
|
|
|
if (buff[8] != 2) exit(0); //revision 2
|
|
|
|
if (buff[36] != 0) exit(0); //16550 only
|
|
|
|
if (buff[40] != 1) exit(0); //IO only
|
|
|
|
address = *(uint64_t *)(buff + 44);
|
|
|
|
currspeed = buff[58];
|
2020-04-15 20:50:33 +00:00
|
|
|
offset = buff + 10;
|
2019-03-12 23:31:16 +00:00
|
|
|
if (address == COM1) {
|
|
|
|
strncpy(buff, "/dev/ttyS0", 128);
|
|
|
|
} else if (address == COM2) {
|
|
|
|
strncpy(buff, "/dev/ttyS1", 128);
|
|
|
|
} else if (address == COM3) {
|
|
|
|
strncpy(buff, "/dev/ttyS2", 128);
|
|
|
|
} else if (address == COM4) {
|
|
|
|
strncpy(buff, "/dev/ttyS3", 128);
|
|
|
|
} else {
|
|
|
|
exit(0);
|
|
|
|
}
|
2020-09-24 12:26:37 +00:00
|
|
|
ttyf = open(buff, O_RDWR | O_NOCTTY);
|
2019-03-12 23:31:16 +00:00
|
|
|
if (currspeed == SPEED9600) {
|
|
|
|
cspeed = B9600;
|
2020-04-15 20:50:33 +00:00
|
|
|
strcpy(offset, ",9600");
|
2019-03-12 23:31:16 +00:00
|
|
|
} else if (currspeed == SPEED19200) {
|
|
|
|
cspeed = B19200;
|
2020-04-15 20:50:33 +00:00
|
|
|
strcpy(offset, ",19200");
|
2019-03-12 23:31:16 +00:00
|
|
|
} else if (currspeed == SPEED57600) {
|
|
|
|
cspeed = B57600;
|
2020-04-15 20:50:33 +00:00
|
|
|
strcpy(offset, ",57600");
|
2019-03-12 23:31:16 +00:00
|
|
|
} else if (currspeed == SPEED115200) {
|
|
|
|
cspeed = B115200;
|
2020-04-15 20:50:33 +00:00
|
|
|
strcpy(offset, ",115200");
|
2019-03-12 23:31:16 +00:00
|
|
|
} else {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
tcgetattr(ttyf, &tty);
|
|
|
|
if (cspeed) {
|
2021-03-13 17:33:53 +00:00
|
|
|
cfsetospeed(&tty, cspeed);
|
|
|
|
cfsetispeed(&tty, cspeed);
|
2019-03-12 23:31:16 +00:00
|
|
|
}
|
2021-03-13 17:33:53 +00:00
|
|
|
printf("%s\n", buff);
|
|
|
|
tcgetattr(ttyf, &tty2);
|
|
|
|
cfmakeraw(&tty2);
|
|
|
|
tcsetattr(ttyf, TCSANOW, &tty2);
|
|
|
|
flags = fcntl(ttyf, F_GETFL, 0);
|
|
|
|
fcntl(ttyf, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
while (read(ttyf, buff, 64) > 0) {
|
|
|
|
// Drain any pending reads
|
|
|
|
}
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 500000;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(ttyf, &set);
|
|
|
|
write(ttyf, "\0337\033[999;999H\033[6n\0338", 18);
|
|
|
|
while (select(ttyf + 1, &set, NULL, NULL, &timeout) > 0) {
|
|
|
|
if ((tmpi = read(ttyf, buff + bufflen, 127 - bufflen)) < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bufflen += tmpi;
|
|
|
|
buff[bufflen] = 0;
|
|
|
|
if (strchr(buff, 'R')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fcntl(ttyf, F_SETFL, flags);
|
|
|
|
ws.ws_xpixel = 0;
|
|
|
|
ws.ws_ypixel = 0;
|
|
|
|
if (sscanf(buff, "\033[%u;%uR", &height, &width) == 2) {
|
|
|
|
ws.ws_col = width;
|
|
|
|
ws.ws_row = height;
|
|
|
|
} else {
|
|
|
|
ws.ws_col = 100;
|
|
|
|
ws.ws_row = 31;
|
|
|
|
}
|
|
|
|
ioctl(ttyf, TIOCSWINSZ, &ws);
|
2019-03-12 23:31:16 +00:00
|
|
|
tcsetattr(ttyf, TCSANOW, &tty);
|
|
|
|
ioctl(ttyf, TIOCCONS, 0);
|
|
|
|
}
|
|
|
|
|