-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsgabios.S
2434 lines (2280 loc) · 79.2 KB
/
sgabios.S
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sgabios.h"
#define BUILD_CL "$Id: sgabios.S 8 2010-04-22 00:03:40Z nlaredo $"
.code16
.text
.section ".init","ax"
.globl _start
.type _start,@object
_start:
/* option rom header */
.byte 0x55
.byte 0xaa
.byte _rom_size_byte
.size _start, .-_start
.globl legacy_entry
.type legacy_entry,@function
legacy_entry:
jmp sga_init
/* pnp entry here to avoid changing PnP table as code moves */
pnp_init:
jmp pnp_sga_init
/*
* do_old_int10h
*
* Patched at option rom init to be a far jump to old int 10h isr
*
*/
do_old_int10h:
.byte 0xea /* jmp absolute segment:offset */
old_int10h: /* store what was at offset 0x40 */
.word 0xf065 /* placeholder for chained ISR offset */
/* if the chained segment is detected as 0xc000, use 80 cols only */
/* since it's assumed that a vga card is attached and 80 cols max */
old_int10h_seg:
.word 0xf000 /* placeholder for chained ISR segment */
/*
* do_old_int16h
*
* Patched at option rom init to be a far jump to old int 16h isr
*
*/
do_old_int16h:
.byte 0xea /* jmp absolute segment:offset */
old_int16h: /* store what was at offset 0x58 */
.word 0xe82e /* placeholder for chained ISR offset */
.word 0xf000 /* placeholder for chained ISR segment */
.org 0x18
.word 0 /* offset to PCI data, 0 = none */
.word pnp_table /* offset to PnP expansion header */
.org 0x20
pnp_table:
/* FIXME: **** PnP header currently disabled by PoO **** */
/* legacy entry only called once, PnP entry called multiple times */
/* The code isn't yet written to deal with multiple inits properly */
.ascii "$PoO" /* PnP expansion header signature */
.byte 1 /* structure revision */
.byte 2 /* length in 16-byte increments */
.word 0 /* offset of next header, 0 if none */
.byte 0 /* reserved */
.byte 0x52 /* checksum - update manually! FIXME */
.long 0 /* device identifier */
.word mfg_string /* pointer to manufacturer string */
.word prod_string /* pointer to product name string */
.byte 3, 0x80, 0x80 /* device type code = other display */
.byte 0xe3 /* device indicators, kbd/display dev */
.word 0 /* boot connection vector, 0 if none */
.word 0 /* disconnect vector, 0 if none */
.word pnp_init /* bootstrap entry vector */
.word 0 /* reserved */
.word 0 /* static resource information vector */
/* WARNING: changing mfg_string / prod_string locations will */
/* affect pnp table above -- recalculate checksum manually! */
mfg_string:
.asciz "Google, Inc."
prod_string:
.ascii "Serial Graphics Adapter "
build_date:
.asciz BUILD_SHORT_DATE
long_version:
.ascii "SGABIOS "
.ascii BUILD_CL
.ascii " ("
.ascii BUILD_USER
.ascii "@"
.ascii BUILD_HOST
.ascii ") "
.asciz BUILD_DATE
term_cols:
.byte 80 /* overwritten at rom init with detected value */
term_rows:
.byte 24 /* overwritten at rom init with detected value */
term_init_string: /* terminal reply: \033[n;mR n=row, m=col */
.asciz "\033[1;256r\033[256;256H\033[6n"
/* reset the scroll, move to col 256, row 256, ask current position */
/* bios cursor positions >255 rows or cols can't be used anyway */
term_info:
.asciz "Term: "
ebda_info:
.asciz "EBDA: "
/*
* do_old_irq3 - exception 0x0b, int 0x0a
*
* Patched at option rom init to be a far jump to old irq 3 isr
*
*/
do_old_irq3:
.byte 0xea /* jmp absolute segment:offset */
old_irq3: /* store what was at offset 0x28 */
.word 0xeef3 /* placeholder for chained ISR offset */
.word 0xf000 /* placeholder for chained ISR segment */
/*
* do_old_irq4 - exception 0x0c, int 0x0b
*
* Patched at option rom init to be a far jump to old irq 4 isr
*
*/
do_old_irq4:
.byte 0xea /* jmp absolute segment:offset */
old_irq4: /* store what was at offset 0x2c */
.word 0xeef3 /* placeholder for chained ISR offset */
.word 0xf000 /* placeholder for chained ISR segment */
/*
* do_old_int14h
*
* Patched at option rom init to be a far jump to old int 14h isr
*
*/
do_old_int14h:
.byte 0xea /* jmp absolute segment:offset */
old_int14h: /* store what was at offset 0x50 */
.word 0xe739 /* placeholder for chained ISR offset */
.word 0xf000 /* placeholder for chained ISR segment */
.align 16, 0xff /* aligning this table only makes hexdump prettier */
/* ascii -> scancode, bit 7=shifted, char < 32 = +ctrl */
/* except chars 8, 9, 13, 27 (bs, tab, enter, esc) */
/* most int16h consumers will probably never use */
ascii2scan:
/*00*/ .byte 0x00, 0x1e, 0x30, 0x2e, 0x20, 0x12, 0x21, 0x22
/*08*/ .byte 0x0e, 0x17, 0x24, 0x25, 0x26, 0x1c, 0x31, 0x18
/*10*/ .byte 0x19, 0x0f, 0x13, 0x1f, 0x14, 0x16, 0x2f, 0x11
/*18*/ .byte 0x2d, 0x15, 0x2c, 0x01, 0x2b, 0x1b, 0x87, 0x8c
/*20*/ .byte 0x39, 0x82, 0xa8, 0x84, 0x85, 0x86, 0x88, 0x28
/*28*/ .byte 0x8a, 0x8b, 0x89, 0x8d, 0x33, 0x0c, 0x34, 0x35
/*30*/ .byte 0x0b, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
/*38*/ .byte 0x09, 0x0a, 0xa7, 0x27, 0xb3, 0x0d, 0x34, 0xb5
/*40*/ .byte 0x83, 0x9e, 0xb0, 0xae, 0xa0, 0x92, 0xa1, 0xa2
/*48*/ .byte 0xa3, 0x97, 0xa4, 0xa5, 0xa6, 0xb2, 0xb1, 0x98
/*50*/ .byte 0x99, 0x90, 0x93, 0x9f, 0x94, 0x96, 0xaf, 0x91
/*58*/ .byte 0xad, 0x95, 0xac, 0x1a, 0x2b, 0x1b, 0x87, 0x8c
/*60*/ .byte 0x29, 0x1e, 0x30, 0x2e, 0x20, 0x12, 0x21, 0x22
/*68*/ .byte 0x23, 0x17, 0x24, 0x25, 0x26, 0x32, 0x31, 0x18
/*70*/ .byte 0x19, 0x10, 0x13, 0x1f, 0x14, 0x16, 0x2f, 0x11
/*78*/ .byte 0x2d, 0x15, 0x2c, 0x9a, 0xab, 0x9b, 0xa9, 0x0e
/* TABLES FOR NON-ASCII VGA CHARACTERS (CP437) TO ASCII */
/* Unicode at: http://en.wikipedia.org/wiki/Code_page_437 */
ctrl2ascii:
/* translate vga (CP437) first 32 characters to ascii */
/* for char 0, update the cursor position, but output nothing */
/* lilo uses this "trick" for a background attribute update */
.ascii "\0@@v***........*><|!PS-|^v><L-^v"
high2ascii:
/* translate vga (CP437) chars 0x80 to 0xff to ascii */
/* these characters are mostly to visually approximate */
/* line art characters will probably need tweaking */
/*80*/ .ascii "CueaaaaceeeiiiAAEaAooouuyOUcLYPf"
/*a0*/ .ascii "aiounNao?--24!<>###||||++||+++++"
/*c0*/ .ascii "+--|-+||++--|-+----++++++++#-||-"
/*e0*/ .ascii "abgpesut00osiye^=+><||-=...vn2* "
colortable:
/* vga text color is IRGB, ansi color is BGR */
/* this table is effectively a nibble bit-reverse */
.byte 0, 4, 2, 6, 1, 5, 3, 7
serial_port_base_address:
.word COM_BASE_ADDR
/* in-memory console log
*
* It's expected that the EBDA contains a magic signature
* like 0xdeadbabe, followed by a byte of flags, followed
* by a 32-bit buffer pointer, followed by a 16-bit start
* index, followed by a 16-bit end index, followed by 16-
* bit logged character count, followed by an 8-bit flag.
*/
#define MEMCONSOLE_BUFFER_SIZE 32768
#define MEMCONSOLE_SIGNATURE 0xdeadbabe
#define MEMCONSOLE_ENDINDEX_OFF 0x0b
#define SGABIOS_EBDA_SIGNATURE 0x00414753
memconsole_buffer_start: /* pulled from ebda struct */
.long 0x00000000 /* 0 = not found/no logging */
memconsole_ebda_deadbabe_offset: /* bytes from start of ebda */
.word 0x0000 /* 40:0e contains ebda seg */
sgabios_ebda_logbuf_offset: /* bytes from start of ebda */
.word 0x0000 /* 40:0e contains ebda seg */
/*
* setup_memconsole
*
* Initialize the option rom variables associated with logging
* of the legacy console output
*
* If these variables are left at zero, no logging will occur
*
* There are no parameters
* All registers except flags should be preserved
*/
setup_memconsole:
pushaw
pushw %ds
pushw %es
pushw $BDA_SEG
popw %ds /* ds = 0x40 */
pushw BDA_EBDA /* push word at 0x0e */
popw %es /* es = EBDA_SEG */
/* search for memconsole signature in ebda */
movl $MEMCONSOLE_SIGNATURE, %eax
xorw %di, %di /* start at zero */
movzbw %es:(%di), %cx /* cx = size of EBDA in KB */
shlw $8, %cx /* cx = (cx * 1024) / 4 */
cld
repnz
scasl /* search until sig found */
subw $4, %di /* scasl always increments di, undo */
cmpl %eax, %es:(%di) /* is signature here? */
jnz setup_memconsole_end /* bail if so */
movw %di, %cs:memconsole_ebda_deadbabe_offset /* save offset */
movl %es:5(%di), %eax /* get 32-bit buffer base address */
movl %eax, %cs:memconsole_buffer_start
setup_memconsole_end:
popw %es
popw %ds
popaw
ret
/*
* memconsole_log_char
*
* Log the character passed in %al to the next available memory
* console log position, if any.
*
* If memconsole_buffer_start is zero, no logging will occur
*
* %al = character to be logged
* All registers except flags should be preserved
*/
memconsole_log_char:
pushaw
pushw %ds
pushw %es
pushw %fs
pushw $BDA_SEG
popw %ds /* ds = 0x40 */
pushw BDA_EBDA /* push word at 0x0e */
popw %es /* es = EBDA_SEG */
movw %ax, %si /* %si = %al = byte to write */
movl %cs:memconsole_buffer_start, %ebp
movw %cs:memconsole_ebda_deadbabe_offset, %di
addw $MEMCONSOLE_ENDINDEX_OFF, %di /* %di points to char pos */
orl %ebp, %ebp
jz memconsole_log_tail /* bufptr==0, no logging */
movw %es:(%di), %bx /* bx = current position in buffer */
cmpw $MEMCONSOLE_BUFFER_SIZE, %bx /* at end of buffer? */
jnc memconsole_log_tail /* don't log any more if so */
cmpb $0xd, %al /* is the char CR? */
jz memconsole_log_tail /* if so, ignore it */
cmpb $0x8, %al /* is the char backspace? */
jnz memconsole_update_fsbase /* if not, log char as usual... */
orw %bx, %bx /* make sure ptr isn't already zero */
jz memconsole_log_tail /* if so, bail */
decw %bx /* else point to previous character */
jmp memconsole_update_end_ptr /* and go directly to save it */
memconsole_update_fsbase:
movl $0xc0000100, %ecx /* ecx = IA32_FS_BASE (AMD64+) */
rdmsr /* read what was there before */
pushl %eax /* save away previous FS_BASE eax */
pushl %edx /* save away previous FS_BASE edx */
xorl %edx, %edx /* clear high 32 bits */
movl %ebp, %eax /* eax = memconsole buffer start */
wrmsr /* fs_base = memconsole buffer start */
movw %si, %ax /* %ax = saved value on entry */
movb %al, %fs:(%bx) /* log character */
popl %edx /* restore previous FS_BASE edx */
popl %eax /* restore previous FS_BASE eax */
wrmsr /* write what was there before */
incw %bx /* update character count */
memconsole_update_end_ptr:
movw %bx, %es:(%di) /* save new end pointer */
addw $2, %di /* numchars stored at next word */
movw %bx, %es:(%di) /* save new numchar value */
memconsole_log_tail:
popw %fs
popw %es
popw %ds
popaw
ret
/* sgabioslog_setup_ebda
*
* SGABIOS makes its own 1KB EBDA allocation to save non-
* translated characters with associated cursor positions
* for the last 256 characters output. This is organized
* with 256 bytes reserved for houskeeping, 256 bytes for
* the raw character codes, and 512 bytes of 16bit cursor
* positions to record the associated position for each.
*
* The first 4 bytes contain "SGA\0" followed by a 16-bit
* size of the allocation in bytes, followed by a 16-bit
* index indicating the next spot to be overwritten.
*
* There are no parameters
* All registers should be preserved
*/
sgabioslog_setup_ebda:
pushf
pushaw
pushw %ds
pushw %es
pushw $BDA_SEG
popw %ds /* ds = 0x40 */
movw BDA_EBDA, %ax /* ax = old ebda segment from 0x0e */
subw $SGABIOS_EBDA_DELTA, %ax
movw %ax, %es /* es = new EBDA segment start */
cmpw $EBDA_MIN_SEG, %ax /* is there room for the allocation? */
jc sgabioslog_setup_ebda_tail /* if not, don't change anything */
cli /* paranoid in case irq uses EBDA */
movw %ax, BDA_EBDA /* save new EBDA segment start */
subw $SGABIOS_EBDA_KB, BDA_MEM_SIZE /* subtract extra allocation */
movw %ax, %ds /* ds = new EBDA segment start */
movw $SGABIOS_EBDA_BYTES, %si /* si = offset of first byte to move */
movzbw (%si), %cx /* cx = number of KB in EBDA */
addb $SGABIOS_EBDA_KB, (%si) /* update EBDA size in kb */
shlw $10, %cx /* cx = KB * 1024 = bytes in EBDA */
movw %cx, %cs:sgabios_ebda_logbuf_offset /* new ebda space */
xorw %di, %di /* di = new EBDA start */
cld
rep
movsb /* move ebda by SGABIOS_EBDA_BYTES */
movw %cs:sgabios_ebda_logbuf_offset, %bx /* bx = new buffer */
movl $SGABIOS_EBDA_SIGNATURE, (%bx) /* setup signature */
movw $SGABIOS_EBDA_BYTES, 4(%bx) /* bytes in new ebda buffer */
movw $0, 6(%bx) /* next log index, new ebda buffer */
sgabioslog_setup_ebda_tail:
popw %es
popw %ds
popaw
popf
ret
/*
* sgabioslog_save_char
*
* Like memconsole_log_char, except the original, untranslated
* character is expected to be given in the %al register.
*
* The original character and its corresponding cursor position
* are logged to the sgabios ebda memory allocation.
*
* %al = character to be logged
* All registers except flags should be preserved
*/
sgabioslog_save_char:
pushaw
pushw %ds
pushw %es
pushw $BDA_SEG
popw %ds /* ds = 0x40 */
pushw BDA_EBDA /* push word at 0x0e */
popw %es /* es = EBDA_SEG */
movw %cs:sgabios_ebda_logbuf_offset, %di
orw %di, %di /* is offset zero? */
jz sgabioslog_save_tail /* if so, bail */
cmpl $SGABIOS_EBDA_SIGNATURE, %es:(%di)
jnz sgabioslog_save_tail /* bail if magic not found */
movw %es:6(%di), %bx /* bx = index of next char output */
movb %al, %es:SGABIOS_EBDA_LOG_START(%bx,%di) /* store character */
movzbw %bl, %ax /* %ax = next cursor buffer index */
shlw $1, %ax /* %ax = offset to cursor storage */
call get_current_cursor /* %dh = row, %dl = column */
addw $SGABIOS_EBDA_POS_START, %di /* cursor storage */
addw %ax, %di /* %di = next cursor storage offset */
movw %dx, %es:(%di) /* save position for logged char */
incw %bx /* point to next char to log */
cmpw $SGABIOS_EBDA_LOG_SIZE, %bx
jnz sgabioslog_save_index
xorw %bx, %bx /* wrap around to start */
sgabioslog_save_index:
movw %cs:sgabios_ebda_logbuf_offset, %di
movw %bx, %es:6(%di) /* save new index */
sgabioslog_save_tail:
popw %es
popw %ds
popaw
ret
/*
* sgabioslog_get_char
*
* Return the character at current cursor position, last recorded
* to sgabios ebda allocation, if available.
*
* If the current cursor postition contains one of the last 256 characters
* written to the ebda buffer, return that character, else return 0.
*
* If sgabios_ebdda_logbuf_offset is zero, %al will be 0 and zf set
*
* All registers except flags and %al should be preserved
*/
sgabioslog_get_char:
pushaw
movw %sp, %bp
movb $0, 14(%bp) /* %al on stack = 0 */
pushw %ds
pushw %es
pushw $BDA_SEG
popw %ds /* ds = 0x40 */
pushw BDA_EBDA /* push word at 0x0e */
popw %es /* es = EBDA_SEG */
movw %cs:sgabios_ebda_logbuf_offset, %di
orw %di, %di
jz sgabioslog_get_tail /* offset==0, no logging */
cmpl $SGABIOS_EBDA_SIGNATURE, %es:(%di)
jnz sgabioslog_get_tail /* bail if magic not found */
call get_current_cursor /* dh = row, dl = col */
std /* scan backwards in mem */
movw %es:6(%di), %bx /* bx = index of next char output */
decw %bx /* %bx = offset of last char in buf */
jnc sgabioslog_got_pos
addw $SGABIOS_EBDA_LOG_SIZE, %bx /* bx position wrap around */
sgabioslog_got_pos:
movw %bx, %ax /* %ax = last cursor pos written */
shlw $1, %ax /* %ax = offset of last cursor pos */
addw $SGABIOS_EBDA_POS_START, %di /* %di = first cursor position */
addw %ax, %di /* %di = offset in ebda */
movw %dx, %ax /* %ax = cursor pos to compare */
movw %bx, %cx /* %cx = positions before wrap */
jcxz sgabioslog_cmp_wrap /* if zero, try from end next */
repnz
scasw /* search until position match */
addw $2, %di /* scasd always decrements di, undo */
cmpw %ax, %es:(%di) /* did it really match? */
jz sgabioslog_cursor_match /* if so, do something */
sgabioslog_cmp_wrap:
movw %cs:sgabios_ebda_logbuf_offset, %di
addw $SGABIOS_EBDA_POS_LAST, %di /* %di = last cursor storage */
movw $SGABIOS_EBDA_LOG_SIZE, %cx /* %cx = compare all positions */
repnz
scasw /* search until position match */
addw $2, %di /* scasd always decrements di, undo */
cmpw %ax, %es:(%di) /* did it really match? */
jnz sgabioslog_get_tail /* if not, bail */
sgabioslog_cursor_match:
/* %di contains the EBDA offset of the matching position */
/* convert this into a memconsole offset */
subw $512, %di /* take off the storage offset */
subw %cs:sgabios_ebda_logbuf_offset, %di /* and ebda offset */
shrw $1, %di /* %di = char position index */
addw %cs:sgabios_ebda_logbuf_offset, %di /* add back ebda offset */
addw $SGABIOS_EBDA_LOG_START, %di /* and add back log offset */
movb %es:(%di), %al /* get related saved character */
movb %al, 14(%bp) /* %al on stack = logged char */
sgabioslog_get_tail:
popw %es
popw %ds
popaw
ret
/*
* multibyteinput
*
* When an escape key is detected, the input routines will attempt to
* capture as many characters as arrive up until a timeout, or six,
* whichever is less.
*
* This table is intended to decide what the characters after the
* initial escape key translate to in terms of high and low bytes
* that go into the keyboard buffer the high byte is the scancode,
* the low byte is ascii, but for special keys this is usually 0xe0
* or 0x00.
*
* This table is formatted so that the first word is a scancode +
* ascii pair (as returned by int 16h, ah = 10h or 11h). Immediately
* following is a nul-terminated ascii string to match in order to
* use the corresponding scancode+ascii word.
*
* The search through this table is terminated by a match or finding
* a 0 scancode+ascii word.
*
* FIXME: all the low bytes are now zero, get rid of them?
*/
multibyteinput:
.byte 0x3b /* F1 */
.asciz "[[A" /* F1/screen */
.byte 0x3b /* F1 */
.asciz "OP" /* F1/xterm/ansi */
.byte 0x3b /* F1 */
.asciz "[11~" /* F1/vt400 */
.byte 0x3c /* F2 */
.asciz "[[B" /* F2/screen */
.byte 0x3c /* F2 */
.asciz "OQ" /* F2/xterm/ansi */
.byte 0x3c /* F2 */
.asciz "[12~" /* F2/vt400 */
.byte 0x3d /* F3 */
.asciz "[[C" /* F3/screen */
.byte 0x3d /* F3 */
.asciz "OR" /* F3/xterm/ansi */
.byte 0x3d /* F3 */
.asciz "[13~" /* F3/vt400 */
.byte 0x3e /* F4 */
.asciz "[[D" /* F4/screen */
.byte 0x3e /* F4 */
.asciz "OS" /* F4/xterm/ansi */
.byte 0x3e /* F4 */
.asciz "[14~" /* F4/vt400 */
.byte 0x3f /* F5 */
.asciz "[[E" /* F5/screen */
.byte 0x3f /* F5 */
.asciz "[15~" /* F5/xterm */
.byte 0x3f /* F5 */
.asciz "OT" /* F5/ansi */
.byte 0x40 /* F6 */
.asciz "[17~" /* F6/screen/vt220/xterm/vt400 */
.byte 0x40 /* F6 */
.asciz "OU" /* F6/ansi */
.byte 0x41 /* F7 */
.asciz "[18~" /* F7/screen/vt220/xterm/vt400 */
.byte 0x41 /* F7 */
.asciz "OV" /* F7/ansi */
.byte 0x42 /* F8 */
.asciz "[19~" /* F8/screen/vt220/xterm/vt400 */
.byte 0x42 /* F8 */
.asciz "OW" /* F8/ansi */
.byte 0x43 /* F9 */
.asciz "[20~" /* F9/screen/vt220/xterm/vt400 */
.byte 0x43 /* F9 */
.asciz "OX" /* F9/ansi */
.byte 0x44 /* F10 */
.asciz "[21~" /* F10/screen/vt220/xterm/vt400 */
.byte 0x44 /* F10 */
.asciz "OY" /* F10/ansi */
.byte 0x85 /* F11 */
.asciz "[23~" /* F11/screen/xterm/vt400 */
.byte 0x85 /* F11 */
.asciz "OZ" /* F11/ansi */
.byte 0x86 /* F12 */
.asciz "[24~" /* F12/screen/xterm/vt400 */
.byte 0x52 /* Insert */
.asciz "[2~" /* Insert/screen/vt102/xterm */
.byte 0x53 /* Delete */
.asciz "[3~" /* Delete/screen/vt102/xterm */
.byte 0x4b /* Left */
.asciz "OD" /* Left/screen/vt102 */
.byte 0x4b /* Left */
.asciz "[D" /* Left/xterm */
.byte 0x47 /* Home */
.asciz "[1~" /* Home/screen/vt102 */
.byte 0x47 /* Home */
.asciz "[H" /* Home/xterm */
.byte 0x4f /* End */
.asciz "[4~" /* End/screen/vt102 */
.byte 0x4f /* End */
.asciz "[F" /* End/xterm */
.byte 0x48 /* Up */
.asciz "OA" /* Up/screen/vt102 app */
.byte 0x48 /* Up */
.asciz "[A" /* Up/xterm/vt102 ansi */
.byte 0x50 /* Down */
.asciz "OB" /* Down/screen/vt102 app */
.byte 0x50 /* Down */
.asciz "[B" /* Down/xterm/vt102 ansi */
.byte 0x49 /* PageUp */
.asciz "[5~" /* PageUp/screen/vt102/xterm */
.byte 0x51 /* PageDown */
.asciz "[6~" /* PageDown/screen/vt102/xterm */
.byte 0x4d /* Right */
.asciz "OC" /* Right/screen/vt102 app */
.byte 0x4d /* Right */
.asciz "[C" /* Right/xterm/vt102 ansi */
.byte 0 /* end of table marker */
/* init_serial_port
*
* Initialize serial port to 115200,8n1
* Serial interrupts disabled
*
* All registers except flags preserved
*/
init_serial_port:
pushw %ax
pushw %dx
pushw %bx
movw %cs:serial_port_base_address, %dx
addw $IER_OFFSET, %dx
xorb %al, %al
outb %al, %dx /* disable all serial interrupts */
addw $(LCR_OFFSET - IER_OFFSET), %dx /* LCR */
movb $(LCR_VALUE|LCR_DLAB), %al
outb %al, %dx /* enable divisor access */
movw %cs:serial_port_base_address, %dx
movw $(PORT_DIVISOR/PORT_SPEED), %bx
movb %bl, %al /* al = lsb of divisor */
outb %al, %dx /* set divisor latch lsb */
movb %bh, %al /* al = msb of divisor */
incw %dx
outb %al, %dx /* set divisor latch msb */
movw %cs:serial_port_base_address, %dx
addw $LCR_OFFSET, %dx
movb $LCR_VALUE, %al
outb %al, %dx /* disable divisor access */
addw $(MCR_OFFSET - LCR_OFFSET), %dx /* MCR */
movb $MCR_DTRRTS, %al
outb %al, %dx /* enable DTR + RTS */
movw %cs:serial_port_base_address, %dx
addw $FCR_OFFSET, %dx
movb $FCR_FIFO_ENABLE, %al
outb %al, %dx /* enable FIFOs */
popw %bx
popw %dx
popw %ax
ret
/* get_serial_lsr
*
* return serial line status register in %al
* return offset to serial port line status register io port in %dx
* all other registers except flags unchanged
*
* if status == 0xff return ZF=1, else return ZF=0
*/
get_serial_lsr:
movw %cs:serial_port_base_address, %dx
addw $LSR_OFFSET, %dx
inb %dx, %al
cmpb $0xff, %al
ret
/*
* get_byte
*
* get serial byte in %al, scancode in %ah [FIXME: EFI console input]
*
* all registers except %ax preserved
*
*/
get_byte:
pushw %dx
pushw %bx
next_serial_char:
call get_serial_lsr /* get serial lsr in %al */
jz get_byte_tail /* no port present... */
testb $1, %al /* bit 0 of LSR = 1 = data available */
jz get_byte_tail /* no input waiting */
/* new character found on serial port */
/* convert it to a scancode */
movw %cs:serial_port_base_address, %dx
inb %dx, %al /* al = serial input char */
testb $0x80, %al /* non-ascii char received? */
jnz next_serial_char /* throw char away */
movb %al, %dl /* dl = character read */
pushw %ds
pushw %cs
popw %ds /* ds = cs */
movw $ascii2scan, %bx /* table to translate ascii->scan */
xlatb /* translate char to scancode */
popw %ds
/* shift status is ignored at this point, may be used later */
andb $0x7f, %al /* strip shift status from table */
movb %al, %ah /* scancode goes in high byte */
movb %dl, %al /* "translated" ascii in lower byte */
cmpb $0x7f, %al /* Did the user transmit ascii DEL? */
jnz get_byte_not_del /* if not, don't do anything to al */
movb $0x08, %al /* else delete becomes backspace */
get_byte_not_del:
testw %ax, %ax /* clear zero flag */
get_byte_tail:
popw %bx
popw %dx
ret
/*
* poll_byte
*
* get serial byte in %al, scancode in %ah [FIXME: EFI console input]
* retry up to 65536 times for an expected input byte
*
* all registers except %ax preserved
*
*/
poll_byte:
pushw %cx
xorw %cx, %cx
poll_byte_retry:
inb $0xed, %al
call get_byte
loopz poll_byte_retry /* repeat while zf set or cx != 0 */
popw %cx
ret
/*
* get_multibyte
*
* after an escape character, poll for terminal keys that generate
* an escape code plus multiple bytes (up to four).
*
* if no byte is waiting, all registers preserved except flags
* if more bytes are waiting, all registers preserved except %ax and flags
*
*/
get_multibyte:
pushw %bp /* bp points to temp buffer on stack */
pushw %bx /* bx points to multibyteinput table */
pushw %cx /* cx will count chars */
pushw %ax /* ax will receive chars */
pushl $0 /* make space on stack for 4 chars */
xorw %cx, %cx /* cx = 0 */
movw %sp, %bp /* point bp at temp data */
call poll_byte /* is a character waiting? */
jz get_multibyte_tail /* if not, bail */
get_multibyte_store:
movb %al, (%bp) /* store char received */
incb %cl /* mark one char received */
incw %bp /* point to next char */
cmpb $4, %cl /* got enough chars? */
jz got_multibyte /* no strings longer than 4 chars */
call poll_byte /* is another char waiting? */
jnz get_multibyte_store /* store a new one if it's there */
got_multibyte:
movw $multibyteinput, %bx /* point to first scancode */
got_multibyte_findkey:
movw %sp, %bp /* bp = start of buffer */
movb %cs:(%bx), %ah /* ah = scancode */
incw %bx /* bx = start of test string */
orb %ah, %ah /* is it zero? */
jz get_multibyte_tail /* if so, bail, key not found */
got_multibyte_nextchar:
movb %cs:(%bx), %ch /* ch = test char to compare */
incw %bx /* point to next char */
orb %ch, %ch /* is char to compare NUL? */
jz got_multibyte_key /* matched to end of a string! */
cmpb %ch, (%bp) /* input tmp buf equal to test char? */
jnz got_multibyte_try_next_key
/* note: expected that test string will be nul before input string */
/* no attempt is made to ensure no more than 4 bytes stack read */
incw %bp /* point to next input */
jmp got_multibyte_nextchar
got_multibyte_try_next_key: /* align to next scancode/ascii pair */
movb %cs:(%bx), %ch /* ch = test char to compare */
incw %bx /* point to next char */
orb %ch, %ch /* is char to compare NUL? */
jnz got_multibyte_try_next_key
jmp got_multibyte_findkey
got_multibyte_key:
xorb %al, %al /* ascii value = 0 for special keys */
movw %sp, %bp
movw %ax, 4(%bp) /* overwrite old %ax value with key */
get_multibyte_tail:
addw $4, %sp /* pop temp space */
popw %ax
popw %cx
popw %bx
popw %bp
ret
/*
* send_byte
*
* send character in %al to serial port [FIXME: EFI console out]
*
* all registers preserved except flags
*
*/
send_byte:
pushw %ax
pushw %dx
pushw %cx
testb $0x80, %al /* don't send non-ascii chars */
jnz send_tail /* these should be translated earlier */
movb %al, %ah /* save char to output in %ah */
movw $0xFFF0, %cx /* only retry 65520 times */
serial_ready_test:
call get_serial_lsr /* get serial lsr in %al */
testb $TRANSMIT_READY_BIT, %al
loopz serial_ready_test /* if !tx ready, loop while cx!=0 */
movb %ah, %al
movw %cs:serial_port_base_address, %dx
outb %al, %dx
send_tail:
popw %cx
popw %dx
popw %ax
ret
/*
* translate_char
*
* translate vga character in %al to ascii
*
* returns:
* al = translated character
*
* all registers except %al preserved
*
*/
translate_char:
pushw %bx
pushw %ds
pushw %cs
popw %ds /* ds = cs */
testb $0x80, %al
jz translate_char_ctrl
andb $0x7f, %al
movw $high2ascii, %bx
xlatb
translate_char_ctrl:
cmpb $0x20, %al
jnc translate_char_tail
movw $ctrl2ascii, %bx
xlatb
translate_char_tail:
popw %ds
popw %bx
ret
/*
* translate_char_tty
*
* translate vga character in %al to ascii
* unless %al == 7, 8, 10, or 13 (bell, bs, lf, cr)
*
* returns:
* al = translated character
*
* all registers except %al preserved
*
*/
translate_char_tty:
cmpb $0x07, %al /* bell */
jz translate_char_tty_tail
cmpb $0x08, %al /* backspace */
jz translate_char_tty_tail
cmpb $0x0a, %al /* LF */
jz translate_char_tty_tail
cmpb $0x0d, %al /* CR */
jz translate_char_tty_tail
call translate_char
translate_char_tty_tail:
ret
/*
* send_char
*
* send character 0 - 255 in %al out through serial port
* increment cursor position without control processing
*
* send_byte is used for data that isn't tracked
*
* send_char is used for text that should be tracked
* send_char outputs all characters as non-control chars
*
* returns:
* al = translated character
*
* all registers except %al preserved
*
*/
send_char:
call sgabioslog_save_char /* save original char+pos */
call translate_char
jmp send_char_tty_out
/* after ctrl translation, same as send_char_tty */
/*
* send_char_tty
*
* send character 0 - 255 in %al out through serial port
* increment cursor position *with* control processing
* for bell, linefeed, cr, and backspace (others all printable)
*
* send_byte is used for data that isn't tracked
*
* send_char_tty is used for text that should be tracked
*
* returns:
* al = translated character
*
* all registers except %al preserved
*
*/
/* send character 0 - 255 in %al out through serial port */
/* increment cursor position with CR/LF/Backspace processing */
send_char_tty:
call sgabioslog_save_char /* save original char+pos */
call translate_char_tty
send_char_tty_out:
pushw %dx
call update_serial_cursor
call get_current_cursor /* vga cursor in %dx */
cmpb $0x0d, %al /* CR */
jnz send_char_tty_nul /* if not CR, check for NUL */
orb %dl, %dl /* already at col 0? */
jz send_char_tty_tail /* no need to re-send CR */
send_char_tty_nul:
orb %al, %al /* %al == 0 ? (nul) */
/* more than likely, we have NUL at this point because the caller */
/* tried to read a char using int $0x10, %ah=8, and is trying */
/* to re-output it with different attributes - for now send nothing */
jz send_char_tty_tail
send_char_tty_write:
call memconsole_log_char /* log character sent */
call send_byte
cmpb $0x07, %al /* bell */
jz send_char_tty_tail /* no cursor update for bell */