printk: add ptr support and minor refactor

This commit is contained in:
Felix Kopp 2021-01-04 14:56:57 +01:00
parent 2f8404678b
commit 6f10f507da
No known key found for this signature in database
GPG key ID: C478BA0A85F75728

View file

@ -5,22 +5,52 @@
/* Using GCC's stdarg.h is recommended even with -nodefaultlibs and -fno-builtin */ /* Using GCC's stdarg.h is recommended even with -nodefaultlibs and -fno-builtin */
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <ardix/malloc.h>
#include <ardix/printk.h> #include <ardix/printk.h>
#include <ardix/serial.h> #include <ardix/serial.h>
#include <ardix/string.h> #include <ardix/string.h>
#define PRINTK_BUFSZ 24 #include <toolchain.h>
static int handle_uint(unsigned int u) /*
* TODO: THIS CAUSES A STACK BUFFER OVERFLOW ON SYSTEMS WHERE INT IS 64 BITS
*/
/* 10 decimal digits (2 ** 32 - 1) + ASCII NUL */
#define PRINTK_UINT_BUFSZ 11
__rodata static const char fmt_hex_table[] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f',
};
static int fmt_handle_ptr(uintptr_t ptr)
{ {
int ret = 0; int ret;
char *buf = malloc(PRINTK_BUFSZ); /* 2 chars per byte, plus 2 for the "0x" hex prefix */
char *pos = buf + PRINTK_BUFSZ - 1; char buf[2 * sizeof(uintptr_t) + 2];
char *pos = &buf[2 * sizeof(uintptr_t) + 1];
if (buf == NULL) buf[0] = '0';
return -ENOMEM; buf[1] = 'x';
do {
*pos-- = fmt_hex_table[ptr & 0xf];
ptr >>= 4;
} while (pos != &buf[2]);
ret = serial_write(serial_default_interface, &buf[0], 2 * sizeof(uintptr_t) + 2);
return ret;
}
static int fmt_handle_uint(unsigned int u)
{
int ret;
char buf[PRINTK_UINT_BUFSZ];
char *pos = &buf[PRINTK_UINT_BUFSZ - 1];
do { do {
/* stupid big endian humans, forcing us to do the whole thing in reverse */ /* stupid big endian humans, forcing us to do the whole thing in reverse */
@ -30,13 +60,11 @@ static int handle_uint(unsigned int u)
pos++; pos++;
ret = serial_write(serial_default_interface, pos, ret = serial_write(serial_default_interface, pos,
PRINTK_BUFSZ - ( (size_t)pos - (size_t)buf )); PRINTK_UINT_BUFSZ - ( (size_t)pos - (size_t)&buf[0] ));
free(buf);
return ret; return ret;
} }
static inline int handle_int(int i) static inline int fmt_handle_int(int i)
{ {
int ret = 0; int ret = 0;
char minus = '-'; char minus = '-';
@ -46,7 +74,7 @@ static inline int handle_int(int i)
i = -i; i = -i;
} }
ret += handle_uint((unsigned int)i); ret += fmt_handle_uint((unsigned int)i);
return ret; return ret;
} }
@ -61,37 +89,46 @@ static inline int handle_int(int i)
* @param args: A pointer to the varargs list. Will be manipulated. * @param args: A pointer to the varargs list. Will be manipulated.
* @returns The amount of bytes written, or a negative POSIX error code. * @returns The amount of bytes written, or a negative POSIX error code.
*/ */
static inline int handle_fmt(const char **pos, va_list args) static inline int fmt_handle(const char **pos, va_list args)
{ {
int ret = 0; int ret = 0;
int i; union {
unsigned int u; int c;
char *tmp; int d;
uintptr_t p;
char *s;
unsigned int u;
} val;
switch (**pos) { switch (**pos) {
case '%': /* literal percent sign */ case '%': /* literal percent sign */
ret = serial_write(serial_default_interface, *pos, sizeof(**pos)); ret = serial_write(serial_default_interface, *pos, sizeof(**pos));
break; break;
case 'c': /* single char */ case 'c': /* char */
i = va_arg(args, int); val.c = va_arg(args, typeof(val.c));
ret = serial_write(serial_default_interface, &i, sizeof(char)); ret = serial_write(serial_default_interface, &val.c, sizeof(val.c));
break; break;
case 'd': /* int */ case 'd': /* int */
i = va_arg(args, int); val.d = va_arg(args, typeof(val.d));
ret = handle_int(i); ret = fmt_handle_int(val.d);
break;
case 'p': /* ptr */
val.p = va_arg(args, typeof(val.p));
ret = fmt_handle_ptr(val.p);
break; break;
case 's': /* string */ case 's': /* string */
tmp = va_arg(args, char *); val.s = va_arg(args, typeof(val.s));
ret = (int)strlen(tmp); ret = (int)strlen(val.s);
ret = serial_write(serial_default_interface, tmp, (size_t)ret); ret = serial_write(serial_default_interface, val.s, (size_t)ret);
break; break;
case 'u': /* unsigned int */ case 'u': /* unsigned int */
u = va_arg(args, unsigned int); val.u = va_arg(args, typeof(val.u));
ret = handle_uint(u); ret = fmt_handle_uint(val.u);
break; break;
} }
@ -115,7 +152,7 @@ int printk(const char *fmt, ...)
ret += (int)serial_write(serial_default_interface, fmt, ret += (int)serial_write(serial_default_interface, fmt,
(size_t)tmp - (size_t)fmt - 1); (size_t)tmp - (size_t)fmt - 1);
tmpret = handle_fmt(&tmp, args); tmpret = fmt_handle(&tmp, args);
/* /*
* act as if the current position were the beginning in * act as if the current position were the beginning in
* order to make the first step of this if block easier * order to make the first step of this if block easier