From 07ccf71451d7377b1a6f3367f738ce7ddb1f2a24 Mon Sep 17 00:00:00 2001 From: Mitchell Horne Date: Fri, 24 Feb 2023 13:19:54 -0400 Subject: [PATCH] bcm_dma: don't dereference NULL softc This file defines a small API to be used by other drivers. If any of these functions are called before the bcm_dma device has attached we should handle the error gracefully. Fix a formatting quirk while here. Reviewed by: manu MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D38756 --- sys/arm/broadcom/bcm2835/bcm2835_dma.c | 29 +++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/sys/arm/broadcom/bcm2835/bcm2835_dma.c b/sys/arm/broadcom/bcm2835/bcm2835_dma.c index 49a3938282a2..e9633674d9f6 100644 --- a/sys/arm/broadcom/bcm2835/bcm2835_dma.c +++ b/sys/arm/broadcom/bcm2835/bcm2835_dma.c @@ -328,6 +328,9 @@ bcm_dma_allocate(int req_ch) int ch = BCM_DMA_CH_INVALID; int i; + if (sc == NULL) + return (BCM_DMA_CH_INVALID); + if (req_ch >= BCM_DMA_CH_MAX) return (BCM_DMA_CH_INVALID); @@ -343,13 +346,10 @@ bcm_dma_allocate(int req_ch) break; } } - } - else { - if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) { - ch = req_ch; - sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE; - sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED; - } + } else if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) { + ch = req_ch; + sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE; + sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED; } mtx_unlock(&sc->sc_mtx); @@ -364,6 +364,9 @@ bcm_dma_free(int ch) { struct bcm_dma_softc *sc = bcm_dma_sc; + if (sc == NULL) + return (-1); + if (ch < 0 || ch >= BCM_DMA_CH_MAX) return (-1); @@ -392,6 +395,9 @@ bcm_dma_setup_intr(int ch, void (*func)(int, void *), void *arg) struct bcm_dma_softc *sc = bcm_dma_sc; struct bcm_dma_cb *cb; + if (sc == NULL) + return (-1); + if (ch < 0 || ch >= BCM_DMA_CH_MAX) return (-1); @@ -531,6 +537,9 @@ bcm_dma_reg_dump(int ch) int i; uint32_t reg; + if (sc == NULL) + return; + if (ch < 0 || ch >= BCM_DMA_CH_MAX) return; @@ -558,6 +567,9 @@ bcm_dma_start(int ch, vm_paddr_t src, vm_paddr_t dst, int len) struct bcm_dma_softc *sc = bcm_dma_sc; struct bcm_dma_cb *cb; + if (sc == NULL) + return (-1); + if (ch < 0 || ch >= BCM_DMA_CH_MAX) return (-1); @@ -597,6 +609,9 @@ bcm_dma_length(int ch) struct bcm_dma_softc *sc = bcm_dma_sc; struct bcm_dma_cb *cb; + if (sc == NULL) + return (0); + if (ch < 0 || ch >= BCM_DMA_CH_MAX) return (0);