console.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * (C) Copyright 2000
  3. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <stdarg.h>
  25. #include <malloc.h>
  26. #include <console.h>
  27. #include <exports.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #ifdef CONFIG_AMIGAONEG3SE
  30. int console_changed = 0;
  31. #endif
  32. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  33. /*
  34. * if overwrite_console returns 1, the stdin, stderr and stdout
  35. * are switched to the serial port, else the settings in the
  36. * environment are used
  37. */
  38. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  39. extern int overwrite_console (void);
  40. #define OVERWRITE_CONSOLE overwrite_console ()
  41. #else
  42. #define OVERWRITE_CONSOLE 0
  43. #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
  44. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  45. static int console_setfile (int file, device_t * dev)
  46. {
  47. int error = 0;
  48. if (dev == NULL)
  49. return -1;
  50. switch (file) {
  51. case stdin:
  52. case stdout:
  53. case stderr:
  54. /* Start new device */
  55. if (dev->start) {
  56. error = dev->start ();
  57. /* If it's not started dont use it */
  58. if (error < 0)
  59. break;
  60. }
  61. /* Assign the new device (leaving the existing one started) */
  62. stdio_devices[file] = dev;
  63. /*
  64. * Update monitor functions
  65. * (to use the console stuff by other applications)
  66. */
  67. switch (file) {
  68. case stdin:
  69. gd->jt[XF_getc] = dev->getc;
  70. gd->jt[XF_tstc] = dev->tstc;
  71. break;
  72. case stdout:
  73. gd->jt[XF_putc] = dev->putc;
  74. gd->jt[XF_puts] = dev->puts;
  75. gd->jt[XF_printf] = printf;
  76. break;
  77. }
  78. break;
  79. default: /* Invalid file ID */
  80. error = -1;
  81. }
  82. return error;
  83. }
  84. #if defined(CONFIG_CONSOLE_MUX)
  85. /** Console I/O multiplexing *******************************************/
  86. static device_t *tstcdev;
  87. device_t **console_devices[MAX_FILES];
  88. int cd_count[MAX_FILES];
  89. /*
  90. * This depends on tstc() always being called before getc().
  91. * This is guaranteed to be true because this routine is called
  92. * only from fgetc() which assures it.
  93. * No attempt is made to demultiplex multiple input sources.
  94. */
  95. static int iomux_getc(void)
  96. {
  97. unsigned char ret;
  98. /* This is never called with testcdev == NULL */
  99. ret = tstcdev->getc();
  100. tstcdev = NULL;
  101. return ret;
  102. }
  103. static int iomux_tstc(int file)
  104. {
  105. int i, ret;
  106. device_t *dev;
  107. disable_ctrlc(1);
  108. for (i = 0; i < cd_count[file]; i++) {
  109. dev = console_devices[file][i];
  110. if (dev->tstc != NULL) {
  111. ret = dev->tstc();
  112. if (ret > 0) {
  113. tstcdev = dev;
  114. disable_ctrlc(0);
  115. return ret;
  116. }
  117. }
  118. }
  119. disable_ctrlc(0);
  120. return 0;
  121. }
  122. static void iomux_putc(int file, const char c)
  123. {
  124. int i;
  125. device_t *dev;
  126. for (i = 0; i < cd_count[file]; i++) {
  127. dev = console_devices[file][i];
  128. if (dev->putc != NULL)
  129. dev->putc(c);
  130. }
  131. }
  132. static void iomux_puts(int file, const char *s)
  133. {
  134. int i;
  135. device_t *dev;
  136. for (i = 0; i < cd_count[file]; i++) {
  137. dev = console_devices[file][i];
  138. if (dev->puts != NULL)
  139. dev->puts(s);
  140. }
  141. }
  142. #endif /* defined(CONFIG_CONSOLE_MUX) */
  143. /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
  144. void serial_printf (const char *fmt, ...)
  145. {
  146. va_list args;
  147. uint i;
  148. char printbuffer[CONFIG_SYS_PBSIZE];
  149. va_start (args, fmt);
  150. /* For this to work, printbuffer must be larger than
  151. * anything we ever want to print.
  152. */
  153. i = vsprintf (printbuffer, fmt, args);
  154. va_end (args);
  155. serial_puts (printbuffer);
  156. }
  157. int fgetc (int file)
  158. {
  159. if (file < MAX_FILES) {
  160. #if defined(CONFIG_CONSOLE_MUX)
  161. /*
  162. * Effectively poll for input wherever it may be available.
  163. */
  164. for (;;) {
  165. /*
  166. * Upper layer may have already called tstc() so
  167. * check for that first.
  168. */
  169. if (tstcdev != NULL)
  170. return iomux_getc();
  171. iomux_tstc(file);
  172. #ifdef CONFIG_WATCHDOG
  173. /*
  174. * If the watchdog must be rate-limited then it should
  175. * already be handled in board-specific code.
  176. */
  177. udelay(1);
  178. #endif
  179. }
  180. #else
  181. return stdio_devices[file]->getc ();
  182. #endif
  183. }
  184. return -1;
  185. }
  186. int ftstc (int file)
  187. {
  188. if (file < MAX_FILES)
  189. #if defined(CONFIG_CONSOLE_MUX)
  190. return iomux_tstc(file);
  191. #else
  192. return stdio_devices[file]->tstc ();
  193. #endif
  194. return -1;
  195. }
  196. void fputc (int file, const char c)
  197. {
  198. if (file < MAX_FILES)
  199. #if defined(CONFIG_CONSOLE_MUX)
  200. iomux_putc(file, c);
  201. #else
  202. stdio_devices[file]->putc (c);
  203. #endif
  204. }
  205. void fputs (int file, const char *s)
  206. {
  207. if (file < MAX_FILES)
  208. #if defined(CONFIG_CONSOLE_MUX)
  209. iomux_puts(file, s);
  210. #else
  211. stdio_devices[file]->puts (s);
  212. #endif
  213. }
  214. void fprintf (int file, const char *fmt, ...)
  215. {
  216. va_list args;
  217. uint i;
  218. char printbuffer[CONFIG_SYS_PBSIZE];
  219. va_start (args, fmt);
  220. /* For this to work, printbuffer must be larger than
  221. * anything we ever want to print.
  222. */
  223. i = vsprintf (printbuffer, fmt, args);
  224. va_end (args);
  225. /* Send to desired file */
  226. fputs (file, printbuffer);
  227. }
  228. /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
  229. int getc (void)
  230. {
  231. #ifdef CONFIG_DISABLE_CONSOLE
  232. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  233. return 0;
  234. #endif
  235. if (gd->flags & GD_FLG_DEVINIT) {
  236. /* Get from the standard input */
  237. return fgetc (stdin);
  238. }
  239. /* Send directly to the handler */
  240. return serial_getc ();
  241. }
  242. int tstc (void)
  243. {
  244. #ifdef CONFIG_DISABLE_CONSOLE
  245. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  246. return 0;
  247. #endif
  248. if (gd->flags & GD_FLG_DEVINIT) {
  249. /* Test the standard input */
  250. return ftstc (stdin);
  251. }
  252. /* Send directly to the handler */
  253. return serial_tstc ();
  254. }
  255. void putc (const char c)
  256. {
  257. #ifdef CONFIG_SILENT_CONSOLE
  258. if (gd->flags & GD_FLG_SILENT)
  259. return;
  260. #endif
  261. #ifdef CONFIG_DISABLE_CONSOLE
  262. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  263. return;
  264. #endif
  265. if (gd->flags & GD_FLG_DEVINIT) {
  266. /* Send to the standard output */
  267. fputc (stdout, c);
  268. } else {
  269. /* Send directly to the handler */
  270. serial_putc (c);
  271. }
  272. }
  273. void puts (const char *s)
  274. {
  275. #ifdef CONFIG_SILENT_CONSOLE
  276. if (gd->flags & GD_FLG_SILENT)
  277. return;
  278. #endif
  279. #ifdef CONFIG_DISABLE_CONSOLE
  280. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  281. return;
  282. #endif
  283. if (gd->flags & GD_FLG_DEVINIT) {
  284. /* Send to the standard output */
  285. fputs (stdout, s);
  286. } else {
  287. /* Send directly to the handler */
  288. serial_puts (s);
  289. }
  290. }
  291. void printf (const char *fmt, ...)
  292. {
  293. va_list args;
  294. uint i;
  295. char printbuffer[CONFIG_SYS_PBSIZE];
  296. va_start (args, fmt);
  297. /* For this to work, printbuffer must be larger than
  298. * anything we ever want to print.
  299. */
  300. i = vsprintf (printbuffer, fmt, args);
  301. va_end (args);
  302. /* Print the string */
  303. puts (printbuffer);
  304. }
  305. void vprintf (const char *fmt, va_list args)
  306. {
  307. uint i;
  308. char printbuffer[CONFIG_SYS_PBSIZE];
  309. /* For this to work, printbuffer must be larger than
  310. * anything we ever want to print.
  311. */
  312. i = vsprintf (printbuffer, fmt, args);
  313. /* Print the string */
  314. puts (printbuffer);
  315. }
  316. /* test if ctrl-c was pressed */
  317. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  318. static int ctrlc_was_pressed = 0;
  319. int ctrlc (void)
  320. {
  321. if (!ctrlc_disabled && gd->have_console) {
  322. if (tstc ()) {
  323. switch (getc ()) {
  324. case 0x03: /* ^C - Control C */
  325. ctrlc_was_pressed = 1;
  326. return 1;
  327. default:
  328. break;
  329. }
  330. }
  331. }
  332. return 0;
  333. }
  334. /* pass 1 to disable ctrlc() checking, 0 to enable.
  335. * returns previous state
  336. */
  337. int disable_ctrlc (int disable)
  338. {
  339. int prev = ctrlc_disabled; /* save previous state */
  340. ctrlc_disabled = disable;
  341. return prev;
  342. }
  343. int had_ctrlc (void)
  344. {
  345. return ctrlc_was_pressed;
  346. }
  347. void clear_ctrlc (void)
  348. {
  349. ctrlc_was_pressed = 0;
  350. }
  351. #ifdef CONFIG_MODEM_SUPPORT_DEBUG
  352. char screen[1024];
  353. char *cursor = screen;
  354. int once = 0;
  355. inline void dbg(const char *fmt, ...)
  356. {
  357. va_list args;
  358. uint i;
  359. char printbuffer[CONFIG_SYS_PBSIZE];
  360. if (!once) {
  361. memset(screen, 0, sizeof(screen));
  362. once++;
  363. }
  364. va_start(args, fmt);
  365. /* For this to work, printbuffer must be larger than
  366. * anything we ever want to print.
  367. */
  368. i = vsprintf(printbuffer, fmt, args);
  369. va_end(args);
  370. if ((screen + sizeof(screen) - 1 - cursor) < strlen(printbuffer)+1) {
  371. memset(screen, 0, sizeof(screen));
  372. cursor = screen;
  373. }
  374. sprintf(cursor, printbuffer);
  375. cursor += strlen(printbuffer);
  376. }
  377. #else
  378. inline void dbg(const char *fmt, ...)
  379. {
  380. }
  381. #endif
  382. /** U-Boot INIT FUNCTIONS *************************************************/
  383. device_t *search_device (int flags, char *name)
  384. {
  385. device_t *dev;
  386. dev = device_get_by_name(name);
  387. if(dev && (dev->flags & flags))
  388. return dev;
  389. return NULL;
  390. }
  391. int console_assign (int file, char *devname)
  392. {
  393. int flag;
  394. device_t *dev;
  395. /* Check for valid file */
  396. switch (file) {
  397. case stdin:
  398. flag = DEV_FLAGS_INPUT;
  399. break;
  400. case stdout:
  401. case stderr:
  402. flag = DEV_FLAGS_OUTPUT;
  403. break;
  404. default:
  405. return -1;
  406. }
  407. /* Check for valid device name */
  408. dev = search_device(flag, devname);
  409. if(dev)
  410. return console_setfile (file, dev);
  411. return -1;
  412. }
  413. /* Called before relocation - use serial functions */
  414. int console_init_f (void)
  415. {
  416. gd->have_console = 1;
  417. #ifdef CONFIG_SILENT_CONSOLE
  418. if (getenv("silent") != NULL)
  419. gd->flags |= GD_FLG_SILENT;
  420. #endif
  421. return (0);
  422. }
  423. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  424. /* Called after the relocation - use desired console functions */
  425. int console_init_r (void)
  426. {
  427. char *stdinname, *stdoutname, *stderrname;
  428. device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  429. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  430. int i;
  431. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  432. #ifdef CONFIG_CONSOLE_MUX
  433. int iomux_err = 0;
  434. #endif
  435. /* set default handlers at first */
  436. gd->jt[XF_getc] = serial_getc;
  437. gd->jt[XF_tstc] = serial_tstc;
  438. gd->jt[XF_putc] = serial_putc;
  439. gd->jt[XF_puts] = serial_puts;
  440. gd->jt[XF_printf] = serial_printf;
  441. /* stdin stdout and stderr are in environment */
  442. /* scan for it */
  443. stdinname = getenv ("stdin");
  444. stdoutname = getenv ("stdout");
  445. stderrname = getenv ("stderr");
  446. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  447. inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
  448. outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
  449. errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
  450. #ifdef CONFIG_CONSOLE_MUX
  451. iomux_err = iomux_doenv(stdin, stdinname);
  452. iomux_err += iomux_doenv(stdout, stdoutname);
  453. iomux_err += iomux_doenv(stderr, stderrname);
  454. if (!iomux_err)
  455. /* Successful, so skip all the code below. */
  456. goto done;
  457. #endif
  458. }
  459. /* if the devices are overwritten or not found, use default device */
  460. if (inputdev == NULL) {
  461. inputdev = search_device (DEV_FLAGS_INPUT, "serial");
  462. }
  463. if (outputdev == NULL) {
  464. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  465. }
  466. if (errdev == NULL) {
  467. errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  468. }
  469. /* Initializes output console first */
  470. if (outputdev != NULL) {
  471. #ifdef CONFIG_CONSOLE_MUX
  472. /* need to set a console if not done above. */
  473. iomux_doenv(stdout, outputdev->name);
  474. #else
  475. console_setfile (stdout, outputdev);
  476. #endif
  477. }
  478. if (errdev != NULL) {
  479. #ifdef CONFIG_CONSOLE_MUX
  480. /* need to set a console if not done above. */
  481. iomux_doenv(stderr, errdev->name);
  482. #else
  483. console_setfile (stderr, errdev);
  484. #endif
  485. }
  486. if (inputdev != NULL) {
  487. #ifdef CONFIG_CONSOLE_MUX
  488. /* need to set a console if not done above. */
  489. iomux_doenv(stdin, inputdev->name);
  490. #else
  491. console_setfile (stdin, inputdev);
  492. #endif
  493. }
  494. #ifdef CONFIG_CONSOLE_MUX
  495. done:
  496. #endif
  497. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  498. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  499. /* Print information */
  500. puts ("In: ");
  501. if (stdio_devices[stdin] == NULL) {
  502. puts ("No input devices available!\n");
  503. } else {
  504. #ifdef CONFIG_CONSOLE_MUX
  505. iomux_printdevs(stdin);
  506. #else
  507. printf ("%s\n", stdio_devices[stdin]->name);
  508. #endif
  509. }
  510. puts ("Out: ");
  511. if (stdio_devices[stdout] == NULL) {
  512. puts ("No output devices available!\n");
  513. } else {
  514. #ifdef CONFIG_CONSOLE_MUX
  515. iomux_printdevs(stdout);
  516. #else
  517. printf ("%s\n", stdio_devices[stdout]->name);
  518. #endif
  519. }
  520. puts ("Err: ");
  521. if (stdio_devices[stderr] == NULL) {
  522. puts ("No error devices available!\n");
  523. } else {
  524. #ifdef CONFIG_CONSOLE_MUX
  525. iomux_printdevs(stderr);
  526. #else
  527. printf ("%s\n", stdio_devices[stderr]->name);
  528. #endif
  529. }
  530. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  531. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  532. /* set the environment variables (will overwrite previous env settings) */
  533. for (i = 0; i < 3; i++) {
  534. setenv (stdio_names[i], stdio_devices[i]->name);
  535. }
  536. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  537. #if 0
  538. /* If nothing usable installed, use only the initial console */
  539. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  540. return (0);
  541. #endif
  542. return (0);
  543. }
  544. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  545. /* Called after the relocation - use desired console functions */
  546. int console_init_r (void)
  547. {
  548. device_t *inputdev = NULL, *outputdev = NULL;
  549. int i;
  550. struct list_head *list = device_get_list();
  551. struct list_head *pos;
  552. device_t *dev;
  553. #ifdef CONFIG_SPLASH_SCREEN
  554. /* suppress all output if splash screen is enabled and we have
  555. a bmp to display */
  556. if (getenv("splashimage") != NULL)
  557. gd->flags |= GD_FLG_SILENT;
  558. #endif
  559. /* Scan devices looking for input and output devices */
  560. list_for_each(pos, list) {
  561. dev = list_entry(pos, device_t, list);
  562. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  563. inputdev = dev;
  564. }
  565. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  566. outputdev = dev;
  567. }
  568. if(inputdev && outputdev)
  569. break;
  570. }
  571. /* Initializes output console first */
  572. if (outputdev != NULL) {
  573. console_setfile (stdout, outputdev);
  574. console_setfile (stderr, outputdev);
  575. #ifdef CONFIG_CONSOLE_MUX
  576. console_devices[stdout][0] = outputdev;
  577. console_devices[stderr][0] = outputdev;
  578. #endif
  579. }
  580. /* Initializes input console */
  581. if (inputdev != NULL) {
  582. console_setfile (stdin, inputdev);
  583. #ifdef CONFIG_CONSOLE_MUX
  584. console_devices[stdin][0] = inputdev;
  585. #endif
  586. }
  587. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  588. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  589. /* Print information */
  590. puts ("In: ");
  591. if (stdio_devices[stdin] == NULL) {
  592. puts ("No input devices available!\n");
  593. } else {
  594. printf ("%s\n", stdio_devices[stdin]->name);
  595. }
  596. puts ("Out: ");
  597. if (stdio_devices[stdout] == NULL) {
  598. puts ("No output devices available!\n");
  599. } else {
  600. printf ("%s\n", stdio_devices[stdout]->name);
  601. }
  602. puts ("Err: ");
  603. if (stdio_devices[stderr] == NULL) {
  604. puts ("No error devices available!\n");
  605. } else {
  606. printf ("%s\n", stdio_devices[stderr]->name);
  607. }
  608. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  609. /* Setting environment variables */
  610. for (i = 0; i < 3; i++) {
  611. setenv (stdio_names[i], stdio_devices[i]->name);
  612. }
  613. #if 0
  614. /* If nothing usable installed, use only the initial console */
  615. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  616. return (0);
  617. #endif
  618. return (0);
  619. }
  620. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */