console.c 15 KB

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