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 <syscall.h>
  28. void **syscall_tbl;
  29. #ifdef CONFIG_AMIGAONEG3SE
  30. int console_changed = 0;
  31. #endif
  32. #ifdef CFG_CONSOLE_IS_IN_ENV
  33. /*
  34. * if overwrite_console returns 1, the stdin, stderr and stdout
  35. * are switched to the serial port, else the settings in the
  36. * environment are used
  37. */
  38. #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
  39. extern int overwrite_console (void);
  40. #else
  41. int overwrite_console (void)
  42. {
  43. return (0);
  44. }
  45. #endif /* CFG_CONSOLE_OVERWRITE_ROUTINE */
  46. #endif /* CFG_CONSOLE_IS_IN_ENV */
  47. static int console_setfile (int file, device_t * dev)
  48. {
  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. syscall_tbl[SYSCALL_GETC] = dev->getc;
  72. syscall_tbl[SYSCALL_TSTC] = dev->tstc;
  73. break;
  74. case stdout:
  75. syscall_tbl[SYSCALL_PUTC] = dev->putc;
  76. syscall_tbl[SYSCALL_PUTS] = dev->puts;
  77. syscall_tbl[SYSCALL_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. char *stdinname, *stdoutname, *stderrname;
  328. device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  329. /* set default handlers at first */
  330. syscall_tbl[SYSCALL_GETC] = serial_getc;
  331. syscall_tbl[SYSCALL_TSTC] = serial_tstc;
  332. syscall_tbl[SYSCALL_PUTC] = serial_putc;
  333. syscall_tbl[SYSCALL_PUTS] = serial_puts;
  334. syscall_tbl[SYSCALL_PRINTF] = serial_printf;
  335. /* stdin stdout and stderr are in environment */
  336. /* scan for it */
  337. stdinname = getenv ("stdin");
  338. stdoutname = getenv ("stdout");
  339. stderrname = getenv ("stderr");
  340. if (overwrite_console () == 0) { /* if not overwritten by config switch */
  341. inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
  342. outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
  343. errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
  344. }
  345. /* if the devices are overwritten or not found, use default device */
  346. if (inputdev == NULL) {
  347. inputdev = search_device (DEV_FLAGS_INPUT, "serial");
  348. }
  349. if (outputdev == NULL) {
  350. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  351. }
  352. if (errdev == NULL) {
  353. errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  354. }
  355. /* Initializes output console first */
  356. if (outputdev != NULL) {
  357. console_setfile (stdout, outputdev);
  358. }
  359. if (errdev != NULL) {
  360. console_setfile (stderr, errdev);
  361. }
  362. if (inputdev != NULL) {
  363. console_setfile (stdin, inputdev);
  364. }
  365. #ifndef CFG_CONSOLE_INFO_QUIET
  366. /* Print information */
  367. printf ("In: ");
  368. if (stdio_devices[stdin] == NULL) {
  369. printf ("No input devices available!\n");
  370. } else {
  371. printf ("%s\n", stdio_devices[stdin]->name);
  372. }
  373. printf ("Out: ");
  374. if (stdio_devices[stdout] == NULL) {
  375. printf ("No output devices available!\n");
  376. } else {
  377. printf ("%s\n", stdio_devices[stdout]->name);
  378. }
  379. printf ("Err: ");
  380. if (stdio_devices[stderr] == NULL) {
  381. printf ("No error devices available!\n");
  382. } else {
  383. printf ("%s\n", stdio_devices[stderr]->name);
  384. }
  385. #endif /* CFG_CONSOLE_INFO_QUIET */
  386. #ifdef CFG_CONSOLE_ENV_OVERWRITE
  387. /* set the environment variables (will overwrite previous env settings) */
  388. for (i = 0; i < 3; i++) {
  389. setenv (stdio_names[i], stdio_devices[i]->name);
  390. }
  391. #endif /* CFG_CONSOLE_ENV_OVERWRITE */
  392. #if 0
  393. /* If nothing usable installed, use only the initial console */
  394. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  395. return (0);
  396. #endif
  397. return (0);
  398. }
  399. #else /* CFG_CONSOLE_IS_IN_ENV */
  400. /* Called after the relocation - use desired console functions */
  401. int console_init_r (void)
  402. {
  403. device_t *inputdev = NULL, *outputdev = NULL;
  404. int i, items = ListNumItems (devlist);
  405. #ifdef CONFIG_SPLASH_SCREEN
  406. /* suppress all output if splash screen is enabled */
  407. outputdev = search_device (DEV_FLAGS_OUTPUT, "nulldev");
  408. #endif
  409. /* Scan devices looking for input and output devices */
  410. for (i = 1;
  411. (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
  412. i++
  413. ) {
  414. device_t *dev = ListGetPtrToItem (devlist, i);
  415. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  416. inputdev = dev;
  417. }
  418. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  419. outputdev = dev;
  420. }
  421. }
  422. /* Initializes output console first */
  423. if (outputdev != NULL) {
  424. console_setfile (stdout, outputdev);
  425. console_setfile (stderr, outputdev);
  426. }
  427. /* Initializes input console */
  428. if (inputdev != NULL) {
  429. console_setfile (stdin, inputdev);
  430. }
  431. #ifndef CFG_CONSOLE_INFO_QUIET
  432. /* Print information */
  433. printf ("In: ");
  434. if (stdio_devices[stdin] == NULL) {
  435. printf ("No input devices available!\n");
  436. } else {
  437. printf ("%s\n", stdio_devices[stdin]->name);
  438. }
  439. printf ("Out: ");
  440. if (stdio_devices[stdout] == NULL) {
  441. printf ("No output devices available!\n");
  442. } else {
  443. printf ("%s\n", stdio_devices[stdout]->name);
  444. }
  445. printf ("Err: ");
  446. if (stdio_devices[stderr] == NULL) {
  447. printf ("No error devices available!\n");
  448. } else {
  449. printf ("%s\n", stdio_devices[stderr]->name);
  450. }
  451. #endif /* CFG_CONSOLE_INFO_QUIET */
  452. /* Setting environment variables */
  453. for (i = 0; i < 3; i++) {
  454. setenv (stdio_names[i], stdio_devices[i]->name);
  455. }
  456. #if 0
  457. /* If nothing usable installed, use only the initial console */
  458. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  459. return (0);
  460. #endif
  461. return (0);
  462. }
  463. #endif /* CFG_CONSOLE_IS_IN_ENV */