prom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * Copyright (C) Paul Mackerras 1997.
  3. *
  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>
  13. extern __u32 __div64_32(unsigned long long *dividend, __u32 divisor);
  14. /* The unnecessary pointer compare is there
  15. * to check for type safety (n must be 64bit)
  16. */
  17. # define do_div(n,base) ({ \
  18. __u32 __base = (base); \
  19. __u32 __rem; \
  20. (void)(((typeof((n)) *)0) == ((unsigned long long *)0)); \
  21. if (((n) >> 32) == 0) { \
  22. __rem = (__u32)(n) % __base; \
  23. (n) = (__u32)(n) / __base; \
  24. } else \
  25. __rem = __div64_32(&(n), __base); \
  26. __rem; \
  27. })
  28. int (*prom)(void *);
  29. void *chosen_handle;
  30. void *stdin;
  31. void *stdout;
  32. void *stderr;
  33. void exit(void);
  34. void *finddevice(const char *name);
  35. int getprop(void *phandle, const char *name, void *buf, int buflen);
  36. void chrpboot(int a1, int a2, void *prom); /* in main.c */
  37. int printf(char *fmt, ...);
  38. /* there is no convenient header to get this from... -- paulus */
  39. extern unsigned long strlen(const char *);
  40. int
  41. write(void *handle, void *ptr, int nb)
  42. {
  43. struct prom_args {
  44. char *service;
  45. int nargs;
  46. int nret;
  47. void *ihandle;
  48. void *addr;
  49. int len;
  50. int actual;
  51. } args;
  52. args.service = "write";
  53. args.nargs = 3;
  54. args.nret = 1;
  55. args.ihandle = handle;
  56. args.addr = ptr;
  57. args.len = nb;
  58. args.actual = -1;
  59. (*prom)(&args);
  60. return args.actual;
  61. }
  62. int
  63. read(void *handle, void *ptr, int nb)
  64. {
  65. struct prom_args {
  66. char *service;
  67. int nargs;
  68. int nret;
  69. void *ihandle;
  70. void *addr;
  71. int len;
  72. int actual;
  73. } args;
  74. args.service = "read";
  75. args.nargs = 3;
  76. args.nret = 1;
  77. args.ihandle = handle;
  78. args.addr = ptr;
  79. args.len = nb;
  80. args.actual = -1;
  81. (*prom)(&args);
  82. return args.actual;
  83. }
  84. void
  85. exit()
  86. {
  87. struct prom_args {
  88. char *service;
  89. } args;
  90. for (;;) {
  91. args.service = "exit";
  92. (*prom)(&args);
  93. }
  94. }
  95. void
  96. pause(void)
  97. {
  98. struct prom_args {
  99. char *service;
  100. } args;
  101. args.service = "enter";
  102. (*prom)(&args);
  103. }
  104. void *
  105. finddevice(const char *name)
  106. {
  107. struct prom_args {
  108. char *service;
  109. int nargs;
  110. int nret;
  111. const char *devspec;
  112. void *phandle;
  113. } args;
  114. args.service = "finddevice";
  115. args.nargs = 1;
  116. args.nret = 1;
  117. args.devspec = name;
  118. args.phandle = (void *) -1;
  119. (*prom)(&args);
  120. return args.phandle;
  121. }
  122. void *
  123. claim(unsigned long virt, unsigned long size, unsigned long align)
  124. {
  125. struct prom_args {
  126. char *service;
  127. int nargs;
  128. int nret;
  129. unsigned int virt;
  130. unsigned int size;
  131. unsigned int align;
  132. void *ret;
  133. } args;
  134. args.service = "claim";
  135. args.nargs = 3;
  136. args.nret = 1;
  137. args.virt = virt;
  138. args.size = size;
  139. args.align = align;
  140. (*prom)(&args);
  141. return args.ret;
  142. }
  143. int
  144. getprop(void *phandle, const char *name, void *buf, int buflen)
  145. {
  146. struct prom_args {
  147. char *service;
  148. int nargs;
  149. int nret;
  150. void *phandle;
  151. const char *name;
  152. void *buf;
  153. int buflen;
  154. int size;
  155. } args;
  156. args.service = "getprop";
  157. args.nargs = 4;
  158. args.nret = 1;
  159. args.phandle = phandle;
  160. args.name = name;
  161. args.buf = buf;
  162. args.buflen = buflen;
  163. args.size = -1;
  164. (*prom)(&args);
  165. return args.size;
  166. }
  167. int
  168. putc(int c, void *f)
  169. {
  170. char ch = c;
  171. if (c == '\n')
  172. putc('\r', f);
  173. return write(f, &ch, 1) == 1? c: -1;
  174. }
  175. int
  176. putchar(int c)
  177. {
  178. return putc(c, stdout);
  179. }
  180. int
  181. fputs(char *str, void *f)
  182. {
  183. int n = strlen(str);
  184. return write(f, str, n) == n? 0: -1;
  185. }
  186. int
  187. readchar(void)
  188. {
  189. char ch;
  190. for (;;) {
  191. switch (read(stdin, &ch, 1)) {
  192. case 1:
  193. return ch;
  194. case -1:
  195. printf("read(stdin) returned -1\r\n");
  196. return -1;
  197. }
  198. }
  199. }
  200. static char line[256];
  201. static char *lineptr;
  202. static int lineleft;
  203. int
  204. getchar(void)
  205. {
  206. int c;
  207. if (lineleft == 0) {
  208. lineptr = line;
  209. for (;;) {
  210. c = readchar();
  211. if (c == -1 || c == 4)
  212. break;
  213. if (c == '\r' || c == '\n') {
  214. *lineptr++ = '\n';
  215. putchar('\n');
  216. break;
  217. }
  218. switch (c) {
  219. case 0177:
  220. case '\b':
  221. if (lineptr > line) {
  222. putchar('\b');
  223. putchar(' ');
  224. putchar('\b');
  225. --lineptr;
  226. }
  227. break;
  228. case 'U' & 0x1F:
  229. while (lineptr > line) {
  230. putchar('\b');
  231. putchar(' ');
  232. putchar('\b');
  233. --lineptr;
  234. }
  235. break;
  236. default:
  237. if (lineptr >= &line[sizeof(line) - 1])
  238. putchar('\a');
  239. else {
  240. putchar(c);
  241. *lineptr++ = c;
  242. }
  243. }
  244. }
  245. lineleft = lineptr - line;
  246. lineptr = line;
  247. }
  248. if (lineleft == 0)
  249. return -1;
  250. --lineleft;
  251. return *lineptr++;
  252. }
  253. /* String functions lifted from lib/vsprintf.c and lib/ctype.c */
  254. unsigned char _ctype[] = {
  255. _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
  256. _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
  257. _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
  258. _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
  259. _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
  260. _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
  261. _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
  262. _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
  263. _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
  264. _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
  265. _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
  266. _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
  267. _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
  268. _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
  269. _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
  270. _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
  271. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
  272. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
  273. _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
  274. _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
  275. _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
  276. _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
  277. _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
  278. _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
  279. size_t strnlen(const char * s, size_t count)
  280. {
  281. const char *sc;
  282. for (sc = s; count-- && *sc != '\0'; ++sc)
  283. /* nothing */;
  284. return sc - s;
  285. }
  286. unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
  287. {
  288. unsigned long result = 0,value;
  289. if (!base) {
  290. base = 10;
  291. if (*cp == '0') {
  292. base = 8;
  293. cp++;
  294. if ((*cp == 'x') && isxdigit(cp[1])) {
  295. cp++;
  296. base = 16;
  297. }
  298. }
  299. }
  300. while (isxdigit(*cp) &&
  301. (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
  302. result = result*base + value;
  303. cp++;
  304. }
  305. if (endp)
  306. *endp = (char *)cp;
  307. return result;
  308. }
  309. long simple_strtol(const char *cp,char **endp,unsigned int base)
  310. {
  311. if(*cp=='-')
  312. return -simple_strtoul(cp+1,endp,base);
  313. return simple_strtoul(cp,endp,base);
  314. }
  315. static int skip_atoi(const char **s)
  316. {
  317. int i=0;
  318. while (isdigit(**s))
  319. i = i*10 + *((*s)++) - '0';
  320. return i;
  321. }
  322. #define ZEROPAD 1 /* pad with zero */
  323. #define SIGN 2 /* unsigned/signed long */
  324. #define PLUS 4 /* show plus */
  325. #define SPACE 8 /* space if plus */
  326. #define LEFT 16 /* left justified */
  327. #define SPECIAL 32 /* 0x */
  328. #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
  329. static char * number(char * str, unsigned long long num, int base, int size, int precision, int type)
  330. {
  331. char c,sign,tmp[66];
  332. const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
  333. int i;
  334. if (type & LARGE)
  335. digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  336. if (type & LEFT)
  337. type &= ~ZEROPAD;
  338. if (base < 2 || base > 36)
  339. return 0;
  340. c = (type & ZEROPAD) ? '0' : ' ';
  341. sign = 0;
  342. if (type & SIGN) {
  343. if ((signed long long)num < 0) {
  344. sign = '-';
  345. num = - (signed long long)num;
  346. size--;
  347. } else if (type & PLUS) {
  348. sign = '+';
  349. size--;
  350. } else if (type & SPACE) {
  351. sign = ' ';
  352. size--;
  353. }
  354. }
  355. if (type & SPECIAL) {
  356. if (base == 16)
  357. size -= 2;
  358. else if (base == 8)
  359. size--;
  360. }
  361. i = 0;
  362. if (num == 0)
  363. tmp[i++]='0';
  364. else while (num != 0) {
  365. tmp[i++] = digits[do_div(num, base)];
  366. }
  367. if (i > precision)
  368. precision = i;
  369. size -= precision;
  370. if (!(type&(ZEROPAD+LEFT)))
  371. while(size-->0)
  372. *str++ = ' ';
  373. if (sign)
  374. *str++ = sign;
  375. if (type & SPECIAL) {
  376. if (base==8)
  377. *str++ = '0';
  378. else if (base==16) {
  379. *str++ = '0';
  380. *str++ = digits[33];
  381. }
  382. }
  383. if (!(type & LEFT))
  384. while (size-- > 0)
  385. *str++ = c;
  386. while (i < precision--)
  387. *str++ = '0';
  388. while (i-- > 0)
  389. *str++ = tmp[i];
  390. while (size-- > 0)
  391. *str++ = ' ';
  392. return str;
  393. }
  394. /* Forward decl. needed for IP address printing stuff... */
  395. int sprintf(char * buf, const char *fmt, ...);
  396. int vsprintf(char *buf, const char *fmt, va_list args)
  397. {
  398. int len;
  399. unsigned long long num;
  400. int i, base;
  401. char * str;
  402. const char *s;
  403. int flags; /* flags to number() */
  404. int field_width; /* width of output field */
  405. int precision; /* min. # of digits for integers; max
  406. number of chars for from string */
  407. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  408. /* 'z' support added 23/7/1999 S.H. */
  409. /* 'z' changed to 'Z' --davidm 1/25/99 */
  410. for (str=buf ; *fmt ; ++fmt) {
  411. if (*fmt != '%') {
  412. *str++ = *fmt;
  413. continue;
  414. }
  415. /* process flags */
  416. flags = 0;
  417. repeat:
  418. ++fmt; /* this also skips first '%' */
  419. switch (*fmt) {
  420. case '-': flags |= LEFT; goto repeat;
  421. case '+': flags |= PLUS; goto repeat;
  422. case ' ': flags |= SPACE; goto repeat;
  423. case '#': flags |= SPECIAL; goto repeat;
  424. case '0': flags |= ZEROPAD; goto repeat;
  425. }
  426. /* get field width */
  427. field_width = -1;
  428. if (isdigit(*fmt))
  429. field_width = skip_atoi(&fmt);
  430. else if (*fmt == '*') {
  431. ++fmt;
  432. /* it's the next argument */
  433. field_width = va_arg(args, int);
  434. if (field_width < 0) {
  435. field_width = -field_width;
  436. flags |= LEFT;
  437. }
  438. }
  439. /* get the precision */
  440. precision = -1;
  441. if (*fmt == '.') {
  442. ++fmt;
  443. if (isdigit(*fmt))
  444. precision = skip_atoi(&fmt);
  445. else if (*fmt == '*') {
  446. ++fmt;
  447. /* it's the next argument */
  448. precision = va_arg(args, int);
  449. }
  450. if (precision < 0)
  451. precision = 0;
  452. }
  453. /* get the conversion qualifier */
  454. qualifier = -1;
  455. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
  456. qualifier = *fmt;
  457. ++fmt;
  458. }
  459. /* default base */
  460. base = 10;
  461. switch (*fmt) {
  462. case 'c':
  463. if (!(flags & LEFT))
  464. while (--field_width > 0)
  465. *str++ = ' ';
  466. *str++ = (unsigned char) va_arg(args, int);
  467. while (--field_width > 0)
  468. *str++ = ' ';
  469. continue;
  470. case 's':
  471. s = va_arg(args, char *);
  472. if (!s)
  473. s = "<NULL>";
  474. len = strnlen(s, precision);
  475. if (!(flags & LEFT))
  476. while (len < field_width--)
  477. *str++ = ' ';
  478. for (i = 0; i < len; ++i)
  479. *str++ = *s++;
  480. while (len < field_width--)
  481. *str++ = ' ';
  482. continue;
  483. case 'p':
  484. if (field_width == -1) {
  485. field_width = 2*sizeof(void *);
  486. flags |= ZEROPAD;
  487. }
  488. str = number(str,
  489. (unsigned long) va_arg(args, void *), 16,
  490. field_width, precision, flags);
  491. continue;
  492. case 'n':
  493. if (qualifier == 'l') {
  494. long * ip = va_arg(args, long *);
  495. *ip = (str - buf);
  496. } else if (qualifier == 'Z') {
  497. size_t * ip = va_arg(args, size_t *);
  498. *ip = (str - buf);
  499. } else {
  500. int * ip = va_arg(args, int *);
  501. *ip = (str - buf);
  502. }
  503. continue;
  504. case '%':
  505. *str++ = '%';
  506. continue;
  507. /* integer number formats - set up the flags and "break" */
  508. case 'o':
  509. base = 8;
  510. break;
  511. case 'X':
  512. flags |= LARGE;
  513. case 'x':
  514. base = 16;
  515. break;
  516. case 'd':
  517. case 'i':
  518. flags |= SIGN;
  519. case 'u':
  520. break;
  521. default:
  522. *str++ = '%';
  523. if (*fmt)
  524. *str++ = *fmt;
  525. else
  526. --fmt;
  527. continue;
  528. }
  529. if (qualifier == 'l') {
  530. num = va_arg(args, unsigned long);
  531. if (flags & SIGN)
  532. num = (signed long) num;
  533. } else if (qualifier == 'Z') {
  534. num = va_arg(args, size_t);
  535. } else if (qualifier == 'h') {
  536. num = (unsigned short) va_arg(args, int);
  537. if (flags & SIGN)
  538. num = (signed short) num;
  539. } else {
  540. num = va_arg(args, unsigned int);
  541. if (flags & SIGN)
  542. num = (signed int) num;
  543. }
  544. str = number(str, num, base, field_width, precision, flags);
  545. }
  546. *str = '\0';
  547. return str-buf;
  548. }
  549. int sprintf(char * buf, const char *fmt, ...)
  550. {
  551. va_list args;
  552. int i;
  553. va_start(args, fmt);
  554. i=vsprintf(buf,fmt,args);
  555. va_end(args);
  556. return i;
  557. }
  558. static char sprint_buf[1024];
  559. int
  560. printf(char *fmt, ...)
  561. {
  562. va_list args;
  563. int n;
  564. va_start(args, fmt);
  565. n = vsprintf(sprint_buf, fmt, args);
  566. va_end(args);
  567. write(stdout, sprint_buf, n);
  568. return n;
  569. }