console.c 12 KB

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