console.c 12 KB

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