ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / boot / common / misc-common.c
1 /*
2  * arch/ppc/boot/common/misc-common.c
3  *
4  * Misc. bootloader code (almost) all platforms can use
5  *
6  * Author: Johnnie Peters <jpeters@mvista.com>
7  * Editor: Tom Rini <trini@mvista.com>
8  *
9  * Derived from arch/ppc/boot/prep/misc.c
10  *
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
14  * or implied.
15  */
16
17 #include <stdarg.h>     /* for va_ bits */
18 #include <linux/config.h>
19 #include <linux/string.h>
20 #include "zlib.h"
21 #include "nonstdio.h"
22
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
25  * console - Tom */
26 #if defined(CONFIG_PPC_PREP) && defined(CONFIG_VGA_CONSOLE)
27 extern void cursor(int x, int y);
28 extern void scroll(void);
29 extern char *vidmem;
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);
35 #else
36 int cursor(int x, int y) {return 0;}
37 void scroll(void) {}
38 char vidmem[1];
39 #define lines 0
40 #define cols 0
41 int orig_x = 0;
42 int orig_y = 0;
43 #define keyb_present 0
44 int CRT_tstc(void) {return 0;}
45 int CRT_getc(void) {return 0;}
46 #endif
47
48 extern char *avail_ram;
49 extern char *end_avail;
50 extern char _end[];
51
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);
58
59 void _vprintk(void(*putc)(const char), const char *fmt0, va_list ap);
60 unsigned char *ISA_io = NULL;
61
62 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE)
63 extern unsigned long com_port;
64
65 extern int serial_tstc(unsigned long com_port);
66 extern unsigned char serial_getc(unsigned long com_port);
67 extern void serial_putc(unsigned long com_port, unsigned char c);
68 #endif
69
70 void pause(void)
71 {
72         puts("pause\n");
73 }
74
75 void exit(void)
76 {
77         puts("exit\n");
78         while(1);
79 }
80
81 int tstc(void)
82 {
83 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE)
84         if(keyb_present)
85                 return (CRT_tstc() || serial_tstc(com_port));
86         else
87                 return (serial_tstc(com_port));
88 #else
89         return CRT_tstc();
90 #endif
91 }
92
93 int getc(void)
94 {
95         while (1) {
96 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE)
97                 if (serial_tstc(com_port))
98                         return (serial_getc(com_port));
99 #endif /* serial console */
100                 if (keyb_present)
101                         if(CRT_tstc())
102                                 return (CRT_getc());
103         }
104 }
105
106 void
107 putc(const char c)
108 {
109         int x,y;
110
111 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE)
112         serial_putc(com_port, c);
113         if ( c == '\n' )
114                 serial_putc(com_port, '\r');
115 #endif /* serial console */
116
117         x = orig_x;
118         y = orig_y;
119
120         if ( c == '\n' ) {
121                 x = 0;
122                 if ( ++y >= lines ) {
123                         scroll();
124                         y--;
125                 }
126         } else if (c == '\r') {
127                 x = 0;
128         } else if (c == '\b') {
129                 if (x > 0) {
130                         x--;
131                 }
132         } else {
133                 vidmem [ ( x + cols * y ) * 2 ] = c;
134                 if ( ++x >= cols ) {
135                         x = 0;
136                         if ( ++y >= lines ) {
137                                 scroll();
138                                 y--;
139                         }
140                 }
141         }
142
143         cursor(x, y);
144
145         orig_x = x;
146         orig_y = y;
147 }
148
149 void puts(const char *s)
150 {
151         int x,y;
152         char c;
153
154         x = orig_x;
155         y = orig_y;
156
157         while ( ( c = *s++ ) != '\0' ) {
158 #if defined(CONFIG_SERIAL_CONSOLE) || defined(CONFIG_SERIAL_8250_CONSOLE)
159                 serial_putc(com_port, c);
160                 if ( c == '\n' ) serial_putc(com_port, '\r');
161 #endif /* serial console */
162
163                 if ( c == '\n' ) {
164                         x = 0;
165                         if ( ++y >= lines ) {
166                                 scroll();
167                                 y--;
168                         }
169                 } else if (c == '\b') {
170                   if (x > 0) {
171                     x--;
172                   }
173                 } else {
174                         vidmem [ ( x + cols * y ) * 2 ] = c;
175                         if ( ++x >= cols ) {
176                                 x = 0;
177                                 if ( ++y >= lines ) {
178                                         scroll();
179                                         y--;
180                                 }
181                         }
182                 }
183         }
184
185         cursor(x, y);
186
187         orig_x = x;
188         orig_y = y;
189 }
190
191 void error(char *x)
192 {
193         puts("\n\n");
194         puts(x);
195         puts("\n\n -- System halted");
196
197         while(1);       /* Halt */
198 }
199
200 void *zalloc(void *x, unsigned items, unsigned size)
201 {
202         void *p = avail_ram;
203
204         size *= items;
205         size = (size + 7) & -8;
206         avail_ram += size;
207         if (avail_ram > end_avail) {
208                 puts("oops... out of memory\n");
209                 pause();
210         }
211         return p;
212 }
213
214 void zfree(void *x, void *addr, unsigned nb)
215 {
216 }
217
218 #define HEAD_CRC        2
219 #define EXTRA_FIELD     4
220 #define ORIG_NAME       8
221 #define COMMENT         0x10
222 #define RESERVED        0xe0
223
224 #define DEFLATED        8
225
226 void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
227 {
228         z_stream s;
229         int r, i, flags;
230
231         /* skip header */
232         i = 10;
233         flags = src[3];
234         if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
235                 puts("bad gzipped data\n");
236                 exit();
237         }
238         if ((flags & EXTRA_FIELD) != 0)
239                 i = 12 + src[10] + (src[11] << 8);
240         if ((flags & ORIG_NAME) != 0)
241                 while (src[i++] != 0)
242                         ;
243         if ((flags & COMMENT) != 0)
244                 while (src[i++] != 0)
245                         ;
246         if ((flags & HEAD_CRC) != 0)
247                 i += 2;
248         if (i >= *lenp) {
249                 puts("gunzip: ran out of data in header\n");
250                 exit();
251         }
252
253         s.zalloc = zalloc;
254         s.zfree = zfree;
255         r = inflateInit2(&s, -MAX_WBITS);
256         if (r != Z_OK) {
257                 puts("inflateInit2 returned "); puthex(r); puts("\n");
258                 exit();
259         }
260         s.next_in = src + i;
261         s.avail_in = *lenp - i;
262         s.next_out = dst;
263         s.avail_out = dstlen;
264         r = inflate(&s, Z_FINISH);
265         if (r != Z_OK && r != Z_STREAM_END) {
266                 puts("inflate returned "); puthex(r); puts("\n");
267                 exit();
268         }
269         *lenp = s.next_out - (unsigned char *) dst;
270         inflateEnd(&s);
271 }
272
273 void
274 puthex(unsigned long val)
275 {
276
277         unsigned char buf[10];
278         int i;
279         for (i = 7;  i >= 0;  i--)
280         {
281                 buf[i] = "0123456789ABCDEF"[val & 0x0F];
282                 val >>= 4;
283         }
284         buf[8] = '\0';
285         puts(buf);
286 }
287
288 #define FALSE 0
289 #define TRUE  1
290
291 void
292 _printk(char const *fmt, ...)
293 {
294         va_list ap;
295
296         va_start(ap, fmt);
297         _vprintk(putc, fmt, ap);
298         va_end(ap);
299         return;
300 }
301
302 #define is_digit(c) ((c >= '0') && (c <= '9'))
303
304 void
305 _vprintk(void(*putc)(const char), const char *fmt0, va_list ap)
306 {
307         char c, sign, *cp = 0;
308         int left_prec, right_prec, zero_fill, length = 0, pad, pad_on_right;
309         char buf[32];
310         long val;
311         while ((c = *fmt0++))
312         {
313                 if (c == '%')
314                 {
315                         c = *fmt0++;
316                         left_prec = right_prec = pad_on_right = 0;
317                         if (c == '-')
318                         {
319                                 c = *fmt0++;
320                                 pad_on_right++;
321                         }
322                         if (c == '0')
323                         {
324                                 zero_fill = TRUE;
325                                 c = *fmt0++;
326                         } else
327                         {
328                                 zero_fill = FALSE;
329                         }
330                         while (is_digit(c))
331                         {
332                                 left_prec = (left_prec * 10) + (c - '0');
333                                 c = *fmt0++;
334                         }
335                         if (c == '.')
336                         {
337                                 c = *fmt0++;
338                                 zero_fill++;
339                                 while (is_digit(c))
340                                 {
341                                         right_prec = (right_prec * 10) + (c - '0');
342                                         c = *fmt0++;
343                                 }
344                         } else
345                         {
346                                 right_prec = left_prec;
347                         }
348                         sign = '\0';
349                         switch (c)
350                         {
351                         case 'd':
352                         case 'x':
353                         case 'X':
354                                 val = va_arg(ap, long);
355                                 switch (c)
356                                 {
357                                 case 'd':
358                                         if (val < 0)
359                                         {
360                                                 sign = '-';
361                                                 val = -val;
362                                         }
363                                         length = _cvt(val, buf, 10, "0123456789");
364                                         break;
365                                 case 'x':
366                                         length = _cvt(val, buf, 16, "0123456789abcdef");
367                                         break;
368                                 case 'X':
369                                         length = _cvt(val, buf, 16, "0123456789ABCDEF");
370                                         break;
371                                 }
372                                 cp = buf;
373                                 break;
374                         case 's':
375                                 cp = va_arg(ap, char *);
376                                 length = strlen(cp);
377                                 break;
378                         case 'c':
379                                 c = va_arg(ap, long /*char*/);
380                                 (*putc)(c);
381                                 continue;
382                         default:
383                                 (*putc)('?');
384                         }
385                         pad = left_prec - length;
386                         if (sign != '\0')
387                         {
388                                 pad--;
389                         }
390                         if (zero_fill)
391                         {
392                                 c = '0';
393                                 if (sign != '\0')
394                                 {
395                                         (*putc)(sign);
396                                         sign = '\0';
397                                 }
398                         } else
399                         {
400                                 c = ' ';
401                         }
402                         if (!pad_on_right)
403                         {
404                                 while (pad-- > 0)
405                                 {
406                                         (*putc)(c);
407                                 }
408                         }
409                         if (sign != '\0')
410                         {
411                                 (*putc)(sign);
412                         }
413                         while (length-- > 0)
414                         {
415                                 (*putc)(c = *cp++);
416                                 if (c == '\n')
417                                 {
418                                         (*putc)('\r');
419                                 }
420                         }
421                         if (pad_on_right)
422                         {
423                                 while (pad-- > 0)
424                                 {
425                                         (*putc)(c);
426                                 }
427                         }
428                 } else
429                 {
430                         (*putc)(c);
431                         if (c == '\n')
432                         {
433                                 (*putc)('\r');
434                         }
435                 }
436         }
437 }
438
439 int
440 _cvt(unsigned long val, char *buf, long radix, char *digits)
441 {
442         char temp[80];
443         char *cp = temp;
444         int length = 0;
445         if (val == 0)
446         { /* Special case */
447                 *cp++ = '0';
448         } else
449                 while (val)
450                 {
451                         *cp++ = digits[val % radix];
452                         val /= radix;
453                 }
454         while (cp != temp)
455         {
456                 *buf++ = *--cp;
457                 length++;
458         }
459         *buf = '\0';
460         return (length);
461 }
462
463 void
464 _dump_buf_with_offset(unsigned char *p, int s, unsigned char *base)
465 {
466         int i, c;
467         if ((unsigned int)s > (unsigned int)p)
468         {
469                 s = (unsigned int)s - (unsigned int)p;
470         }
471         while (s > 0)
472         {
473                 if (base)
474                 {
475                         _printk("%06X: ", (int)p - (int)base);
476                 } else
477                 {
478                         _printk("%06X: ", p);
479                 }
480                 for (i = 0;  i < 16;  i++)
481                 {
482                         if (i < s)
483                         {
484                                 _printk("%02X", p[i] & 0xFF);
485                         } else
486                         {
487                                 _printk("  ");
488                         }
489                         if ((i % 2) == 1) _printk(" ");
490                         if ((i % 8) == 7) _printk(" ");
491                 }
492                 _printk(" |");
493                 for (i = 0;  i < 16;  i++)
494                 {
495                         if (i < s)
496                         {
497                                 c = p[i] & 0xFF;
498                                 if ((c < 0x20) || (c >= 0x7F)) c = '.';
499                         } else
500                         {
501                                 c = ' ';
502                         }
503                         _printk("%c", c);
504                 }
505                 _printk("|\n");
506                 s -= 16;
507                 p += 16;
508         }
509 }
510
511 void
512 _dump_buf(unsigned char *p, int s)
513 {
514         _printk("\n");
515         _dump_buf_with_offset(p, s, 0);
516 }
517
518 /* Very simple inb/outb routines.  We declare ISA_io to be 0 above, and
519  * then modify it on platforms which need to.  We do it like this
520  * because on some platforms we give inb/outb an exact location, and
521  * on others it's an offset from a given location. -- Tom
522  */
523
524 void
525 outb(int port, unsigned char val)
526 {
527         /* Ensure I/O operations complete */
528         __asm__ volatile("eieio");
529         ISA_io[port] = val;
530 }
531
532 unsigned char
533 inb(int port)
534 {
535         /* Ensure I/O operations complete */
536         __asm__ volatile("eieio");
537         return (ISA_io[port]);
538 }
539
540 /*
541  * Local variables:
542  *  c-indent-level: 8
543  *  c-basic-offset: 8
544  *  tab-width: 8
545  * End:
546  */