console.c 12 KB

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