freebsd-dev/sys/i386/boot/biosboot/io.c

309 lines
6.4 KiB
C
Raw Normal View History

1993-06-12 14:58:17 +00:00
/*
* Mach Operating System
* Copyright (c) 1992, 1991 Carnegie Mellon University
* All Rights Reserved.
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* Carnegie Mellon requests users of this software to return to
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*
* from: Mach, Revision 2.2 92/04/04 11:35:57 rpd
* $Id: io.c,v 1.24 1997/07/12 10:23:19 joerg Exp $
1993-06-12 14:58:17 +00:00
*/
Load the kernel symbol table in the boot loader and not at compile time. (Boot with the -D flag if you want symbols.) Make it easier to extend `struct bootinfo' without losing either forwards or backwards compatibility. ddb_aout.c: Get the symbol table from wherever the loader put it. Nuke db_symtab[SYMTAB_SPACE]. boot.c: Enable loading of symbols. Align them on a page boundary. Add printfs about the symbol table sizes. Pass the memory sizes to the kernel. Fix initialization of `unit' (it got moved out of the loop). Fix adding the bss size (it got moved inside an ifdef). Initialize serial port when RB_SERIAL is toggled on. Fix comments. Clean up formatting of recently added code. io.c: Clean up formatting of recently added code. netboot/main.c, machdep.c, wd.c: Change names of bootinfo fields. LINT: Nuke SYMTAB_SPACE. Fix comment about DODUMP. Makefile.i386: Nuke use of dbsym. Exclude gcc symbols from kernel unless compiling with -g. Remove unused macro. Fix comments and formatting. genassym.c: Generate defines for some new bootinfo fields. Change names of old ones. locore.s: Copy only the valid part of the `struct bootinfo' passed by the loader. Reserve space for symbol table, if any. machdep.c: Check the memory sizes passed by the loader, if any. Don't use them yet. bootinfo.h: Add a size field so that we can resolve some mismatches between the loader bootinfo and the kernel boot info. The version number is not so good for this because of historical botches and because it's harder to maintain. Add memory size and symbol table fields. Change the names of everything. Hacks to save a few bytes: asm.S, boot.c, boot2.S: Replace `ouraddr' by `(BOOTSEG << 4)'. boot.c: Don't statically initialize `loadflags' to 0. Disable the "REDUNDANT" code that skips the BIOS variables. Eliminate `total'. Combine some more printfs. boot.h, disk.c, io.c, table.c: Move all statically initialzed data to table.c. io.c: Don't put the A20 gate bits in a variable.
1995-01-25 21:40:47 +00:00
#include "boot.h"
#include <machine/cpufunc.h>
#include <sys/reboot.h>
1993-06-12 14:58:17 +00:00
#define K_RDWR 0x60 /* keyboard data & cmds (read/write) */
#define K_STATUS 0x64 /* keyboard status */
#define K_CMD 0x64 /* keybd ctlr command (write-only) */
#define K_OBUF_FUL 0x01 /* output buffer full */
#define K_IBUF_FUL 0x02 /* input buffer full */
#define KC_CMD_WIN 0xd0 /* read output port */
#define KC_CMD_WOUT 0xd1 /* write output port */
#define KB_A20 0xdf /* enable A20,
1993-06-12 14:58:17 +00:00
enable output buffer full interrupt
enable data line
enable clock line */
1993-06-12 14:58:17 +00:00
1997-05-27 16:26:39 +00:00
static int getchar(int in_buf);
1993-06-12 14:58:17 +00:00
/*
* Gate A20 for high memory
*/
void
gateA20(void)
1993-06-12 14:58:17 +00:00
{
#ifdef IBM_L40
outb(0x92, 0x2);
#else /* !IBM_L40 */
1993-06-12 14:58:17 +00:00
while (inb(K_STATUS) & K_IBUF_FUL);
while (inb(K_STATUS) & K_OBUF_FUL)
(void)inb(K_RDWR);
outb(K_CMD, KC_CMD_WOUT);
while (inb(K_STATUS) & K_IBUF_FUL);
Load the kernel symbol table in the boot loader and not at compile time. (Boot with the -D flag if you want symbols.) Make it easier to extend `struct bootinfo' without losing either forwards or backwards compatibility. ddb_aout.c: Get the symbol table from wherever the loader put it. Nuke db_symtab[SYMTAB_SPACE]. boot.c: Enable loading of symbols. Align them on a page boundary. Add printfs about the symbol table sizes. Pass the memory sizes to the kernel. Fix initialization of `unit' (it got moved out of the loop). Fix adding the bss size (it got moved inside an ifdef). Initialize serial port when RB_SERIAL is toggled on. Fix comments. Clean up formatting of recently added code. io.c: Clean up formatting of recently added code. netboot/main.c, machdep.c, wd.c: Change names of bootinfo fields. LINT: Nuke SYMTAB_SPACE. Fix comment about DODUMP. Makefile.i386: Nuke use of dbsym. Exclude gcc symbols from kernel unless compiling with -g. Remove unused macro. Fix comments and formatting. genassym.c: Generate defines for some new bootinfo fields. Change names of old ones. locore.s: Copy only the valid part of the `struct bootinfo' passed by the loader. Reserve space for symbol table, if any. machdep.c: Check the memory sizes passed by the loader, if any. Don't use them yet. bootinfo.h: Add a size field so that we can resolve some mismatches between the loader bootinfo and the kernel boot info. The version number is not so good for this because of historical botches and because it's harder to maintain. Add memory size and symbol table fields. Change the names of everything. Hacks to save a few bytes: asm.S, boot.c, boot2.S: Replace `ouraddr' by `(BOOTSEG << 4)'. boot.c: Don't statically initialize `loadflags' to 0. Disable the "REDUNDANT" code that skips the BIOS variables. Eliminate `total'. Combine some more printfs. boot.h, disk.c, io.c, table.c: Move all statically initialzed data to table.c. io.c: Don't put the A20 gate bits in a variable.
1995-01-25 21:40:47 +00:00
outb(K_RDWR, KB_A20);
1993-06-12 14:58:17 +00:00
while (inb(K_STATUS) & K_IBUF_FUL);
#endif /* IBM_L40 */
1993-06-12 14:58:17 +00:00
}
/* printf - only handles %d as decimal, %c as char, %s as string */
void
printf(const char *format, ...)
1993-06-12 14:58:17 +00:00
{
int *dataptr = (int *)&format;
1993-06-12 14:58:17 +00:00
char c;
dataptr++;
while ((c = *format++))
1993-06-12 14:58:17 +00:00
if (c != '%')
putchar(c);
else
switch (c = *format++) {
case 'd': {
int num = *dataptr++;
char buf[10], *ptr = buf;
if (num<0) {
num = -num;
putchar('-');
}
do
*ptr++ = '0'+num%10;
while (num /= 10);
do
putchar(*--ptr);
while (ptr != buf);
break;
}
case 'x': {
unsigned int num = *dataptr++, dig;
1993-06-12 14:58:17 +00:00
char buf[8], *ptr = buf;
do
*ptr++ = (dig=(num&0xf)) > 9?
'a' + dig - 10 :
'0' + dig;
while (num >>= 4);
do
putchar(*--ptr);
while (ptr != buf);
break;
}
case 'c': putchar((*dataptr++)&0xff); break;
case 's': {
char *ptr = (char *)*dataptr++;
while ((c = *ptr++))
1993-06-12 14:58:17 +00:00
putchar(c);
break;
}
}
}
void
putchar(int c)
1993-06-12 14:58:17 +00:00
{
if (c == '\n')
putchar('\r');
if (loadflags & RB_DUAL) {
putc(c);
serial_putc(c);
} else if (loadflags & RB_SERIAL)
Load the kernel symbol table in the boot loader and not at compile time. (Boot with the -D flag if you want symbols.) Make it easier to extend `struct bootinfo' without losing either forwards or backwards compatibility. ddb_aout.c: Get the symbol table from wherever the loader put it. Nuke db_symtab[SYMTAB_SPACE]. boot.c: Enable loading of symbols. Align them on a page boundary. Add printfs about the symbol table sizes. Pass the memory sizes to the kernel. Fix initialization of `unit' (it got moved out of the loop). Fix adding the bss size (it got moved inside an ifdef). Initialize serial port when RB_SERIAL is toggled on. Fix comments. Clean up formatting of recently added code. io.c: Clean up formatting of recently added code. netboot/main.c, machdep.c, wd.c: Change names of bootinfo fields. LINT: Nuke SYMTAB_SPACE. Fix comment about DODUMP. Makefile.i386: Nuke use of dbsym. Exclude gcc symbols from kernel unless compiling with -g. Remove unused macro. Fix comments and formatting. genassym.c: Generate defines for some new bootinfo fields. Change names of old ones. locore.s: Copy only the valid part of the `struct bootinfo' passed by the loader. Reserve space for symbol table, if any. machdep.c: Check the memory sizes passed by the loader, if any. Don't use them yet. bootinfo.h: Add a size field so that we can resolve some mismatches between the loader bootinfo and the kernel boot info. The version number is not so good for this because of historical botches and because it's harder to maintain. Add memory size and symbol table fields. Change the names of everything. Hacks to save a few bytes: asm.S, boot.c, boot2.S: Replace `ouraddr' by `(BOOTSEG << 4)'. boot.c: Don't statically initialize `loadflags' to 0. Disable the "REDUNDANT" code that skips the BIOS variables. Eliminate `total'. Combine some more printfs. boot.h, disk.c, io.c, table.c: Move all statically initialzed data to table.c. io.c: Don't put the A20 gate bits in a variable.
1995-01-25 21:40:47 +00:00
serial_putc(c);
else
putc(c);
1993-06-12 14:58:17 +00:00
}
1997-05-27 16:26:39 +00:00
static int
getchar(int in_buf)
1993-06-12 14:58:17 +00:00
{
int c;
loop:
if (loadflags & RB_DUAL) {
if (ischar())
c = getc();
else if (serial_ischar())
c = serial_getc();
else
goto loop;
} else if (loadflags & RB_SERIAL)
c = serial_getc();
else
c = getc();
if (c == '\r')
1993-06-12 14:58:17 +00:00
c = '\n';
if (c == '\b') {
if (in_buf != 0) {
putchar('\b');
putchar(' ');
} else {
goto loop;
}
1993-06-12 14:58:17 +00:00
}
putchar(c);
return(c);
}
/*
* This routine uses an inb to an unused port, the time to execute that
* inb is approximately 1.25uS. This value is pretty constant across
* all CPU's and all buses, with the exception of some PCI implentations
* that do not forward this I/O adress to the ISA bus as they know it
* is not a valid ISA bus address, those machines execute this inb in
* 60 nS :-(.
*
* XXX this should be converted to use bios_tick.
*/
void
delay1ms(void)
{
int i = 800;
while (--i >= 0)
(void)inb(0x84);
}
static __inline int
isch(void)
{
int isc;
/*
* Checking the keyboard has the side effect of enabling clock
* interrupts so that bios_tick works. Check the keyboard to
* get this side effect even if we only want the serial status.
*/
isc = ischar();
if (loadflags & RB_DUAL) {
if (isc != 0)
return (isc);
} else if (!(loadflags & RB_SERIAL))
return (isc);
return (serial_ischar());
}
static __inline unsigned
pword(unsigned physaddr)
{
unsigned result;
/*
* Give the fs prefix separately because gas omits it for
* "movl %fs:0x46c, %eax".
*/
__asm __volatile("fs; movl %1, %0" : "=r" (result)
: "m" (*(unsigned *)physaddr));
return (result);
}
int
gets(char *buf)
1993-06-12 14:58:17 +00:00
{
#define bios_tick pword(0x46c)
#define BIOS_TICK_MS 55
unsigned initial_bios_tick;
1993-06-12 14:58:17 +00:00
char *ptr=buf;
#if BOOTWAIT
for (initial_bios_tick = bios_tick;
bios_tick - initial_bios_tick < BOOTWAIT / BIOS_TICK_MS;)
#endif
if (isch())
for (;;) {
switch(*ptr = getchar(ptr - buf) & 0xff) {
1993-06-12 14:58:17 +00:00
case '\n':
case '\r':
*ptr = '\0';
return 1;
case '\b':
if (ptr > buf) ptr--;
continue;
default:
ptr++;
}
#if TIMEOUT + 0
#if !BOOTWAIT
#error "TIMEOUT without BOOTWAIT"
#endif
for (initial_bios_tick = bios_tick;;) {
if (isch())
break;
if (bios_tick - initial_bios_tick >=
TIMEOUT / BIOS_TICK_MS)
return 0;
}
#endif
}
1993-06-12 14:58:17 +00:00
return 0;
}
int
strcmp(const char *s1, const char *s2)
1993-06-12 14:58:17 +00:00
{
while (*s1 == *s2) {
if (!*s1++)
return 0;
s2++;
}
return 1;
}
#ifdef CDBOOT
int
strcasecmp(const char *s1, const char *s2)
{
/*
* We only consider ASCII chars and don't anticipate
* control characters (they are invalid in filenames
* anyway).
*/
while ((*s1 & 0x5f) == (*s2 & 0x5f)) {
if (!*s1++)
return 0;
s2++;
}
return 1;
}
#endif /* !CDBOOT */
void
bcopy(const void *from, void *to, size_t len)
1993-06-12 14:58:17 +00:00
{
char *fp = (char *)from;
char *tp = (char *)to;
1993-06-12 14:58:17 +00:00
while (len-- > 0)
*tp++ = *fp++;
1993-06-12 14:58:17 +00:00
}
/* To quote Ken: "You are not expected to understand this." :) */
void
twiddle(void)
{
putchar((char)tw_chars);
tw_chars = (tw_chars >> 8) | ((tw_chars & (unsigned long)0xFF) << 24);
putchar('\b');
}