From 01241e5d6c99ec9613e6a94bcba9b58fb09316b0 Mon Sep 17 00:00:00 2001 From: Liu Aleaxander Date: Thu, 27 May 2010 15:49:07 +0800 Subject: [PATCH] Add hexdump lib function Also add two stdio.h and hexdump.h files, and a new sprintk function added. Signed-off-by: Liu Aleaxander --- include/hexdump.h | 6 ++++++ include/stdio.h | 10 ++++++++++ init/init.c | 24 ++++++++++++------------ kernel/Makefile | 2 +- kernel/hexdump.c | 41 +++++++++++++++++++++++++++++++++++++++++ kernel/printk.c | 18 ++++++++++++++++-- 6 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 include/hexdump.h create mode 100644 include/stdio.h create mode 100644 kernel/hexdump.c diff --git a/include/hexdump.h b/include/hexdump.h new file mode 100644 index 0000000..281f141 --- /dev/null +++ b/include/hexdump.h @@ -0,0 +1,6 @@ +#ifndef HEXDUMP_H +#define HEXDUMP_H + +extern void hexdump(const char *, int); + +#endif /* hexdump.h */ diff --git a/include/stdio.h b/include/stdio.h new file mode 100644 index 0000000..b724110 --- /dev/null +++ b/include/stdio.h @@ -0,0 +1,10 @@ +#ifndef STDIO_H +#define STDIO_H + +#include + +extern int vsprintf (char *, const char *, va_list); +extern int sprintk (char *, const char *, va_list); +extern int printk(const char *, ...); + +#endif /* stdio.h */ diff --git a/init/init.c b/init/init.c index 5af294a..89ba5b3 100755 --- a/init/init.c +++ b/init/init.c @@ -180,19 +180,9 @@ void init(void) ram_mke2fs(); printk("\t\t%s\n", ok); - printk("\n"); - printk("\t**************************\n"); - printk("\t* *\n"); - printk("\t* Welcome to Thunix *\n"); - printk("\t* *\n"); - printk("\t**************************\n"); - printk("\tType 'help' for more information\n"); - printk("\n"); - /* Hope it quite safe now */ sti(); - shell_init(); @@ -252,7 +242,6 @@ void init(void) - /* * the floppy driver is not ready , and i think.... */ @@ -288,6 +277,17 @@ void init(void) #endif - + printk("\n"); + printk("\t**************************\n"); + printk("\t* *\n"); + printk("\t* Welcome to Thunix *\n"); + printk("\t* *\n"); + printk("\t**************************\n"); + printk("\tType 'help' for more information\n"); + printk("\n"); + + + shell_init(); + pause(); } diff --git a/kernel/Makefile b/kernel/Makefile index 785b0a5..f149299 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -5,7 +5,7 @@ LDFLAGS = --oformat binary -N CPP =gcc -E -nostdinc -I../include OBJS = asm.o console.o kb.o mktime.o panic.o printk.o \ - timer.o traps.o vsprintf.o fd.o shell.o reboot.o halt.o + timer.o traps.o vsprintf.o fd.o shell.o reboot.o halt.o hexdump.o kernel.o: ${OBJS} diff --git a/kernel/hexdump.c b/kernel/hexdump.c new file mode 100644 index 0000000..1e78a4d --- /dev/null +++ b/kernel/hexdump.c @@ -0,0 +1,41 @@ +/* + * The hex dump lib. + * + * Copyright (C) 2009 Liu Aleaxander -- All rights reserved. This file + * may be redistributed under the terms of the GNU Public License. + */ + +#include +#include + +void hexdump(const void *data, int len) +{ + int i = 0; + int index = 0; + unsigned int base = 0; + unsigned char *p = data; + unsigned char hex[16 * 3 + 1] = {0, }; + unsigned char text[16 + 1] = {0, }; + unsigned char c; + + for (i = 0; i < len; i++) { + index = i & 0xf; + if (i && index == 0) { + /* print the buffer before reset it */ + printk("%08x %-48s |%-16s|\n", base, hex, text); + base += 0x10; + memset(hex, 0, sizeof hex); + memset(text, 0, sizeof text); + } + + c = *p++; + sprintk((char *)&hex[index * 3], "%02x ", c); + if (c < 0x20 || c > 0x7f) + text[index] = '.'; + else + text[index] = c; + } + + /* print the last part */ + printk("%08x %-48s |%-16s|\n", base, hex, text); +} diff --git a/kernel/printk.c b/kernel/printk.c index bbd3a8c..ad4059a 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -8,12 +8,12 @@ #include #include -static char buf[1024]; extern int vsprintf (char *buf, const char *fmt, va_list args); int printk(const char *fmt, ...) { + char buf[1024]; va_list args; int i; @@ -21,8 +21,9 @@ int printk(const char *fmt, ...) i = vsprintf(buf, fmt, args); va_end(args); - /*con_write(buf, i);*/ + con_write(buf, i); +/* __asm__("push %%fs\n\t" "push %%ds\n\t" "pop %%fs\n\t" @@ -33,5 +34,18 @@ int printk(const char *fmt, ...) "pop %0\n\t" "pop %%fs" ::"r"(i)); +*/ return i; } + +int sprintk(char *buf, const char *fmt, ...) +{ + va_list args; + int i; + + va_start(args,fmt); + i = vsprintf(buf, fmt, args); + va_end(args); + + return i; +} -- 2.11.4.GIT