console.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. #ifdef CFG_CONSOLE_ENV_OVERWRITE
  343. int i;
  344. #endif /* CFG_CONSOLE_ENV_OVERWRITE */
  345. /* set default handlers at first */
  346. gd->jt[XF_getc] = serial_getc;
  347. gd->jt[XF_tstc] = serial_tstc;
  348. gd->jt[XF_putc] = serial_putc;
  349. gd->jt[XF_puts] = serial_puts;
  350. gd->jt[XF_printf] = serial_printf;
  351. /* stdin stdout and stderr are in environment */
  352. /* scan for it */
  353. stdinname = getenv ("stdin");
  354. stdoutname = getenv ("stdout");
  355. stderrname = getenv ("stderr");
  356. if (overwrite_console () == 0) { /* if not overwritten by config switch */
  357. inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
  358. outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
  359. errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
  360. }
  361. /* if the devices are overwritten or not found, use default device */
  362. if (inputdev == NULL) {
  363. inputdev = search_device (DEV_FLAGS_INPUT, "serial");
  364. }
  365. if (outputdev == NULL) {
  366. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  367. }
  368. if (errdev == NULL) {
  369. errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  370. }
  371. /* Initializes output console first */
  372. if (outputdev != NULL) {
  373. console_setfile (stdout, outputdev);
  374. }
  375. if (errdev != NULL) {
  376. console_setfile (stderr, errdev);
  377. }
  378. if (inputdev != NULL) {
  379. console_setfile (stdin, inputdev);
  380. }
  381. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  382. #ifndef CFG_CONSOLE_INFO_QUIET
  383. /* Print information */
  384. puts ("In: ");
  385. if (stdio_devices[stdin] == NULL) {
  386. puts ("No input devices available!\n");
  387. } else {
  388. printf ("%s\n", stdio_devices[stdin]->name);
  389. }
  390. puts ("Out: ");
  391. if (stdio_devices[stdout] == NULL) {
  392. puts ("No output devices available!\n");
  393. } else {
  394. printf ("%s\n", stdio_devices[stdout]->name);
  395. }
  396. puts ("Err: ");
  397. if (stdio_devices[stderr] == NULL) {
  398. puts ("No error devices available!\n");
  399. } else {
  400. printf ("%s\n", stdio_devices[stderr]->name);
  401. }
  402. #endif /* CFG_CONSOLE_INFO_QUIET */
  403. #ifdef CFG_CONSOLE_ENV_OVERWRITE
  404. /* set the environment variables (will overwrite previous env settings) */
  405. for (i = 0; i < 3; i++) {
  406. setenv (stdio_names[i], stdio_devices[i]->name);
  407. }
  408. #endif /* CFG_CONSOLE_ENV_OVERWRITE */
  409. #if 0
  410. /* If nothing usable installed, use only the initial console */
  411. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  412. return (0);
  413. #endif
  414. return (0);
  415. }
  416. #else /* CFG_CONSOLE_IS_IN_ENV */
  417. /* Called after the relocation - use desired console functions */
  418. int console_init_r (void)
  419. {
  420. DECLARE_GLOBAL_DATA_PTR;
  421. device_t *inputdev = NULL, *outputdev = NULL;
  422. int i, items = ListNumItems (devlist);
  423. #ifdef CONFIG_SPLASH_SCREEN
  424. /* suppress all output if splash screen is enabled and we have
  425. a bmp to display */
  426. if (getenv("splashimage") != NULL)
  427. outputdev = search_device (DEV_FLAGS_OUTPUT, "nulldev");
  428. #endif
  429. #ifdef CONFIG_SILENT_CONSOLE
  430. /* Suppress all output if "silent" mode requested */
  431. if (gd->flags & GD_FLG_SILENT)
  432. outputdev = search_device (DEV_FLAGS_OUTPUT, "nulldev");
  433. #endif
  434. /* Scan devices looking for input and output devices */
  435. for (i = 1;
  436. (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
  437. i++
  438. ) {
  439. device_t *dev = ListGetPtrToItem (devlist, i);
  440. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  441. inputdev = dev;
  442. }
  443. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  444. outputdev = dev;
  445. }
  446. }
  447. /* Initializes output console first */
  448. if (outputdev != NULL) {
  449. console_setfile (stdout, outputdev);
  450. console_setfile (stderr, outputdev);
  451. }
  452. /* Initializes input console */
  453. if (inputdev != NULL) {
  454. console_setfile (stdin, inputdev);
  455. }
  456. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  457. #ifndef CFG_CONSOLE_INFO_QUIET
  458. /* Print information */
  459. puts ("In: ");
  460. if (stdio_devices[stdin] == NULL) {
  461. puts ("No input devices available!\n");
  462. } else {
  463. printf ("%s\n", stdio_devices[stdin]->name);
  464. }
  465. puts ("Out: ");
  466. if (stdio_devices[stdout] == NULL) {
  467. puts ("No output devices available!\n");
  468. } else {
  469. printf ("%s\n", stdio_devices[stdout]->name);
  470. }
  471. puts ("Err: ");
  472. if (stdio_devices[stderr] == NULL) {
  473. puts ("No error devices available!\n");
  474. } else {
  475. printf ("%s\n", stdio_devices[stderr]->name);
  476. }
  477. #endif /* CFG_CONSOLE_INFO_QUIET */
  478. /* Setting environment variables */
  479. for (i = 0; i < 3; i++) {
  480. setenv (stdio_names[i], stdio_devices[i]->name);
  481. }
  482. #if 0
  483. /* If nothing usable installed, use only the initial console */
  484. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  485. return (0);
  486. #endif
  487. return (0);
  488. }
  489. #endif /* CFG_CONSOLE_IS_IN_ENV */