Adding upstream version 4.00~pre53+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / vdprintf.c
blob330279b3344629b2870634ca28737329c5e0480b
1 /*
2 * vdprintf.c
3 */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <unistd.h>
9 #include <inttypes.h>
10 #include <sys/io.h>
11 #include <sys/cpu.h>
13 #undef DEBUG
14 #define DEBUG 1
15 #include <dprintf.h>
17 #define BUFFER_SIZE 4096
19 enum serial_port_regs {
20 THR = 0,
21 RBR = 0,
22 DLL = 0,
23 DLM = 1,
24 IER = 1,
25 IIR = 2,
26 FCR = 2,
27 LCR = 3,
28 MCR = 4,
29 LSR = 5,
30 MSR = 6,
31 SCR = 7,
33 static const uint16_t debug_base = 0x03f8; /* I/O base address */
35 static void debug_putc(char c)
37 if (c == '\n')
38 debug_putc('\r');
40 while ((inb(debug_base + LSR) & 0x20) == 0)
41 cpu_relax();
42 outb(c, debug_base + THR);
45 void vdprintf(const char *format, va_list ap)
47 int rv;
48 char buffer[BUFFER_SIZE];
49 char *p;
50 static bool debug_init = false;
51 static bool debug_ok = false;
53 rv = vsnprintf(buffer, BUFFER_SIZE, format, ap);
55 if (rv < 0)
56 return;
58 if (rv > BUFFER_SIZE - 1)
59 rv = BUFFER_SIZE - 1;
62 * This unconditionally outputs to a serial port at 0x3f8 regardless of
63 * if one is enabled or not (this means we don't have to enable the real
64 * serial console and therefore get conflicting output.)
66 if (__unlikely(!debug_init)) {
67 uint8_t dll, dlm, lcr;
69 debug_init = true;
71 cli();
73 /* Initialize the serial port to 115200 n81 with FIFOs enabled */
74 outb(0x83, debug_base + LCR);
75 outb(0x01, debug_base + DLL);
76 outb(0x00, debug_base + DLM);
77 (void)inb(debug_base + IER); /* Synchronize */
78 dll = inb(debug_base + DLL);
79 dlm = inb(debug_base + DLM);
80 lcr = inb(debug_base + LCR);
82 outb(0x03, debug_base + LCR);
83 (void)inb(debug_base + IER); /* Synchronize */
85 outb(0x00, debug_base + IER);
86 (void)inb(debug_base + IER); /* Synchronize */
88 sti();
90 if (dll != 0x01 || dlm != 0x00 || lcr != 0x83) {
91 /* No serial port present */
92 return;
95 outb(0x01, debug_base + FCR);
96 (void)inb(debug_base + IER); /* Synchronize */
97 if (inb(debug_base + IIR) < 0xc0) {
98 outb(0x00, debug_base + FCR); /* Disable non-functional FIFOs */
99 (void)inb(debug_base + IER); /* Synchronize */
102 debug_ok = true;
105 if (!debug_ok)
106 return;
108 p = buffer;
109 while (rv--)
110 debug_putc(*p++);