console.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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->flags & GD_FLG_DEVINIT) {
  256. /* Get from the standard input */
  257. return fgetc(stdin);
  258. }
  259. /* Send directly to the handler */
  260. return serial_getc();
  261. }
  262. int tstc(void)
  263. {
  264. #ifdef CONFIG_DISABLE_CONSOLE
  265. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  266. return 0;
  267. #endif
  268. if (gd->flags & GD_FLG_DEVINIT) {
  269. /* Test the standard input */
  270. return ftstc(stdin);
  271. }
  272. /* Send directly to the handler */
  273. return serial_tstc();
  274. }
  275. void putc(const char c)
  276. {
  277. #ifdef CONFIG_SILENT_CONSOLE
  278. if (gd->flags & GD_FLG_SILENT)
  279. return;
  280. #endif
  281. #ifdef CONFIG_DISABLE_CONSOLE
  282. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  283. return;
  284. #endif
  285. if (gd->flags & GD_FLG_DEVINIT) {
  286. /* Send to the standard output */
  287. fputc(stdout, c);
  288. } else {
  289. /* Send directly to the handler */
  290. serial_putc(c);
  291. }
  292. }
  293. void puts(const char *s)
  294. {
  295. #ifdef CONFIG_SILENT_CONSOLE
  296. if (gd->flags & GD_FLG_SILENT)
  297. return;
  298. #endif
  299. #ifdef CONFIG_DISABLE_CONSOLE
  300. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  301. return;
  302. #endif
  303. if (gd->flags & GD_FLG_DEVINIT) {
  304. /* Send to the standard output */
  305. fputs(stdout, s);
  306. } else {
  307. /* Send directly to the handler */
  308. serial_puts(s);
  309. }
  310. }
  311. int printf(const char *fmt, ...)
  312. {
  313. va_list args;
  314. uint i;
  315. char printbuffer[CONFIG_SYS_PBSIZE];
  316. va_start(args, fmt);
  317. /* For this to work, printbuffer must be larger than
  318. * anything we ever want to print.
  319. */
  320. i = vsprintf(printbuffer, fmt, args);
  321. va_end(args);
  322. /* Print the string */
  323. puts(printbuffer);
  324. return i;
  325. }
  326. int vprintf(const char *fmt, va_list args)
  327. {
  328. uint i;
  329. char printbuffer[CONFIG_SYS_PBSIZE];
  330. /* For this to work, printbuffer must be larger than
  331. * anything we ever want to print.
  332. */
  333. i = vsprintf(printbuffer, fmt, args);
  334. /* Print the string */
  335. puts(printbuffer);
  336. return i;
  337. }
  338. /* test if ctrl-c was pressed */
  339. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  340. static int ctrlc_was_pressed = 0;
  341. int ctrlc(void)
  342. {
  343. if (!ctrlc_disabled && gd->have_console) {
  344. if (tstc()) {
  345. switch (getc()) {
  346. case 0x03: /* ^C - Control C */
  347. ctrlc_was_pressed = 1;
  348. return 1;
  349. default:
  350. break;
  351. }
  352. }
  353. }
  354. return 0;
  355. }
  356. /* pass 1 to disable ctrlc() checking, 0 to enable.
  357. * returns previous state
  358. */
  359. int disable_ctrlc(int disable)
  360. {
  361. int prev = ctrlc_disabled; /* save previous state */
  362. ctrlc_disabled = disable;
  363. return prev;
  364. }
  365. int had_ctrlc (void)
  366. {
  367. return ctrlc_was_pressed;
  368. }
  369. void clear_ctrlc(void)
  370. {
  371. ctrlc_was_pressed = 0;
  372. }
  373. #ifdef CONFIG_MODEM_SUPPORT_DEBUG
  374. char screen[1024];
  375. char *cursor = screen;
  376. int once = 0;
  377. inline void dbg(const char *fmt, ...)
  378. {
  379. va_list args;
  380. uint i;
  381. char printbuffer[CONFIG_SYS_PBSIZE];
  382. if (!once) {
  383. memset(screen, 0, sizeof(screen));
  384. once++;
  385. }
  386. va_start(args, fmt);
  387. /* For this to work, printbuffer must be larger than
  388. * anything we ever want to print.
  389. */
  390. i = vsprintf(printbuffer, fmt, args);
  391. va_end(args);
  392. if ((screen + sizeof(screen) - 1 - cursor)
  393. < strlen(printbuffer) + 1) {
  394. memset(screen, 0, sizeof(screen));
  395. cursor = screen;
  396. }
  397. sprintf(cursor, printbuffer);
  398. cursor += strlen(printbuffer);
  399. }
  400. #else
  401. inline void dbg(const char *fmt, ...)
  402. {
  403. }
  404. #endif
  405. /** U-Boot INIT FUNCTIONS *************************************************/
  406. struct stdio_dev *search_device(int flags, char *name)
  407. {
  408. struct stdio_dev *dev;
  409. dev = stdio_get_by_name(name);
  410. if (dev && (dev->flags & flags))
  411. return dev;
  412. return NULL;
  413. }
  414. int console_assign(int file, char *devname)
  415. {
  416. int flag;
  417. struct stdio_dev *dev;
  418. /* Check for valid file */
  419. switch (file) {
  420. case stdin:
  421. flag = DEV_FLAGS_INPUT;
  422. break;
  423. case stdout:
  424. case stderr:
  425. flag = DEV_FLAGS_OUTPUT;
  426. break;
  427. default:
  428. return -1;
  429. }
  430. /* Check for valid device name */
  431. dev = search_device(flag, devname);
  432. if (dev)
  433. return console_setfile(file, dev);
  434. return -1;
  435. }
  436. /* Called before relocation - use serial functions */
  437. int console_init_f(void)
  438. {
  439. gd->have_console = 1;
  440. #ifdef CONFIG_SILENT_CONSOLE
  441. if (getenv("silent") != NULL)
  442. gd->flags |= GD_FLG_SILENT;
  443. #endif
  444. return 0;
  445. }
  446. void stdio_print_current_devices(void)
  447. {
  448. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  449. /* Print information */
  450. puts("In: ");
  451. if (stdio_devices[stdin] == NULL) {
  452. puts("No input devices available!\n");
  453. } else {
  454. printf ("%s\n", stdio_devices[stdin]->name);
  455. }
  456. puts("Out: ");
  457. if (stdio_devices[stdout] == NULL) {
  458. puts("No output devices available!\n");
  459. } else {
  460. printf ("%s\n", stdio_devices[stdout]->name);
  461. }
  462. puts("Err: ");
  463. if (stdio_devices[stderr] == NULL) {
  464. puts("No error devices available!\n");
  465. } else {
  466. printf ("%s\n", stdio_devices[stderr]->name);
  467. }
  468. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  469. }
  470. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  471. /* Called after the relocation - use desired console functions */
  472. int console_init_r(void)
  473. {
  474. char *stdinname, *stdoutname, *stderrname;
  475. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  476. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  477. int i;
  478. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  479. #ifdef CONFIG_CONSOLE_MUX
  480. int iomux_err = 0;
  481. #endif
  482. /* set default handlers at first */
  483. gd->jt[XF_getc] = serial_getc;
  484. gd->jt[XF_tstc] = serial_tstc;
  485. gd->jt[XF_putc] = serial_putc;
  486. gd->jt[XF_puts] = serial_puts;
  487. gd->jt[XF_printf] = serial_printf;
  488. /* stdin stdout and stderr are in environment */
  489. /* scan for it */
  490. stdinname = getenv("stdin");
  491. stdoutname = getenv("stdout");
  492. stderrname = getenv("stderr");
  493. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  494. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  495. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  496. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  497. #ifdef CONFIG_CONSOLE_MUX
  498. iomux_err = iomux_doenv(stdin, stdinname);
  499. iomux_err += iomux_doenv(stdout, stdoutname);
  500. iomux_err += iomux_doenv(stderr, stderrname);
  501. if (!iomux_err)
  502. /* Successful, so skip all the code below. */
  503. goto done;
  504. #endif
  505. }
  506. /* if the devices are overwritten or not found, use default device */
  507. if (inputdev == NULL) {
  508. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  509. }
  510. if (outputdev == NULL) {
  511. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  512. }
  513. if (errdev == NULL) {
  514. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  515. }
  516. /* Initializes output console first */
  517. if (outputdev != NULL) {
  518. /* need to set a console if not done above. */
  519. console_doenv(stdout, outputdev);
  520. }
  521. if (errdev != NULL) {
  522. /* need to set a console if not done above. */
  523. console_doenv(stderr, errdev);
  524. }
  525. if (inputdev != NULL) {
  526. /* need to set a console if not done above. */
  527. console_doenv(stdin, inputdev);
  528. }
  529. #ifdef CONFIG_CONSOLE_MUX
  530. done:
  531. #endif
  532. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  533. stdio_print_current_devices();
  534. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  535. /* set the environment variables (will overwrite previous env settings) */
  536. for (i = 0; i < 3; i++) {
  537. setenv(stdio_names[i], stdio_devices[i]->name);
  538. }
  539. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  540. #if 0
  541. /* If nothing usable installed, use only the initial console */
  542. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  543. return 0;
  544. #endif
  545. return 0;
  546. }
  547. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  548. /* Called after the relocation - use desired console functions */
  549. int console_init_r(void)
  550. {
  551. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  552. int i;
  553. struct list_head *list = stdio_get_list();
  554. struct list_head *pos;
  555. struct stdio_dev *dev;
  556. #ifdef CONFIG_SPLASH_SCREEN
  557. /*
  558. * suppress all output if splash screen is enabled and we have
  559. * a bmp to display. We redirect the output from frame buffer
  560. * console to serial console in this case or suppress it if
  561. * "silent" mode was requested.
  562. */
  563. if (getenv("splashimage") != NULL) {
  564. if (!(gd->flags & GD_FLG_SILENT))
  565. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  566. }
  567. #endif
  568. /* Scan devices looking for input and output devices */
  569. list_for_each(pos, list) {
  570. dev = list_entry(pos, struct stdio_dev, list);
  571. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  572. inputdev = dev;
  573. }
  574. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  575. outputdev = dev;
  576. }
  577. if(inputdev && outputdev)
  578. break;
  579. }
  580. /* Initializes output console first */
  581. if (outputdev != NULL) {
  582. console_setfile(stdout, outputdev);
  583. console_setfile(stderr, outputdev);
  584. #ifdef CONFIG_CONSOLE_MUX
  585. console_devices[stdout][0] = outputdev;
  586. console_devices[stderr][0] = outputdev;
  587. #endif
  588. }
  589. /* Initializes input console */
  590. if (inputdev != NULL) {
  591. console_setfile(stdin, inputdev);
  592. #ifdef CONFIG_CONSOLE_MUX
  593. console_devices[stdin][0] = inputdev;
  594. #endif
  595. }
  596. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  597. stdio_print_current_devices();
  598. /* Setting environment variables */
  599. for (i = 0; i < 3; i++) {
  600. setenv(stdio_names[i], stdio_devices[i]->name);
  601. }
  602. #if 0
  603. /* If nothing usable installed, use only the initial console */
  604. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  605. return 0;
  606. #endif
  607. return 0;
  608. }
  609. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */