console.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. int console_assign (int file, char *devname)
  274. {
  275. int flag, i;
  276. /* Check for valid file */
  277. switch (file) {
  278. case stdin:
  279. flag = DEV_FLAGS_INPUT;
  280. break;
  281. case stdout:
  282. case stderr:
  283. flag = DEV_FLAGS_OUTPUT;
  284. break;
  285. default:
  286. return -1;
  287. }
  288. /* Check for valid device name */
  289. for (i = 1; i <= ListNumItems (devlist); i++) {
  290. device_t *dev = ListGetPtrToItem (devlist, i);
  291. if (strcmp (devname, dev->name) == 0) {
  292. if (dev->flags & flag)
  293. return console_setfile (file, dev);
  294. return -1;
  295. }
  296. }
  297. return -1;
  298. }
  299. /* Called before relocation - use serial functions */
  300. int console_init_f (void)
  301. {
  302. gd->have_console = 1;
  303. #ifdef CONFIG_SILENT_CONSOLE
  304. if (getenv("silent") != NULL)
  305. gd->flags |= GD_FLG_SILENT;
  306. #endif
  307. return (0);
  308. }
  309. #if defined(CFG_CONSOLE_IS_IN_ENV) || defined(CONFIG_SPLASH_SCREEN) || defined(CONFIG_SILENT_CONSOLE)
  310. /* search a device */
  311. device_t *search_device (int flags, char *name)
  312. {
  313. int i, items;
  314. device_t *dev = NULL;
  315. items = ListNumItems (devlist);
  316. if (name == NULL)
  317. return dev;
  318. for (i = 1; i <= items; i++) {
  319. dev = ListGetPtrToItem (devlist, i);
  320. if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) {
  321. break;
  322. }
  323. }
  324. return dev;
  325. }
  326. #endif /* CFG_CONSOLE_IS_IN_ENV || CONFIG_SPLASH_SCREEN */
  327. #ifdef CFG_CONSOLE_IS_IN_ENV
  328. /* Called after the relocation - use desired console functions */
  329. int console_init_r (void)
  330. {
  331. char *stdinname, *stdoutname, *stderrname;
  332. device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  333. #ifdef CFG_CONSOLE_ENV_OVERWRITE
  334. int i;
  335. #endif /* CFG_CONSOLE_ENV_OVERWRITE */
  336. /* set default handlers at first */
  337. gd->jt[XF_getc] = serial_getc;
  338. gd->jt[XF_tstc] = serial_tstc;
  339. gd->jt[XF_putc] = serial_putc;
  340. gd->jt[XF_puts] = serial_puts;
  341. gd->jt[XF_printf] = serial_printf;
  342. /* stdin stdout and stderr are in environment */
  343. /* scan for it */
  344. stdinname = getenv ("stdin");
  345. stdoutname = getenv ("stdout");
  346. stderrname = getenv ("stderr");
  347. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  348. inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
  349. outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
  350. errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
  351. }
  352. /* if the devices are overwritten or not found, use default device */
  353. if (inputdev == NULL) {
  354. inputdev = search_device (DEV_FLAGS_INPUT, "serial");
  355. }
  356. if (outputdev == NULL) {
  357. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  358. }
  359. if (errdev == NULL) {
  360. errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  361. }
  362. /* Initializes output console first */
  363. if (outputdev != NULL) {
  364. console_setfile (stdout, outputdev);
  365. }
  366. if (errdev != NULL) {
  367. console_setfile (stderr, errdev);
  368. }
  369. if (inputdev != NULL) {
  370. console_setfile (stdin, inputdev);
  371. }
  372. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  373. #ifndef CFG_CONSOLE_INFO_QUIET
  374. /* Print information */
  375. puts ("In: ");
  376. if (stdio_devices[stdin] == NULL) {
  377. puts ("No input devices available!\n");
  378. } else {
  379. printf ("%s\n", stdio_devices[stdin]->name);
  380. }
  381. puts ("Out: ");
  382. if (stdio_devices[stdout] == NULL) {
  383. puts ("No output devices available!\n");
  384. } else {
  385. printf ("%s\n", stdio_devices[stdout]->name);
  386. }
  387. puts ("Err: ");
  388. if (stdio_devices[stderr] == NULL) {
  389. puts ("No error devices available!\n");
  390. } else {
  391. printf ("%s\n", stdio_devices[stderr]->name);
  392. }
  393. #endif /* CFG_CONSOLE_INFO_QUIET */
  394. #ifdef CFG_CONSOLE_ENV_OVERWRITE
  395. /* set the environment variables (will overwrite previous env settings) */
  396. for (i = 0; i < 3; i++) {
  397. setenv (stdio_names[i], stdio_devices[i]->name);
  398. }
  399. #endif /* CFG_CONSOLE_ENV_OVERWRITE */
  400. #if 0
  401. /* If nothing usable installed, use only the initial console */
  402. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  403. return (0);
  404. #endif
  405. return (0);
  406. }
  407. #else /* CFG_CONSOLE_IS_IN_ENV */
  408. /* Called after the relocation - use desired console functions */
  409. int console_init_r (void)
  410. {
  411. device_t *inputdev = NULL, *outputdev = NULL;
  412. int i, items = ListNumItems (devlist);
  413. #ifdef CONFIG_SPLASH_SCREEN
  414. /* suppress all output if splash screen is enabled and we have
  415. a bmp to display */
  416. if (getenv("splashimage") != NULL)
  417. gd->flags |= GD_FLG_SILENT;
  418. #endif
  419. /* Scan devices looking for input and output devices */
  420. for (i = 1;
  421. (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
  422. i++
  423. ) {
  424. device_t *dev = ListGetPtrToItem (devlist, i);
  425. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  426. inputdev = dev;
  427. }
  428. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  429. outputdev = dev;
  430. }
  431. }
  432. /* Initializes output console first */
  433. if (outputdev != NULL) {
  434. console_setfile (stdout, outputdev);
  435. console_setfile (stderr, outputdev);
  436. }
  437. /* Initializes input console */
  438. if (inputdev != NULL) {
  439. console_setfile (stdin, inputdev);
  440. }
  441. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  442. #ifndef CFG_CONSOLE_INFO_QUIET
  443. /* Print information */
  444. puts ("In: ");
  445. if (stdio_devices[stdin] == NULL) {
  446. puts ("No input devices available!\n");
  447. } else {
  448. printf ("%s\n", stdio_devices[stdin]->name);
  449. }
  450. puts ("Out: ");
  451. if (stdio_devices[stdout] == NULL) {
  452. puts ("No output devices available!\n");
  453. } else {
  454. printf ("%s\n", stdio_devices[stdout]->name);
  455. }
  456. puts ("Err: ");
  457. if (stdio_devices[stderr] == NULL) {
  458. puts ("No error devices available!\n");
  459. } else {
  460. printf ("%s\n", stdio_devices[stderr]->name);
  461. }
  462. #endif /* CFG_CONSOLE_INFO_QUIET */
  463. /* Setting environment variables */
  464. for (i = 0; i < 3; i++) {
  465. setenv (stdio_names[i], stdio_devices[i]->name);
  466. }
  467. #if 0
  468. /* If nothing usable installed, use only the initial console */
  469. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  470. return (0);
  471. #endif
  472. return (0);
  473. }
  474. #endif /* CFG_CONSOLE_IS_IN_ENV */