[U-Boot] [PATCH 2/4] serial: protect access to serial rx buffer
Simon Glass
sjg at chromium.org
Wed Aug 8 09:56:01 UTC 2018
On 3 August 2018 at 05:38, Patrick Delaunay <patrick.delaunay at st.com> wrote:
> Add test to avoid access to rx buffer when this buffer is empty.
> In this case directly call getc() function to avoid issue when tstc()
> is not called.
>
> Signed-off-by: Patrick Delaunay <patrick.delaunay at st.com>
> ---
>
> drivers/serial/serial-uclass.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/serial/serial-uclass.c b/drivers/serial/serial-uclass.c
> index 321d23e..4121a37 100644
> --- a/drivers/serial/serial-uclass.c
> +++ b/drivers/serial/serial-uclass.c
> @@ -228,6 +228,9 @@ static int _serial_getc(struct udevice *dev)
> struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
> char val;
>
> + if (upriv->rd_ptr == upriv->wr_ptr)
> + return __serial_getc(dev);
> +
> val = upriv->buf[upriv->rd_ptr++];
> upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
>
> --
> 2.7.4
>
Reviewed-by: Simon Glass <sjg at chromium.org>
BTW I think the code in _serial_tstc() is wrong, or at least sub-optimal:
/* Read all available chars into the RX buffer */
while (__serial_tstc(dev)) {
upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
}
It should call serial_getc() until it returns -EAGAIN. There should be
no need to call __serial_tstc() first,
Regards,
Simon
More information about the U-Boot
mailing list