console.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  5. */
  6. #include <common.h>
  7. #include <console.h>
  8. #include <debug_uart.h>
  9. #include <display_options.h>
  10. #include <dm.h>
  11. #include <env.h>
  12. #include <stdarg.h>
  13. #include <iomux.h>
  14. #include <malloc.h>
  15. #include <mapmem.h>
  16. #include <os.h>
  17. #include <serial.h>
  18. #include <stdio_dev.h>
  19. #include <exports.h>
  20. #include <env_internal.h>
  21. #include <watchdog.h>
  22. #include <asm/global_data.h>
  23. #include <linux/delay.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. static int on_console(const char *name, const char *value, enum env_op op,
  26. int flags)
  27. {
  28. int console = -1;
  29. /* Check for console redirection */
  30. if (strcmp(name, "stdin") == 0)
  31. console = stdin;
  32. else if (strcmp(name, "stdout") == 0)
  33. console = stdout;
  34. else if (strcmp(name, "stderr") == 0)
  35. console = stderr;
  36. /* if not actually setting a console variable, we don't care */
  37. if (console == -1 || (gd->flags & GD_FLG_DEVINIT) == 0)
  38. return 0;
  39. switch (op) {
  40. case env_op_create:
  41. case env_op_overwrite:
  42. if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
  43. if (iomux_doenv(console, value))
  44. return 1;
  45. } else {
  46. /* Try assigning specified device */
  47. if (console_assign(console, value) < 0)
  48. return 1;
  49. }
  50. return 0;
  51. case env_op_delete:
  52. if ((flags & H_FORCE) == 0)
  53. printf("Can't delete \"%s\"\n", name);
  54. return 1;
  55. default:
  56. return 0;
  57. }
  58. }
  59. U_BOOT_ENV_CALLBACK(console, on_console);
  60. #ifdef CONFIG_SILENT_CONSOLE
  61. static int on_silent(const char *name, const char *value, enum env_op op,
  62. int flags)
  63. {
  64. if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_SET))
  65. if (flags & H_INTERACTIVE)
  66. return 0;
  67. if (!CONFIG_IS_ENABLED(SILENT_CONSOLE_UPDATE_ON_RELOC))
  68. if ((flags & H_INTERACTIVE) == 0)
  69. return 0;
  70. if (value != NULL)
  71. gd->flags |= GD_FLG_SILENT;
  72. else
  73. gd->flags &= ~GD_FLG_SILENT;
  74. return 0;
  75. }
  76. U_BOOT_ENV_CALLBACK(silent, on_silent);
  77. #endif
  78. #ifdef CONFIG_CONSOLE_RECORD
  79. /* helper function: access to gd->console_out and gd->console_in */
  80. static void console_record_putc(const char c)
  81. {
  82. if (!(gd->flags & GD_FLG_RECORD))
  83. return;
  84. if (gd->console_out.start &&
  85. !membuff_putbyte((struct membuff *)&gd->console_out, c))
  86. gd->flags |= GD_FLG_RECORD_OVF;
  87. }
  88. static void console_record_puts(const char *s)
  89. {
  90. if (!(gd->flags & GD_FLG_RECORD))
  91. return;
  92. if (gd->console_out.start) {
  93. int len = strlen(s);
  94. if (membuff_put((struct membuff *)&gd->console_out, s, len) !=
  95. len)
  96. gd->flags |= GD_FLG_RECORD_OVF;
  97. }
  98. }
  99. static int console_record_getc(void)
  100. {
  101. if (!(gd->flags & GD_FLG_RECORD))
  102. return -1;
  103. if (!gd->console_in.start)
  104. return -1;
  105. return membuff_getbyte((struct membuff *)&gd->console_in);
  106. }
  107. static int console_record_tstc(void)
  108. {
  109. if (!(gd->flags & GD_FLG_RECORD))
  110. return 0;
  111. if (gd->console_in.start) {
  112. if (membuff_peekbyte((struct membuff *)&gd->console_in) != -1)
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. #else
  118. static void console_record_putc(char c)
  119. {
  120. }
  121. static void console_record_puts(const char *s)
  122. {
  123. }
  124. static int console_record_getc(void)
  125. {
  126. return -1;
  127. }
  128. static int console_record_tstc(void)
  129. {
  130. return 0;
  131. }
  132. #endif
  133. #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
  134. /*
  135. * if overwrite_console returns 1, the stdin, stderr and stdout
  136. * are switched to the serial port, else the settings in the
  137. * environment are used
  138. */
  139. #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
  140. extern int overwrite_console(void);
  141. #define OVERWRITE_CONSOLE overwrite_console()
  142. #else
  143. #define OVERWRITE_CONSOLE 0
  144. #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
  145. #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
  146. static int console_setfile(int file, struct stdio_dev * dev)
  147. {
  148. int error = 0;
  149. if (dev == NULL)
  150. return -1;
  151. switch (file) {
  152. case stdin:
  153. case stdout:
  154. case stderr:
  155. error = console_start(file, dev);
  156. if (error)
  157. break;
  158. /* Assign the new device (leaving the existing one started) */
  159. stdio_devices[file] = dev;
  160. /*
  161. * Update monitor functions
  162. * (to use the console stuff by other applications)
  163. */
  164. switch (file) {
  165. case stdin:
  166. gd->jt->getc = getchar;
  167. gd->jt->tstc = tstc;
  168. break;
  169. case stdout:
  170. gd->jt->putc = putc;
  171. gd->jt->puts = puts;
  172. STDIO_DEV_ASSIGN_FLUSH(gd->jt, flush);
  173. gd->jt->printf = printf;
  174. break;
  175. }
  176. break;
  177. default: /* Invalid file ID */
  178. error = -1;
  179. }
  180. return error;
  181. }
  182. /**
  183. * console_dev_is_serial() - Check if a stdio device is a serial device
  184. *
  185. * @sdev: Device to check
  186. * Return: true if this device is in the serial uclass (or for pre-driver-model,
  187. * whether it is called "serial".
  188. */
  189. static bool console_dev_is_serial(struct stdio_dev *sdev)
  190. {
  191. bool is_serial;
  192. if (IS_ENABLED(CONFIG_DM_SERIAL) && (sdev->flags & DEV_FLAGS_DM)) {
  193. struct udevice *dev = sdev->priv;
  194. is_serial = device_get_uclass_id(dev) == UCLASS_SERIAL;
  195. } else {
  196. is_serial = !strcmp(sdev->name, "serial");
  197. }
  198. return is_serial;
  199. }
  200. #if CONFIG_IS_ENABLED(CONSOLE_MUX)
  201. /** Console I/O multiplexing *******************************************/
  202. /* tstcdev: save the last stdio device with pending characters, with tstc != 0 */
  203. static struct stdio_dev *tstcdev;
  204. struct stdio_dev **console_devices[MAX_FILES];
  205. int cd_count[MAX_FILES];
  206. static void console_devices_set(int file, struct stdio_dev *dev)
  207. {
  208. console_devices[file][0] = dev;
  209. cd_count[file] = 1;
  210. }
  211. /**
  212. * console_needs_start_stop() - check if we need to start or stop the STDIO device
  213. * @file: STDIO file
  214. * @sdev: STDIO device in question
  215. *
  216. * This function checks if we need to start or stop the stdio device used for
  217. * a console. For IOMUX case it simply enforces one time start and one time
  218. * stop of the device independently of how many STDIO files are using it. In
  219. * other words, we start console once before first STDIO device wants it and
  220. * stop after the last is gone.
  221. */
  222. static bool console_needs_start_stop(int file, struct stdio_dev *sdev)
  223. {
  224. int i;
  225. for (i = 0; i < ARRAY_SIZE(cd_count); i++) {
  226. if (i == file)
  227. continue;
  228. if (iomux_match_device(console_devices[i], cd_count[i], sdev) >= 0)
  229. return false;
  230. }
  231. return true;
  232. }
  233. /*
  234. * This depends on tstc() always being called before getchar().
  235. * This is guaranteed to be true because this routine is called
  236. * only from fgetc() which assures it.
  237. * No attempt is made to demultiplex multiple input sources.
  238. */
  239. static int console_getc(int file)
  240. {
  241. unsigned char ret;
  242. /* This is never called with testcdev == NULL */
  243. ret = tstcdev->getc(tstcdev);
  244. tstcdev = NULL;
  245. return ret;
  246. }
  247. /* Upper layer may have already called tstc(): check the saved result */
  248. static bool console_has_tstc(void)
  249. {
  250. return !!tstcdev;
  251. }
  252. static int console_tstc(int file)
  253. {
  254. int i, ret;
  255. struct stdio_dev *dev;
  256. int prev;
  257. prev = disable_ctrlc(1);
  258. for_each_console_dev(i, file, dev) {
  259. if (dev->tstc != NULL) {
  260. ret = dev->tstc(dev);
  261. if (ret > 0) {
  262. tstcdev = dev;
  263. disable_ctrlc(prev);
  264. return ret;
  265. }
  266. }
  267. }
  268. disable_ctrlc(prev);
  269. return 0;
  270. }
  271. static void console_putc(int file, const char c)
  272. {
  273. int i;
  274. struct stdio_dev *dev;
  275. for_each_console_dev(i, file, dev) {
  276. if (dev->putc != NULL)
  277. dev->putc(dev, c);
  278. }
  279. }
  280. /**
  281. * console_puts_select() - Output a string to all console devices
  282. *
  283. * @file: File number to output to (e,g, stdout, see stdio.h)
  284. * @serial_only: true to output only to serial, false to output to everything
  285. * else
  286. * @s: String to output
  287. */
  288. static void console_puts_select(int file, bool serial_only, const char *s)
  289. {
  290. int i;
  291. struct stdio_dev *dev;
  292. for_each_console_dev(i, file, dev) {
  293. bool is_serial = console_dev_is_serial(dev);
  294. if (dev->puts && serial_only == is_serial)
  295. dev->puts(dev, s);
  296. }
  297. }
  298. void console_puts_select_stderr(bool serial_only, const char *s)
  299. {
  300. if (gd->flags & GD_FLG_DEVINIT)
  301. console_puts_select(stderr, serial_only, s);
  302. }
  303. static void console_puts(int file, const char *s)
  304. {
  305. int i;
  306. struct stdio_dev *dev;
  307. for_each_console_dev(i, file, dev) {
  308. if (dev->puts != NULL)
  309. dev->puts(dev, s);
  310. }
  311. }
  312. #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
  313. static void console_flush(int file)
  314. {
  315. int i;
  316. struct stdio_dev *dev;
  317. for_each_console_dev(i, file, dev) {
  318. if (dev->flush != NULL)
  319. dev->flush(dev);
  320. }
  321. }
  322. #endif
  323. #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
  324. static inline void console_doenv(int file, struct stdio_dev *dev)
  325. {
  326. iomux_doenv(file, dev->name);
  327. }
  328. #endif
  329. #else
  330. static void console_devices_set(int file, struct stdio_dev *dev)
  331. {
  332. }
  333. static inline bool console_needs_start_stop(int file, struct stdio_dev *sdev)
  334. {
  335. return true;
  336. }
  337. static inline int console_getc(int file)
  338. {
  339. return stdio_devices[file]->getc(stdio_devices[file]);
  340. }
  341. static bool console_has_tstc(void)
  342. {
  343. return false;
  344. }
  345. static inline int console_tstc(int file)
  346. {
  347. return stdio_devices[file]->tstc(stdio_devices[file]);
  348. }
  349. static inline void console_putc(int file, const char c)
  350. {
  351. stdio_devices[file]->putc(stdio_devices[file], c);
  352. }
  353. void console_puts_select(int file, bool serial_only, const char *s)
  354. {
  355. if ((gd->flags & GD_FLG_DEVINIT) &&
  356. serial_only == console_dev_is_serial(stdio_devices[file]))
  357. stdio_devices[file]->puts(stdio_devices[file], s);
  358. }
  359. static inline void console_puts(int file, const char *s)
  360. {
  361. stdio_devices[file]->puts(stdio_devices[file], s);
  362. }
  363. #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
  364. static inline void console_flush(int file)
  365. {
  366. if (stdio_devices[file]->flush)
  367. stdio_devices[file]->flush(stdio_devices[file]);
  368. }
  369. #endif
  370. #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
  371. static inline void console_doenv(int file, struct stdio_dev *dev)
  372. {
  373. console_setfile(file, dev);
  374. }
  375. #endif
  376. #endif /* CONIFIG_IS_ENABLED(CONSOLE_MUX) */
  377. static void __maybe_unused console_setfile_and_devices(int file, struct stdio_dev *dev)
  378. {
  379. console_setfile(file, dev);
  380. console_devices_set(file, dev);
  381. }
  382. int console_start(int file, struct stdio_dev *sdev)
  383. {
  384. int error;
  385. if (!console_needs_start_stop(file, sdev))
  386. return 0;
  387. /* Start new device */
  388. if (sdev->start) {
  389. error = sdev->start(sdev);
  390. /* If it's not started don't use it */
  391. if (error < 0)
  392. return error;
  393. }
  394. return 0;
  395. }
  396. void console_stop(int file, struct stdio_dev *sdev)
  397. {
  398. if (!console_needs_start_stop(file, sdev))
  399. return;
  400. if (sdev->stop)
  401. sdev->stop(sdev);
  402. }
  403. /** U-Boot INITIAL CONSOLE-NOT COMPATIBLE FUNCTIONS *************************/
  404. int serial_printf(const char *fmt, ...)
  405. {
  406. va_list args;
  407. uint i;
  408. char printbuffer[CONFIG_SYS_PBSIZE];
  409. va_start(args, fmt);
  410. /* For this to work, printbuffer must be larger than
  411. * anything we ever want to print.
  412. */
  413. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  414. va_end(args);
  415. serial_puts(printbuffer);
  416. return i;
  417. }
  418. int fgetc(int file)
  419. {
  420. if ((unsigned int)file < MAX_FILES) {
  421. /*
  422. * Effectively poll for input wherever it may be available.
  423. */
  424. for (;;) {
  425. schedule();
  426. if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
  427. /*
  428. * Upper layer may have already called tstc() so
  429. * check for that first.
  430. */
  431. if (console_has_tstc())
  432. return console_getc(file);
  433. console_tstc(file);
  434. } else {
  435. if (console_tstc(file))
  436. return console_getc(file);
  437. }
  438. /*
  439. * If the watchdog must be rate-limited then it should
  440. * already be handled in board-specific code.
  441. */
  442. if (IS_ENABLED(CONFIG_WATCHDOG))
  443. udelay(1);
  444. }
  445. }
  446. return -1;
  447. }
  448. int ftstc(int file)
  449. {
  450. if ((unsigned int)file < MAX_FILES)
  451. return console_tstc(file);
  452. return -1;
  453. }
  454. void fputc(int file, const char c)
  455. {
  456. if ((unsigned int)file < MAX_FILES)
  457. console_putc(file, c);
  458. }
  459. void fputs(int file, const char *s)
  460. {
  461. if ((unsigned int)file < MAX_FILES)
  462. console_puts(file, s);
  463. }
  464. #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
  465. void fflush(int file)
  466. {
  467. if ((unsigned int)file < MAX_FILES)
  468. console_flush(file);
  469. }
  470. #endif
  471. int fprintf(int file, const char *fmt, ...)
  472. {
  473. va_list args;
  474. uint i;
  475. char printbuffer[CONFIG_SYS_PBSIZE];
  476. va_start(args, fmt);
  477. /* For this to work, printbuffer must be larger than
  478. * anything we ever want to print.
  479. */
  480. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  481. va_end(args);
  482. /* Send to desired file */
  483. fputs(file, printbuffer);
  484. return i;
  485. }
  486. /** U-Boot INITIAL CONSOLE-COMPATIBLE FUNCTION *****************************/
  487. int getchar(void)
  488. {
  489. int ch;
  490. if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
  491. return 0;
  492. if (!gd->have_console)
  493. return 0;
  494. ch = console_record_getc();
  495. if (ch != -1)
  496. return ch;
  497. if (gd->flags & GD_FLG_DEVINIT) {
  498. /* Get from the standard input */
  499. return fgetc(stdin);
  500. }
  501. /* Send directly to the handler */
  502. return serial_getc();
  503. }
  504. int tstc(void)
  505. {
  506. if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
  507. return 0;
  508. if (!gd->have_console)
  509. return 0;
  510. if (console_record_tstc())
  511. return 1;
  512. if (gd->flags & GD_FLG_DEVINIT) {
  513. /* Test the standard input */
  514. return ftstc(stdin);
  515. }
  516. /* Send directly to the handler */
  517. return serial_tstc();
  518. }
  519. #define PRE_CONSOLE_FLUSHPOINT1_SERIAL 0
  520. #define PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL 1
  521. #if CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  522. #define CIRC_BUF_IDX(idx) ((idx) % (unsigned long)CONFIG_VAL(PRE_CON_BUF_SZ))
  523. static void pre_console_putc(const char c)
  524. {
  525. char *buffer;
  526. if (gd->precon_buf_idx < 0)
  527. return;
  528. buffer = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ));
  529. buffer[CIRC_BUF_IDX(gd->precon_buf_idx++)] = c;
  530. unmap_sysmem(buffer);
  531. }
  532. static void pre_console_puts(const char *s)
  533. {
  534. if (gd->precon_buf_idx < 0)
  535. return;
  536. while (*s)
  537. pre_console_putc(*s++);
  538. }
  539. static void print_pre_console_buffer(int flushpoint)
  540. {
  541. long in = 0, out = 0;
  542. char buf_out[CONFIG_VAL(PRE_CON_BUF_SZ) + 1];
  543. char *buf_in;
  544. if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
  545. return;
  546. buf_in = map_sysmem(CONFIG_VAL(PRE_CON_BUF_ADDR), CONFIG_VAL(PRE_CON_BUF_SZ));
  547. if (gd->precon_buf_idx > CONFIG_VAL(PRE_CON_BUF_SZ))
  548. in = gd->precon_buf_idx - CONFIG_VAL(PRE_CON_BUF_SZ);
  549. while (in < gd->precon_buf_idx)
  550. buf_out[out++] = buf_in[CIRC_BUF_IDX(in++)];
  551. unmap_sysmem(buf_in);
  552. buf_out[out] = 0;
  553. gd->precon_buf_idx = -1;
  554. switch (flushpoint) {
  555. case PRE_CONSOLE_FLUSHPOINT1_SERIAL:
  556. puts(buf_out);
  557. break;
  558. case PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL:
  559. console_puts_select(stdout, false, buf_out);
  560. break;
  561. }
  562. gd->precon_buf_idx = in;
  563. }
  564. #else
  565. static inline void pre_console_putc(const char c) {}
  566. static inline void pre_console_puts(const char *s) {}
  567. static inline void print_pre_console_buffer(int flushpoint) {}
  568. #endif
  569. void putc(const char c)
  570. {
  571. if (!gd)
  572. return;
  573. console_record_putc(c);
  574. /* sandbox can send characters to stdout before it has a console */
  575. if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
  576. os_putc(c);
  577. return;
  578. }
  579. /* if we don't have a console yet, use the debug UART */
  580. if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
  581. printch(c);
  582. return;
  583. }
  584. if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
  585. if (!(gd->flags & GD_FLG_DEVINIT))
  586. pre_console_putc(c);
  587. return;
  588. }
  589. if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
  590. return;
  591. if (!gd->have_console)
  592. return pre_console_putc(c);
  593. if (gd->flags & GD_FLG_DEVINIT) {
  594. /* Send to the standard output */
  595. fputc(stdout, c);
  596. } else {
  597. /* Send directly to the handler */
  598. pre_console_putc(c);
  599. serial_putc(c);
  600. }
  601. }
  602. void puts(const char *s)
  603. {
  604. if (!gd)
  605. return;
  606. console_record_puts(s);
  607. /* sandbox can send characters to stdout before it has a console */
  608. if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
  609. os_puts(s);
  610. return;
  611. }
  612. if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY)) {
  613. while (*s) {
  614. int ch = *s++;
  615. printch(ch);
  616. }
  617. return;
  618. }
  619. if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT)) {
  620. if (!(gd->flags & GD_FLG_DEVINIT))
  621. pre_console_puts(s);
  622. return;
  623. }
  624. if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
  625. return;
  626. if (!gd->have_console)
  627. return pre_console_puts(s);
  628. if (gd->flags & GD_FLG_DEVINIT) {
  629. /* Send to the standard output */
  630. fputs(stdout, s);
  631. } else {
  632. /* Send directly to the handler */
  633. pre_console_puts(s);
  634. serial_puts(s);
  635. }
  636. }
  637. #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
  638. void flush(void)
  639. {
  640. if (!gd)
  641. return;
  642. /* sandbox can send characters to stdout before it has a console */
  643. if (IS_ENABLED(CONFIG_SANDBOX) && !(gd->flags & GD_FLG_SERIAL_READY)) {
  644. os_flush();
  645. return;
  646. }
  647. if (IS_ENABLED(CONFIG_DEBUG_UART) && !(gd->flags & GD_FLG_SERIAL_READY))
  648. return;
  649. if (IS_ENABLED(CONFIG_SILENT_CONSOLE) && (gd->flags & GD_FLG_SILENT))
  650. return;
  651. if (IS_ENABLED(CONFIG_DISABLE_CONSOLE) && (gd->flags & GD_FLG_DISABLE_CONSOLE))
  652. return;
  653. if (!gd->have_console)
  654. return;
  655. if (gd->flags & GD_FLG_DEVINIT) {
  656. /* Send to the standard output */
  657. fflush(stdout);
  658. } else {
  659. /* Send directly to the handler */
  660. serial_flush();
  661. }
  662. }
  663. #endif
  664. #ifdef CONFIG_CONSOLE_RECORD
  665. int console_record_init(void)
  666. {
  667. int ret;
  668. ret = membuff_new((struct membuff *)&gd->console_out,
  669. gd->flags & GD_FLG_RELOC ?
  670. CONFIG_CONSOLE_RECORD_OUT_SIZE :
  671. CONFIG_CONSOLE_RECORD_OUT_SIZE_F);
  672. if (ret)
  673. return ret;
  674. ret = membuff_new((struct membuff *)&gd->console_in,
  675. CONFIG_CONSOLE_RECORD_IN_SIZE);
  676. return ret;
  677. }
  678. void console_record_reset(void)
  679. {
  680. membuff_purge((struct membuff *)&gd->console_out);
  681. membuff_purge((struct membuff *)&gd->console_in);
  682. gd->flags &= ~GD_FLG_RECORD_OVF;
  683. }
  684. int console_record_reset_enable(void)
  685. {
  686. console_record_reset();
  687. gd->flags |= GD_FLG_RECORD;
  688. return 0;
  689. }
  690. int console_record_readline(char *str, int maxlen)
  691. {
  692. if (gd->flags & GD_FLG_RECORD_OVF)
  693. return -ENOSPC;
  694. return membuff_readline((struct membuff *)&gd->console_out, str,
  695. maxlen, '\0');
  696. }
  697. int console_record_avail(void)
  698. {
  699. return membuff_avail((struct membuff *)&gd->console_out);
  700. }
  701. int console_in_puts(const char *str)
  702. {
  703. return membuff_put((struct membuff *)&gd->console_in, str, strlen(str));
  704. }
  705. #endif
  706. /* test if ctrl-c was pressed */
  707. static int ctrlc_disabled = 0; /* see disable_ctrl() */
  708. static int ctrlc_was_pressed = 0;
  709. int ctrlc(void)
  710. {
  711. if (!ctrlc_disabled && gd->have_console) {
  712. if (tstc()) {
  713. switch (getchar()) {
  714. case 0x03: /* ^C - Control C */
  715. ctrlc_was_pressed = 1;
  716. return 1;
  717. default:
  718. break;
  719. }
  720. }
  721. }
  722. return 0;
  723. }
  724. /* Reads user's confirmation.
  725. Returns 1 if user's input is "y", "Y", "yes" or "YES"
  726. */
  727. int confirm_yesno(void)
  728. {
  729. int i;
  730. char str_input[5];
  731. /* Flush input */
  732. while (tstc())
  733. getchar();
  734. i = 0;
  735. while (i < sizeof(str_input)) {
  736. str_input[i] = getchar();
  737. putc(str_input[i]);
  738. if (str_input[i] == '\r')
  739. break;
  740. i++;
  741. }
  742. putc('\n');
  743. if (strncmp(str_input, "y\r", 2) == 0 ||
  744. strncmp(str_input, "Y\r", 2) == 0 ||
  745. strncmp(str_input, "yes\r", 4) == 0 ||
  746. strncmp(str_input, "YES\r", 4) == 0)
  747. return 1;
  748. return 0;
  749. }
  750. /* pass 1 to disable ctrlc() checking, 0 to enable.
  751. * returns previous state
  752. */
  753. int disable_ctrlc(int disable)
  754. {
  755. int prev = ctrlc_disabled; /* save previous state */
  756. ctrlc_disabled = disable;
  757. return prev;
  758. }
  759. int had_ctrlc (void)
  760. {
  761. return ctrlc_was_pressed;
  762. }
  763. void clear_ctrlc(void)
  764. {
  765. ctrlc_was_pressed = 0;
  766. }
  767. /** U-Boot INIT FUNCTIONS *************************************************/
  768. struct stdio_dev *console_search_dev(int flags, const char *name)
  769. {
  770. struct stdio_dev *dev;
  771. dev = stdio_get_by_name(name);
  772. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  773. if (!dev && !strcmp(name, CONFIG_VIDCONSOLE_AS_NAME))
  774. dev = stdio_get_by_name("vidconsole");
  775. #endif
  776. if (dev && (dev->flags & flags))
  777. return dev;
  778. return NULL;
  779. }
  780. int console_assign(int file, const char *devname)
  781. {
  782. int flag;
  783. struct stdio_dev *dev;
  784. /* Check for valid file */
  785. flag = stdio_file_to_flags(file);
  786. if (flag < 0)
  787. return flag;
  788. /* Check for valid device name */
  789. dev = console_search_dev(flag, devname);
  790. if (dev)
  791. return console_setfile(file, dev);
  792. return -1;
  793. }
  794. /* return true if the 'silent' flag is removed */
  795. static bool console_update_silent(void)
  796. {
  797. unsigned long flags = gd->flags;
  798. if (!IS_ENABLED(CONFIG_SILENT_CONSOLE))
  799. return false;
  800. if (IS_ENABLED(CONFIG_SILENT_CONSOLE_UNTIL_ENV) && !(gd->flags & GD_FLG_ENV_READY)) {
  801. gd->flags |= GD_FLG_SILENT;
  802. return false;
  803. }
  804. if (env_get("silent")) {
  805. gd->flags |= GD_FLG_SILENT;
  806. return false;
  807. }
  808. gd->flags &= ~GD_FLG_SILENT;
  809. return !!(flags & GD_FLG_SILENT);
  810. }
  811. int console_announce_r(void)
  812. {
  813. #if !CONFIG_IS_ENABLED(PRE_CONSOLE_BUFFER)
  814. char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
  815. display_options_get_banner(false, buf, sizeof(buf));
  816. console_puts_select(stdout, false, buf);
  817. #endif
  818. return 0;
  819. }
  820. /* Called before relocation - use serial functions */
  821. int console_init_f(void)
  822. {
  823. gd->have_console = 1;
  824. console_update_silent();
  825. print_pre_console_buffer(PRE_CONSOLE_FLUSHPOINT1_SERIAL);
  826. return 0;
  827. }
  828. static void stdio_print_current_devices(void)
  829. {
  830. char *stdinname, *stdoutname, *stderrname;
  831. if (CONFIG_IS_ENABLED(CONSOLE_MUX) &&
  832. CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)) {
  833. /* stdin stdout and stderr are in environment */
  834. stdinname = env_get("stdin");
  835. stdoutname = env_get("stdout");
  836. stderrname = env_get("stderr");
  837. stdinname = stdinname ? : "No input devices available!";
  838. stdoutname = stdoutname ? : "No output devices available!";
  839. stderrname = stderrname ? : "No error devices available!";
  840. } else {
  841. stdinname = stdio_devices[stdin] ?
  842. stdio_devices[stdin]->name :
  843. "No input devices available!";
  844. stdoutname = stdio_devices[stdout] ?
  845. stdio_devices[stdout]->name :
  846. "No output devices available!";
  847. stderrname = stdio_devices[stderr] ?
  848. stdio_devices[stderr]->name :
  849. "No error devices available!";
  850. }
  851. /* Print information */
  852. puts("In: ");
  853. printf("%s\n", stdinname);
  854. puts("Out: ");
  855. printf("%s\n", stdoutname);
  856. puts("Err: ");
  857. printf("%s\n", stderrname);
  858. }
  859. #if CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV)
  860. /* Called after the relocation - use desired console functions */
  861. int console_init_r(void)
  862. {
  863. char *stdinname, *stdoutname, *stderrname;
  864. struct stdio_dev *inputdev = NULL, *outputdev = NULL, *errdev = NULL;
  865. int i;
  866. int iomux_err = 0;
  867. int flushpoint;
  868. /* update silent for env loaded from flash (initr_env) */
  869. if (console_update_silent())
  870. flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
  871. else
  872. flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
  873. /* set default handlers at first */
  874. gd->jt->getc = serial_getc;
  875. gd->jt->tstc = serial_tstc;
  876. gd->jt->putc = serial_putc;
  877. gd->jt->puts = serial_puts;
  878. gd->jt->printf = serial_printf;
  879. /* stdin stdout and stderr are in environment */
  880. /* scan for it */
  881. stdinname = env_get("stdin");
  882. stdoutname = env_get("stdout");
  883. stderrname = env_get("stderr");
  884. if (OVERWRITE_CONSOLE == 0) { /* if not overwritten by config switch */
  885. inputdev = console_search_dev(DEV_FLAGS_INPUT, stdinname);
  886. outputdev = console_search_dev(DEV_FLAGS_OUTPUT, stdoutname);
  887. errdev = console_search_dev(DEV_FLAGS_OUTPUT, stderrname);
  888. if (CONFIG_IS_ENABLED(CONSOLE_MUX)) {
  889. iomux_err = iomux_doenv(stdin, stdinname);
  890. iomux_err += iomux_doenv(stdout, stdoutname);
  891. iomux_err += iomux_doenv(stderr, stderrname);
  892. if (!iomux_err)
  893. /* Successful, so skip all the code below. */
  894. goto done;
  895. }
  896. }
  897. /* if the devices are overwritten or not found, use default device */
  898. if (inputdev == NULL) {
  899. inputdev = console_search_dev(DEV_FLAGS_INPUT, "serial");
  900. }
  901. if (outputdev == NULL) {
  902. outputdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial");
  903. }
  904. if (errdev == NULL) {
  905. errdev = console_search_dev(DEV_FLAGS_OUTPUT, "serial");
  906. }
  907. /* Initializes output console first */
  908. if (outputdev != NULL) {
  909. /* need to set a console if not done above. */
  910. console_doenv(stdout, outputdev);
  911. }
  912. if (errdev != NULL) {
  913. /* need to set a console if not done above. */
  914. console_doenv(stderr, errdev);
  915. }
  916. if (inputdev != NULL) {
  917. /* need to set a console if not done above. */
  918. console_doenv(stdin, inputdev);
  919. }
  920. done:
  921. if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
  922. stdio_print_current_devices();
  923. #ifdef CONFIG_VIDCONSOLE_AS_LCD
  924. if (strstr(stdoutname, CONFIG_VIDCONSOLE_AS_NAME))
  925. printf("Warning: Please change '%s' to 'vidconsole' in stdout/stderr environment vars\n",
  926. CONFIG_VIDCONSOLE_AS_NAME);
  927. #endif
  928. if (IS_ENABLED(CONFIG_SYS_CONSOLE_ENV_OVERWRITE)) {
  929. /* set the environment variables (will overwrite previous env settings) */
  930. for (i = 0; i < MAX_FILES; i++)
  931. env_set(stdio_names[i], stdio_devices[i]->name);
  932. }
  933. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  934. print_pre_console_buffer(flushpoint);
  935. return 0;
  936. }
  937. #else /* !CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */
  938. /* Called after the relocation - use desired console functions */
  939. int console_init_r(void)
  940. {
  941. struct stdio_dev *inputdev = NULL, *outputdev = NULL;
  942. int i;
  943. struct list_head *list = stdio_get_list();
  944. struct list_head *pos;
  945. struct stdio_dev *dev;
  946. int flushpoint;
  947. /* update silent for env loaded from flash (initr_env) */
  948. if (console_update_silent())
  949. flushpoint = PRE_CONSOLE_FLUSHPOINT1_SERIAL;
  950. else
  951. flushpoint = PRE_CONSOLE_FLUSHPOINT2_EVERYTHING_BUT_SERIAL;
  952. /*
  953. * suppress all output if splash screen is enabled and we have
  954. * a bmp to display. We redirect the output from frame buffer
  955. * console to serial console in this case or suppress it if
  956. * "silent" mode was requested.
  957. */
  958. if (IS_ENABLED(CONFIG_SPLASH_SCREEN) && env_get("splashimage")) {
  959. if (!(gd->flags & GD_FLG_SILENT))
  960. outputdev = console_search_dev (DEV_FLAGS_OUTPUT, "serial");
  961. }
  962. /* Scan devices looking for input and output devices */
  963. list_for_each(pos, list) {
  964. dev = list_entry(pos, struct stdio_dev, list);
  965. if ((dev->flags & DEV_FLAGS_INPUT) && (inputdev == NULL)) {
  966. inputdev = dev;
  967. }
  968. if ((dev->flags & DEV_FLAGS_OUTPUT) && (outputdev == NULL)) {
  969. outputdev = dev;
  970. }
  971. if(inputdev && outputdev)
  972. break;
  973. }
  974. /* Initializes output console first */
  975. if (outputdev != NULL) {
  976. console_setfile_and_devices(stdout, outputdev);
  977. console_setfile_and_devices(stderr, outputdev);
  978. }
  979. /* Initializes input console */
  980. if (inputdev != NULL)
  981. console_setfile_and_devices(stdin, inputdev);
  982. if (!IS_ENABLED(CONFIG_SYS_CONSOLE_INFO_QUIET))
  983. stdio_print_current_devices();
  984. /* Setting environment variables */
  985. for (i = 0; i < MAX_FILES; i++) {
  986. env_set(stdio_names[i], stdio_devices[i]->name);
  987. }
  988. gd->flags |= GD_FLG_DEVINIT; /* device initialization completed */
  989. print_pre_console_buffer(flushpoint);
  990. return 0;
  991. }
  992. #endif /* CONFIG_IS_ENABLED(SYS_CONSOLE_IS_IN_ENV) */