console.c 15 KB

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