prom.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 <stddef.h>
  11. #include "string.h"
  12. #include "stdio.h"
  13. #include "prom.h"
  14. int (*prom)(void *);
  15. void *chosen_handle;
  16. void *stdin;
  17. void *stdout;
  18. void *stderr;
  19. int
  20. write(void *handle, void *ptr, int nb)
  21. {
  22. struct prom_args {
  23. char *service;
  24. int nargs;
  25. int nret;
  26. void *ihandle;
  27. void *addr;
  28. int len;
  29. int actual;
  30. } args;
  31. args.service = "write";
  32. args.nargs = 3;
  33. args.nret = 1;
  34. args.ihandle = handle;
  35. args.addr = ptr;
  36. args.len = nb;
  37. args.actual = -1;
  38. (*prom)(&args);
  39. return args.actual;
  40. }
  41. int
  42. read(void *handle, void *ptr, int nb)
  43. {
  44. struct prom_args {
  45. char *service;
  46. int nargs;
  47. int nret;
  48. void *ihandle;
  49. void *addr;
  50. int len;
  51. int actual;
  52. } args;
  53. args.service = "read";
  54. args.nargs = 3;
  55. args.nret = 1;
  56. args.ihandle = handle;
  57. args.addr = ptr;
  58. args.len = nb;
  59. args.actual = -1;
  60. (*prom)(&args);
  61. return args.actual;
  62. }
  63. void
  64. exit()
  65. {
  66. struct prom_args {
  67. char *service;
  68. } args;
  69. for (;;) {
  70. args.service = "exit";
  71. (*prom)(&args);
  72. }
  73. }
  74. void
  75. pause(void)
  76. {
  77. struct prom_args {
  78. char *service;
  79. } args;
  80. args.service = "enter";
  81. (*prom)(&args);
  82. }
  83. void *
  84. finddevice(const char *name)
  85. {
  86. struct prom_args {
  87. char *service;
  88. int nargs;
  89. int nret;
  90. const char *devspec;
  91. void *phandle;
  92. } args;
  93. args.service = "finddevice";
  94. args.nargs = 1;
  95. args.nret = 1;
  96. args.devspec = name;
  97. args.phandle = (void *) -1;
  98. (*prom)(&args);
  99. return args.phandle;
  100. }
  101. void *
  102. claim(unsigned long virt, unsigned long size, unsigned long align)
  103. {
  104. struct prom_args {
  105. char *service;
  106. int nargs;
  107. int nret;
  108. unsigned int virt;
  109. unsigned int size;
  110. unsigned int align;
  111. void *ret;
  112. } args;
  113. args.service = "claim";
  114. args.nargs = 3;
  115. args.nret = 1;
  116. args.virt = virt;
  117. args.size = size;
  118. args.align = align;
  119. (*prom)(&args);
  120. return args.ret;
  121. }
  122. int
  123. getprop(void *phandle, const char *name, void *buf, int buflen)
  124. {
  125. struct prom_args {
  126. char *service;
  127. int nargs;
  128. int nret;
  129. void *phandle;
  130. const char *name;
  131. void *buf;
  132. int buflen;
  133. int size;
  134. } args;
  135. args.service = "getprop";
  136. args.nargs = 4;
  137. args.nret = 1;
  138. args.phandle = phandle;
  139. args.name = name;
  140. args.buf = buf;
  141. args.buflen = buflen;
  142. args.size = -1;
  143. (*prom)(&args);
  144. return args.size;
  145. }
  146. int
  147. putc(int c, void *f)
  148. {
  149. char ch = c;
  150. if (c == '\n')
  151. putc('\r', f);
  152. return write(f, &ch, 1) == 1? c: -1;
  153. }
  154. int
  155. putchar(int c)
  156. {
  157. return putc(c, stdout);
  158. }
  159. int
  160. fputs(char *str, void *f)
  161. {
  162. int n = strlen(str);
  163. return write(f, str, n) == n? 0: -1;
  164. }
  165. size_t strnlen(const char * s, size_t count)
  166. {
  167. const char *sc;
  168. for (sc = s; count-- && *sc != '\0'; ++sc)
  169. /* nothing */;
  170. return sc - s;
  171. }
  172. extern unsigned int __div64_32(unsigned long long *dividend,
  173. unsigned int divisor);
  174. /* The unnecessary pointer compare is there
  175. * to check for type safety (n must be 64bit)
  176. */
  177. # define do_div(n,base) ({ \
  178. unsigned int __base = (base); \
  179. unsigned int __rem; \
  180. (void)(((typeof((n)) *)0) == ((unsigned long long *)0)); \
  181. if (((n) >> 32) == 0) { \
  182. __rem = (unsigned int)(n) % __base; \
  183. (n) = (unsigned int)(n) / __base; \
  184. } else \
  185. __rem = __div64_32(&(n), __base); \
  186. __rem; \
  187. })
  188. static int skip_atoi(const char **s)
  189. {
  190. int i, c;
  191. for (i = 0; '0' <= (c = **s) && c <= '9'; ++*s)
  192. i = i*10 + c - '0';
  193. return i;
  194. }
  195. #define ZEROPAD 1 /* pad with zero */
  196. #define SIGN 2 /* unsigned/signed long */
  197. #define PLUS 4 /* show plus */
  198. #define SPACE 8 /* space if plus */
  199. #define LEFT 16 /* left justified */
  200. #define SPECIAL 32 /* 0x */
  201. #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
  202. static char * number(char * str, unsigned long long num, int base, int size, int precision, int type)
  203. {
  204. char c,sign,tmp[66];
  205. const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
  206. int i;
  207. if (type & LARGE)
  208. digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  209. if (type & LEFT)
  210. type &= ~ZEROPAD;
  211. if (base < 2 || base > 36)
  212. return 0;
  213. c = (type & ZEROPAD) ? '0' : ' ';
  214. sign = 0;
  215. if (type & SIGN) {
  216. if ((signed long long)num < 0) {
  217. sign = '-';
  218. num = - (signed long long)num;
  219. size--;
  220. } else if (type & PLUS) {
  221. sign = '+';
  222. size--;
  223. } else if (type & SPACE) {
  224. sign = ' ';
  225. size--;
  226. }
  227. }
  228. if (type & SPECIAL) {
  229. if (base == 16)
  230. size -= 2;
  231. else if (base == 8)
  232. size--;
  233. }
  234. i = 0;
  235. if (num == 0)
  236. tmp[i++]='0';
  237. else while (num != 0) {
  238. tmp[i++] = digits[do_div(num, base)];
  239. }
  240. if (i > precision)
  241. precision = i;
  242. size -= precision;
  243. if (!(type&(ZEROPAD+LEFT)))
  244. while(size-->0)
  245. *str++ = ' ';
  246. if (sign)
  247. *str++ = sign;
  248. if (type & SPECIAL) {
  249. if (base==8)
  250. *str++ = '0';
  251. else if (base==16) {
  252. *str++ = '0';
  253. *str++ = digits[33];
  254. }
  255. }
  256. if (!(type & LEFT))
  257. while (size-- > 0)
  258. *str++ = c;
  259. while (i < precision--)
  260. *str++ = '0';
  261. while (i-- > 0)
  262. *str++ = tmp[i];
  263. while (size-- > 0)
  264. *str++ = ' ';
  265. return str;
  266. }
  267. int vsprintf(char *buf, const char *fmt, va_list args)
  268. {
  269. int len;
  270. unsigned long long num;
  271. int i, base;
  272. char * str;
  273. const char *s;
  274. int flags; /* flags to number() */
  275. int field_width; /* width of output field */
  276. int precision; /* min. # of digits for integers; max
  277. number of chars for from string */
  278. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  279. /* 'z' support added 23/7/1999 S.H. */
  280. /* 'z' changed to 'Z' --davidm 1/25/99 */
  281. for (str=buf ; *fmt ; ++fmt) {
  282. if (*fmt != '%') {
  283. *str++ = *fmt;
  284. continue;
  285. }
  286. /* process flags */
  287. flags = 0;
  288. repeat:
  289. ++fmt; /* this also skips first '%' */
  290. switch (*fmt) {
  291. case '-': flags |= LEFT; goto repeat;
  292. case '+': flags |= PLUS; goto repeat;
  293. case ' ': flags |= SPACE; goto repeat;
  294. case '#': flags |= SPECIAL; goto repeat;
  295. case '0': flags |= ZEROPAD; goto repeat;
  296. }
  297. /* get field width */
  298. field_width = -1;
  299. if ('0' <= *fmt && *fmt <= '9')
  300. field_width = skip_atoi(&fmt);
  301. else if (*fmt == '*') {
  302. ++fmt;
  303. /* it's the next argument */
  304. field_width = va_arg(args, int);
  305. if (field_width < 0) {
  306. field_width = -field_width;
  307. flags |= LEFT;
  308. }
  309. }
  310. /* get the precision */
  311. precision = -1;
  312. if (*fmt == '.') {
  313. ++fmt;
  314. if ('0' <= *fmt && *fmt <= '9')
  315. precision = skip_atoi(&fmt);
  316. else if (*fmt == '*') {
  317. ++fmt;
  318. /* it's the next argument */
  319. precision = va_arg(args, int);
  320. }
  321. if (precision < 0)
  322. precision = 0;
  323. }
  324. /* get the conversion qualifier */
  325. qualifier = -1;
  326. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
  327. qualifier = *fmt;
  328. ++fmt;
  329. }
  330. /* default base */
  331. base = 10;
  332. switch (*fmt) {
  333. case 'c':
  334. if (!(flags & LEFT))
  335. while (--field_width > 0)
  336. *str++ = ' ';
  337. *str++ = (unsigned char) va_arg(args, int);
  338. while (--field_width > 0)
  339. *str++ = ' ';
  340. continue;
  341. case 's':
  342. s = va_arg(args, char *);
  343. if (!s)
  344. s = "<NULL>";
  345. len = strnlen(s, precision);
  346. if (!(flags & LEFT))
  347. while (len < field_width--)
  348. *str++ = ' ';
  349. for (i = 0; i < len; ++i)
  350. *str++ = *s++;
  351. while (len < field_width--)
  352. *str++ = ' ';
  353. continue;
  354. case 'p':
  355. if (field_width == -1) {
  356. field_width = 2*sizeof(void *);
  357. flags |= ZEROPAD;
  358. }
  359. str = number(str,
  360. (unsigned long) va_arg(args, void *), 16,
  361. field_width, precision, flags);
  362. continue;
  363. case 'n':
  364. if (qualifier == 'l') {
  365. long * ip = va_arg(args, long *);
  366. *ip = (str - buf);
  367. } else if (qualifier == 'Z') {
  368. size_t * ip = va_arg(args, size_t *);
  369. *ip = (str - buf);
  370. } else {
  371. int * ip = va_arg(args, int *);
  372. *ip = (str - buf);
  373. }
  374. continue;
  375. case '%':
  376. *str++ = '%';
  377. continue;
  378. /* integer number formats - set up the flags and "break" */
  379. case 'o':
  380. base = 8;
  381. break;
  382. case 'X':
  383. flags |= LARGE;
  384. case 'x':
  385. base = 16;
  386. break;
  387. case 'd':
  388. case 'i':
  389. flags |= SIGN;
  390. case 'u':
  391. break;
  392. default:
  393. *str++ = '%';
  394. if (*fmt)
  395. *str++ = *fmt;
  396. else
  397. --fmt;
  398. continue;
  399. }
  400. if (qualifier == 'l') {
  401. num = va_arg(args, unsigned long);
  402. if (flags & SIGN)
  403. num = (signed long) num;
  404. } else if (qualifier == 'Z') {
  405. num = va_arg(args, size_t);
  406. } else if (qualifier == 'h') {
  407. num = (unsigned short) va_arg(args, int);
  408. if (flags & SIGN)
  409. num = (signed short) num;
  410. } else {
  411. num = va_arg(args, unsigned int);
  412. if (flags & SIGN)
  413. num = (signed int) num;
  414. }
  415. str = number(str, num, base, field_width, precision, flags);
  416. }
  417. *str = '\0';
  418. return str-buf;
  419. }
  420. int sprintf(char * buf, const char *fmt, ...)
  421. {
  422. va_list args;
  423. int i;
  424. va_start(args, fmt);
  425. i=vsprintf(buf,fmt,args);
  426. va_end(args);
  427. return i;
  428. }
  429. static char sprint_buf[1024];
  430. int
  431. printf(const char *fmt, ...)
  432. {
  433. va_list args;
  434. int n;
  435. va_start(args, fmt);
  436. n = vsprintf(sprint_buf, fmt, args);
  437. va_end(args);
  438. write(stdout, sprint_buf, n);
  439. return n;
  440. }