console.c 12 KB

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