console.c 23 KB

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