[serial] Serial logging.

This commit is contained in:
Kevin Lange 2011-02-10 21:45:29 -06:00
parent eb3fa7d499
commit 52a5b9e6cf
7 changed files with 99 additions and 1 deletions

View File

@ -19,7 +19,7 @@ install: kernel initrd
@${ECHO} "\r\033[34;1m -- Kernel and ramdisk installed.\033[0m"
run: kernel initrd
${EMU} -kernel kernel -initrd initrd
${EMU} -kernel kernel -initrd initrd -serial stdio
kernel: start.o link.ld main.o ${MODULES} ${FILESYSTEMS}
@${ECHO} -n "\033[32m LD $<\033[0m"

43
core/serial.c Normal file
View File

@ -0,0 +1,43 @@
/**
* Serial Port Driver
*/
#include <system.h>
#define SERIAL_PORT_A 0x3F8
#define SERIAL_PORT_B 0x2F8
#define SERIAL_PORT_C 0x3E8
#define SERIAL_PORT_D 0x2E8
void
serial_install() {
/* We will initialize the first serial port */
outportb(SERIAL_PORT_A + 1, 0x00);
outportb(SERIAL_PORT_A + 3, 0x80);
outportb(SERIAL_PORT_A + 0, 0x03);
outportb(SERIAL_PORT_A + 1, 0x00);
outportb(SERIAL_PORT_A + 3, 0x03);
outportb(SERIAL_PORT_A + 2, 0xC7);
outportb(SERIAL_PORT_A + 4, 0x0B);
}
int
serial_rcvd() {
return inportb(SERIAL_PORT_A + 5) & 1;
}
char
serial_recv() {
while (serial_rcvd() == 0);
return inportb(SERIAL_PORT_A);
}
int
serial_transmit_empty() {
return inportb(SERIAL_PORT_A + 5) & 0x20;
}
void
serial_send(char out) {
while (serial_transmit_empty() == 0);
outportb(SERIAL_PORT_A, out);
}

View File

@ -157,6 +157,33 @@ start_shell() {
kprintf(" From here, you have access to the virtual file system layer and \n");
kprintf(" can read files, list files in directories, dump memory, registers,\n");
kprintf(" and a few other things.\n");
} else if (!strcmp(cmd, "out")) {
if (tokenid < 3) {
kprintf("Need a port and a character (both as numbers, please) to write...\n");
} else {
int port;
port = atoi(argv[1]);
int val;
val = atoi(argv[2]);
kprintf("Writing %d (%c) to port %d\n", val, (unsigned char)val, port);
outportb((short)port, (unsigned char)val);
}
} else if (!strcmp(cmd, "serial")) {
if (tokenid < 2) {
kprintf("Need some arguments.\n");
} else {
int i = 1;
for (i = 1; i < tokenid; ++i) {
int j = 0;
for (j = 0; j < strlen(argv[i]); ++j) {
serial_send(argv[i][j]);
writech(argv[i][j]);
}
writech(' ');
}
serial_send('\n');
writech('\n');
}
} else {
kprintf("Unrecognized command: %s\n", cmd);
}

View File

@ -91,6 +91,25 @@ strlen(
return i;
}
/*
* atoi
* Naïve implementation thereof.
*/
int
atoi(
const char *str
) {
uint32_t len = strlen(str);
uint32_t out = 0;
uint32_t i;
uint32_t pow = 1;
for (i = len; i > 0; --i) {
out += (str[i-1] - 48) * pow;
pow *= 10;
}
return out;
}
/*
* inportb
* Read from an I/O port.

View File

@ -131,6 +131,7 @@ writech(
*where = c | att;
csr_x++;
}
serial_send(c);
if (csr_x >= 80) {
csr_x = 0;

View File

@ -27,6 +27,7 @@ extern void *memcpy(void *restrict dest, const void *restrict src, size_t count)
extern void *memset(void *dest, int val, size_t count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
extern int atoi(const char *str);
extern unsigned char inportb(unsigned short _port);
extern void outportb(unsigned short _port, unsigned char _data);
extern int strcmp(const char *a, const char *b);
@ -146,4 +147,10 @@ void free(void *ptr);
/* shell */
extern void start_shell();
/* Serial */
extern void serial_install();
extern char serial_recv();
extern void serial_send(char out);
#endif

1
main.c
View File

@ -82,6 +82,7 @@ int main(struct multiboot *mboot_ptr, uint32_t mboot_mag)
/* Hardware drivers */
timer_install();
keyboard_install();
serial_install();
/* Memory management */
paging_install(mboot_ptr->mem_upper);