2 * arch/ppc/boot/common/misc-common.c
4 * Misc. bootloader code (almost) all platforms can use
6 * Author: Johnnie Peters <jpeters@mvista.com>
7 * Editor: Tom Rini <trini@mvista.com>
9 * Derived from arch/ppc/boot/prep/misc.c
11 * 2000-2001 (c) MontaVista, Software, Inc. This file is licensed under
12 * the terms of the GNU General Public License version 2. This program
13 * is licensed "as is" without any warranty of any kind, whether express
17 #include <stdarg.h> /* for va_ bits */
18 #include <linux/config.h>
19 #include <linux/string.h>
23 /* If we're on a PReP, assume we have a keyboard controller
24 * Also note, if we're not PReP, we assume you are a serial
26 #if defined(CONFIG_PPC_PREP) && defined(CONFIG_VGA_CONSOLE)
27 extern void cursor(int x, int y);
28 extern void scroll(void);
30 extern int lines, cols;
31 extern int orig_x, orig_y;
32 extern int keyb_present;
33 extern int CRT_tstc(void);
34 extern int CRT_getc(void);
36 int cursor(int x, int y) {return 0;}
43 #define keyb_present 0
44 int CRT_tstc(void) {return 0;}
45 int CRT_getc(void) {return 0;}
48 extern char *avail_ram;
49 extern char *end_avail;
52 void puts(const char *);
53 void putc(const char c);
54 void puthex(unsigned long val);
55 void _bcopy(char *src, char *dst, int len);
56 void gunzip(void *, int, unsigned char *, int *);
57 static int _cvt(unsigned long val, char *buf, long radix, char *digits);
59 void _vprintk(void(*putc)(const char), const char *fmt0, va_list ap);
60 unsigned char *ISA_io = NULL;
62 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE) || \
63 defined(CONFIG_SERIAL_MPSC_CONSOLE)
64 extern unsigned long com_port;
66 extern int serial_tstc(unsigned long com_port);
67 extern unsigned char serial_getc(unsigned long com_port);
68 extern void serial_putc(unsigned long com_port, unsigned char c);
84 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE) || \
85 defined(CONFIG_SERIAL_MPSC_CONSOLE)
87 return (CRT_tstc() || serial_tstc(com_port));
89 return (serial_tstc(com_port));
98 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE) || \
99 defined(CONFIG_SERIAL_MPSC_CONSOLE)
100 if (serial_tstc(com_port))
101 return (serial_getc(com_port));
102 #endif /* serial console */
114 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE) || \
115 defined(CONFIG_SERIAL_MPSC_CONSOLE)
116 serial_putc(com_port, c);
118 serial_putc(com_port, '\r');
119 #endif /* serial console */
126 if ( ++y >= lines ) {
130 } else if (c == '\r') {
132 } else if (c == '\b') {
137 vidmem [ ( x + cols * y ) * 2 ] = c;
140 if ( ++y >= lines ) {
153 void puts(const char *s)
161 while ( ( c = *s++ ) != '\0' ) {
162 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE) || \
163 defined(CONFIG_SERIAL_MPSC_CONSOLE)
164 serial_putc(com_port, c);
165 if ( c == '\n' ) serial_putc(com_port, '\r');
166 #endif /* serial console */
170 if ( ++y >= lines ) {
174 } else if (c == '\b') {
179 vidmem [ ( x + cols * y ) * 2 ] = c;
182 if ( ++y >= lines ) {
200 puts("\n\n -- System halted");
205 void *zalloc(void *x, unsigned items, unsigned size)
210 size = (size + 7) & -8;
212 if (avail_ram > end_avail) {
213 puts("oops... out of memory\n");
219 void zfree(void *x, void *addr, unsigned nb)
224 #define EXTRA_FIELD 4
227 #define RESERVED 0xe0
231 void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
239 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
240 puts("bad gzipped data\n");
243 if ((flags & EXTRA_FIELD) != 0)
244 i = 12 + src[10] + (src[11] << 8);
245 if ((flags & ORIG_NAME) != 0)
246 while (src[i++] != 0)
248 if ((flags & COMMENT) != 0)
249 while (src[i++] != 0)
251 if ((flags & HEAD_CRC) != 0)
254 puts("gunzip: ran out of data in header\n");
260 r = inflateInit2(&s, -MAX_WBITS);
262 puts("inflateInit2 returned "); puthex(r); puts("\n");
266 s.avail_in = *lenp - i;
268 s.avail_out = dstlen;
269 r = inflate(&s, Z_FINISH);
270 if (r != Z_OK && r != Z_STREAM_END) {
271 puts("inflate returned "); puthex(r); puts("\n");
274 *lenp = s.next_out - (unsigned char *) dst;
279 puthex(unsigned long val)
282 unsigned char buf[10];
284 for (i = 7; i >= 0; i--)
286 buf[i] = "0123456789ABCDEF"[val & 0x0F];
297 _printk(char const *fmt, ...)
302 _vprintk(putc, fmt, ap);
307 #define is_digit(c) ((c >= '0') && (c <= '9'))
310 _vprintk(void(*putc)(const char), const char *fmt0, va_list ap)
312 char c, sign, *cp = 0;
313 int left_prec, right_prec, zero_fill, length = 0, pad, pad_on_right;
316 while ((c = *fmt0++))
321 left_prec = right_prec = pad_on_right = 0;
337 left_prec = (left_prec * 10) + (c - '0');
346 right_prec = (right_prec * 10) + (c - '0');
351 right_prec = left_prec;
359 val = va_arg(ap, long);
368 length = _cvt(val, buf, 10, "0123456789");
371 length = _cvt(val, buf, 16, "0123456789abcdef");
374 length = _cvt(val, buf, 16, "0123456789ABCDEF");
380 cp = va_arg(ap, char *);
384 c = va_arg(ap, long /*char*/);
390 pad = left_prec - length;
445 _cvt(unsigned long val, char *buf, long radix, char *digits)
456 *cp++ = digits[val % radix];
469 _dump_buf_with_offset(unsigned char *p, int s, unsigned char *base)
472 if ((unsigned int)s > (unsigned int)p)
474 s = (unsigned int)s - (unsigned int)p;
480 _printk("%06X: ", (int)p - (int)base);
483 _printk("%06X: ", p);
485 for (i = 0; i < 16; i++)
489 _printk("%02X", p[i] & 0xFF);
494 if ((i % 2) == 1) _printk(" ");
495 if ((i % 8) == 7) _printk(" ");
498 for (i = 0; i < 16; i++)
503 if ((c < 0x20) || (c >= 0x7F)) c = '.';
517 _dump_buf(unsigned char *p, int s)
520 _dump_buf_with_offset(p, s, 0);
523 /* Very simple inb/outb routines. We declare ISA_io to be 0 above, and
524 * then modify it on platforms which need to. We do it like this
525 * because on some platforms we give inb/outb an exact location, and
526 * on others it's an offset from a given location. -- Tom
530 outb(int port, unsigned char val)
532 /* Ensure I/O operations complete */
533 __asm__ volatile("eieio");
540 /* Ensure I/O operations complete */
541 __asm__ volatile("eieio");
542 return (ISA_io[port]);