console: Implement flush() function
PATCH 1/1 console: Implement flush() function
On certain places it is required to flush output print buffers to ensure that text strings were sent to console or serial devices. For example when printing message that U-Boot is going to boot kernel or when U-Boot is going to change baudrate of terminal device.
Some console devices, like UART, have putc/puts functions which just put characters into HW transmit queue and do not wait until all data are transmitted. Doing some sensitive operations (like changing baudrate or starting kernel which resets UART HW) cause that U-Boot messages are lost.
Therefore introduce a new flush() function, implement it for all serial devices via pending(false) callback and use this new flush() function on sensitive places after which output device may go into reset state.
This change fixes printing of U-Boot messages:
"## Starting application at ..."
"## Switch baudrate to ..."
--- arch/sandbox/cpu/os.c | 5 +++ boot/bootm_os.c | 1 + cmd/boot.c | 1 + cmd/elf.c | 2 ++ cmd/load.c | 5 +++ common/console.c | 56 ++++++++++++++++++++++++++++++++++ common/stdio.c | 6 ++++ drivers/serial/serial-uclass.c | 25 +++++++++++++++ drivers/serial/serial.c | 1 + include/_exports.h | 1 + include/os.h | 8 +++++ include/serial.h | 2 ++ include/stdio.h | 7 +++++ include/stdio_dev.h | 2 ++ 14 files changed, 122 insertions(+)
arch/sandbox/cpu/os.c +5 -0
@@ -653,6 +653,11 @@ void os_puts(const char *str)os_putc (*str++); } +void os_flush (void ) +{ +fflush (stdout); +} +int os_write_ram_buf (const char *fname) {struct sandbox_state *state =state_get_current ();
boot/bootm_os.c +1 -0
@@ -303,6 +303,7 @@ static void do_bootvx_fdt(bootm_headers_t *images)#else printf ("## Starting vxWorks at 0x%08lx\n" , (ulong )images->ep);#endif +flush ();boot_jump_vxworks (images);
cmd/boot.c +1 -0
@@ -32,6 +32,7 @@ static int do_go(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) addr =hextoul (argv[1 ],NULL );printf ("## Starting application at 0x%08lX ...\n" , addr); +flush ();/* * pass address parameter as argv[0] (aka command name),
cmd/elf.c +2 -0
@@ -72,6 +72,7 @@ int do_bootelf(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])return rcode;printf ("## Starting application at 0x%08lx ...\n" , addr); +flush ();/* * pass address parameter as argv[0] (aka command name),
@@ -274,6 +275,7 @@ int do_bootvx(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])puts ("## Not an ELF image, assuming binary\n" );printf ("## Starting vxWorks at 0x%08lx ...\n" , addr); +flush ();dcache_disable ();#if defined(CONFIG_ARM64 ) && defined(CONFIG_ARMV8_PSCI )
cmd/load.c +5 -0
@@ -81,6 +81,7 @@ static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,printf ("## Switch baudrate to %d bps and press ENTER ...\n" , load_baudrate);udelay (50000 ); +flush (); gd->baudrate = load_baudrate;serial_setbrg ();udelay (50000 );
@@ -124,6 +125,7 @@ static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,printf ("## Switch baudrate to %d bps and press ESC ...\n" , current_baudrate);udelay (50000 ); +flush (); gd->baudrate = current_baudrate;serial_setbrg ();udelay (50000 );
@@ -315,6 +317,7 @@ int do_save_serial(struct cmd_tbl *cmdtp, int flag, int argc,printf ("## Switch baudrate to %d bps and press ESC ...\n" , (int )current_baudrate);udelay (50000 ); +flush (); gd->baudrate = current_baudrate;serial_setbrg ();udelay (50000 );
@@ -469,6 +472,7 @@ static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,printf ("## Switch baudrate to %d bps and press ENTER ...\n" , load_baudrate);udelay (50000 ); +flush (); gd->baudrate = load_baudrate;serial_setbrg ();udelay (50000 );
@@ -531,6 +535,7 @@ static int do_load_serial_bin(struct cmd_tbl *cmdtp, int flag, int argc,printf ("## Switch baudrate to %d bps and press ESC ...\n" , current_baudrate);udelay (50000 ); +flush (); gd->baudrate = current_baudrate;serial_setbrg ();udelay (50000 );
common/console.c +56 -0
86 lines not on this page · raw message
common/stdio.c +6 -0
@@ -87,6 +87,11 @@ static void stdio_serial_puts(struct stdio_dev *dev, const char *s)serial_puts (s); } +static void stdio_serial_flush (struct stdio_dev *dev) +{ +serial_flush (); +} +static int stdio_serial_getc (struct stdio_dev *dev) {return serial_getc ();
@@ -112,6 +117,7 @@ static void drv_system_init (void) dev.flags =DEV_FLAGS_OUTPUT |DEV_FLAGS_INPUT ; dev.putc = stdio_serial_putc; dev.puts = stdio_serial_puts; + dev.flush = stdio_serial_flush; dev.getc = stdio_serial_getc; dev.tstc = stdio_serial_tstc;stdio_register (&dev);
drivers/serial/serial-uclass.c +25 -0
@@ -203,6 +203,16 @@ static void _serial_puts(struct udevice *dev, const char *str)_serial_putc (dev, *str++); } +static void _serial_flush (struct udevice *dev) +{ +struct dm_serial_ops *ops =serial_get_ops (dev); + +if (!ops->pending) +return ; +while (ops->pending (dev, false) >0 ) + ; +} +static int __serial_getc (struct udevice *dev) {struct dm_serial_ops *ops =serial_get_ops (dev);
@@ -280,6 +290,14 @@ void serial_puts(const char *str)_serial_puts (gd->cur_serial_dev, str); } +void serial_flush (void ) +{ +if (!gd->cur_serial_dev) +return ; + +_serial_flush (gd->cur_serial_dev); +} +int serial_getc (void ) {if (!gd->cur_serial_dev)
@@ -363,6 +381,11 @@ static void serial_stub_puts(struct stdio_dev *sdev, const char *str)_serial_puts (sdev->priv, str); } +static void serial_stub_flush (struct stdio_dev *sdev) +{ +_serial_flush (sdev->priv); +} +static int serial_stub_getc (struct stdio_dev *sdev) {return _serial_getc (sdev->priv);
@@ -412,6 +435,7 @@ static int on_baudrate(const char *name, const char *value, enum env_op op,printf ("## Switch baudrate to %d bps and press ENTER ...\n" , baudrate);udelay (50000 ); +flush (); } gd->baudrate = baudrate;
@@ -485,6 +509,7 @@ static int serial_post_probe(struct udevice *dev) sdev.priv = dev; sdev.putc = serial_stub_putc; sdev.puts = serial_stub_puts; + sdev.flush = serial_stub_flush; sdev.getc = serial_stub_getc; sdev.tstc = serial_stub_tstc;
drivers/serial/serial.c +1 -0
@@ -81,6 +81,7 @@ static int on_baudrate(const char *name, const char *value, enum env_op op,printf ("## Switch baudrate to %d" " bps and press ENTER ...\n" , baudrate);udelay (50000 ); +flush (); } gd->baudrate = baudrate;
include/_exports.h +1 -0
@@ -12,6 +12,7 @@ EXPORT_FUNC(tstc, int, tstc, void) EXPORT_FUNC(putc, void, putc, const char) EXPORT_FUNC(puts, void, puts, const char *) + EXPORT_FUNC(flush, void, flush, void) EXPORT_FUNC(printf, int, printf, const char*, ...) #if (defined(CONFIG_X86) && !defined(CONFIG_X86_64)) || defined(CONFIG_PPC) EXPORT_FUNC(irq_install_handler, void, install_hdlr,
include/os.h +8 -0
@@ -288,6 +288,14 @@ void os_putc(int ch);*/ void os_puts (const char *str); +/** +* os_flush() - flush controlling OS terminal +* +* This bypasses the U-Boot console support and flushes directly the OS +* stdout file descriptor. +*/ +void os_flush (void ); +/** * os_write_ram_buf() - write the sandbox RAM buffer to a existing file *
include/serial.h +2 -0
@@ -14,6 +14,7 @@ struct serial_device {int (*tstc)(void );void (*putc)(const char c);void (*puts)(const char *s); +void (*flush)(void );#if CONFIG_POST &CONFIG_SYS_POST_UART void (*loop)(int );#endif
@@ -343,6 +344,7 @@ void serial_setbrg(void);void serial_putc (const char ch);void serial_putc_raw (const char ch);void serial_puts (const char *str); +void serial_flush (void );int serial_getc (void );int serial_tstc (void );
include/stdio.h +7 -0
@@ -15,6 +15,7 @@ int tstc(void); defined(CONFIG_SPL_SERIAL))void putc (const char c);void puts (const char *s); +void flush (void ); int __printf(1, 2) printf(const char *fmt, ...); int vprintf(const char *fmt, va_list args);#else
@@ -26,6 +27,10 @@ static inline void puts(const char *s) { } +static inline void flush (void ) +{ +} + static inline int __printf(1, 2) printf(const char *fmt, ...) { return 0;
@@ -48,11 +53,13 @@ static inline int vprintf(const char *fmt, va_list args)/* stderr */ #define eputc (c) fputc(stderr, c)#define eputs (s) fputs(stderr, s) +#define eflush () fflush(stderr) #define eprintf(fmt, args...) fprintf(stderr, fmt, ##args) int __printf(2, 3) fprintf(int file, const char *fmt, ...); void fputs(int file, const char *s); void fputc(int file, const char c); +void fflush(int file); int ftstc(int file); int fgetc(int file);
include/stdio_dev.h +2 -0
@@ -37,6 +37,8 @@ struct stdio_dev {void (*putc)(struct stdio_dev *dev,const char c);/* To put a string (accelerator) */ void (*puts)(struct stdio_dev *dev,const char *s); +/* To flush output queue */ +void (*flush)(struct stdio_dev *dev);/* INPUT functions */
Signature · 1 line
2.20.1
Hi Pali,
Quoted 20 lines · Pali Rohár
On certain places it is required to flush output print buffers to ensure that text strings were sent to console or serial devices. For example when printing message that U-Boot is going to boot kernel or when U-Boot is going to change baudrate of terminal device.
Some console devices, like UART, have putc/puts functions which just put characters into HW transmit queue and do not wait until all data are transmitted. Doing some sensitive operations (like changing baudrate or starting kernel which resets UART HW) cause that U-Boot messages are lost.
Therefore introduce a new
flush()function, implement it for all serial devices via pending(false) callback and use this newflush()function on sensitive places after which output device may go into reset state.This change fixes printing of U-Boot messages:
"## Starting application at ..."
"## Switch baudrate to ..."Signed-off-by Pali Rohár <[email protected]>
Quoted 16 lines
--- arch/sandbox/cpu/os.c | 5 +++ boot/bootm_os.c | 1 + cmd/boot.c | 1 + cmd/elf.c | 2 ++ cmd/load.c | 5 +++ common/console.c | 56 ++++++++++++++++++++++++++++++++++ common/stdio.c | 6 ++++ drivers/serial/serial-uclass.c | 25 +++++++++++++++ drivers/serial/serial.c | 1 + include/_exports.h | 1 + include/os.h | 8 +++++ include/serial.h | 2 ++ include/stdio.h | 7 +++++ include/stdio_dev.h | 2 ++ 14 files changed, 122 insertions(+)
There is a lot going on in this patch and I think it should be split into a small series, something like:
- driver model changes (don't forget to update test/dm/serial.c)
- stdio changes
- sandbox changes
- plumbing into certain commands
You must not implement new features in the non-DM code as we are migrating away from that. So just implement it for driver model.
Also, given the code-size impact I think this should have a Kconfig option.
Regards,
Simon