From e3ae755575e0646f9dd47400a1dfc6018e16e9a8 Mon Sep 17 00:00:00 2001 From: Felix Held Date: Fri, 10 Dec 2021 18:38:16 +0100 Subject: drivers/spi/spi-generic: fix edge case in spi_crop_chunk In the case of deduct_cmd_len being set and the adjusted cmd_len >= ctrlr_max, ctrlr_max wasn't being adjusted and still had the value of ctrlr->max_xfer_size. Handle this edge case (which we should never run into) by setting ctrlr_max to 0 and printing a warning to the console. Signed-off-by: Felix Held Change-Id: I9941b2947bb0a44dfae8ee69f509795dfb0cb241 Reviewed-on: https://review.coreboot.org/c/coreboot/+/60121 Tested-by: build bot (Jenkins) Reviewed-by: Raul Rangel --- src/drivers/spi/spi-generic.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/drivers/spi/spi-generic.c b/src/drivers/spi/spi-generic.c index 116daf9088..9796663da2 100644 --- a/src/drivers/spi/spi-generic.c +++ b/src/drivers/spi/spi-generic.c @@ -98,8 +98,14 @@ unsigned int spi_crop_chunk(const struct spi_slave *slave, unsigned int cmd_len, if (deduct_opcode_len) cmd_len--; - if (deduct_cmd_len && (ctrlr_max > cmd_len)) - ctrlr_max -= cmd_len; + if (deduct_cmd_len) { + if (ctrlr_max >= cmd_len) { + ctrlr_max -= cmd_len; + } else { + ctrlr_max = 0; + printk(BIOS_WARNING, "%s: Command longer than buffer\n", __func__); + } + } return MIN(ctrlr_max, buf_len); } -- cgit v1.2.3