[coreboot-gerrit] Change in coreboot[master]: drivers/spi/spi_flash: don't allocate unbounded stack memory

Patrick Georgi (Code Review) gerrit at coreboot.org
Tue Aug 21 17:52:51 CEST 2018


Patrick Georgi has uploaded this change for review. ( https://review.coreboot.org/28254


Change subject: drivers/spi/spi_flash: don't allocate unbounded stack memory
......................................................................

drivers/spi/spi_flash: don't allocate unbounded stack memory

This open-codes flash_cmd, but until the API is fixed for real, it uses
xfer's existing scatter-gather ability to write command and data in one
go.

BUG=chromium:446201
TEST=emerge-coral coreboot succeeds

Change-Id: Ic81b7c9f7e0f2647e59b81d61abd68d36051e578
Signed-off-by: Patrick Georgi <pgeorgi at google.com>
---
M src/drivers/spi/spi_flash.c
1 file changed, 15 insertions(+), 10 deletions(-)



  git pull ssh://review.coreboot.org:29418/coreboot refs/changes/54/28254/1

diff --git a/src/drivers/spi/spi_flash.c b/src/drivers/spi/spi_flash.c
index f271479..82333e9 100644
--- a/src/drivers/spi/spi_flash.c
+++ b/src/drivers/spi/spi_flash.c
@@ -82,24 +82,29 @@
 	return ret;
 }
 
-/* TODO: This code is quite possibly broken and overflowing stacks. Fix ASAP! */
-#pragma GCC diagnostic push
-#if defined(__GNUC__) && !defined(__clang__)
-#pragma GCC diagnostic ignored "-Wstack-usage="
-#endif
 int spi_flash_cmd_write(const struct spi_slave *spi, const u8 *cmd,
 			size_t cmd_len, const void *data, size_t data_len)
 {
-	int ret;
-	u8 buff[cmd_len + data_len];
-	memcpy(buff, cmd, cmd_len);
-	memcpy(buff + cmd_len, data, data_len);
+	int ret = 1;
+	struct spi_op vectors[] = {
+		[0] = { .dout = cmd, .bytesout = cmd_len,
+			.din = NULL, .bytesin = 0, },
+		[1] = { .dout = data, .bytesout = data_len,
+			.din = NULL, .bytesin = 0, }
+	};
+	size_t count = ARRAY_SIZE(vectors);
 
-	ret = do_spi_flash_cmd(spi, buff, cmd_len + data_len, NULL, 0);
+	if (spi_claim_bus(spi))
+		return ret;
+
+	if (spi_xfer_vector(spi, vectors, count) == 0)
+		ret = 0;
+
 	if (ret) {
 		printk(BIOS_WARNING, "SF: Failed to send write command (%zu bytes): %d\n",
 				data_len, ret);
 	}
+	spi_release_bus(spi);
 
 	return ret;
 }

-- 
To view, visit https://review.coreboot.org/28254
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic81b7c9f7e0f2647e59b81d61abd68d36051e578
Gerrit-Change-Number: 28254
Gerrit-PatchSet: 1
Gerrit-Owner: Patrick Georgi <pgeorgi at google.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.coreboot.org/pipermail/coreboot-gerrit/attachments/20180821/b952f30c/attachment.html>


More information about the coreboot-gerrit mailing list