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