console.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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)
  371. < strlen(printbuffer) + 1) {
  372. memset(screen, 0, sizeof(screen));
  373. cursor = screen;
  374. }
  375. sprintf(cursor, printbuffer);
  376. cursor += strlen(printbuffer);
  377. }
  378. #else
  379. inline void dbg(const char *fmt, ...)
  380. {
  381. }
  382. #endif
  383. /** U-Boot INIT FUNCTIONS *************************************************/
  384. device_t *search_device(int flags, char *name)
  385. {
  386. device_t *dev;
  387. dev = device_get_by_name(name);
  388. if (dev && (dev->flags & flags))
  389. return dev;
  390. return NULL;
  391. }
  392. int console_assign(int file, char *devname)
  393. {
  394. int flag;
  395. device_t *dev;
  396. /* Check for valid file */
  397. switch (file) {
  398. case stdin:
  399. flag = DEV_FLAGS_INPUT;
  400. break;
  401. case stdout:
  402. case stderr:
  403. flag = DEV_FLAGS_OUTPUT;
  404. break;
  405. default:
  406. return -1;
  407. }
  408. /* Check for valid device name */
  409. dev = search_device(flag, devname);
  410. if (dev)
  411. return console_setfile(file, dev);
  412. return -1;
  413. }
  414. /* Called before relocation - use serial functions */
  415. int console_init_f(void)
  416. {
  417. gd->have_console = 1;
  418. #ifdef CONFIG_SILENT_CONSOLE
  419. if (getenv("silent") != NULL)
  420. gd->flags |= GD_FLG_SILENT;
  421. #endif
  422. return 0;
  423. }
  424. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  425. /* Called after the relocation - use desired console functions */
  426. int console_init_r(void)
  427. {
  428. char *stdinname, *stdoutname, *stderrname;
  429. device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  430. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  431. int i;
  432. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  433. #ifdef CONFIG_CONSOLE_MUX
  434. int iomux_err = 0;
  435. #endif
  436. /* set default handlers at first */
  437. gd->jt[XF_getc] = serial_getc;
  438. gd->jt[XF_tstc] = serial_tstc;
  439. gd->jt[XF_putc] = serial_putc;
  440. gd->jt[XF_puts] = serial_puts;
  441. gd->jt[XF_printf] = serial_printf;
  442. /* stdin stdout and stderr are in environment */
  443. /* scan for it */
  444. stdinname = getenv("stdin");
  445. stdoutname = getenv("stdout");
  446. stderrname = getenv("stderr");
  447. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  448. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  449. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  450. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  451. #ifdef CONFIG_CONSOLE_MUX
  452. iomux_err = iomux_doenv(stdin, stdinname);
  453. iomux_err += iomux_doenv(stdout, stdoutname);
  454. iomux_err += iomux_doenv(stderr, stderrname);
  455. if (!iomux_err)
  456. /* Successful, so skip all the code below. */
  457. goto done;
  458. #endif
  459. }
  460. /* if the devices are overwritten or not found, use default device */
  461. if (inputdev == NULL) {
  462. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  463. }
  464. if (outputdev == NULL) {
  465. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  466. }
  467. if (errdev == NULL) {
  468. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  469. }
  470. /* Initializes output console first */
  471. if (outputdev != NULL) {
  472. #ifdef CONFIG_CONSOLE_MUX
  473. /* need to set a console if not done above. */
  474. iomux_doenv(stdout, outputdev->name);
  475. #else
  476. console_setfile(stdout, outputdev);
  477. #endif
  478. }
  479. if (errdev != NULL) {
  480. #ifdef CONFIG_CONSOLE_MUX
  481. /* need to set a console if not done above. */
  482. iomux_doenv(stderr, errdev->name);
  483. #else
  484. console_setfile(stderr, errdev);
  485. #endif
  486. }
  487. if (inputdev != NULL) {
  488. #ifdef CONFIG_CONSOLE_MUX
  489. /* need to set a console if not done above. */
  490. iomux_doenv(stdin, inputdev->name);
  491. #else
  492. console_setfile(stdin, inputdev);
  493. #endif
  494. }
  495. #ifdef CONFIG_CONSOLE_MUX
  496. done:
  497. #endif
  498. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  499. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  500. /* Print information */
  501. puts("In: ");
  502. if (stdio_devices[stdin] == NULL) {
  503. puts("No input devices available!\n");
  504. } else {
  505. #ifdef CONFIG_CONSOLE_MUX
  506. iomux_printdevs(stdin);
  507. #else
  508. printf("%s\n", stdio_devices[stdin]->name);
  509. #endif
  510. }
  511. puts("Out: ");
  512. if (stdio_devices[stdout] == NULL) {
  513. puts("No output devices available!\n");
  514. } else {
  515. #ifdef CONFIG_CONSOLE_MUX
  516. iomux_printdevs(stdout);
  517. #else
  518. printf("%s\n", stdio_devices[stdout]->name);
  519. #endif
  520. }
  521. puts("Err: ");
  522. if (stdio_devices[stderr] == NULL) {
  523. puts("No error devices available!\n");
  524. } else {
  525. #ifdef CONFIG_CONSOLE_MUX
  526. iomux_printdevs(stderr);
  527. #else
  528. printf("%s\n", stdio_devices[stderr]->name);
  529. #endif
  530. }
  531. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  532. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  533. /* set the environment variables (will overwrite previous env settings) */
  534. for (i = 0; i < 3; i++) {
  535. setenv(stdio_names[i], stdio_devices[i]->name);
  536. }
  537. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  538. #if 0
  539. /* If nothing usable installed, use only the initial console */
  540. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  541. return 0;
  542. #endif
  543. return 0;
  544. }
  545. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  546. /* Called after the relocation - use desired console functions */
  547. int console_init_r(void)
  548. {
  549. device_t *inputdev = NULL, *outputdev = NULL;
  550. int i;
  551. struct list_head *list = device_get_list();
  552. struct list_head *pos;
  553. device_t *dev;
  554. #ifdef CONFIG_SPLASH_SCREEN
  555. /*
  556. * suppress all output if splash screen is enabled and we have
  557. * a bmp to display
  558. */
  559. if (getenv("splashimage") != NULL)
  560. gd->flags |= GD_FLG_SILENT;
  561. #endif
  562. /* Scan devices looking for input and output devices */
  563. list_for_each(pos, list) {
  564. dev = list_entry(pos, device_t, list);
  565. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  566. inputdev = dev;
  567. }
  568. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  569. outputdev = dev;
  570. }
  571. if(inputdev && outputdev)
  572. break;
  573. }
  574. /* Initializes output console first */
  575. if (outputdev != NULL) {
  576. console_setfile(stdout, outputdev);
  577. console_setfile(stderr, outputdev);
  578. #ifdef CONFIG_CONSOLE_MUX
  579. console_devices[stdout][0] = outputdev;
  580. console_devices[stderr][0] = outputdev;
  581. #endif
  582. }
  583. /* Initializes input console */
  584. if (inputdev != NULL) {
  585. console_setfile(stdin, inputdev);
  586. #ifdef CONFIG_CONSOLE_MUX
  587. console_devices[stdin][0] = inputdev;
  588. #endif
  589. }
  590. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  591. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  592. /* Print information */
  593. puts("In: ");
  594. if (stdio_devices[stdin] == NULL) {
  595. puts("No input devices available!\n");
  596. } else {
  597. printf("%s\n", stdio_devices[stdin]->name);
  598. }
  599. puts("Out: ");
  600. if (stdio_devices[stdout] == NULL) {
  601. puts("No output devices available!\n");
  602. } else {
  603. printf("%s\n", stdio_devices[stdout]->name);
  604. }
  605. puts("Err: ");
  606. if (stdio_devices[stderr] == NULL) {
  607. puts("No error devices available!\n");
  608. } else {
  609. printf("%s\n", stdio_devices[stderr]->name);
  610. }
  611. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  612. /* Setting environment variables */
  613. for (i = 0; i < 3; i++) {
  614. setenv(stdio_names[i], stdio_devices[i]->name);
  615. }
  616. #if 0
  617. /* If nothing usable installed, use only the initial console */
  618. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  619. return 0;
  620. #endif
  621. return 0;
  622. }
  623. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */