Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ppc64 / boot / start.c
blobea247e79b55e412c4c5b421d6370e648164418f7
1 /*
2 * Copyright (C) Paul Mackerras 1997.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9 #include <stdarg.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
12 #include <linux/ctype.h>
14 #include <asm/div64.h>
16 int (*prom)(void *);
18 void *chosen_handle;
19 void *stdin;
20 void *stdout;
21 void *stderr;
23 void exit(void);
24 void *finddevice(const char *name);
25 int getprop(void *phandle, const char *name, void *buf, int buflen);
26 void chrpboot(int a1, int a2, void *prom); /* in main.c */
28 void printk(char *fmt, ...);
30 void
31 start(int a1, int a2, void *promptr)
33 prom = (int (*)(void *)) promptr;
34 chosen_handle = finddevice("/chosen");
35 if (chosen_handle == (void *) -1)
36 exit();
37 if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
38 exit();
39 stderr = stdout;
40 if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
41 exit();
43 chrpboot(a1, a2, promptr);
44 for (;;)
45 exit();
48 int
49 write(void *handle, void *ptr, int nb)
51 struct prom_args {
52 char *service;
53 int nargs;
54 int nret;
55 void *ihandle;
56 void *addr;
57 int len;
58 int actual;
59 } args;
61 args.service = "write";
62 args.nargs = 3;
63 args.nret = 1;
64 args.ihandle = handle;
65 args.addr = ptr;
66 args.len = nb;
67 args.actual = -1;
68 (*prom)(&args);
69 return args.actual;
72 int
73 read(void *handle, void *ptr, int nb)
75 struct prom_args {
76 char *service;
77 int nargs;
78 int nret;
79 void *ihandle;
80 void *addr;
81 int len;
82 int actual;
83 } args;
85 args.service = "read";
86 args.nargs = 3;
87 args.nret = 1;
88 args.ihandle = handle;
89 args.addr = ptr;
90 args.len = nb;
91 args.actual = -1;
92 (*prom)(&args);
93 return args.actual;
96 void
97 exit()
99 struct prom_args {
100 char *service;
101 } args;
103 for (;;) {
104 args.service = "exit";
105 (*prom)(&args);
109 void
110 pause(void)
112 struct prom_args {
113 char *service;
114 } args;
116 args.service = "enter";
117 (*prom)(&args);
120 void *
121 finddevice(const char *name)
123 struct prom_args {
124 char *service;
125 int nargs;
126 int nret;
127 const char *devspec;
128 void *phandle;
129 } args;
131 args.service = "finddevice";
132 args.nargs = 1;
133 args.nret = 1;
134 args.devspec = name;
135 args.phandle = (void *) -1;
136 (*prom)(&args);
137 return args.phandle;
140 void *
141 claim(unsigned long virt, unsigned long size, unsigned long align)
143 struct prom_args {
144 char *service;
145 int nargs;
146 int nret;
147 unsigned int virt;
148 unsigned int size;
149 unsigned int align;
150 void *ret;
151 } args;
153 args.service = "claim";
154 args.nargs = 3;
155 args.nret = 1;
156 args.virt = virt;
157 args.size = size;
158 args.align = align;
159 (*prom)(&args);
160 return args.ret;
164 getprop(void *phandle, const char *name, void *buf, int buflen)
166 struct prom_args {
167 char *service;
168 int nargs;
169 int nret;
170 void *phandle;
171 const char *name;
172 void *buf;
173 int buflen;
174 int size;
175 } args;
177 args.service = "getprop";
178 args.nargs = 4;
179 args.nret = 1;
180 args.phandle = phandle;
181 args.name = name;
182 args.buf = buf;
183 args.buflen = buflen;
184 args.size = -1;
185 (*prom)(&args);
186 return args.size;
190 putc(int c, void *f)
192 char ch = c;
194 if (c == '\n')
195 putc('\r', f);
196 return write(f, &ch, 1) == 1? c: -1;
200 putchar(int c)
202 return putc(c, stdout);
206 fputs(char *str, void *f)
208 int n = strlen(str);
210 return write(f, str, n) == n? 0: -1;
214 readchar(void)
216 char ch;
218 for (;;) {
219 switch (read(stdin, &ch, 1)) {
220 case 1:
221 return ch;
222 case -1:
223 printk("read(stdin) returned -1\r\n");
224 return -1;
229 static char line[256];
230 static char *lineptr;
231 static int lineleft;
234 getchar(void)
236 int c;
238 if (lineleft == 0) {
239 lineptr = line;
240 for (;;) {
241 c = readchar();
242 if (c == -1 || c == 4)
243 break;
244 if (c == '\r' || c == '\n') {
245 *lineptr++ = '\n';
246 putchar('\n');
247 break;
249 switch (c) {
250 case 0177:
251 case '\b':
252 if (lineptr > line) {
253 putchar('\b');
254 putchar(' ');
255 putchar('\b');
256 --lineptr;
258 break;
259 case 'U' & 0x1F:
260 while (lineptr > line) {
261 putchar('\b');
262 putchar(' ');
263 putchar('\b');
264 --lineptr;
266 break;
267 default:
268 if (lineptr >= &line[sizeof(line) - 1])
269 putchar('\a');
270 else {
271 putchar(c);
272 *lineptr++ = c;
276 lineleft = lineptr - line;
277 lineptr = line;
279 if (lineleft == 0)
280 return -1;
281 --lineleft;
282 return *lineptr++;
287 /* String functions lifted from lib/vsprintf.c and lib/ctype.c */
288 unsigned char _ctype[] = {
289 _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
290 _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
291 _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
292 _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
293 _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
294 _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
295 _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
296 _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
297 _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
298 _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
299 _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
300 _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
301 _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
302 _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
303 _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
304 _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
305 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
306 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
307 _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
308 _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
309 _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
310 _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
311 _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
312 _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
314 size_t strnlen(const char * s, size_t count)
316 const char *sc;
318 for (sc = s; count-- && *sc != '\0'; ++sc)
319 /* nothing */;
320 return sc - s;
323 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
325 unsigned long result = 0,value;
327 if (!base) {
328 base = 10;
329 if (*cp == '0') {
330 base = 8;
331 cp++;
332 if ((*cp == 'x') && isxdigit(cp[1])) {
333 cp++;
334 base = 16;
338 while (isxdigit(*cp) &&
339 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
340 result = result*base + value;
341 cp++;
343 if (endp)
344 *endp = (char *)cp;
345 return result;
348 long simple_strtol(const char *cp,char **endp,unsigned int base)
350 if(*cp=='-')
351 return -simple_strtoul(cp+1,endp,base);
352 return simple_strtoul(cp,endp,base);
355 static int skip_atoi(const char **s)
357 int i=0;
359 while (isdigit(**s))
360 i = i*10 + *((*s)++) - '0';
361 return i;
364 #define ZEROPAD 1 /* pad with zero */
365 #define SIGN 2 /* unsigned/signed long */
366 #define PLUS 4 /* show plus */
367 #define SPACE 8 /* space if plus */
368 #define LEFT 16 /* left justified */
369 #define SPECIAL 32 /* 0x */
370 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
372 static char * number(char * str, long long num, int base, int size, int precision, int type)
374 char c,sign,tmp[66];
375 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
376 int i;
378 if (type & LARGE)
379 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
380 if (type & LEFT)
381 type &= ~ZEROPAD;
382 if (base < 2 || base > 36)
383 return 0;
384 c = (type & ZEROPAD) ? '0' : ' ';
385 sign = 0;
386 if (type & SIGN) {
387 if (num < 0) {
388 sign = '-';
389 num = -num;
390 size--;
391 } else if (type & PLUS) {
392 sign = '+';
393 size--;
394 } else if (type & SPACE) {
395 sign = ' ';
396 size--;
399 if (type & SPECIAL) {
400 if (base == 16)
401 size -= 2;
402 else if (base == 8)
403 size--;
405 i = 0;
406 if (num == 0)
407 tmp[i++]='0';
408 else while (num != 0)
409 tmp[i++] = digits[do_div(num,base)];
410 if (i > precision)
411 precision = i;
412 size -= precision;
413 if (!(type&(ZEROPAD+LEFT)))
414 while(size-->0)
415 *str++ = ' ';
416 if (sign)
417 *str++ = sign;
418 if (type & SPECIAL) {
419 if (base==8)
420 *str++ = '0';
421 else if (base==16) {
422 *str++ = '0';
423 *str++ = digits[33];
426 if (!(type & LEFT))
427 while (size-- > 0)
428 *str++ = c;
429 while (i < precision--)
430 *str++ = '0';
431 while (i-- > 0)
432 *str++ = tmp[i];
433 while (size-- > 0)
434 *str++ = ' ';
435 return str;
438 /* Forward decl. needed for IP address printing stuff... */
439 int sprintf(char * buf, const char *fmt, ...);
441 int vsprintf(char *buf, const char *fmt, va_list args)
443 int len;
444 unsigned long long num;
445 int i, base;
446 char * str;
447 const char *s;
449 int flags; /* flags to number() */
451 int field_width; /* width of output field */
452 int precision; /* min. # of digits for integers; max
453 number of chars for from string */
454 int qualifier; /* 'h', 'l', or 'L' for integer fields */
455 /* 'z' support added 23/7/1999 S.H. */
456 /* 'z' changed to 'Z' --davidm 1/25/99 */
459 for (str=buf ; *fmt ; ++fmt) {
460 if (*fmt != '%') {
461 *str++ = *fmt;
462 continue;
465 /* process flags */
466 flags = 0;
467 repeat:
468 ++fmt; /* this also skips first '%' */
469 switch (*fmt) {
470 case '-': flags |= LEFT; goto repeat;
471 case '+': flags |= PLUS; goto repeat;
472 case ' ': flags |= SPACE; goto repeat;
473 case '#': flags |= SPECIAL; goto repeat;
474 case '0': flags |= ZEROPAD; goto repeat;
477 /* get field width */
478 field_width = -1;
479 if (isdigit(*fmt))
480 field_width = skip_atoi(&fmt);
481 else if (*fmt == '*') {
482 ++fmt;
483 /* it's the next argument */
484 field_width = va_arg(args, int);
485 if (field_width < 0) {
486 field_width = -field_width;
487 flags |= LEFT;
491 /* get the precision */
492 precision = -1;
493 if (*fmt == '.') {
494 ++fmt;
495 if (isdigit(*fmt))
496 precision = skip_atoi(&fmt);
497 else if (*fmt == '*') {
498 ++fmt;
499 /* it's the next argument */
500 precision = va_arg(args, int);
502 if (precision < 0)
503 precision = 0;
506 /* get the conversion qualifier */
507 qualifier = -1;
508 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
509 qualifier = *fmt;
510 ++fmt;
513 /* default base */
514 base = 10;
516 switch (*fmt) {
517 case 'c':
518 if (!(flags & LEFT))
519 while (--field_width > 0)
520 *str++ = ' ';
521 *str++ = (unsigned char) va_arg(args, int);
522 while (--field_width > 0)
523 *str++ = ' ';
524 continue;
526 case 's':
527 s = va_arg(args, char *);
528 if (!s)
529 s = "<NULL>";
531 len = strnlen(s, precision);
533 if (!(flags & LEFT))
534 while (len < field_width--)
535 *str++ = ' ';
536 for (i = 0; i < len; ++i)
537 *str++ = *s++;
538 while (len < field_width--)
539 *str++ = ' ';
540 continue;
542 case 'p':
543 if (field_width == -1) {
544 field_width = 2*sizeof(void *);
545 flags |= ZEROPAD;
547 str = number(str,
548 (unsigned long) va_arg(args, void *), 16,
549 field_width, precision, flags);
550 continue;
553 case 'n':
554 if (qualifier == 'l') {
555 long * ip = va_arg(args, long *);
556 *ip = (str - buf);
557 } else if (qualifier == 'Z') {
558 size_t * ip = va_arg(args, size_t *);
559 *ip = (str - buf);
560 } else {
561 int * ip = va_arg(args, int *);
562 *ip = (str - buf);
564 continue;
566 case '%':
567 *str++ = '%';
568 continue;
570 /* integer number formats - set up the flags and "break" */
571 case 'o':
572 base = 8;
573 break;
575 case 'X':
576 flags |= LARGE;
577 case 'x':
578 base = 16;
579 break;
581 case 'd':
582 case 'i':
583 flags |= SIGN;
584 case 'u':
585 break;
587 default:
588 *str++ = '%';
589 if (*fmt)
590 *str++ = *fmt;
591 else
592 --fmt;
593 continue;
595 if (qualifier == 'L')
596 num = va_arg(args, long long);
597 else if (qualifier == 'l') {
598 num = va_arg(args, unsigned long);
599 if (flags & SIGN)
600 num = (signed long) num;
601 } else if (qualifier == 'Z') {
602 num = va_arg(args, size_t);
603 } else if (qualifier == 'h') {
604 num = (unsigned short) va_arg(args, int);
605 if (flags & SIGN)
606 num = (signed short) num;
607 } else {
608 num = va_arg(args, unsigned int);
609 if (flags & SIGN)
610 num = (signed int) num;
612 str = number(str, num, base, field_width, precision, flags);
614 *str = '\0';
615 return str-buf;
618 int sprintf(char * buf, const char *fmt, ...)
620 va_list args;
621 int i;
623 va_start(args, fmt);
624 i=vsprintf(buf,fmt,args);
625 va_end(args);
626 return i;
629 static char sprint_buf[1024];
631 void
632 printk(char *fmt, ...)
634 va_list args;
635 int n;
637 va_start(args, fmt);
638 n = vsprintf(sprint_buf, fmt, args);
639 va_end(args);
640 write(stdout, sprint_buf, n);
644 printf(char *fmt, ...)
646 va_list args;
647 int n;
649 va_start(args, fmt);
650 n = vsprintf(sprint_buf, fmt, args);
651 va_end(args);
652 write(stdout, sprint_buf, n);
653 return n;