console.c 15 KB

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