From 8b031676adc12c53fd744a6a511b47aafdcd52db Mon Sep 17 00:00:00 2001 From: zvecr Date: Thu, 12 Nov 2026 02:50:15 +0000 Subject: [PATCH] Do not copy OUT packets past the transfer buffer --- os/hal/ports/HT32/LLD/USBv1/hal_usb_lld.c | 19 ++++++----- .../ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c | 7 ++-- os/hal/ports/RP/LLD/USBDv1/hal_usb_lld.c | 15 ++++---- os/hal/ports/WB32/LLD/USBv1/hal_usb_lld.c | 34 +++++++++++-------- 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/os/hal/ports/HT32/LLD/USBv1/hal_usb_lld.c b/os/hal/ports/HT32/LLD/USBv1/hal_usb_lld.c index 85b6d1f5b9..ea990eaa14 100644 --- a/os/hal/ports/HT32/LLD/USBv1/hal_usb_lld.c +++ b/os/hal/ports/HT32/LLD/USBv1/hal_usb_lld.c @@ -149,23 +149,26 @@ static size_t usb_packet_receive(USBDriver *usbp, usbep_t ep) { const uint32_t cfgr = USB->EP[ep].CFGR; volatile uint32_t *const EPSRAM = (void *)(USB_SRAM_BASE + (cfgr & 0x3ff) + ((ep == 0) ? ((cfgr >> 10) & 0x7f) : 0)); - for (size_t i = 0; i < n; i += 4) { + // Reads the packet into the defined buffer. The host can send a full + // packet when less room is left, the excess is discarded. + const size_t m = n < osp->rxsize ? n : osp->rxsize; + for (size_t i = 0; i < m; i += 4) { if(!osp->rxbuf) break; const uint32_t word = EPSRAM[i/4]; - if (i + 0 < n) + if (i + 0 < m) osp->rxbuf[i + 0] = word >> 0; - if (i + 1 < n) + if (i + 1 < m) osp->rxbuf[i + 1] = word >> 8; - if (i + 2 < n) + if (i + 2 < m) osp->rxbuf[i + 2] = word >> 16; - if (i + 3 < n) + if (i + 3 < m) osp->rxbuf[i + 3] = word >> 24; } - osp->rxbuf += n; - osp->rxcnt += n; - osp->rxsize -= n; + osp->rxbuf += m; + osp->rxcnt += m; + osp->rxsize -= m; osp->rxpkts--; return n; } diff --git a/os/hal/ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c b/os/hal/ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c index c5d52def82..39e06bfd69 100644 --- a/os/hal/ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c +++ b/os/hal/ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c @@ -185,9 +185,12 @@ void usb_packet_receive(USBDriver *usbp, usbep_t ep, size_t n) if (n > (size_t)epc->out_maxsize) n = (size_t)epc->out_maxsize; - /* Copy from _usbb[] to buf */ + /* Reads the packet into the defined buffer. The host can send a full + packet when less room is left, the excess is discarded.*/ + const size_t m = n < osp->rxsize ? n : osp->rxsize; + size_t i=0; - for(i=0;irxbuf[i] = bd->addr[i]; /* Update the Buffer status diff --git a/os/hal/ports/RP/LLD/USBDv1/hal_usb_lld.c b/os/hal/ports/RP/LLD/USBDv1/hal_usb_lld.c index be243aa023..f70282921e 100644 --- a/os/hal/ports/RP/LLD/USBDv1/hal_usb_lld.c +++ b/os/hal/ports/RP/LLD/USBDv1/hal_usb_lld.c @@ -298,7 +298,7 @@ static void usb_serve_endpoint(USBDriver *usbp, usbep_t ep, bool is_in) { const USBEndpointConfig *epcp = usbp->epc[ep]; USBOutEndpointState *oesp; USBInEndpointState *iesp; - uint16_t n; + uint16_t n, m; if (is_in) { /* IN endpoint */ @@ -318,14 +318,17 @@ static void usb_serve_endpoint(USBDriver *usbp, usbep_t ep, bool is_in) { /* OUT endpoint */ oesp = usbp->epc[ep]->out_state; - /* Length received */ + /* Length received. The host can send a full + packet when less room is left, the excess is discarded. */ n = BUF_CTRL(ep).OUT & USB_BUFFER_BUFFER0_TRANS_LENGTH_Msk; + m = n < oesp->rxsize ? n : oesp->rxsize; /* Copy received data into user buffer */ - memcpy((void *)oesp->rxbuf, (void *)oesp->hw_buf, n); - oesp->rxbuf += n; - oesp->rxcnt += n; - oesp->rxsize -= n; + memcpy((void *)oesp->rxbuf, (void *)oesp->hw_buf, m); + + oesp->rxbuf += m; + oesp->rxcnt += m; + oesp->rxsize -= m; oesp->rxpkts -= 1; diff --git a/os/hal/ports/WB32/LLD/USBv1/hal_usb_lld.c b/os/hal/ports/WB32/LLD/USBv1/hal_usb_lld.c index 2f08a64d5d..c234abffd7 100644 --- a/os/hal/ports/WB32/LLD/USBv1/hal_usb_lld.c +++ b/os/hal/ports/WB32/LLD/USBv1/hal_usb_lld.c @@ -180,11 +180,14 @@ static void _wbusb_ep0out(USBDriver *usbp, usbep_t ep) { * * @param[in] ep endpoint number * @param[out] buf buffer where to copy the packet data - * @return The size of the receivee packet. + * @param[in] max maximum number of bytes to copy, the rest of the + * packet is discarded + * @return The size of the received packet. * * @notapi */ -static size_t usb_packet_read_to_buffer(usbep_t ep, uint8_t *buf) { +static size_t usb_packet_read_to_buffer(usbep_t ep, uint8_t *buf, + size_t max) { size_t rx_cnt, n; uint8_t ep_num; uint32_t ep_fifo_addr; @@ -200,7 +203,7 @@ static size_t usb_packet_read_to_buffer(usbep_t ep, uint8_t *buf) { rx_cnt = WB32_USB->COUNT0; } - n = rx_cnt; + n = rx_cnt < max ? rx_cnt : max; while (n) { *buf++ = *((volatile uint8_t *)ep_fifo_addr); @@ -238,7 +241,7 @@ static void usb_packet_write_from_buffer(usbep_t ep, * @notapi */ static void usb_serve_ep0_handler(USBDriver *usbp) { - size_t n; + size_t n, m; uint8_t ControlReg; const USBEndpointConfig *epcp = usbp->epc[0]; @@ -315,12 +318,13 @@ static void usb_serve_ep0_handler(USBDriver *usbp) { if (ControlReg & USB_CSR0_OUTPKTRDY) { /* Reads the packet into the defined buffer.*/ - n = usb_packet_read_to_buffer(0, osp->rxbuf); - osp->rxbuf += n; + n = usb_packet_read_to_buffer(0, osp->rxbuf, osp->rxsize); + m = n < osp->rxsize ? n : osp->rxsize; + osp->rxbuf += m; /* Transaction data updated.*/ - osp->rxcnt += n; - osp->rxsize -= n; + osp->rxcnt += m; + osp->rxsize -= m; osp->rxpkts -= 1; /* The transaction is completed if the specified number of packets @@ -393,7 +397,7 @@ static void usb_serve_epin_handler(USBDriver *usbp, usbep_t ep) { * @notapi */ static void usb_serve_epout_handler(USBDriver *usbp, usbep_t ep) { - size_t n; + size_t n, m; uint8_t ControlReg; const USBEndpointConfig *epcp = usbp->epc[ep]; /* OUT endpoint, receive.*/ @@ -404,14 +408,16 @@ static void usb_serve_epout_handler(USBDriver *usbp, usbep_t ep) { ControlReg = WB32_USB->OUTCSR1; if (ControlReg & USB_OUTCSR1_OUTPKTRDY) { - /* Reads the packet into the defined buffer.*/ - n = usb_packet_read_to_buffer(ep, osp->rxbuf); + /* Reads the packet into the defined buffer. The host can send a full + packet when less room is left, the excess is discarded.*/ + n = usb_packet_read_to_buffer(ep, osp->rxbuf, osp->rxsize); + m = n < osp->rxsize ? n : osp->rxsize; - osp->rxbuf += n; + osp->rxbuf += m; /* Transaction data updated.*/ - osp->rxcnt += n; - osp->rxsize -= n; + osp->rxcnt += m; + osp->rxsize -= m; osp->rxpkts -= 1; /* The transaction is completed if the specified number of packets