summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/PC/BIOS/print.c
blob: 88b08c886fe6c5f95e936e7cd7b69dcf7fa283d0 (plain)
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
/* $Id: print.c $ */
/** @file
 * PC BIOS - ???
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 * --------------------------------------------------------------------
 *
 * This code is based on:
 *
 *  ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
 *
 *  Copyright (C) 2002  MandrakeSoft S.A.
 *
 *    MandrakeSoft S.A.
 *    43, rue d'Aboukir
 *    75002 Paris - France
 *    http://www.linux-mandrake.com/
 *    http://www.mandrakesoft.com/
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

/*
 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
 * a choice of LGPL license versions is made available with the language indicating
 * that LGPLv2 or any later version may be used, or where a choice of which version
 * of the LGPL is applied is otherwise unspecified.
 */


#include <stdint.h>
#include <stdarg.h>
#include "inlines.h"
#include "biosint.h"

// Debug printf support

/* Redirect INFO output to backdoor logging port. */
#define INFO_PORT   0x504
#define DEBUG_PORT  0x403

const char bios_prefix_string[] = "BIOS: ";

void wrch(uint8_t c);
#pragma aux wrch = "mov ah, 0eh" "int 10h" parm [al] modify exact [ax bx];

void send(uint16_t action, uint8_t c)
{
#if BX_DEBUG_SERIAL
    if (c == '\n')
        uart_tx_byte(BX_DEBUG_PORT, '\r');
    uart_tx_byte(BX_DEBUG_PORT, c);
#endif
#if BX_VIRTUAL_PORTS
    if (action & BIOS_PRINTF_DEBUG)
        outb(DEBUG_PORT, c);
    if (action & BIOS_PRINTF_INFO)
        outb(INFO_PORT, c);
#endif
    if (action & BIOS_PRINTF_SCREEN) {
        if (c == '\n')
            wrch('\r');
        wrch(c);
    }
}

void put_int(uint16_t action, short val, short width, bx_bool neg)
{
    short   nval = val / 10;
    if (nval)
        put_int(action, nval, width - 1, neg);
    else {
        while (--width > 0)
            send(action, ' ');
        if (neg)
            send(action, '-');
    }
    send(action, val - (nval * 10) + '0');
}

void put_uint(uint16_t action, unsigned short val, short width, bx_bool neg)
{
    unsigned short nval = val / 10;
    if (nval)
        put_uint(action, nval, width - 1, neg);
    else {
        while (--width > 0)
            send(action, ' ');
        if (neg)
            send(action, '-');
    }
    send(action, val - (nval * 10) + '0');
}

void put_luint(uint16_t action, unsigned long val, short width, bx_bool neg)
{
    unsigned long   nval = val / 10;
    if (nval)
        put_luint(action, nval, width - 1, neg);
    else {
        while (--width > 0)
            send(action, ' ');
        if (neg)
            send(action, '-');
    }
    send(action, val - (nval * 10) + '0');
}

void put_str(uint16_t action, const char __far *s)
{
    uint8_t c;

    while (c = *s) {
        send(action, c);
        s++;
    }
}

void put_str_near(uint16_t action, const char __near *s)
{
    uint8_t c;

    while (c = *s) {
        send(action, c);
        s++;
    }
}


//--------------------------------------------------------------------------
// bios_printf()
//   A compact variable argument printf function.
//
//   Supports %[format_width][length]format
//   where format can be x,X,u,d,s,S,c
//   and the optional length modifier is l (ell, long 32-bit) or ll
//   (long long, 64-bit).
//   Only x,X work with ll
//--------------------------------------------------------------------------
void bios_printf(uint16_t action, const char *s, ...)
{
    uint8_t     c;
    bx_bool     in_format;
    int         i;
    uint16_t    arg, nibble, hibyte, format_width, hexadd;
    va_list     args;

    va_start( args, s );

    in_format = 0;
    format_width = 0;

    if ((action & BIOS_PRINTF_DEBHALT) == BIOS_PRINTF_DEBHALT) {
        bios_printf (BIOS_PRINTF_SCREEN, "FATAL: ");
    }

    while (c = *s) {
        if ( c == '%' ) {
            in_format = 1;
            format_width = 0;
        }
        else if (in_format) {
            if ( (c>='0') && (c<='9') ) {
                format_width = (format_width * 10) + (c - '0');
            }
            else {
                arg = va_arg( args, uint16_t );
                if (c == 'x' || c == 'X') {
                    if (format_width == 0)
                        format_width = 4;
                    if (c == 'x')
                        hexadd = 'a';
                    else
                        hexadd = 'A';
                    for (i=format_width-1; i>=0; i--) {
                        nibble = (arg >> (4 * i)) & 0x000f;
                        send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
                    }
                }
                else if (c == 'u') {
                    put_uint(action, arg, format_width, 0);
                }
                else if (c == 'l' && s[1] == 'l') {
                    uint64_t llval;
                    uint16_t __far *cp16;

                    s += 2;
                    c = *s;
                    cp16 = (uint16_t __far *)&llval;
                    cp16[0] = arg;
                    cp16[1] = va_arg( args, uint16_t );
                    cp16[2] = va_arg( args, uint16_t );
                    cp16[3] = va_arg( args, uint16_t );
                    if (c == 'x' || c == 'X') {
                        if (format_width == 0)
                            format_width = 16;
                        if (c == 'x')
                            hexadd = 'a';
                        else
                            hexadd = 'A';
                        for (i=format_width-1; i>=0; i--) {
                            nibble =  (llval >> (i * 4)) & 0x000f;
                            send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
                        }
                    } else {
                        BX_PANIC("bios_printf: unknown %ll format\n");
                    }
                }
                else if (c == 'l') {
                    s++;
                    c = *s; /* is it ld,lx,lu? */
                    hibyte = va_arg( args, uint16_t );
                    if (c == 'd') {
                        if (hibyte & 0x8000)
                            put_luint(action, 0L-(((uint32_t) hibyte << 16) | arg), format_width-1, 1);
                        else
                            put_luint(action, ((uint32_t) hibyte << 16) | arg, format_width, 0);
                    }
                    else if (c == 'u') {
                        put_luint(action, ((uint32_t) hibyte << 16) | arg, format_width, 0);
                    }
                    else if (c == 'x' || c == 'X')
                    {
                        if (format_width == 0)
                            format_width = 8;
                        if (c == 'x')
                            hexadd = 'a';
                        else
                            hexadd = 'A';
                        for (i=format_width-1; i>=0; i--) {
                            nibble = ((((uint32_t)hibyte << 16) | arg) >> (4 * i)) & 0x000f;
                            send (action, (nibble<=9)? (nibble+'0') : (nibble-10+hexadd));
                        }
                    }
                }
                else if (c == 'd') {
                    if (arg & 0x8000)
                        put_int(action, -arg, format_width - 1, 1);
                    else
                        put_int(action, arg, format_width, 0);
                }
                else if (c == 's') {
                    put_str(action, (char *)arg);
                }
                else if (c == 'S') {
                    hibyte = arg;
                    arg = va_arg( args, uint16_t );
                    put_str(action, hibyte :> (char *)arg);
                }
                else if (c == 'c') {
                    send(action, arg);
                }
                else
                    BX_PANIC("bios_printf: unknown format\n");
                in_format = 0;
            }
        }
        else {
            send(action, c);
        }
        ++s;
    }
    va_end( args );
    if (action & BIOS_PRINTF_HALT) {
        // freeze in a busy loop.
        int_disable();
        halt_forever();
    }
}

// End of printf support