2011-12-11 03:34:10 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
2011-04-15 06:02:44 +04:00
|
|
|
* Kernel printf implementation
|
2011-03-26 03:54:48 +03:00
|
|
|
*
|
|
|
|
* Simple, painfully lacking, implementation of printf(),
|
|
|
|
* for the kernel of all things.
|
|
|
|
*/
|
2011-01-17 19:24:55 +03:00
|
|
|
#include <system.h>
|
2011-12-09 01:25:48 +04:00
|
|
|
#include <process.h>
|
2011-12-15 03:47:30 +04:00
|
|
|
#include <va_list.h>
|
2012-09-04 09:35:11 +04:00
|
|
|
#include <fs.h>
|
2011-01-17 19:24:55 +03:00
|
|
|
|
|
|
|
/*
|
2011-04-15 06:02:44 +04:00
|
|
|
* Integer to string
|
2011-01-17 19:24:55 +03:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
parse_num(
|
|
|
|
unsigned int value,
|
2011-12-15 05:06:11 +04:00
|
|
|
unsigned int base,
|
|
|
|
char * buf,
|
|
|
|
int * ptr
|
2011-01-17 19:24:55 +03:00
|
|
|
) {
|
|
|
|
unsigned int n = value / base;
|
|
|
|
int r = value % base;
|
|
|
|
if (r < 0) {
|
|
|
|
r += base;
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
if (value >= base) {
|
2011-12-15 05:06:11 +04:00
|
|
|
parse_num(n, base, buf, ptr);
|
2011-01-17 19:24:55 +03:00
|
|
|
}
|
2011-12-15 05:06:11 +04:00
|
|
|
buf[*ptr] = (r+'0');
|
|
|
|
*ptr = *ptr + 1;
|
2011-01-17 19:24:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-04-15 06:02:44 +04:00
|
|
|
* Hexadecimal to string
|
2011-01-17 19:24:55 +03:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
parse_hex(
|
2011-12-15 05:06:11 +04:00
|
|
|
unsigned int value,
|
|
|
|
char * buf,
|
|
|
|
int * ptr
|
2011-01-17 19:24:55 +03:00
|
|
|
) {
|
|
|
|
int i = 8;
|
|
|
|
while (i-- > 0) {
|
2011-12-15 05:06:11 +04:00
|
|
|
buf[*ptr] = "0123456789abcdef"[(value>>(i*4))&0xF];
|
|
|
|
*ptr = *ptr + 1;
|
2011-01-17 19:24:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 05:19:51 +04:00
|
|
|
/*
|
|
|
|
* vasprintf()
|
2011-01-17 19:24:55 +03:00
|
|
|
*/
|
2011-12-15 05:19:51 +04:00
|
|
|
size_t
|
|
|
|
vasprintf(char * buf, const char *fmt, va_list args) {
|
2011-01-17 19:24:55 +03:00
|
|
|
int i = 0;
|
|
|
|
char *s;
|
2011-12-15 05:19:51 +04:00
|
|
|
int ptr = 0;
|
2011-12-16 03:46:57 +04:00
|
|
|
int len = strlen(fmt);
|
|
|
|
for ( ; i < len && fmt[i]; ++i) {
|
2011-04-15 06:02:44 +04:00
|
|
|
if (fmt[i] != '%') {
|
2011-01-17 19:24:55 +03:00
|
|
|
buf[ptr++] = fmt[i];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* fmt[i] == '%' */
|
|
|
|
switch (fmt[++i]) {
|
2011-04-15 06:02:44 +04:00
|
|
|
case 's': /* String pointer -> String */
|
2011-02-11 08:55:03 +03:00
|
|
|
s = (char *)va_arg(args, char *);
|
2011-01-17 19:24:55 +03:00
|
|
|
while (*s) {
|
|
|
|
buf[ptr++] = *s++;
|
|
|
|
}
|
|
|
|
break;
|
2011-04-15 06:02:44 +04:00
|
|
|
case 'c': /* Single character */
|
2011-02-11 08:55:03 +03:00
|
|
|
buf[ptr++] = (char)va_arg(args, int);
|
2011-01-17 19:24:55 +03:00
|
|
|
break;
|
2011-04-15 06:02:44 +04:00
|
|
|
case 'x': /* Hexadecimal number */
|
2011-12-15 05:06:11 +04:00
|
|
|
parse_hex((unsigned long)va_arg(args, unsigned long), buf, &ptr);
|
2011-01-17 19:24:55 +03:00
|
|
|
break;
|
2011-04-15 06:02:44 +04:00
|
|
|
case 'd': /* Decimal number */
|
2011-12-15 05:06:11 +04:00
|
|
|
parse_num((unsigned long)va_arg(args, unsigned long), 10, buf, &ptr);
|
2011-01-17 19:24:55 +03:00
|
|
|
break;
|
2011-04-15 06:02:44 +04:00
|
|
|
case '%': /* Escape */
|
2011-01-17 19:24:55 +03:00
|
|
|
buf[ptr++] = '%';
|
|
|
|
break;
|
2011-04-15 06:02:44 +04:00
|
|
|
default: /* Nothing at all, just dump it */
|
2011-01-17 19:24:55 +03:00
|
|
|
buf[ptr++] = fmt[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-15 06:02:44 +04:00
|
|
|
/* Ensure the buffer ends in a null */
|
2011-01-17 19:24:55 +03:00
|
|
|
buf[ptr] = '\0';
|
2011-12-15 05:19:51 +04:00
|
|
|
return ptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-04 09:35:11 +04:00
|
|
|
short kprint_to_serial = 0;
|
2012-10-04 09:03:21 +04:00
|
|
|
short kprint_to_screen = 0;
|
2012-09-04 09:35:11 +04:00
|
|
|
void * kprint_to_file = NULL;
|
|
|
|
|
2012-10-04 09:03:21 +04:00
|
|
|
unsigned short * textmemptr = (unsigned short *)0xB8000;
|
|
|
|
void placech(unsigned char c, int x, int y, int attr) {
|
|
|
|
unsigned short *where;
|
|
|
|
unsigned att = attr << 8;
|
|
|
|
where = textmemptr + (y * 80 + x);
|
|
|
|
*where = c | att;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = 0, y = 0;
|
|
|
|
int _not_ready = 1;
|
|
|
|
int _off = 0;
|
|
|
|
|
2011-12-15 05:19:51 +04:00
|
|
|
/**
|
|
|
|
* (Kernel) Print a formatted string.
|
|
|
|
* %s, %c, %x, %d, %%
|
|
|
|
*
|
|
|
|
* @param fmt Formatted string to print
|
|
|
|
* @param ... Additional arguments to format
|
|
|
|
*/
|
2012-09-04 09:35:11 +04:00
|
|
|
#ifndef EXTREME_KPRINTF_DEBUGGING
|
2011-12-15 05:43:14 +04:00
|
|
|
int
|
2011-12-15 05:19:51 +04:00
|
|
|
kprintf(
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
) {
|
|
|
|
char buf[1024] = {-1};
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2011-12-15 05:43:14 +04:00
|
|
|
int out = vasprintf(buf, fmt, args);
|
2011-04-15 06:02:44 +04:00
|
|
|
/* We're done with our arguments */
|
2011-02-11 08:55:03 +03:00
|
|
|
va_end(args);
|
2011-04-15 06:02:44 +04:00
|
|
|
/* Print that sucker */
|
2012-09-04 09:35:11 +04:00
|
|
|
if (kprint_to_serial) {
|
|
|
|
serial_string(buf);
|
2012-09-13 10:14:01 +04:00
|
|
|
}
|
2012-10-04 09:03:21 +04:00
|
|
|
if (kprint_to_screen) {
|
|
|
|
/*
|
|
|
|
* VGA output for debugging. This is not nearly as feature-complete as the old
|
|
|
|
* kernel terminal; it is here to provide acceptable parsing of the debug messages
|
|
|
|
* and print them to the screen while in early boot on real hardware without a
|
|
|
|
* serial line and should not be used otherwise.
|
|
|
|
*/
|
|
|
|
if (_not_ready) {
|
|
|
|
int temp = 0xFFFF;
|
|
|
|
outportb(0x3D4, 14);
|
|
|
|
outportb(0x3D5, temp >> 8);
|
|
|
|
outportb(0x3d4, 15);
|
|
|
|
outportb(0x3d5, temp);
|
|
|
|
for (int y = 0; y < 80; ++y) {
|
|
|
|
for (int x = 0; x < 24; ++x) {
|
|
|
|
placech(' ', x, y, 0x00);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_not_ready = 0;
|
|
|
|
}
|
|
|
|
unsigned char *c = (uint8_t *)buf;
|
|
|
|
while (*c) {
|
|
|
|
if (_off) {
|
|
|
|
if (*c > 'a' && *c < 'z') {
|
|
|
|
_off = 0;
|
|
|
|
} else {
|
|
|
|
c++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (*c == '\033') {
|
|
|
|
c++;
|
|
|
|
if (*c == '[') {
|
|
|
|
_off = 1;
|
|
|
|
}
|
|
|
|
} else if (*c == '\n') {
|
|
|
|
y += 1;
|
|
|
|
x = 0;
|
|
|
|
} else {
|
|
|
|
placech(*c, x, y, 0x07);
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
if (x == 80) {
|
|
|
|
x = 0;
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
if (y == 24) {
|
|
|
|
y = 0;
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
2012-09-13 10:14:01 +04:00
|
|
|
/* Registered output file */
|
|
|
|
if (kprint_to_file) {
|
|
|
|
fs_node_t * node = (fs_node_t *)kprint_to_file;
|
|
|
|
uint32_t out = write_fs(node, node->offset, strlen(buf), (uint8_t *)buf);
|
|
|
|
node->offset += out;
|
2012-09-04 09:35:11 +04:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int
|
|
|
|
_kprintf(
|
|
|
|
char * file,
|
|
|
|
int line,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
) {
|
|
|
|
char buf[1024] = {-1};
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
int out = vasprintf(buf, fmt, args);
|
|
|
|
/* We're done with our arguments */
|
|
|
|
va_end(args);
|
|
|
|
/* Print that sucker */
|
|
|
|
if (buf[strlen(buf)-1] == '\n') {
|
|
|
|
buf[strlen(buf)-1] = '\0';
|
|
|
|
serial_string(buf);
|
|
|
|
char buf2[1024];
|
|
|
|
sprintf(buf2, " %s:%d\n", file, line);
|
|
|
|
serial_string(buf2);
|
|
|
|
} else {
|
|
|
|
serial_string(buf);
|
|
|
|
}
|
2011-12-15 05:43:14 +04:00
|
|
|
return out;
|
2011-01-17 19:24:55 +03:00
|
|
|
}
|
2012-09-04 09:35:11 +04:00
|
|
|
#endif
|
2011-02-07 23:30:17 +03:00
|
|
|
|
2011-11-18 01:55:59 +04:00
|
|
|
int
|
|
|
|
sprintf(
|
|
|
|
char * buf,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2011-12-15 05:19:51 +04:00
|
|
|
int out = vasprintf(buf, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
return out;
|
2011-11-18 01:55:59 +04:00
|
|
|
}
|
|
|
|
|