1 /*
2 * Creation Date: <2004/08/28 18:38:22 greg>
3 * Time-stamp: <2004/08/28 18:38:22 greg>
4 *
5 * <briq.c>
6 *
7 * Copyright (C) 2004, Greg Watson
8 *
9 * derived from mol.c
10 *
11 * Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * version 2
16 *
17 */
18
19 #include "config.h"
20 #include "kernel/kernel.h"
21 #include "arch/common/nvram.h"
22 #include "libc/vsprintf.h"
23 #include "libc/string.h"
24 #include "briq/briq.h"
25 #include <stdarg.h>
26
27 #define UART_BASE 0x3f8
28
29 unsigned long virt_offset = 0;
30
31 void
exit(int status)32 exit( int status )
33 {
34 for (;;);
35 }
36
37 void
fatal_error(const char * err)38 fatal_error( const char *err )
39 {
40 printk("Fatal error: %s\n", err );
41 exit(0);
42 }
43
44 void
panic(const char * err)45 panic( const char *err )
46 {
47 printk("Panic: %s\n", err );
48 exit(0);
49
50 /* won't come here... this keeps the gcc happy */
51 for( ;; )
52 ;
53 }
54
55
56 /************************************************************************/
57 /* print using OSI interface */
58 /************************************************************************/
59
60 static int do_indent;
61
62 int
printk(const char * fmt,...)63 printk( const char *fmt, ... )
64 {
65 char *p, buf[1024];
66 va_list args;
67 int i;
68
69 va_start(args, fmt);
70 i = vnsprintf(buf, sizeof(buf), fmt, args);
71 va_end(args);
72
73 for( p=buf; *p; p++ ) {
74 if( *p == '\n' )
75 do_indent = 0;
76 if( do_indent++ == 1 ) {
77 putchar( '>' );
78 putchar( '>' );
79 putchar( ' ' );
80 }
81 putchar( *p );
82 }
83 return i;
84 }
85
86
87 /************************************************************************/
88 /* TTY iface */
89 /************************************************************************/
90
91 static int ttychar = -1;
92
93 static int
tty_avail(void)94 tty_avail( void )
95 {
96 return 1;
97 }
98
99 static int
tty_putchar(int c)100 tty_putchar( int c )
101 {
102 if( tty_avail() ) {
103 while (!(inb(UART_BASE + 0x05) & 0x20))
104 ;
105 outb(c, UART_BASE);
106 while (!(inb(UART_BASE + 0x05) & 0x40))
107 ;
108 }
109 return c;
110 }
111
112 int
availchar(void)113 availchar( void )
114 {
115 if( !tty_avail() )
116 return 0;
117
118 if( ttychar < 0 )
119 ttychar = inb(UART_BASE);
120 return (ttychar >= 0);
121 }
122
123 int
getchar(void)124 getchar( void )
125 {
126 int ch;
127
128 if( !tty_avail() )
129 return 0;
130
131 if( ttychar < 0 )
132 return inb(UART_BASE);
133 ch = ttychar;
134 ttychar = -1;
135 return ch;
136 }
137
138 int
putchar(int c)139 putchar( int c )
140 {
141 if (c == '\n')
142 tty_putchar('\r');
143 return tty_putchar(c);
144 }
145
146
147 /************************************************************************/
148 /* briQ specific stuff */
149 /************************************************************************/
150
151 static char nvram[2048];
152
153 void
dump_nvram(void)154 dump_nvram(void)
155 {
156 static char hexdigit[] = "0123456789abcdef";
157 int i;
158 for (i = 0; i < 16*4; i++)
159 {
160 printk ("%c", hexdigit[nvram[i] >> 4]);
161 printk ("%c", hexdigit[nvram[i] % 16]);
162 if (!((i + 1) % 16))
163 {
164 printk ("\n");
165 }
166 else
167 {
168 printk (" ");
169 }
170 }
171 }
172
173
174 int
arch_nvram_size(void)175 arch_nvram_size( void )
176 {
177 return sizeof(nvram);
178 }
179
180 void
arch_nvram_put(char * buf)181 arch_nvram_put( char *buf )
182 {
183 memcpy(nvram, buf, sizeof(nvram));
184 printk("new nvram:\n");
185 dump_nvram();
186 }
187
188 void
arch_nvram_get(char * buf)189 arch_nvram_get( char *buf )
190 {
191 memcpy(buf, nvram, sizeof(nvram));
192 printk("current nvram:\n");
193 dump_nvram();
194 }
195