console.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
  85. void serial_printf (const char *fmt, ...)
  86. {
  87. va_list args;
  88. uint i;
  89. char printbuffer[CONFIG_SYS_PBSIZE];
  90. va_start (args, fmt);
  91. /* For this to work, printbuffer must be larger than
  92. * anything we ever want to print.
  93. */
  94. i = vsprintf (printbuffer, fmt, args);
  95. va_end (args);
  96. serial_puts (printbuffer);
  97. }
  98. int fgetc (int file)
  99. {
  100. if (file < MAX_FILES)
  101. return stdio_devices[file]->getc ();
  102. return -1;
  103. }
  104. int ftstc (int file)
  105. {
  106. if (file < MAX_FILES)
  107. return stdio_devices[file]->tstc ();
  108. return -1;
  109. }
  110. void fputc (int file, const char c)
  111. {
  112. if (file < MAX_FILES)
  113. stdio_devices[file]->putc (c);
  114. }
  115. void fputs (int file, const char *s)
  116. {
  117. if (file < MAX_FILES)
  118. stdio_devices[file]->puts (s);
  119. }
  120. void fprintf (int file, const char *fmt, ...)
  121. {
  122. va_list args;
  123. uint i;
  124. char printbuffer[CONFIG_SYS_PBSIZE];
  125. va_start (args, fmt);
  126. /* For this to work, printbuffer must be larger than
  127. * anything we ever want to print.
  128. */
  129. i = vsprintf (printbuffer, fmt, args);
  130. va_end (args);
  131. /* Send to desired file */
  132. fputs (file, printbuffer);
  133. }
  134. /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
  135. int getc (void)
  136. {
  137. #ifdef CONFIG_DISABLE_CONSOLE
  138. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  139. return 0;
  140. #endif
  141. if (gd->flags & GD_FLG_DEVINIT) {
  142. /* Get from the standard input */
  143. return fgetc (stdin);
  144. }
  145. /* Send directly to the handler */
  146. return serial_getc ();
  147. }
  148. int tstc (void)
  149. {
  150. #ifdef CONFIG_DISABLE_CONSOLE
  151. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  152. return 0;
  153. #endif
  154. if (gd->flags & GD_FLG_DEVINIT) {
  155. /* Test the standard input */
  156. return ftstc (stdin);
  157. }
  158. /* Send directly to the handler */
  159. return serial_tstc ();
  160. }
  161. void putc (const char c)
  162. {
  163. #ifdef CONFIG_SILENT_CONSOLE
  164. if (gd->flags & GD_FLG_SILENT)
  165. return;
  166. #endif
  167. #ifdef CONFIG_DISABLE_CONSOLE
  168. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  169. return;
  170. #endif
  171. if (gd->flags & GD_FLG_DEVINIT) {
  172. /* Send to the standard output */
  173. fputc (stdout, c);
  174. } else {
  175. /* Send directly to the handler */
  176. serial_putc (c);
  177. }
  178. }
  179. void puts (const char *s)
  180. {
  181. #ifdef CONFIG_SILENT_CONSOLE
  182. if (gd->flags & GD_FLG_SILENT)
  183. return;
  184. #endif
  185. #ifdef CONFIG_DISABLE_CONSOLE
  186. if (gd->flags & GD_FLG_DISABLE_CONSOLE)
  187. return;
  188. #endif
  189. if (gd->flags & GD_FLG_DEVINIT) {
  190. /* Send to the standard output */
  191. fputs (stdout, s);
  192. } else {
  193. /* Send directly to the handler */
  194. serial_puts (s);
  195. }
  196. }
  197. void printf (const char *fmt, ...)
  198. {
  199. va_list args;
  200. uint i;
  201. char printbuffer[CONFIG_SYS_PBSIZE];
  202. va_start (args, fmt);
  203. /* For this to work, printbuffer must be larger than
  204. * anything we ever want to print.
  205. */
  206. i = vsprintf (printbuffer, fmt, args);
  207. va_end (args);
  208. /* Print the string */
  209. puts (printbuffer);
  210. }
  211. void vprintf (const char *fmt, va_list args)
  212. {
  213. uint i;
  214. char printbuffer[CONFIG_SYS_PBSIZE];
  215. /* For this to work, printbuffer must be larger than
  216. * anything we ever want to print.
  217. */
  218. i = vsprintf (printbuffer, fmt, args);
  219. /* Print the string */
  220. puts (printbuffer);
  221. }
  222. /* test if ctrl-c was pressed */
  223. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  224. static int ctrlc_was_pressed = 0;
  225. int ctrlc (void)
  226. {
  227. if (!ctrlc_disabled && gd->have_console) {
  228. if (tstc ()) {
  229. switch (getc ()) {
  230. case 0x03: /* ^C - Control C */
  231. ctrlc_was_pressed = 1;
  232. return 1;
  233. default:
  234. break;
  235. }
  236. }
  237. }
  238. return 0;
  239. }
  240. /* pass 1 to disable ctrlc() checking, 0 to enable.
  241. * returns previous state
  242. */
  243. int disable_ctrlc (int disable)
  244. {
  245. int prev = ctrlc_disabled; /* save previous state */
  246. ctrlc_disabled = disable;
  247. return prev;
  248. }
  249. int had_ctrlc (void)
  250. {
  251. return ctrlc_was_pressed;
  252. }
  253. void clear_ctrlc (void)
  254. {
  255. ctrlc_was_pressed = 0;
  256. }
  257. #ifdef CONFIG_MODEM_SUPPORT_DEBUG
  258. char screen[1024];
  259. char *cursor = screen;
  260. int once = 0;
  261. inline void dbg(const char *fmt, ...)
  262. {
  263. va_list args;
  264. uint i;
  265. char printbuffer[CONFIG_SYS_PBSIZE];
  266. if (!once) {
  267. memset(screen, 0, sizeof(screen));
  268. once++;
  269. }
  270. va_start(args, fmt);
  271. /* For this to work, printbuffer must be larger than
  272. * anything we ever want to print.
  273. */
  274. i = vsprintf(printbuffer, fmt, args);
  275. va_end(args);
  276. if ((screen + sizeof(screen) - 1 - cursor) < strlen(printbuffer)+1) {
  277. memset(screen, 0, sizeof(screen));
  278. cursor = screen;
  279. }
  280. sprintf(cursor, printbuffer);
  281. cursor += strlen(printbuffer);
  282. }
  283. #else
  284. inline void dbg(const char *fmt, ...)
  285. {
  286. }
  287. #endif
  288. /** U-Boot INIT FUNCTIONS *************************************************/
  289. device_t *search_device (int flags, char *name)
  290. {
  291. device_t *dev;
  292. dev = device_get_by_name(name);
  293. if(dev && (dev->flags & flags))
  294. return dev;
  295. return NULL;
  296. }
  297. int console_assign (int file, char *devname)
  298. {
  299. int flag;
  300. device_t *dev;
  301. /* Check for valid file */
  302. switch (file) {
  303. case stdin:
  304. flag = DEV_FLAGS_INPUT;
  305. break;
  306. case stdout:
  307. case stderr:
  308. flag = DEV_FLAGS_OUTPUT;
  309. break;
  310. default:
  311. return -1;
  312. }
  313. /* Check for valid device name */
  314. dev = search_device(flag, devname);
  315. if(dev)
  316. return console_setfile (file, dev);
  317. return -1;
  318. }
  319. /* Called before relocation - use serial functions */
  320. int console_init_f (void)
  321. {
  322. gd->have_console = 1;
  323. #ifdef CONFIG_SILENT_CONSOLE
  324. if (getenv("silent") != NULL)
  325. gd->flags |= GD_FLG_SILENT;
  326. #endif
  327. return (0);
  328. }
  329. #ifdef CONFIG_SYS_CONSOLE_IS_IN_ENV
  330. /* Called after the relocation - use desired console functions */
  331. int console_init_r (void)
  332. {
  333. char *stdinname, *stdoutname, *stderrname;
  334. device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  335. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  336. int i;
  337. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  338. /* set default handlers at first */
  339. gd->jt[XF_getc] = serial_getc;
  340. gd->jt[XF_tstc] = serial_tstc;
  341. gd->jt[XF_putc] = serial_putc;
  342. gd->jt[XF_puts] = serial_puts;
  343. gd->jt[XF_printf] = serial_printf;
  344. /* stdin stdout and stderr are in environment */
  345. /* scan for it */
  346. stdinname = getenv ("stdin");
  347. stdoutname = getenv ("stdout");
  348. stderrname = getenv ("stderr");
  349. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  350. inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
  351. outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
  352. errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
  353. }
  354. /* if the devices are overwritten or not found, use default device */
  355. if (inputdev == NULL) {
  356. inputdev = search_device (DEV_FLAGS_INPUT, "serial");
  357. }
  358. if (outputdev == NULL) {
  359. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  360. }
  361. if (errdev == NULL) {
  362. errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  363. }
  364. /* Initializes output console first */
  365. if (outputdev != NULL) {
  366. console_setfile (stdout, outputdev);
  367. }
  368. if (errdev != NULL) {
  369. console_setfile (stderr, errdev);
  370. }
  371. if (inputdev != NULL) {
  372. console_setfile (stdin, inputdev);
  373. }
  374. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  375. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  376. /* Print information */
  377. puts ("In: ");
  378. if (stdio_devices[stdin] == NULL) {
  379. puts ("No input devices available!\n");
  380. } else {
  381. printf ("%s\n", stdio_devices[stdin]->name);
  382. }
  383. puts ("Out: ");
  384. if (stdio_devices[stdout] == NULL) {
  385. puts ("No output devices available!\n");
  386. } else {
  387. printf ("%s\n", stdio_devices[stdout]->name);
  388. }
  389. puts ("Err: ");
  390. if (stdio_devices[stderr] == NULL) {
  391. puts ("No error devices available!\n");
  392. } else {
  393. printf ("%s\n", stdio_devices[stderr]->name);
  394. }
  395. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  396. #ifdef CONFIG_SYS_CONSOLE_ENV_OVERWRITE
  397. /* set the environment variables (will overwrite previous env settings) */
  398. for (i = 0; i < 3; i++) {
  399. setenv (stdio_names[i], stdio_devices[i]->name);
  400. }
  401. #endif /* CONFIG_SYS_CONSOLE_ENV_OVERWRITE */
  402. #if 0
  403. /* If nothing usable installed, use only the initial console */
  404. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  405. return (0);
  406. #endif
  407. return (0);
  408. }
  409. #else /* CONFIG_SYS_CONSOLE_IS_IN_ENV */
  410. /* Called after the relocation - use desired console functions */
  411. int console_init_r (void)
  412. {
  413. device_t *inputdev = NULL, *outputdev = NULL;
  414. int i;
  415. struct list_head *list = device_get_list();
  416. struct list_head *pos;
  417. device_t *dev;
  418. #ifdef CONFIG_SPLASH_SCREEN
  419. /* suppress all output if splash screen is enabled and we have
  420. a bmp to display */
  421. if (getenv("splashimage") != NULL)
  422. gd->flags |= GD_FLG_SILENT;
  423. #endif
  424. /* Scan devices looking for input and output devices */
  425. list_for_each(pos, list) {
  426. dev = list_entry(pos, device_t, list);
  427. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  428. inputdev = dev;
  429. }
  430. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  431. outputdev = dev;
  432. }
  433. if(inputdev && outputdev)
  434. break;
  435. }
  436. /* Initializes output console first */
  437. if (outputdev != NULL) {
  438. console_setfile (stdout, outputdev);
  439. console_setfile (stderr, outputdev);
  440. }
  441. /* Initializes input console */
  442. if (inputdev != NULL) {
  443. console_setfile (stdin, inputdev);
  444. }
  445. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  446. #ifndef CONFIG_SYS_CONSOLE_INFO_QUIET
  447. /* Print information */
  448. puts ("In: ");
  449. if (stdio_devices[stdin] == NULL) {
  450. puts ("No input devices available!\n");
  451. } else {
  452. printf ("%s\n", stdio_devices[stdin]->name);
  453. }
  454. puts ("Out: ");
  455. if (stdio_devices[stdout] == NULL) {
  456. puts ("No output devices available!\n");
  457. } else {
  458. printf ("%s\n", stdio_devices[stdout]->name);
  459. }
  460. puts ("Err: ");
  461. if (stdio_devices[stderr] == NULL) {
  462. puts ("No error devices available!\n");
  463. } else {
  464. printf ("%s\n", stdio_devices[stderr]->name);
  465. }
  466. #endif /* CONFIG_SYS_CONSOLE_INFO_QUIET */
  467. /* Setting environment variables */
  468. for (i = 0; i < 3; i++) {
  469. setenv (stdio_names[i], stdio_devices[i]->name);
  470. }
  471. #if 0
  472. /* If nothing usable installed, use only the initial console */
  473. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  474. return (0);
  475. #endif
  476. return (0);
  477. }
  478. #endif /* CONFIG_SYS_CONSOLE_IS_IN_ENV */