All of lore.kernel.org
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: torvalds@linux-foundation.org, andi@firstfloor.org,
	linux-kernel@vger.kernel.org
Cc: "H. Peter Anvin" <hpa@zytor.com>
Subject: [x86 setup 20/33] Console-writing code for the new x86 setup code
Date: Wed, 11 Jul 2007 12:18:45 -0700	[thread overview]
Message-ID: <1184181541492-git-send-email-hpa@zytor.com> (raw)
Message-ID: <f38ae9eb84193af6271ed2294992f589fabc8b3d.1184181208.git.hpa@zytor.com> (raw)
In-Reply-To: <11841815412175-git-send-email-hpa@zytor.com>
In-Reply-To: <7aca4ce0a222438c94300a2ea535270a21db8812.1184181208.git.hpa@zytor.com>

From: H. Peter Anvin <hpa@zytor.com>

This implements writing text to the console, including printf().

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
---
 arch/i386/boot/printf.c |  307 +++++++++++++++++++++++++++++++++++++++++++++++
 arch/i386/boot/tty.c    |  112 +++++++++++++++++
 2 files changed, 419 insertions(+), 0 deletions(-)
 create mode 100644 arch/i386/boot/printf.c
 create mode 100644 arch/i386/boot/tty.c

diff --git a/arch/i386/boot/printf.c b/arch/i386/boot/printf.c
new file mode 100644
index 0000000..1a09f93
--- /dev/null
+++ b/arch/i386/boot/printf.c
@@ -0,0 +1,307 @@
+/* -*- linux-c -*- ------------------------------------------------------- *
+ *
+ *   Copyright (C) 1991, 1992 Linus Torvalds
+ *   Copyright 2007 rPath, Inc. - All Rights Reserved
+ *
+ *   This file is part of the Linux kernel, and is made available under
+ *   the terms of the GNU General Public License version 2.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * arch/i386/boot/printf.c
+ *
+ * Oh, it's a waste of space, but oh-so-yummy for debugging.  This
+ * version of printf() does not include 64-bit support.  "Live with
+ * it."
+ *
+ */
+
+#include "boot.h"
+
+static int skip_atoi(const char **s)
+{
+	int i = 0;
+
+	while (isdigit(**s))
+		i = i * 10 + *((*s)++) - '0';
+	return i;
+}
+
+#define ZEROPAD	1		/* pad with zero */
+#define SIGN	2		/* unsigned/signed long */
+#define PLUS	4		/* show plus */
+#define SPACE	8		/* space if plus */
+#define LEFT	16		/* left justified */
+#define SPECIAL	32		/* 0x */
+#define LARGE	64		/* use 'ABCDEF' instead of 'abcdef' */
+
+#define do_div(n,base) ({ \
+int __res; \
+__res = ((unsigned long) n) % (unsigned) base; \
+n = ((unsigned long) n) / (unsigned) base; \
+__res; })
+
+static char *number(char *str, long num, int base, int size, int precision,
+		    int type)
+{
+	char c, sign, tmp[66];
+	const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
+	int i;
+
+	if (type & LARGE)
+		digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+	if (type & LEFT)
+		type &= ~ZEROPAD;
+	if (base < 2 || base > 36)
+		return 0;
+	c = (type & ZEROPAD) ? '0' : ' ';
+	sign = 0;
+	if (type & SIGN) {
+		if (num < 0) {
+			sign = '-';
+			num = -num;
+			size--;
+		} else if (type & PLUS) {
+			sign = '+';
+			size--;
+		} else if (type & SPACE) {
+			sign = ' ';
+			size--;
+		}
+	}
+	if (type & SPECIAL) {
+		if (base == 16)
+			size -= 2;
+		else if (base == 8)
+			size--;
+	}
+	i = 0;
+	if (num == 0)
+		tmp[i++] = '0';
+	else
+		while (num != 0)
+			tmp[i++] = digits[do_div(num, base)];
+	if (i > precision)
+		precision = i;
+	size -= precision;
+	if (!(type & (ZEROPAD + LEFT)))
+		while (size-- > 0)
+			*str++ = ' ';
+	if (sign)
+		*str++ = sign;
+	if (type & SPECIAL) {
+		if (base == 8)
+			*str++ = '0';
+		else if (base == 16) {
+			*str++ = '0';
+			*str++ = digits[33];
+		}
+	}
+	if (!(type & LEFT))
+		while (size-- > 0)
+			*str++ = c;
+	while (i < precision--)
+		*str++ = '0';
+	while (i-- > 0)
+		*str++ = tmp[i];
+	while (size-- > 0)
+		*str++ = ' ';
+	return str;
+}
+
+int vsprintf(char *buf, const char *fmt, va_list args)
+{
+	int len;
+	unsigned long num;
+	int i, base;
+	char *str;
+	const char *s;
+
+	int flags;		/* flags to number() */
+
+	int field_width;	/* width of output field */
+	int precision;		/* min. # of digits for integers; max
+				   number of chars for from string */
+	int qualifier;		/* 'h', 'l', or 'L' for integer fields */
+
+	for (str = buf; *fmt; ++fmt) {
+		if (*fmt != '%') {
+			*str++ = *fmt;
+			continue;
+		}
+
+		/* process flags */
+		flags = 0;
+	      repeat:
+		++fmt;		/* this also skips first '%' */
+		switch (*fmt) {
+		case '-':
+			flags |= LEFT;
+			goto repeat;
+		case '+':
+			flags |= PLUS;
+			goto repeat;
+		case ' ':
+			flags |= SPACE;
+			goto repeat;
+		case '#':
+			flags |= SPECIAL;
+			goto repeat;
+		case '0':
+			flags |= ZEROPAD;
+			goto repeat;
+		}
+
+		/* get field width */
+		field_width = -1;
+		if (isdigit(*fmt))
+			field_width = skip_atoi(&fmt);
+		else if (*fmt == '*') {
+			++fmt;
+			/* it's the next argument */
+			field_width = va_arg(args, int);
+			if (field_width < 0) {
+				field_width = -field_width;
+				flags |= LEFT;
+			}
+		}
+
+		/* get the precision */
+		precision = -1;
+		if (*fmt == '.') {
+			++fmt;
+			if (isdigit(*fmt))
+				precision = skip_atoi(&fmt);
+			else if (*fmt == '*') {
+				++fmt;
+				/* it's the next argument */
+				precision = va_arg(args, int);
+			}
+			if (precision < 0)
+				precision = 0;
+		}
+
+		/* get the conversion qualifier */
+		qualifier = -1;
+		if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
+			qualifier = *fmt;
+			++fmt;
+		}
+
+		/* default base */
+		base = 10;
+
+		switch (*fmt) {
+		case 'c':
+			if (!(flags & LEFT))
+				while (--field_width > 0)
+					*str++ = ' ';
+			*str++ = (unsigned char)va_arg(args, int);
+			while (--field_width > 0)
+				*str++ = ' ';
+			continue;
+
+		case 's':
+			s = va_arg(args, char *);
+			len = strnlen(s, precision);
+
+			if (!(flags & LEFT))
+				while (len < field_width--)
+					*str++ = ' ';
+			for (i = 0; i < len; ++i)
+				*str++ = *s++;
+			while (len < field_width--)
+				*str++ = ' ';
+			continue;
+
+		case 'p':
+			if (field_width == -1) {
+				field_width = 2 * sizeof(void *);
+				flags |= ZEROPAD;
+			}
+			str = number(str,
+				     (unsigned long)va_arg(args, void *), 16,
+				     field_width, precision, flags);
+			continue;
+
+		case 'n':
+			if (qualifier == 'l') {
+				long *ip = va_arg(args, long *);
+				*ip = (str - buf);
+			} else {
+				int *ip = va_arg(args, int *);
+				*ip = (str - buf);
+			}
+			continue;
+
+		case '%':
+			*str++ = '%';
+			continue;
+
+			/* integer number formats - set up the flags and "break" */
+		case 'o':
+			base = 8;
+			break;
+
+		case 'X':
+			flags |= LARGE;
+		case 'x':
+			base = 16;
+			break;
+
+		case 'd':
+		case 'i':
+			flags |= SIGN;
+		case 'u':
+			break;
+
+		default:
+			*str++ = '%';
+			if (*fmt)
+				*str++ = *fmt;
+			else
+				--fmt;
+			continue;
+		}
+		if (qualifier == 'l')
+			num = va_arg(args, unsigned long);
+		else if (qualifier == 'h') {
+			num = (unsigned short)va_arg(args, int);
+			if (flags & SIGN)
+				num = (short)num;
+		} else if (flags & SIGN)
+			num = va_arg(args, int);
+		else
+			num = va_arg(args, unsigned int);
+		str = number(str, num, base, field_width, precision, flags);
+	}
+	*str = '\0';
+	return str - buf;
+}
+
+int sprintf(char *buf, const char *fmt, ...)
+{
+	va_list args;
+	int i;
+
+	va_start(args, fmt);
+	i = vsprintf(buf, fmt, args);
+	va_end(args);
+	return i;
+}
+
+int printf(const char *fmt, ...)
+{
+	char printf_buf[1024];
+	va_list args;
+	int printed;
+
+	va_start(args, fmt);
+	printed = vsprintf(printf_buf, fmt, args);
+	va_end(args);
+
+	puts(printf_buf);
+
+	return printed;
+}
diff --git a/arch/i386/boot/tty.c b/arch/i386/boot/tty.c
new file mode 100644
index 0000000..a8db787
--- /dev/null
+++ b/arch/i386/boot/tty.c
@@ -0,0 +1,112 @@
+/* -*- linux-c -*- ------------------------------------------------------- *
+ *
+ *   Copyright (C) 1991, 1992 Linus Torvalds
+ *   Copyright 2007 rPath, Inc. - All Rights Reserved
+ *
+ *   This file is part of the Linux kernel, and is made available under
+ *   the terms of the GNU General Public License version 2.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * arch/i386/boot/tty.c
+ *
+ * Very simple screen I/O
+ * XXX: Probably should add very simple serial I/O?
+ */
+
+#include "boot.h"
+
+/*
+ * These functions are in .inittext so they can be used to signal
+ * error during initialization.
+ */
+
+void __attribute__((section(".inittext"))) putchar(int ch)
+{
+	unsigned char c = ch;
+
+	if (c == '\n')
+		putchar('\r');	/* \n -> \r\n */
+
+	/* int $0x10 is known to have bugs involving touching registers
+	   it shouldn't.  Be extra conservative... */
+	asm volatile("pushal; int $0x10; popal"
+		     : : "b" (0x0007), "c" (0x0001), "a" (0x0e00|ch));
+}
+
+void __attribute__((section(".inittext"))) puts(const char *str)
+{
+	int n = 0;
+	while (*str) {
+		putchar(*str++);
+		n++;
+	}
+}
+
+/*
+ * Read the CMOS clock through the BIOS, and return the
+ * seconds in BCD.
+ */
+
+static u8 gettime(void)
+{
+	u16 ax = 0x0200;
+	u16 cx, dx;
+
+	asm("int $0x1a"
+	    : "+a" (ax), "=c" (cx), "=d" (dx)
+	    : : "ebx", "esi", "edi");
+
+	return dx >> 8;
+}
+
+/*
+ * Read from the keyboard
+ */
+int getchar(void)
+{
+	u16 ax = 0;
+	asm("int $0x16" : "+a" (ax));
+
+	return ax & 0xff;
+}
+
+static int kbd_pending(void)
+{
+	u8 pending;
+	asm("int $0x16; setnz %0"
+	    : "=rm" (pending)
+	    : "a" (0x0100));
+	return pending;
+}
+
+void kbd_flush(void)
+{
+	for (;;) {
+		if (!kbd_pending())
+			break;
+		getchar();
+	}
+}
+
+int getchar_timeout(void)
+{
+	int cnt = 30;
+	int t0, t1;
+
+	t0 = gettime();
+
+	while (cnt) {
+		if (kbd_pending())
+			return getchar();
+
+		t1 = gettime();
+		if (t0 != t1) {
+			cnt--;
+			t0 = t1;
+		}
+	}
+
+	return 0;		/* Timeout! */
+}
-- 
1.5.2.2


  parent reply	other threads:[~2007-07-11 19:28 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-11 19:18 x86 setup code rewrite in C - revised H. Peter Anvin
2007-07-11 19:18 ` [x86 setup 01/33] x86 setup: MAINTAINERS: formally take responsibility for the i386 boot code H. Peter Anvin
2007-07-11 19:18   ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 02/33] hd.c: remove BIOS/CMOS queries H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 03/33] include/asm-i386/boot.h: This is <asm/boot.h>, not <linux/boot.h> H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 04/33] Unify the CPU features vectors between i386 and x86-64 H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 05/33] Change CONFIG_X86_MINIMUM_CPU_MODEL to CONFIG_X86_MINIMUM_CPU_FAMILY H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 06/33] Clean up struct screen_info (<linux/screen_info.h>) H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 07/33] Use a new CPU feature word to cover all Intel features that are spread around H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 08/33] Define zero-page offset 0x1e4 as a scratch field, and use it H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 09/33] Make definitions for struct e820entry and struct e820map consistent H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 10/33] Make struct boot_params a real structure, and remove obsolete fields H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 11/33] x86-64: add CONFIG_PHYSICAL_ALIGN for consistency with i386 H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 12/33] x86-64: add symbolic constants for the boot segment selectors H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 13/33] Header file to produce 16-bit code with gcc H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 14/33] Top header file for new x86 setup code H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 15/33] Simple bitops for the " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 16/33] String-handling functions " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 17/33] A20 handling code H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 18/33] APM probing code H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 19/33] Command-line parsing code for the new x86 setup code H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` H. Peter Anvin [this message]
2007-07-11 19:18     ` [x86 setup 20/33] Console-writing " H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 21/33] Version string " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 22/33] CPU features verification " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 23/33] EDD probing code " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 24/33] MCA support for " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 25/33] Memory probing support for the " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 26/33] Voyager " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 27/33] Video mode probing " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-16 10:59     ` Martin Mares
2007-07-11 19:18   ` [x86 setup 28/33] Code for actual protected-mode entry H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 29/33] Assembly header and main routine for new x86 setup code H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 30/33] Linker script for the " H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 31/33] Use the new x86 setup code for i386 H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-13  9:27       ` Tilman Schmidt
2007-07-11 19:18   ` [x86 setup 32/33] Use the new x86 setup code for x86-64; unify with i386 H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 19:18   ` [x86 setup 33/33] Remove old i386 setup code H. Peter Anvin
2007-07-11 19:18     ` H. Peter Anvin
2007-07-11 20:08 ` x86 setup code rewrite in C - revised Jeff Garzik
2007-07-11 20:29   ` H. Peter Anvin
2007-07-12 17:24 ` Linus Torvalds
2007-07-12 17:30   ` Andrew Morton
2007-07-12 17:49     ` Linus Torvalds
2007-07-12 19:38   ` Andi Kleen
  -- strict thread matches above, loose matches on Subject: below --
2007-07-10  2:51 x86 setup code rewrite in C H. Peter Anvin
2007-07-10  2:51 ` [x86 setup 01/33] x86 setup: MAINTAINERS: formally take responsibility for the i386 boot code H. Peter Anvin
2007-07-10  2:51   ` [x86 setup 02/33] hd.c: remove BIOS/CMOS queries H. Peter Anvin
2007-07-10  2:51     ` [x86 setup 03/33] include/asm-i386/boot.h: This is <asm/boot.h>, not <linux/boot.h> H. Peter Anvin
2007-07-10  2:51       ` [x86 setup 04/33] Unify the CPU features vectors between i386 and x86-64 H. Peter Anvin
2007-07-10  2:51         ` [x86 setup 05/33] Change CONFIG_X86_MINIMUM_CPU_MODEL to CONFIG_X86_MINIMUM_CPU_FAMILY H. Peter Anvin
2007-07-10  2:51           ` [x86 setup 06/33] Clean up struct screen_info (<linux/screen_info.h>) H. Peter Anvin
2007-07-10  2:51             ` [x86 setup 07/33] Use a new CPU feature word to cover all Intel features that are spread around H. Peter Anvin
2007-07-10  2:51               ` [x86 setup 08/33] Define zero-page offset 0x1e4 as a scratch field, and use it H. Peter Anvin
2007-07-10  2:51                 ` [x86 setup 09/33] Make definitions for struct e820entry and struct e820map consistent H. Peter Anvin
2007-07-10  2:51                   ` [x86 setup 10/33] Make struct boot_params a real structure, and remove obsolete fields H. Peter Anvin
2007-07-10  2:51                     ` [x86 setup 11/33] x86-64: add CONFIG_PHYSICAL_ALIGN for consistency with i386 H. Peter Anvin
2007-07-10  2:51                       ` [x86 setup 12/33] x86-64: add symbolic constants for the boot segment selectors H. Peter Anvin
2007-07-10  2:51                         ` [x86 setup 13/33] Header file to produce 16-bit code with gcc H. Peter Anvin
2007-07-10  2:51                           ` [x86 setup 14/33] Top header file for new x86 setup code H. Peter Anvin
2007-07-10  2:51                             ` [x86 setup 15/33] Simple bitops for the " H. Peter Anvin
2007-07-10  2:51                               ` [x86 setup 16/33] String-handling functions " H. Peter Anvin
2007-07-10  2:51                                 ` [x86 setup 17/33] A20 handling code H. Peter Anvin
2007-07-10  2:51                                   ` [x86 setup 18/33] APM probing code H. Peter Anvin
2007-07-10  2:51                                     ` [x86 setup 19/33] Command-line parsing code for the new x86 setup code H. Peter Anvin
2007-07-10  2:51                                       ` [x86 setup 01/33] x86 setup: MAINTAINERS: formally take responsibility for the i386 boot code H. Peter Anvin
2007-07-10  2:51                                         ` [x86 setup 20/33] Console-writing code for the new x86 setup code H. Peter Anvin
2007-07-10  2:51                                           ` H. Peter Anvin
2007-07-10 18:35                                             ` Jan Engelhardt
2007-07-10 18:48                                               ` H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1184181541492-git-send-email-hpa@zytor.com \
    --to=hpa@zytor.com \
    --cc=andi@firstfloor.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.