summaryrefslogtreecommitdiff
path: root/sos-code-article4/drivers
diff options
context:
space:
mode:
authorAlex AUVOLAT <alex.auvolat@ens.fr>2014-03-28 09:10:23 +0100
committerAlex AUVOLAT <alex.auvolat@ens.fr>2014-03-28 09:10:23 +0100
commitbb55beef914e7488350ad1d0419d4bc60ad63c99 (patch)
treeb1cfe0e5c76cbca16e7e3c16e108c23e366a2902 /sos-code-article4/drivers
downloadSOS-bb55beef914e7488350ad1d0419d4bc60ad63c99.tar.gz
SOS-bb55beef914e7488350ad1d0419d4bc60ad63c99.zip
Import code for article1, 2, 3, 4
Diffstat (limited to 'sos-code-article4/drivers')
-rw-r--r--sos-code-article4/drivers/bochs.c119
-rw-r--r--sos-code-article4/drivers/bochs.h54
-rw-r--r--sos-code-article4/drivers/x86_videomem.c128
-rw-r--r--sos-code-article4/drivers/x86_videomem.h98
4 files changed, 399 insertions, 0 deletions
diff --git a/sos-code-article4/drivers/bochs.c b/sos-code-article4/drivers/bochs.c
new file mode 100644
index 0000000..db18599
--- /dev/null
+++ b/sos-code-article4/drivers/bochs.c
@@ -0,0 +1,119 @@
+/* Copyright (C) 2004 David Decotigny
+ Copyright (C) 1999 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ USA.
+*/
+#include <hwcore/ioports.h>
+#include <sos/klibc.h>
+
+#include "bochs.h"
+
+/* This is a special hack that is only useful when running the
+ operating system under the Bochs emulator. */
+#define SOS_BOCHS_IOPORT 0xe9
+
+sos_ret_t sos_bochs_setup(void)
+{
+ return SOS_OK;
+}
+
+
+#define sos_bochs_putchar(chr) \
+ outb((chr), SOS_BOCHS_IOPORT)
+
+sos_ret_t sos_bochs_putstring(const char* str)
+{
+ for ( ; str && (*str != '\0') ; str++)
+ sos_bochs_putchar(*str);
+
+ return SOS_OK;
+}
+
+
+sos_ret_t sos_bochs_puthex(unsigned val, int nbytes)
+{
+ unsigned c;
+
+#define BOCHS_PRTHEX(q) \
+ ({ unsigned char r; if ((q) >= 10) r='a'+(q)-10; \
+ else r='0'+(q); sos_bochs_putchar(r); })
+
+ switch (nbytes)
+ {
+ case 4:
+ c = (val >> 24) & 0xff;
+ BOCHS_PRTHEX((c >> 4)&0xf);
+ BOCHS_PRTHEX(c&0xf);
+ case 3:
+ c = (val >> 16) & 0xff;
+ BOCHS_PRTHEX((c >> 4)&0xf);
+ BOCHS_PRTHEX(c&0xf);
+ case 2:
+ c = (val >> 8) & 0xff;
+ BOCHS_PRTHEX((c >> 4)&0xf);
+ BOCHS_PRTHEX(c&0xf);
+ case 1:
+ c = val & 0xff;
+ BOCHS_PRTHEX((c >> 4)&0xf);
+ BOCHS_PRTHEX(c&0xf);
+ }
+
+ return SOS_OK;
+}
+
+
+sos_ret_t sos_bochs_hexdump(const void* addr, int nbytes)
+{
+ int offs;
+ for (offs = 0 ; offs < nbytes ; offs++)
+ {
+ const unsigned char *c;
+
+ if ((offs % 16) == 0)
+ {
+ sos_bochs_putstring("0x");
+ sos_bochs_puthex(offs, 4);
+ }
+
+ if ((offs % 8) == 0)
+ sos_bochs_putstring(" ");
+
+ c = (const unsigned char*)(addr + offs);
+ sos_bochs_puthex(*c, 1);
+ sos_bochs_putstring(" ");
+
+ if (((offs + 1) % 16) == 0)
+ sos_bochs_putstring("\n");
+ }
+
+ if (offs % 16)
+ sos_bochs_putstring("\n");
+
+ return SOS_OK;
+}
+
+
+sos_ret_t sos_bochs_printf(const char *format, /* args */...)
+{
+ char buff[256];
+ va_list ap;
+
+ va_start(ap, format);
+ vsnprintf(buff, sizeof(buff), format, ap);
+ va_end(ap);
+
+ return sos_bochs_putstring(buff);
+}
diff --git a/sos-code-article4/drivers/bochs.h b/sos-code-article4/drivers/bochs.h
new file mode 100644
index 0000000..310b023
--- /dev/null
+++ b/sos-code-article4/drivers/bochs.h
@@ -0,0 +1,54 @@
+/* Copyright (C) 2004 David Decotigny
+ Copyright (C) 1999 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ USA.
+*/
+#ifndef _SOS_BOCHS_H_
+#define _SOS_BOCHS_H_
+
+/**
+ * @file bochs.h
+ *
+ * If you compiled Bochs with the --enable-e9-hack, then any character
+ * printed to the 0xE9 I/O port is printed to the xterm that is
+ * running Bochs. This may appear to be a detail, but in fact, this
+ * functionnality is *VERY* precious for debugging purposes. This
+ * """driver""" handles this feature.
+ */
+
+#include <sos/errno.h>
+#include <sos/types.h>
+
+sos_ret_t sos_bochs_setup(void);
+
+sos_ret_t sos_bochs_putstring(const char* str);
+
+/** Print the least signficant 32 (nbytes == 4), 24 (nbytes == 3), 16
+ (nbytes == 2) or 8 (nbytes == 1) bits of val in hexadecimal. */
+sos_ret_t sos_bochs_puthex(unsigned val, int nbytes);
+
+/** hexdump-style pretty printing */
+sos_ret_t sos_bochs_hexdump(const void* addr, int nbytes);
+
+/**
+ * Print the formatted string. Very restricted version of printf(3):
+ * 1/ can print max 255 chars, 2/ supports only %d/%i, %c, %s, %x
+ * without any support for flag charachters (eg %08x).
+ */
+sos_ret_t sos_bochs_printf(const char *format, /* args */...)
+ __attribute__ ((format (printf, 1, 2)));
+
+#endif
diff --git a/sos-code-article4/drivers/x86_videomem.c b/sos-code-article4/drivers/x86_videomem.c
new file mode 100644
index 0000000..cc4b79c
--- /dev/null
+++ b/sos-code-article4/drivers/x86_videomem.c
@@ -0,0 +1,128 @@
+/* Copyright (C) 2004 David Decotigny
+ Copyright (C) 1999 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ USA.
+*/
+#include <sos/klibc.h>
+#include <hwcore/ioports.h>
+
+#include "x86_videomem.h"
+
+/* The text video memory starts at address 0xB8000. Odd bytes are the
+ ASCII value of the character, even bytes are attribute for the
+ preceding character. */
+#define VIDEO 0xb8000
+
+
+/* Console screen size */
+#define LINES 25
+#define COLUMNS 80
+
+
+/** The structure of a character element in the video memory. @see
+ http://webster.cs.ucr.edu/AoA DOS edition chapter 23 */
+typedef struct {
+ unsigned char character;
+ unsigned char attribute;
+} __attribute__ ((packed)) x86_video_mem[LINES*COLUMNS];
+
+
+
+/** The base pointer for the video memory */
+static volatile x86_video_mem *video = (volatile x86_video_mem*)VIDEO;
+
+sos_ret_t sos_x86_videomem_setup(void)
+{
+ /*
+ * Hide cursor. @see Ralf Brown's interrupt (and port) list
+ * http://www-2.cs.cmu.edu/~ralf/files.html
+ */
+#define CRT_REG_INDEX 0x3d4
+#define CRT_REG_DATA 0x3d5
+
+ /* CRT index port => ask for access to register 0xa ("cursor
+ start") */
+ outb(0x0a, CRT_REG_INDEX);
+
+ /* (RBIL Tables 708 & 654) CRT Register 0xa => bit 5 = cursor OFF */
+ outb(1 << 5, CRT_REG_DATA);
+
+ return SOS_OK;
+}
+
+
+sos_ret_t sos_x86_videomem_cls(unsigned char attribute)
+{
+ /* Clears the screen */
+ int i;
+ for(i = 0 ; i < LINES*COLUMNS ; i++)
+ {
+ (*video)[i].character = 0;
+ (*video)[i].attribute = attribute;
+ }
+
+ return SOS_OK;
+}
+
+
+sos_ret_t sos_x86_videomem_putstring(unsigned char row, unsigned char col,
+ unsigned char attribute,
+ const char *str)
+{
+ unsigned video_offs = row*COLUMNS + col;
+
+ if (video_offs >= LINES*COLUMNS)
+ return -SOS_EINVAL;
+
+ for ( ; str && *str && (video_offs < LINES*COLUMNS) ; str++, video_offs++)
+ {
+ (*video)[video_offs].character = (unsigned char)*str;
+ (*video)[video_offs].attribute = attribute;
+ }
+
+ return SOS_OK;
+}
+
+
+sos_ret_t sos_x86_videomem_putchar(unsigned char row, unsigned char col,
+ unsigned char attribute,
+ unsigned char c)
+{
+ unsigned video_offs = row*COLUMNS + col;
+
+ if (video_offs >= LINES*COLUMNS)
+ return -SOS_EINVAL;
+
+ (*video)[video_offs].character = c;
+ (*video)[video_offs].attribute = attribute;
+
+ return SOS_OK;
+}
+
+
+sos_ret_t sos_x86_videomem_printf(unsigned char row, unsigned char col,
+ unsigned char attribute,
+ const char *format, /* args */...)
+{
+ char buff[256];
+ va_list ap;
+
+ va_start(ap, format);
+ vsnprintf(buff, sizeof(buff), format, ap);
+ va_end(ap);
+
+ return sos_x86_videomem_putstring(row, col, attribute, buff);
+}
diff --git a/sos-code-article4/drivers/x86_videomem.h b/sos-code-article4/drivers/x86_videomem.h
new file mode 100644
index 0000000..31a9dfc
--- /dev/null
+++ b/sos-code-article4/drivers/x86_videomem.h
@@ -0,0 +1,98 @@
+/* Copyright (C) 2004 David Decotigny
+ Copyright (C) 1999 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ USA.
+*/
+#ifndef _SOS_X86_VIDEOMEM_H_
+#define _SOS_X86_VIDEOMEM_H_
+
+/**
+ * @file x86_videomem.h
+ *
+ * On x86 PC platforms, the text mode screen memory (and CGA/EGA/VGA
+ * too) is mapped into physical memory. This file handles access to
+ * this screen, supposed to be set in text-mode, through this memory
+ * area. All the functions below print the characters directly to the
+ * memory, without interpreting the escaped characters (such as \n,
+ * \r...)
+ */
+
+#include <sos/errno.h>
+
+/**
+ * x86 video attributes
+ * See http://webster.cs.ucr.edu/AoA/DOS/ch23/CH23-1.html
+ */
+/* Normal and Dark/Light foreground */
+#define SOS_X86_VIDEO_FG_BLACK 0
+#define SOS_X86_VIDEO_FG_DKGRAY 8
+#define SOS_X86_VIDEO_FG_BLUE 1
+#define SOS_X86_VIDEO_FG_LTBLUE 9
+#define SOS_X86_VIDEO_FG_GREEN 2
+#define SOS_X86_VIDEO_FG_LTGREEN 10
+#define SOS_X86_VIDEO_FG_CYAN 3
+#define SOS_X86_VIDEO_FG_LTCYAN 11
+#define SOS_X86_VIDEO_FG_RED 4
+#define SOS_X86_VIDEO_FG_LTRED 12
+#define SOS_X86_VIDEO_FG_MAGENTA 5
+#define SOS_X86_VIDEO_FG_LTMAGENTA 13
+#define SOS_X86_VIDEO_FG_BROWN 6
+#define SOS_X86_VIDEO_FG_YELLOW 14
+#define SOS_X86_VIDEO_FG_LTGRAY 7
+#define SOS_X86_VIDEO_FG_WHITE 15
+/* Background */
+#define SOS_X86_VIDEO_BG_BLACK (0 << 4)
+#define SOS_X86_VIDEO_BG_BLUE (1 << 4)
+#define SOS_X86_VIDEO_BG_GREEN (2 << 4)
+#define SOS_X86_VIDEO_BG_CYAN (3 << 4)
+#define SOS_X86_VIDEO_BG_RED (4 << 4)
+#define SOS_X86_VIDEO_BG_MAGENTA (5 << 4)
+#define SOS_X86_VIDEO_BG_BROWN (6 << 4)
+#define SOS_X86_VIDEO_BG_LTGRAY (7 << 4)
+/* Blinking */
+#define SOS_X86_VIDEO_FG_BLINKING (1 << 7)
+
+
+/** Setup the video RAM mapping and clear the screen */
+sos_ret_t sos_x86_videomem_setup(void);
+
+/** Clears the screen and set the background color as given by
+ attribute */
+sos_ret_t sos_x86_videomem_cls(unsigned char attribute);
+
+/** Print the string on the scren with the given attribute. Does not
+ handle scrolling */
+sos_ret_t sos_x86_videomem_putstring(unsigned char row, unsigned char col,
+ unsigned char attribute,
+ const char *str);
+
+/** Print the character on the scren with the given attribute. Does not
+ handle scrolling */
+sos_ret_t sos_x86_videomem_putchar(unsigned char row, unsigned char col,
+ unsigned char attribute,
+ unsigned char c);
+
+/**
+ * Print the formatted string. Very restricted version of printf(3):
+ * 1/ can print max 255 chars, 2/ supports only %d/%i, %c, %s, %x
+ * without any support for flag charachters (eg %08x).
+ */
+sos_ret_t sos_x86_videomem_printf(unsigned char row, unsigned char col,
+ unsigned char attribute,
+ const char *format, /* args */...)
+ __attribute__ ((format (printf, 4, 5)));
+
+#endif /* _SOS_X86_VIDEOMEM_H_ */