1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
// serial console support
//
// Copyright (C) 2016 Gerd Hoffmann <kraxel@redhat.com>
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
#include "biosvar.h" // SET_BDA
#include "bregs.h" // struct bregs
#include "stacks.h" // yield
#include "output.h" // dprintf
#include "util.h" // irqtimer_calc_ticks
#include "string.h" // memcpy
#include "romfile.h" // romfile_loadint
#include "hw/serialio.h" // SEROFF_IER
#include "cp437.h"
static u8 video_rows(void)
{
return GET_BDA(video_rows)+1;
}
static u8 video_cols(void)
{
return GET_BDA(video_cols);
}
static u8 cursor_pos_col(void)
{
u16 pos = GET_BDA(cursor_pos[0]);
return pos & 0xff;
}
static u8 cursor_pos_row(void)
{
u16 pos = GET_BDA(cursor_pos[0]);
return (pos >> 8) & 0xff;
}
static void cursor_pos_set(u8 row, u8 col)
{
u16 pos = ((u16)row << 8) | col;
SET_BDA(cursor_pos[0], pos);
}
/****************************************************************
* serial console output
****************************************************************/
VARLOW u16 sercon_port;
VARLOW u8 sercon_split;
VARLOW u8 sercon_enable;
VARFSEG struct segoff_s sercon_real_vga_handler;
/*
* We have a small output buffer here, for lazy output. That allows
* to avoid a whole bunch of control sequences for pointless cursor
* moves, so when logging the output it'll be *alot* less cluttered.
*
* sercon_char/attr is the actual output buffer.
* sercon_attr_last is the most recent attribute sent to the terminal.
* sercon_col_last is the most recent column sent to the terminal.
* sercon_row_last is the most recent row sent to the terminal.
*/
VARLOW u8 sercon_attr_last;
VARLOW u8 sercon_col_last;
VARLOW u8 sercon_row_last;
VARLOW u8 sercon_char;
VARLOW u8 sercon_attr = 0x07;
static VAR16 u8 sercon_cmap[8] = { '0', '4', '2', '6', '1', '5', '3', '7' };
static int sercon_splitmode(void)
{
return GET_LOW(sercon_split);
}
static void sercon_putchar(u8 chr)
{
u16 addr = GET_LOW(sercon_port);
u32 end = irqtimer_calc_ticks(0x0a);
#if 0
/* for visual control sequence debugging */
if (chr == '\x1b')
chr = '*';
#endif
for (;;) {
u8 lsr = inb(addr+SEROFF_LSR);
if ((lsr & 0x60) == 0x60) {
// Success - can write data
outb(chr, addr+SEROFF_DATA);
break;
}
if (irqtimer_check(end)) {
break;
}
yield();
}
}
static void sercon_term_reset(void)
{
sercon_putchar('\x1b');
sercon_putchar('c');
}
static void sercon_term_clear_screen(void)
{
sercon_putchar('\x1b');
sercon_putchar('[');
sercon_putchar('2');
sercon_putchar('J');
}
static void sercon_term_no_linewrap(void)
{
sercon_putchar('\x1b');
sercon_putchar('[');
sercon_putchar('?');
sercon_putchar('7');
sercon_putchar('l');
}
static void sercon_term_cursor_goto(u8 row, u8 col)
{
row++; col++;
sercon_putchar('\x1b');
sercon_putchar('[');
sercon_putchar('0' + row / 10);
sercon_putchar('0' + row % 10);
sercon_putchar(';');
sercon_putchar('0' + col / 10);
sercon_putchar('0' + col % 10);
sercon_putchar('H');
}
static void sercon_term_set_color(u8 fg, u8 bg, u8 bold)
{
sercon_putchar('\x1b');
sercon_putchar('[');
sercon_putchar('0');
if (fg != 7) {
sercon_putchar(';');
sercon_putchar('3');
sercon_putchar(GET_GLOBAL(sercon_cmap[fg & 7]));
}
if (bg != 0) {
sercon_putchar(';');
sercon_putchar('4');
sercon_putchar(GET_GLOBAL(sercon_cmap[bg & 7]));
}
if (bold) {
sercon_putchar(';');
sercon_putchar('1');
}
sercon_putchar('m');
}
static void sercon_set_attr(u8 attr)
{
if (attr == GET_LOW(sercon_attr_last))
return;
SET_LOW(sercon_attr_last, attr);
sercon_term_set_color((attr >> 0) & 7,
(attr >> 4) & 7,
attr & 0x08);
}
static void sercon_print_utf8(u8 chr)
{
u16 unicode = cp437_to_unicode(chr);
if (unicode < 0x7f) {
sercon_putchar(unicode);
} else if (unicode < 0x7ff) {
sercon_putchar(0xc0 | ((unicode >> 6) & 0x1f));
sercon_putchar(0x80 | ((unicode >> 0) & 0x3f));
} else {
sercon_putchar(0xe0 | ((unicode >> 12) & 0x0f));
sercon_putchar(0x80 | ((unicode >> 6) & 0x3f));
sercon_putchar(0x80 | ((unicode >> 0) & 0x3f));
}
}
static void sercon_cursor_pos_set(u8 row, u8 col)
{
if (!sercon_splitmode()) {
cursor_pos_set(row, col);
} else {
/* let vgabios update cursor */
}
}
static void sercon_lazy_cursor_sync(void)
{
u8 row = cursor_pos_row();
u8 col = cursor_pos_col();
if (GET_LOW(sercon_row_last) == row &&
GET_LOW(sercon_col_last) == col)
return;
if (col == 0 && GET_LOW(sercon_row_last) <= row) {
if (GET_LOW(sercon_col_last) != 0) {
sercon_putchar('\r');
SET_LOW(sercon_col_last, 0);
}
while (GET_LOW(sercon_row_last) < row) {
sercon_putchar('\n');
SET_LOW(sercon_row_last, GET_LOW(sercon_row_last)+1);
}
if (GET_LOW(sercon_row_last) == row &&
GET_LOW(sercon_col_last) == col)
return;
}
sercon_term_cursor_goto(row, col);
SET_LOW(sercon_row_last, row);
SET_LOW(sercon_col_last, col);
}
static void sercon_lazy_flush(void)
{
u8 chr, attr;
chr = GET_LOW(sercon_char);
attr = GET_LOW(sercon_attr);
if (chr) {
sercon_set_attr(attr);
sercon_print_utf8(chr);
SET_LOW(sercon_col_last, GET_LOW(sercon_col_last) + 1);
}
sercon_lazy_cursor_sync();
SET_LOW(sercon_attr, 0x07);
SET_LOW(sercon_char, 0x00);
}
static void sercon_lazy_cursor_update(u8 row, u8 col)
{
sercon_cursor_pos_set(row, col);
SET_LOW(sercon_row_last, row);
SET_LOW(sercon_col_last, col);
}
static void sercon_lazy_backspace(void)
{
u8 col;
sercon_lazy_flush();
col = cursor_pos_col();
if (col > 0) {
sercon_putchar(8);
sercon_lazy_cursor_update(cursor_pos_row(), col-1);
}
}
static void sercon_lazy_cr(void)
{
sercon_cursor_pos_set(cursor_pos_row(), 0);
}
static void sercon_lazy_lf(void)
{
u8 row;
row = cursor_pos_row() + 1;
if (row >= video_rows()) {
/* scrolling up */
row = video_rows()-1;
if (GET_LOW(sercon_row_last) > 0) {
SET_LOW(sercon_row_last, GET_LOW(sercon_row_last) - 1);
}
}
sercon_cursor_pos_set(row, cursor_pos_col());
}
static void sercon_lazy_move_cursor(void)
{
u8 col;
col = cursor_pos_col() + 1;
if (col >= video_cols()) {
sercon_lazy_cr();
sercon_lazy_lf();
} else {
sercon_cursor_pos_set(cursor_pos_row(), col);
}
}
static void sercon_lazy_putchar(u8 chr, u8 attr, u8 teletype)
{
if (cursor_pos_row() != GET_LOW(sercon_row_last) ||
cursor_pos_col() != GET_LOW(sercon_col_last)) {
sercon_lazy_flush();
}
SET_LOW(sercon_char, chr);
if (teletype)
sercon_lazy_move_cursor();
else
SET_LOW(sercon_attr, attr);
}
/* Set video mode */
static void sercon_1000(struct bregs *regs)
{
u8 clearscreen = !(regs->al & 0x80);
u8 mode = regs->al & 0x7f;
u8 rows, cols;
if (!sercon_splitmode()) {
switch (mode) {
case 0x00:
case 0x01:
case 0x04: /* 320x200 */
case 0x05: /* 320x200 */
cols = 40;
rows = 25;
regs->al = 0x30;
break;
case 0x02:
case 0x03:
case 0x06: /* 640x200 */
case 0x07:
default:
cols = 80;
rows = 25;
regs->al = 0x30;
break;
}
cursor_pos_set(0, 0);
SET_BDA(video_mode, mode);
SET_BDA(video_cols, cols);
SET_BDA(video_rows, rows-1);
SET_BDA(cursor_type, 0x0007);
} else {
/* let vgabios handle mode init */
}
SET_LOW(sercon_enable, mode <= 0x07);
SET_LOW(sercon_col_last, 0);
SET_LOW(sercon_row_last, 0);
SET_LOW(sercon_attr_last, 0);
sercon_term_reset();
sercon_term_no_linewrap();
if (clearscreen)
sercon_term_clear_screen();
}
/* Set text-mode cursor shape */
static void sercon_1001(struct bregs *regs)
{
/* show/hide cursor? */
SET_BDA(cursor_type, regs->cx);
}
/* Set cursor position */
static void sercon_1002(struct bregs *regs)
{
sercon_cursor_pos_set(regs->dh, regs->dl);
}
/* Get cursor position */
static void sercon_1003(struct bregs *regs)
{
regs->cx = GET_BDA(cursor_type);
regs->dh = cursor_pos_row();
regs->dl = cursor_pos_col();
}
/* Scroll up window */
static void sercon_1006(struct bregs *regs)
{
sercon_lazy_flush();
if (regs->al == 0) {
/* clear rect, do only in case this looks like a fullscreen clear */
if (regs->ch == 0 &&
regs->cl == 0 &&
regs->dh == video_rows()-1 &&
regs->dl == video_cols()-1) {
sercon_set_attr(regs->bh);
sercon_term_clear_screen();
}
} else {
sercon_putchar('\r');
sercon_putchar('\n');
}
}
/* Read character and attribute at cursor position */
static void sercon_1008(struct bregs *regs)
{
regs->ah = 0x07;
regs->bh = ' ';
}
/* Write character and attribute at cursor position */
static void sercon_1009(struct bregs *regs)
{
u16 count = regs->cx;
if (count == 1) {
sercon_lazy_putchar(regs->al, regs->bl, 0);
} else if (regs->al == 0x20 &&
video_rows() * video_cols() == count &&
cursor_pos_row() == 0 &&
cursor_pos_col() == 0) {
/* override everything with spaces -> this is clear screen */
sercon_lazy_flush();
sercon_set_attr(regs->bl);
sercon_term_clear_screen();
} else {
sercon_lazy_flush();
sercon_set_attr(regs->bl);
while (count) {
sercon_print_utf8(regs->al);
count--;
}
sercon_term_cursor_goto(cursor_pos_row(),
cursor_pos_col());
}
}
/* Teletype output */
static void sercon_100e(struct bregs *regs)
{
switch (regs->al) {
case 7:
sercon_putchar(0x07);
break;
case 8:
sercon_lazy_backspace();
break;
case '\r':
sercon_lazy_cr();
break;
case '\n':
sercon_lazy_lf();
break;
default:
sercon_lazy_putchar(regs->al, 0, 1);
break;
}
}
/* Get current video mode */
static void sercon_100f(struct bregs *regs)
{
regs->al = GET_BDA(video_mode);
regs->ah = GET_BDA(video_cols);
}
/* VBE 2.0 */
static void sercon_104f(struct bregs *regs)
{
if (!sercon_splitmode()) {
regs->ax = 0x0100;
} else {
// Disable sercon entry point on any vesa modeset
if (regs->al == 0x00)
SET_LOW(sercon_enable, 0);
}
}
static void sercon_10XX(struct bregs *regs)
{
warn_unimplemented(regs);
}
void VISIBLE16
handle_sercon(struct bregs *regs)
{
if (!CONFIG_SERCON)
return;
if (!GET_LOW(sercon_port))
return;
switch (regs->ah) {
case 0x01:
case 0x02:
case 0x03:
case 0x08:
case 0x0f:
if (sercon_splitmode())
/* nothing, vgabios handles it */
return;
}
switch (regs->ah) {
case 0x00: sercon_1000(regs); break;
case 0x01: sercon_1001(regs); break;
case 0x02: sercon_1002(regs); break;
case 0x03: sercon_1003(regs); break;
case 0x06: sercon_1006(regs); break;
case 0x08: sercon_1008(regs); break;
case 0x09: sercon_1009(regs); break;
case 0x0e: sercon_100e(regs); break;
case 0x0f: sercon_100f(regs); break;
case 0x4f: sercon_104f(regs); break;
default: sercon_10XX(regs); break;
}
}
void sercon_setup(void)
{
if (!CONFIG_SERCON)
return;
struct segoff_s seabios, vgabios;
u16 addr;
addr = romfile_loadint("etc/sercon-port", 0);
if (!addr)
return;
dprintf(1, "sercon: using ioport 0x%x\n", addr);
vgabios = GET_IVT(0x10);
seabios = FUNC16(entry_10);
if (vgabios.seg != seabios.seg ||
vgabios.offset != seabios.offset) {
dprintf(1, "sercon: configuring in splitmode (vgabios %04x:%04x)\n",
vgabios.seg, vgabios.offset);
sercon_real_vga_handler = vgabios;
SET_LOW(sercon_split, 1);
} else {
dprintf(1, "sercon: configuring as primary display\n");
sercon_real_vga_handler = seabios;
}
SET_IVT(0x10, FUNC16(entry_sercon));
SET_LOW(sercon_port, addr);
outb(0x03, addr + SEROFF_LCR); // 8N1
outb(0x01, addr + 0x02); // enable fifo
}
/****************************************************************
* serial input
****************************************************************/
VARLOW u8 rx_buf[16];
VARLOW u8 rx_bytes;
static VAR16 struct {
char seq[4];
u8 len;
u16 keycode;
} termseq[] = {
{ .seq = "OP", .len = 2, .keycode = 0x3b00 }, // F1
{ .seq = "OQ", .len = 2, .keycode = 0x3c00 }, // F2
{ .seq = "OR", .len = 2, .keycode = 0x3d00 }, // F3
{ .seq = "OS", .len = 2, .keycode = 0x3e00 }, // F4
{ .seq = "[15~", .len = 4, .keycode = 0x3f00 }, // F5
{ .seq = "[17~", .len = 4, .keycode = 0x4000 }, // F6
{ .seq = "[18~", .len = 4, .keycode = 0x4100 }, // F7
{ .seq = "[19~", .len = 4, .keycode = 0x4200 }, // F8
{ .seq = "[20~", .len = 4, .keycode = 0x4300 }, // F9
{ .seq = "[21~", .len = 4, .keycode = 0x4400 }, // F10
{ .seq = "[23~", .len = 4, .keycode = 0x5700 }, // F11
{ .seq = "[24~", .len = 4, .keycode = 0x5800 }, // F12
{ .seq = "[2~", .len = 3, .keycode = 0x52e0 }, // insert
{ .seq = "[3~", .len = 3, .keycode = 0x53e0 }, // delete
{ .seq = "[5~", .len = 3, .keycode = 0x49e0 }, // page up
{ .seq = "[6~", .len = 3, .keycode = 0x51e0 }, // page down
{ .seq = "[A", .len = 2, .keycode = 0x48e0 }, // up
{ .seq = "[B", .len = 2, .keycode = 0x50e0 }, // down
{ .seq = "[C", .len = 2, .keycode = 0x4de0 }, // right
{ .seq = "[D", .len = 2, .keycode = 0x4be0 }, // left
{ .seq = "[H", .len = 2, .keycode = 0x47e0 }, // home
{ .seq = "[F", .len = 2, .keycode = 0x4fe0 }, // end
};
static void shiftbuf(int remove)
{
int i, remaining;
remaining = GET_LOW(rx_bytes) - remove;
SET_LOW(rx_bytes, remaining);
for (i = 0; i < remaining; i++)
SET_LOW(rx_buf[i], GET_LOW(rx_buf[i + remove]));
}
static int cmpbuf(int seq)
{
int chr, len;
len = GET_GLOBAL(termseq[seq].len);
if (GET_LOW(rx_bytes) < len + 1)
return 0;
for (chr = 0; chr < len; chr++)
if (GET_GLOBAL(termseq[seq].seq[chr]) != GET_LOW(rx_buf[chr + 1]))
return 0;
return 1;
}
static int findseq(void)
{
int seq;
for (seq = 0; seq < ARRAY_SIZE(termseq); seq++)
if (cmpbuf(seq))
return seq;
return -1;
}
void
sercon_check_event(void)
{
if (!CONFIG_SERCON)
return;
u16 addr = GET_LOW(sercon_port);
u16 keycode;
u8 byte, count = 0;
int seq, chr;
// check to see if there is a active serial port
if (!addr)
return;
if (inb(addr + SEROFF_LSR) == 0xFF)
return;
// flush pending output
sercon_lazy_flush();
// read all available data
while (inb(addr + SEROFF_LSR) & 0x01) {
byte = inb(addr + SEROFF_DATA);
if (GET_LOW(rx_bytes) < sizeof(rx_buf)) {
SET_LOW(rx_buf[rx_bytes], byte);
SET_LOW(rx_bytes, GET_LOW(rx_bytes) + 1);
count++;
}
}
for (;;) {
// no (more) input data
if (!GET_LOW(rx_bytes))
return;
// lookup escape sequences
if (GET_LOW(rx_bytes) > 1 && GET_LOW(rx_buf[0]) == 0x1b) {
seq = findseq();
if (seq >= 0) {
enqueue_key(GET_GLOBAL(termseq[seq].keycode));
shiftbuf(GET_GLOBAL(termseq[seq].len) + 1);
continue;
}
}
// Seems we got a escape sequence we didn't recognise.
// -> If we received data wait for more, maybe it is just incomplete.
if (GET_LOW(rx_buf[0]) == 0x1b && count)
return;
// Handle input as individual char.
chr = GET_LOW(rx_buf[0]);
keycode = ascii_to_keycode(chr);
if (keycode)
enqueue_key(keycode);
shiftbuf(1);
}
}
|