misc-common.c 9.7 KB

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