From e65c0f890e04ff11b9ce05e20c27cfff9a8f1ae4 Mon Sep 17 00:00:00 2001 From: mikeller Date: Thu, 28 Feb 2019 20:27:04 +1300 Subject: [PATCH] Fixed DMA option index check. --- src/main/cli/cli.c | 13 +++++++------ src/main/drivers/dma_reqmap.c | 4 ---- src/main/drivers/dma_reqmap.h | 4 ++++ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index 9cd3f26dad..aa7e961fb0 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -4701,7 +4701,7 @@ dmaoptEntry_t dmaoptEntryTable[] = { static void dmaoptToString(int optval, char *buf) { - if (optval == -1) { + if (optval == DMA_OPT_UNUSED) { memcpy(buf, "NONE", DMA_OPT_STRING_BUFSIZE); } else { tfp_sprintf(buf, "%d", optval); @@ -4891,7 +4891,7 @@ static void cliDmaopt(char *cmdline) } else { // It's a pin if (!pch || !(strToPin(pch, &ioTag) && IOGetByTag(ioTag))) { - cliPrintErrorLinef("INVALID PIN: %s", pch); + cliPrintErrorLinef("INVALID PIN: '%s'", pch); return; } @@ -4907,13 +4907,13 @@ static void cliDmaopt(char *cmdline) pch = strtok_r(NULL, " ", &saveptr); if (!pch) { if (entry) { - if (orgval == -1) { + if (orgval == DMA_OPT_UNUSED) { cliPrintLinef("%s %d NONE", entry->device, index + 1); } else { cliPrintLinef("%s %d %d", entry->device, index + 1, *optaddr); } } else { - if (orgval == -1) { + if (orgval == DMA_OPT_UNUSED) { cliPrintLinef("pin %c%02d NONE", IO_GPIOPortIdxByTag(ioTag) + 'A', IO_GPIOPinIdxByTag(ioTag)); } else { cliPrintLinef("pin %c%02d %d", IO_GPIOPortIdxByTag(ioTag) + 'A', IO_GPIOPinIdxByTag(ioTag), orgval); @@ -4942,8 +4942,9 @@ static void cliDmaopt(char *cmdline) optval = DMA_OPT_UNUSED; } else { optval = atoi(pch); - if (optval < 0) { // XXX Check against opt max? How? - cliPrintLinef("bad optnum %s", pch); + if (optval < 0 || (entry && optval >= MAX_PERIPHERAL_DMA_OPTIONS) || (!entry && optval >= MAX_TIMER_DMA_OPTIONS)) { + cliPrintErrorLinef("BAD DMA OPTION NUMBER '%s'", pch); + return; } } diff --git a/src/main/drivers/dma_reqmap.c b/src/main/drivers/dma_reqmap.c index d4d1b9b788..36ff889a07 100644 --- a/src/main/drivers/dma_reqmap.c +++ b/src/main/drivers/dma_reqmap.c @@ -34,16 +34,12 @@ #include "dma_reqmap.h" -#define MAX_PERIPHERAL_DMA_OPTIONS 2 - typedef struct dmaPeripheralMapping_s { dmaPeripheral_e device; uint8_t index; dmaChannelSpec_t channelSpec[MAX_PERIPHERAL_DMA_OPTIONS]; } dmaPeripheralMapping_t; -#define MAX_TIMER_DMA_OPTIONS 3 - typedef struct dmaTimerMapping_s { TIM_TypeDef *tim; uint8_t channel; diff --git a/src/main/drivers/dma_reqmap.h b/src/main/drivers/dma_reqmap.h index 2fa13db34e..079cfd4b95 100644 --- a/src/main/drivers/dma_reqmap.h +++ b/src/main/drivers/dma_reqmap.h @@ -52,8 +52,12 @@ typedef enum { } dmaPeripheral_e; typedef int8_t dmaoptValue_t; + #define DMA_OPT_UNUSED (-1) +#define MAX_PERIPHERAL_DMA_OPTIONS 2 +#define MAX_TIMER_DMA_OPTIONS 3 + dmaoptValue_t dmaoptByTag(ioTag_t ioTag); const dmaChannelSpec_t *dmaGetChannelSpecByPeripheral(dmaPeripheral_e device, uint8_t index, int8_t opt); const dmaChannelSpec_t *dmaGetChannelSpecByTimerValue(TIM_TypeDef *tim, uint8_t channel, dmaoptValue_t dmaopt);