[NETLINK]: w1_int.c: fix default netlink group
[linux-2.6/verdex.git] / arch / ppc64 / boot / prom.c
blob5e48b80ff5a07471bd2f07feba550bd85cf9bbaf
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 extern __u32 __div64_32(unsigned long long *dividend, __u32 divisor);
16 /* The unnecessary pointer compare is there
17 * to check for type safety (n must be 64bit)
19 # define do_div(n,base) ({ \
20 __u32 __base = (base); \
21 __u32 __rem; \
22 (void)(((typeof((n)) *)0) == ((unsigned long long *)0)); \
23 if (((n) >> 32) == 0) { \
24 __rem = (__u32)(n) % __base; \
25 (n) = (__u32)(n) / __base; \
26 } else \
27 __rem = __div64_32(&(n), __base); \
28 __rem; \
31 int (*prom)(void *);
33 void *chosen_handle;
34 void *stdin;
35 void *stdout;
36 void *stderr;
38 void exit(void);
39 void *finddevice(const char *name);
40 int getprop(void *phandle, const char *name, void *buf, int buflen);
41 void chrpboot(int a1, int a2, void *prom); /* in main.c */
43 int printf(char *fmt, ...);
45 /* there is no convenient header to get this from... -- paulus */
46 extern unsigned long strlen(const char *);
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 printf("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, unsigned 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 ((signed long long)num < 0) {
388 sign = '-';
389 num = - (signed long long)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)];
411 if (i > precision)
412 precision = i;
413 size -= precision;
414 if (!(type&(ZEROPAD+LEFT)))
415 while(size-->0)
416 *str++ = ' ';
417 if (sign)
418 *str++ = sign;
419 if (type & SPECIAL) {
420 if (base==8)
421 *str++ = '0';
422 else if (base==16) {
423 *str++ = '0';
424 *str++ = digits[33];
427 if (!(type & LEFT))
428 while (size-- > 0)
429 *str++ = c;
430 while (i < precision--)
431 *str++ = '0';
432 while (i-- > 0)
433 *str++ = tmp[i];
434 while (size-- > 0)
435 *str++ = ' ';
436 return str;
439 /* Forward decl. needed for IP address printing stuff... */
440 int sprintf(char * buf, const char *fmt, ...);
442 int vsprintf(char *buf, const char *fmt, va_list args)
444 int len;
445 unsigned long long num;
446 int i, base;
447 char * str;
448 const char *s;
450 int flags; /* flags to number() */
452 int field_width; /* width of output field */
453 int precision; /* min. # of digits for integers; max
454 number of chars for from string */
455 int qualifier; /* 'h', 'l', or 'L' for integer fields */
456 /* 'z' support added 23/7/1999 S.H. */
457 /* 'z' changed to 'Z' --davidm 1/25/99 */
460 for (str=buf ; *fmt ; ++fmt) {
461 if (*fmt != '%') {
462 *str++ = *fmt;
463 continue;
466 /* process flags */
467 flags = 0;
468 repeat:
469 ++fmt; /* this also skips first '%' */
470 switch (*fmt) {
471 case '-': flags |= LEFT; goto repeat;
472 case '+': flags |= PLUS; goto repeat;
473 case ' ': flags |= SPACE; goto repeat;
474 case '#': flags |= SPECIAL; goto repeat;
475 case '0': flags |= ZEROPAD; goto repeat;
478 /* get field width */
479 field_width = -1;
480 if (isdigit(*fmt))
481 field_width = skip_atoi(&fmt);
482 else if (*fmt == '*') {
483 ++fmt;
484 /* it's the next argument */
485 field_width = va_arg(args, int);
486 if (field_width < 0) {
487 field_width = -field_width;
488 flags |= LEFT;
492 /* get the precision */
493 precision = -1;
494 if (*fmt == '.') {
495 ++fmt;
496 if (isdigit(*fmt))
497 precision = skip_atoi(&fmt);
498 else if (*fmt == '*') {
499 ++fmt;
500 /* it's the next argument */
501 precision = va_arg(args, int);
503 if (precision < 0)
504 precision = 0;
507 /* get the conversion qualifier */
508 qualifier = -1;
509 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
510 qualifier = *fmt;
511 ++fmt;
514 /* default base */
515 base = 10;
517 switch (*fmt) {
518 case 'c':
519 if (!(flags & LEFT))
520 while (--field_width > 0)
521 *str++ = ' ';
522 *str++ = (unsigned char) va_arg(args, int);
523 while (--field_width > 0)
524 *str++ = ' ';
525 continue;
527 case 's':
528 s = va_arg(args, char *);
529 if (!s)
530 s = "<NULL>";
532 len = strnlen(s, precision);
534 if (!(flags & LEFT))
535 while (len < field_width--)
536 *str++ = ' ';
537 for (i = 0; i < len; ++i)
538 *str++ = *s++;
539 while (len < field_width--)
540 *str++ = ' ';
541 continue;
543 case 'p':
544 if (field_width == -1) {
545 field_width = 2*sizeof(void *);
546 flags |= ZEROPAD;
548 str = number(str,
549 (unsigned long) va_arg(args, void *), 16,
550 field_width, precision, flags);
551 continue;
554 case 'n':
555 if (qualifier == 'l') {
556 long * ip = va_arg(args, long *);
557 *ip = (str - buf);
558 } else if (qualifier == 'Z') {
559 size_t * ip = va_arg(args, size_t *);
560 *ip = (str - buf);
561 } else {
562 int * ip = va_arg(args, int *);
563 *ip = (str - buf);
565 continue;
567 case '%':
568 *str++ = '%';
569 continue;
571 /* integer number formats - set up the flags and "break" */
572 case 'o':
573 base = 8;
574 break;
576 case 'X':
577 flags |= LARGE;
578 case 'x':
579 base = 16;
580 break;
582 case 'd':
583 case 'i':
584 flags |= SIGN;
585 case 'u':
586 break;
588 default:
589 *str++ = '%';
590 if (*fmt)
591 *str++ = *fmt;
592 else
593 --fmt;
594 continue;
596 if (qualifier == 'l') {
597 num = va_arg(args, unsigned long);
598 if (flags & SIGN)
599 num = (signed long) num;
600 } else if (qualifier == 'Z') {
601 num = va_arg(args, size_t);
602 } else if (qualifier == 'h') {
603 num = (unsigned short) va_arg(args, int);
604 if (flags & SIGN)
605 num = (signed short) num;
606 } else {
607 num = va_arg(args, unsigned int);
608 if (flags & SIGN)
609 num = (signed int) num;
611 str = number(str, num, base, field_width, precision, flags);
613 *str = '\0';
614 return str-buf;
617 int sprintf(char * buf, const char *fmt, ...)
619 va_list args;
620 int i;
622 va_start(args, fmt);
623 i=vsprintf(buf,fmt,args);
624 va_end(args);
625 return i;
628 static char sprint_buf[1024];
631 printf(char *fmt, ...)
633 va_list args;
634 int n;
636 va_start(args, fmt);
637 n = vsprintf(sprint_buf, fmt, args);
638 va_end(args);
639 write(stdout, sprint_buf, n);
640 return n;