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_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, struct stdio_dev * 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 struct stdio_dev *tstcdev;
  87. struct stdio_dev **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. struct stdio_dev *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. struct stdio_dev *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. struct stdio_dev *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, struct stdio_dev *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, struct stdio_dev *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. struct stdio_dev *search_device(int flags, char *name)
  406. {
  407. struct stdio_dev *dev;
  408. dev = stdio_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. struct stdio_dev *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. void stdio_print_current_devices(void)
  446. {
  447. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  448. /* Print information */
  449. puts("In: ");
  450. if (stdio_devices[stdin] == NULL) {
  451. puts("No input devices available!\n");
  452. } else {
  453. printf ("%s\n", stdio_devices[stdin]->name);
  454. }
  455. puts("Out: ");
  456. if (stdio_devices[stdout] == NULL) {
  457. puts("No output devices available!\n");
  458. } else {
  459. printf ("%s\n", stdio_devices[stdout]->name);
  460. }
  461. puts("Err: ");
  462. if (stdio_devices[stderr] == NULL) {
  463. puts("No error devices available!\n");
  464. } else {
  465. printf ("%s\n", stdio_devices[stderr]->name);
  466. }
  467. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  468. }
  469. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  470. /* Called after the relocation - use desired console functions */
  471. int console_init_r(void)
  472. {
  473. char *stdinname, *stdoutname, *stderrname;
  474. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  475. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  476. int i;
  477. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  478. #ifdef CONFIG_CONSOLE_MUX
  479. int iomux_err = 0;
  480. #endif
  481. /* set default handlers at first */
  482. gd->jt[XF_getc] = serial_getc;
  483. gd->jt[XF_tstc] = serial_tstc;
  484. gd->jt[XF_putc] = serial_putc;
  485. gd->jt[XF_puts] = serial_puts;
  486. gd->jt[XF_printf] = serial_printf;
  487. /* stdin stdout and stderr are in environment */
  488. /* scan for it */
  489. stdinname = getenv("stdin");
  490. stdoutname = getenv("stdout");
  491. stderrname = getenv("stderr");
  492. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  493. inputdev = search_device(DEV_FLAGS_INPUT, stdinname);
  494. outputdev = search_device(DEV_FLAGS_OUTPUT, stdoutname);
  495. errdev = search_device(DEV_FLAGS_OUTPUT, stderrname);
  496. #ifdef CONFIG_CONSOLE_MUX
  497. iomux_err = iomux_doenv(stdin, stdinname);
  498. iomux_err += iomux_doenv(stdout, stdoutname);
  499. iomux_err += iomux_doenv(stderr, stderrname);
  500. if (!iomux_err)
  501. /* Successful, so skip all the code below. */
  502. goto done;
  503. #endif
  504. }
  505. /* if the devices are overwritten or not found, use default device */
  506. if (inputdev == NULL) {
  507. inputdev = search_device(DEV_FLAGS_INPUT, "serial");
  508. }
  509. if (outputdev == NULL) {
  510. outputdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  511. }
  512. if (errdev == NULL) {
  513. errdev = search_device(DEV_FLAGS_OUTPUT, "serial");
  514. }
  515. /* Initializes output console first */
  516. if (outputdev != NULL) {
  517. /* need to set a console if not done above. */
  518. console_doenv(stdout, outputdev);
  519. }
  520. if (errdev != NULL) {
  521. /* need to set a console if not done above. */
  522. console_doenv(stderr, errdev);
  523. }
  524. if (inputdev != NULL) {
  525. /* need to set a console if not done above. */
  526. console_doenv(stdin, inputdev);
  527. }
  528. #ifdef CONFIG_CONSOLE_MUX
  529. done:
  530. #endif
  531. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  532. stdio_print_current_devices();
  533. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  534. /* set the environment variables (will overwrite previous env settings) */
  535. for (i = 0; i < 3; i++) {
  536. setenv(stdio_names[i], stdio_devices[i]->name);
  537. }
  538. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  539. #if 0
  540. /* If nothing usable installed, use only the initial console */
  541. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  542. return 0;
  543. #endif
  544. return 0;
  545. }
  546. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  547. /* Called after the relocation - use desired console functions */
  548. int console_init_r(void)
  549. {
  550. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  551. int i;
  552. struct list_head *list = stdio_get_list();
  553. struct list_head *pos;
  554. struct stdio_dev *dev;
  555. #ifdef CONFIG_SPLASH_SCREEN
  556. /*
  557. * suppress all output if splash screen is enabled and we have
  558. * a bmp to display
  559. */
  560. if (getenv("splashimage") != NULL)
  561. gd->flags |= GD_FLG_SILENT;
  562. #endif
  563. /* Scan devices looking for input and output devices */
  564. list_for_each(pos, list) {
  565. dev = list_entry(pos, struct stdio_dev, list);
  566. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  567. inputdev = dev;
  568. }
  569. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  570. outputdev = dev;
  571. }
  572. if(inputdev && outputdev)
  573. break;
  574. }
  575. /* Initializes output console first */
  576. if (outputdev != NULL) {
  577. console_setfile(stdout, outputdev);
  578. console_setfile(stderr, outputdev);
  579. #ifdef CONFIG_CONSOLE_MUX
  580. console_devices[stdout][0] = outputdev;
  581. console_devices[stderr][0] = outputdev;
  582. #endif
  583. }
  584. /* Initializes input console */
  585. if (inputdev != NULL) {
  586. console_setfile(stdin, inputdev);
  587. #ifdef CONFIG_CONSOLE_MUX
  588. console_devices[stdin][0] = inputdev;
  589. #endif
  590. }
  591. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  592. stdio_print_current_devices();
  593. /* Setting environment variables */
  594. for (i = 0; i < 3; i++) {
  595. setenv(stdio_names[i], stdio_devices[i]->name);
  596. }
  597. #if 0
  598. /* If nothing usable installed, use only the initial console */
  599. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  600. return 0;
  601. #endif
  602. return 0;
  603. }
  604. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */