source: trunk/hal/x86_64/x86_printf.c @ 34

Last change on this file since 34 was 32, checked in by max@…, 7 years ago

Update. Retrieve and parse the multiboot info, and dump the mmap. Some
more sanity checks could probably be added.

File size: 4.3 KB
Line 
1/*
2 * x86_printf.c - A printf function for x86 (debug only).
3 *
4 * Copyright (c) [don't know exactly, found on the internet... anyway, this
5 *                file will be removed soon...]
6 *
7 * This file is part of ALMOS-MKH.
8 *
9 * ALMOS-MKH is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2.0 of the License.
12 *
13 * ALMOS-MKH is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <hal_types.h>
24#include <hal_boot.h>
25
26#include <memcpy.h>
27#include <thread.h>
28#include <string.h>
29#include <process.h>
30#include <printk.h>
31#include <vmm.h>
32#include <core.h>
33#include <cluster.h>
34
35#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
36#define CONS_X_SIZE     160
37#define CONS_Y_SIZE     80
38
39extern intptr_t iom_base;
40size_t cons_ptr __in_kdata = 0;
41
42static void x86_putc(char c)
43{
44        if (c == '\n') {
45                cons_ptr = roundup(cons_ptr, CONS_X_SIZE);
46                return;
47        }
48
49        char *video = (char *)iom_base + (0xB8000 - IOM_BEGIN) + cons_ptr;
50        *video = c;
51        cons_ptr++, video++;
52        *video = 0x7;
53        cons_ptr++, video++;
54}
55
56static void x86_itoa(char *buf, unsigned long int n, int base)
57{
58        unsigned long int tmp;
59        int i, j;
60
61        tmp = n;
62        i = 0;
63
64        do {
65                tmp = n % base;
66                buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10);
67        } while (n /= base);
68        buf[i--] = 0;
69
70        for (j = 0; j < i; j++, i--) {
71                tmp = buf[j];
72                buf[j] = buf[i];
73                buf[i] = tmp;
74        }
75}
76
77static void x86_ztoa(char *buf, uint64_t n, uint64_t base)
78{
79        uint64_t tmp;
80        int i, j;
81
82        tmp = n;
83        i = 0;
84
85        do {
86                tmp = n % base;
87                buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10);
88        } while (n /= base);
89        buf[i--] = 0;
90
91        for (j = 0; j < i; j++, i--) {
92                tmp = buf[j];
93                buf[j] = buf[i];
94                buf[i] = tmp;
95        }
96}
97
98void x86_printf(char *s, ...)
99{
100        va_list ap;
101
102        char buf[16];
103        int i, j, size, buflen, neg;
104
105        unsigned char c;
106        int ival;
107        unsigned int uival;
108        uint64_t zval;
109
110        va_start(ap, s);
111
112        while ((c = *s++)) {
113                size = 0;
114                neg = 0;
115
116                if (c == 0)
117                        break;
118                else if (c == '%') {
119                        c = *s++;
120                        if (c >= '0' && c <= '9') {
121                                size = c - '0';
122                                c = *s++;
123                        }
124
125                        if (c == 'z') {
126                                zval = va_arg(ap, uint64_t);
127                                x86_ztoa(buf, zval, 10);
128
129                                buflen = strlen(buf);
130                                if (buflen < size)
131                                        for (i = size, j = buflen; i >= 0; i--, j--)
132                                                buf[i] = (j >= 0) ? buf[j] : '0';
133
134                                x86_printf(buf);
135                        } else if (c == 'Z') {
136                                zval = va_arg(ap, uint64_t);
137                                x86_ztoa(buf, zval, 16);
138
139                                buflen = strlen(buf);
140                                if (buflen < size)
141                                        for (i = size, j = buflen; i >= 0; i--, j--)
142                                                buf[i] = (j >= 0) ? buf[j] : '0';
143
144                                x86_printf("0x%s", buf);
145                        } else if (c == 'd') {
146                                ival = va_arg(ap, int);
147                                if (ival < 0) {
148                                        uival = 0 - ival;
149                                        neg++;
150                                } else
151                                        uival = ival;
152                                x86_itoa(buf, uival, 10);
153
154                                buflen = strlen(buf);
155                                if (buflen < size)
156                                        for (i = size, j = buflen; i >= 0;
157                                             i--, j--)
158                                                buf[i] =
159                                                    (j >=
160                                                     0) ? buf[j] : '0';
161
162                                if (neg)
163                                        x86_printf("-%s", buf);
164                                else
165                                        x86_printf(buf);
166                        } else if (c == 'u') {
167                                uival = va_arg(ap, int);
168                                x86_itoa(buf, uival, 10);
169
170                                buflen = strlen(buf);
171                                if (buflen < size)
172                                        for (i = size, j = buflen; i >= 0;
173                                             i--, j--)
174                                                buf[i] =
175                                                    (j >=
176                                                     0) ? buf[j] : '0';
177
178                                x86_printf(buf);
179                        } else if (c == 'x' || c == 'X') {
180                                uival = va_arg(ap, int);
181                                x86_itoa(buf, uival, 16);
182
183                                buflen = strlen(buf);
184                                if (buflen < size)
185                                        for (i = size, j = buflen; i >= 0;
186                                             i--, j--)
187                                                buf[i] =
188                                                    (j >=
189                                                     0) ? buf[j] : '0';
190
191                                x86_printf("0x%s", buf);
192                        } else if (c == 'p') {
193                                uival = va_arg(ap, int);
194                                x86_itoa(buf, uival, 16);
195                                size = 8;
196
197                                buflen = strlen(buf);
198                                if (buflen < size)
199                                        for (i = size, j = buflen; i >= 0;
200                                             i--, j--)
201                                                buf[i] =
202                                                    (j >=
203                                                     0) ? buf[j] : '0';
204
205                                x86_printf("0x%s", buf);
206                        } else if (c == 's') {
207                                x86_printf((char *) va_arg(ap, int));
208                        } 
209                } else
210                        x86_putc(c);
211        }
212
213        return;
214}
Note: See TracBrowser for help on using the repository browser.