console.c 12 KB

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