console.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. /* test if ctrl-c was pressed */
  194. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  195. static int ctrlc_was_pressed = 0;
  196. int ctrlc (void)
  197. {
  198. DECLARE_GLOBAL_DATA_PTR;
  199. if (!ctrlc_disabled && gd->have_console) {
  200. if (tstc ()) {
  201. switch (getc ()) {
  202. case 0x03: /* ^C - Control C */
  203. ctrlc_was_pressed = 1;
  204. return 1;
  205. default:
  206. break;
  207. }
  208. }
  209. }
  210. return 0;
  211. }
  212. /* pass 1 to disable ctrlc() checking, 0 to enable.
  213. * returns previous state
  214. */
  215. int disable_ctrlc (int disable)
  216. {
  217. int prev = ctrlc_disabled; /* save previous state */
  218. ctrlc_disabled = disable;
  219. return prev;
  220. }
  221. int had_ctrlc (void)
  222. {
  223. return ctrlc_was_pressed;
  224. }
  225. void clear_ctrlc (void)
  226. {
  227. ctrlc_was_pressed = 0;
  228. }
  229. #ifdef CONFIG_MODEM_SUPPORT_DEBUG
  230. char screen[1024];
  231. char *cursor = screen;
  232. int once = 0;
  233. inline void dbg(const char *fmt, ...)
  234. {
  235. va_list args;
  236. uint i;
  237. char printbuffer[CFG_PBSIZE];
  238. if (!once) {
  239. memset(screen, 0, sizeof(screen));
  240. once++;
  241. }
  242. va_start(args, fmt);
  243. /* For this to work, printbuffer must be larger than
  244. * anything we ever want to print.
  245. */
  246. i = vsprintf(printbuffer, fmt, args);
  247. va_end(args);
  248. if ((screen + sizeof(screen) - 1 - cursor) < strlen(printbuffer)+1) {
  249. memset(screen, 0, sizeof(screen));
  250. cursor = screen;
  251. }
  252. sprintf(cursor, printbuffer);
  253. cursor += strlen(printbuffer);
  254. }
  255. #else
  256. inline void dbg(const char *fmt, ...)
  257. {
  258. }
  259. #endif
  260. /** U-Boot INIT FUNCTIONS *************************************************/
  261. int console_assign (int file, char *devname)
  262. {
  263. int flag, i;
  264. /* Check for valid file */
  265. switch (file) {
  266. case stdin:
  267. flag = DEV_FLAGS_INPUT;
  268. break;
  269. case stdout:
  270. case stderr:
  271. flag = DEV_FLAGS_OUTPUT;
  272. break;
  273. default:
  274. return -1;
  275. }
  276. /* Check for valid device name */
  277. for (i = 1; i <= ListNumItems (devlist); i++) {
  278. device_t *dev = ListGetPtrToItem (devlist, i);
  279. if (strcmp (devname, dev->name) == 0) {
  280. if (dev->flags & flag)
  281. return console_setfile (file, dev);
  282. return -1;
  283. }
  284. }
  285. return -1;
  286. }
  287. /* Called before relocation - use serial functions */
  288. int console_init_f (void)
  289. {
  290. DECLARE_GLOBAL_DATA_PTR;
  291. gd->have_console = 1;
  292. return (0);
  293. }
  294. #ifdef CFG_CONSOLE_IS_IN_ENV
  295. /* search a device */
  296. device_t *search_device (int flags, char *name)
  297. {
  298. int i, items;
  299. device_t *dev = NULL;
  300. items = ListNumItems (devlist);
  301. if (name == NULL)
  302. return dev;
  303. for (i = 1; i <= items; i++) {
  304. dev = ListGetPtrToItem (devlist, i);
  305. if ((dev->flags & flags) && (strcmp (name, dev->name) == 0)) {
  306. break;
  307. }
  308. }
  309. return dev;
  310. }
  311. #endif /* CFG_CONSOLE_IS_IN_ENV */
  312. #ifdef CFG_CONSOLE_IS_IN_ENV
  313. /* Called after the relocation - use desired console functions */
  314. int console_init_r (void)
  315. {
  316. char *stdinname, *stdoutname, *stderrname;
  317. device_t *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  318. /* set default handlers at first */
  319. syscall_tbl[SYSCALL_GETC] = serial_getc;
  320. syscall_tbl[SYSCALL_TSTC] = serial_tstc;
  321. syscall_tbl[SYSCALL_PUTC] = serial_putc;
  322. syscall_tbl[SYSCALL_PUTS] = serial_puts;
  323. syscall_tbl[SYSCALL_PRINTF] = serial_printf;
  324. /* stdin stdout and stderr are in environment */
  325. /* scan for it */
  326. stdinname = getenv ("stdin");
  327. stdoutname = getenv ("stdout");
  328. stderrname = getenv ("stderr");
  329. if (overwrite_console () == 0) { /* if not overwritten by config switch */
  330. inputdev = search_device (DEV_FLAGS_INPUT, stdinname);
  331. outputdev = search_device (DEV_FLAGS_OUTPUT, stdoutname);
  332. errdev = search_device (DEV_FLAGS_OUTPUT, stderrname);
  333. }
  334. /* if the devices are overwritten or not found, use default device */
  335. if (inputdev == NULL) {
  336. inputdev = search_device (DEV_FLAGS_INPUT, "serial");
  337. }
  338. if (outputdev == NULL) {
  339. outputdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  340. }
  341. if (errdev == NULL) {
  342. errdev = search_device (DEV_FLAGS_OUTPUT, "serial");
  343. }
  344. /* Initializes output console first */
  345. if (outputdev != NULL) {
  346. console_setfile (stdout, outputdev);
  347. }
  348. if (errdev != NULL) {
  349. console_setfile (stderr, errdev);
  350. }
  351. if (inputdev != NULL) {
  352. console_setfile (stdin, inputdev);
  353. }
  354. #ifndef CFG_CONSOLE_INFO_QUIET
  355. /* Print information */
  356. printf ("In: ");
  357. if (stdio_devices[stdin] == NULL) {
  358. printf ("No input devices available!\n");
  359. } else {
  360. printf ("%s\n", stdio_devices[stdin]->name);
  361. }
  362. printf ("Out: ");
  363. if (stdio_devices[stdout] == NULL) {
  364. printf ("No output devices available!\n");
  365. } else {
  366. printf ("%s\n", stdio_devices[stdout]->name);
  367. }
  368. printf ("Err: ");
  369. if (stdio_devices[stderr] == NULL) {
  370. printf ("No error devices available!\n");
  371. } else {
  372. printf ("%s\n", stdio_devices[stderr]->name);
  373. }
  374. #endif /* CFG_CONSOLE_INFO_QUIET */
  375. #ifdef CFG_CONSOLE_ENV_OVERWRITE
  376. /* set the environment variables (will overwrite previous env settings) */
  377. for (i = 0; i < 3; i++) {
  378. setenv (stdio_names[i], stdio_devices[i]->name);
  379. }
  380. #endif /* CFG_CONSOLE_ENV_OVERWRITE */
  381. #if 0
  382. /* If nothing usable installed, use only the initial console */
  383. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  384. return (0);
  385. #endif
  386. return (0);
  387. }
  388. #else /* CFG_CONSOLE_IS_IN_ENV */
  389. /* Called after the relocation - use desired console functions */
  390. int console_init_r (void)
  391. {
  392. device_t *inputdev = NULL, *outputdev = NULL;
  393. int i, items = ListNumItems (devlist);
  394. /* Scan devices looking for input and output devices */
  395. for (i = 1;
  396. (i <= items) && ((inputdev == NULL) || (outputdev == NULL));
  397. i++
  398. ) {
  399. device_t *dev = ListGetPtrToItem (devlist, i);
  400. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  401. inputdev = dev;
  402. }
  403. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  404. outputdev = dev;
  405. }
  406. }
  407. /* Initializes output console first */
  408. if (outputdev != NULL) {
  409. console_setfile (stdout, outputdev);
  410. console_setfile (stderr, outputdev);
  411. }
  412. /* Initializes input console */
  413. if (inputdev != NULL) {
  414. console_setfile (stdin, inputdev);
  415. }
  416. #ifndef CFG_CONSOLE_INFO_QUIET
  417. /* Print information */
  418. printf ("In: ");
  419. if (stdio_devices[stdin] == NULL) {
  420. printf ("No input devices available!\n");
  421. } else {
  422. printf ("%s\n", stdio_devices[stdin]->name);
  423. }
  424. printf ("Out: ");
  425. if (stdio_devices[stdout] == NULL) {
  426. printf ("No output devices available!\n");
  427. } else {
  428. printf ("%s\n", stdio_devices[stdout]->name);
  429. }
  430. printf ("Err: ");
  431. if (stdio_devices[stderr] == NULL) {
  432. printf ("No error devices available!\n");
  433. } else {
  434. printf ("%s\n", stdio_devices[stderr]->name);
  435. }
  436. #endif /* CFG_CONSOLE_INFO_QUIET */
  437. /* Setting environment variables */
  438. for (i = 0; i < 3; i++) {
  439. setenv (stdio_names[i], stdio_devices[i]->name);
  440. }
  441. #if 0
  442. /* If nothing usable installed, use only the initial console */
  443. if ((stdio_devices[stdin] == NULL) && (stdio_devices[stdout] == NULL))
  444. return (0);
  445. #endif
  446. return (0);
  447. }
  448. #endif /* CFG_CONSOLE_IS_IN_ENV */