1 | /*
|
---|
2 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
3 | *
|
---|
4 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
5 | * available from http://www.virtualbox.org. This file is free software;
|
---|
6 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
7 | * General Public License (GPL) as published by the Free Software
|
---|
8 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
9 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
10 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
11 | *
|
---|
12 | * This code is based on:
|
---|
13 | *
|
---|
14 | * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
|
---|
15 | *
|
---|
16 | * Copyright (C) 2002 MandrakeSoft S.A.
|
---|
17 | *
|
---|
18 | * MandrakeSoft S.A.
|
---|
19 | * 43, rue d'Aboukir
|
---|
20 | * 75002 Paris - France
|
---|
21 | * http://www.linux-mandrake.com/
|
---|
22 | * http://www.mandrakesoft.com/
|
---|
23 | *
|
---|
24 | * This library is free software; you can redistribute it and/or
|
---|
25 | * modify it under the terms of the GNU Lesser General Public
|
---|
26 | * License as published by the Free Software Foundation; either
|
---|
27 | * version 2 of the License, or (at your option) any later version.
|
---|
28 | *
|
---|
29 | * This library is distributed in the hope that it will be useful,
|
---|
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
32 | * Lesser General Public License for more details.
|
---|
33 | *
|
---|
34 | * You should have received a copy of the GNU Lesser General Public
|
---|
35 | * License along with this library; if not, write to the Free Software
|
---|
36 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
37 | *
|
---|
38 | */
|
---|
39 |
|
---|
40 |
|
---|
41 | #include <stdint.h>
|
---|
42 | #include <stdarg.h>
|
---|
43 | #include "inlines.h"
|
---|
44 | #include "biosint.h"
|
---|
45 |
|
---|
46 | // Debug printf support
|
---|
47 |
|
---|
48 | /* Redirect INFO output to backdoor logging port. */
|
---|
49 | #define INFO_PORT 0x504
|
---|
50 | #define DEBUG_PORT 0x403
|
---|
51 |
|
---|
52 | const char bios_prefix_string[] = "BIOS: ";
|
---|
53 |
|
---|
54 | void wrch(uint8_t c);
|
---|
55 | #pragma aux wrch = "mov ah, 0eh" "int 10h" parm [al] modify exact [ax bx];
|
---|
56 |
|
---|
57 | void send(uint16_t action, uint8_t c)
|
---|
58 | {
|
---|
59 | #if BX_DEBUG_SERIAL
|
---|
60 | if (c == '\n')
|
---|
61 | uart_tx_byte(BX_DEBUG_PORT, '\r');
|
---|
62 | uart_tx_byte(BX_DEBUG_PORT, c);
|
---|
63 | #endif
|
---|
64 | #if BX_VIRTUAL_PORTS
|
---|
65 | if (action & BIOS_PRINTF_DEBUG)
|
---|
66 | outb(DEBUG_PORT, c);
|
---|
67 | if (action & BIOS_PRINTF_INFO)
|
---|
68 | outb(INFO_PORT, c);
|
---|
69 | #endif
|
---|
70 | if (action & BIOS_PRINTF_SCREEN) {
|
---|
71 | if (c == '\n')
|
---|
72 | wrch('\r');
|
---|
73 | wrch(c);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | void put_int(uint16_t action, short val, short width, bx_bool neg)
|
---|
78 | {
|
---|
79 | short nval = val / 10;
|
---|
80 | if (nval)
|
---|
81 | put_int(action, nval, width - 1, neg);
|
---|
82 | else {
|
---|
83 | while (--width > 0)
|
---|
84 | send(action, ' ');
|
---|
85 | if (neg)
|
---|
86 | send(action, '-');
|
---|
87 | }
|
---|
88 | send(action, val - (nval * 10) + '0');
|
---|
89 | }
|
---|
90 |
|
---|
91 | void put_uint(uint16_t action, unsigned short val, short width, bx_bool neg)
|
---|
92 | {
|
---|
93 | unsigned short nval = val / 10;
|
---|
94 | if (nval)
|
---|
95 | put_uint(action, nval, width - 1, neg);
|
---|
96 | else {
|
---|
97 | while (--width > 0)
|
---|
98 | send(action, ' ');
|
---|
99 | if (neg)
|
---|
100 | send(action, '-');
|
---|
101 | }
|
---|
102 | send(action, val - (nval * 10) + '0');
|
---|
103 | }
|
---|
104 |
|
---|
105 | void put_luint(uint16_t action, unsigned long val, short width, bx_bool neg)
|
---|
106 | {
|
---|
107 | unsigned long nval = val / 10;
|
---|
108 | if (nval)
|
---|
109 | put_luint(action, nval, width - 1, neg);
|
---|
110 | else {
|
---|
111 | while (--width > 0)
|
---|
112 | send(action, ' ');
|
---|
113 | if (neg)
|
---|
114 | send(action, '-');
|
---|
115 | }
|
---|
116 | send(action, val - (nval * 10) + '0');
|
---|
117 | }
|
---|
118 |
|
---|
119 | void put_str(uint16_t action, const char __far *s)
|
---|
120 | {
|
---|
121 | uint8_t c;
|
---|
122 |
|
---|
123 | while (c = *s) {
|
---|
124 | send(action, c);
|
---|
125 | s++;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | void put_str_near(uint16_t action, const char __near *s)
|
---|
130 | {
|
---|
131 | uint8_t c;
|
---|
132 |
|
---|
133 | while (c = *s) {
|
---|
134 | send(action, c);
|
---|
135 | s++;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | //--------------------------------------------------------------------------
|
---|
141 | // bios_printf()
|
---|
142 | // A compact variable argument printf function.
|
---|
143 | //
|
---|
144 | // Supports %[format_width][length]format
|
---|
145 | // where format can be x,X,u,d,s,S,c
|
---|
146 | // and the optional length modifier is l (ell, long 32-bit) or ll
|
---|
147 | // (long long, 64-bit).
|
---|
148 | // Only x,X work with ll
|
---|
149 | //--------------------------------------------------------------------------
|
---|
150 | void bios_printf(uint16_t action, const char *s, ...)
|
---|
151 | {
|
---|
152 | uint8_t c;
|
---|
153 | bx_bool in_format;
|
---|
154 | int i;
|
---|
155 | uint16_t arg, nibble, hibyte, format_width, hexadd;
|
---|
156 | va_list args;
|
---|
157 |
|
---|
158 | va_start( args, s );
|
---|
159 |
|
---|
160 | in_format = 0;
|
---|
161 | format_width = 0;
|
---|
162 |
|
---|
163 | if ((action & BIOS_PRINTF_DEBHALT) == BIOS_PRINTF_DEBHALT) {
|
---|
164 | bios_printf (BIOS_PRINTF_SCREEN, "FATAL: ");
|
---|
165 | }
|
---|
166 |
|
---|
167 | while (c = *s) {
|
---|
168 | if ( c == '%' ) {
|
---|
169 | in_format = 1;
|
---|
170 | format_width = 0;
|
---|
171 | }
|
---|
172 | else if (in_format) {
|
---|
173 | if ( (c>='0') && (c<='9') ) {
|
---|
174 | format_width = (format_width * 10) + (c - '0');
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | arg = va_arg( args, uint16_t );
|
---|
178 | if (c == 'x' || c == 'X') {
|
---|
179 | if (format_width == 0)
|
---|
180 | format_width = 4;
|
---|
181 | if (c == 'x')
|
---|
182 | hexadd = 'a';
|
---|
183 | else
|
---|
184 | hexadd = 'A';
|
---|
185 | for (i=format_width-1; i>=0; i--) {
|
---|
186 | nibble = (arg >> (4 * i)) & 0x000f;
|
---|
187 | send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
|
---|
188 | }
|
---|
189 | }
|
---|
190 | else if (c == 'u') {
|
---|
191 | put_uint(action, arg, format_width, 0);
|
---|
192 | }
|
---|
193 | else if (c == 'l' && s[1] == 'l') {
|
---|
194 | uint64_t llval;
|
---|
195 | uint16_t __far *cp16;
|
---|
196 |
|
---|
197 | s += 2;
|
---|
198 | c = *s;
|
---|
199 | cp16 = (uint16_t __far *)&llval;
|
---|
200 | cp16[0] = arg;
|
---|
201 | cp16[1] = va_arg( args, uint16_t );
|
---|
202 | cp16[2] = va_arg( args, uint16_t );
|
---|
203 | cp16[3] = va_arg( args, uint16_t );
|
---|
204 | if (c == 'x' || c == 'X') {
|
---|
205 | if (format_width == 0)
|
---|
206 | format_width = 16;
|
---|
207 | if (c == 'x')
|
---|
208 | hexadd = 'a';
|
---|
209 | else
|
---|
210 | hexadd = 'A';
|
---|
211 | for (i=format_width-1; i>=0; i--) {
|
---|
212 | nibble = (llval >> (i * 4)) & 0x000f;
|
---|
213 | send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
|
---|
214 | }
|
---|
215 | } else {
|
---|
216 | BX_PANIC("bios_printf: unknown %ll format\n");
|
---|
217 | }
|
---|
218 | }
|
---|
219 | else if (c == 'l') {
|
---|
220 | s++;
|
---|
221 | c = *s; /* is it ld,lx,lu? */
|
---|
222 | hibyte = va_arg( args, uint16_t );
|
---|
223 | if (c == 'd') {
|
---|
224 | if (hibyte & 0x8000)
|
---|
225 | put_luint(action, 0L-(((uint32_t) hibyte << 16) | arg), format_width-1, 1);
|
---|
226 | else
|
---|
227 | put_luint(action, ((uint32_t) hibyte << 16) | arg, format_width, 0);
|
---|
228 | }
|
---|
229 | else if (c == 'u') {
|
---|
230 | put_luint(action, ((uint32_t) hibyte << 16) | arg, format_width, 0);
|
---|
231 | }
|
---|
232 | else if (c == 'x' || c == 'X')
|
---|
233 | {
|
---|
234 | if (format_width == 0)
|
---|
235 | format_width = 8;
|
---|
236 | if (c == 'x')
|
---|
237 | hexadd = 'a';
|
---|
238 | else
|
---|
239 | hexadd = 'A';
|
---|
240 | for (i=format_width-1; i>=0; i--) {
|
---|
241 | nibble = ((((uint32_t)hibyte << 16) | arg) >> (4 * i)) & 0x000f;
|
---|
242 | send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 | else if (c == 'd') {
|
---|
247 | if (arg & 0x8000)
|
---|
248 | put_int(action, -arg, format_width - 1, 1);
|
---|
249 | else
|
---|
250 | put_int(action, arg, format_width, 0);
|
---|
251 | }
|
---|
252 | else if (c == 's') {
|
---|
253 | put_str(action, (char *)arg);
|
---|
254 | }
|
---|
255 | else if (c == 'S') {
|
---|
256 | hibyte = arg;
|
---|
257 | arg = va_arg( args, uint16_t );
|
---|
258 | put_str(action, hibyte :> (char *)arg);
|
---|
259 | }
|
---|
260 | else if (c == 'c') {
|
---|
261 | send(action, arg);
|
---|
262 | }
|
---|
263 | else
|
---|
264 | BX_PANIC("bios_printf: unknown format\n");
|
---|
265 | in_format = 0;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | else {
|
---|
269 | send(action, c);
|
---|
270 | }
|
---|
271 | ++s;
|
---|
272 | }
|
---|
273 | va_end( args );
|
---|
274 | if (action & BIOS_PRINTF_HALT) {
|
---|
275 | // freeze in a busy loop.
|
---|
276 | int_disable();
|
---|
277 | halt_forever();
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | // End of printf support
|
---|