Subject: [PATCH] fpga zynq: Check the bitstream for validity

From: [email-masked] (Jason Gunthorpe)
Date: Tue, 8 Nov 2016 09:24:51 -0700

On Tue, Nov 08, 2016 at 10:59:43AM +0100, Matthias Brugger wrote:

> > 	/* don't globally reset PL if we're doing partial reconfig */
> > 	if (!(flags & FPGA_MGR_PARTIAL_RECONFIG)) {
> >+		if (!zynq_fpga_has_sync(buf, count)) {
> 
> Maybe we should add an error message here to let the user know what went
> wrong, as I think this error path could easily be hit by an user.

Sure, happy with this wording:

diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
index de475a6a1882..2f3da4c6e2e6 100644
--- a/drivers/fpga/zynq-fpga.c
+++ b/drivers/fpga/zynq-fpga.c
@@ -181,7 +181,7 @@ static irqreturn_t zynq_fpga_isr(int irq, void *data)
  */
 static bool zynq_fpga_has_sync(const char *buf, size_t count)
 {
-	for (; count > 4; buf += 4, --count)
+	for (; count > 4; buf += 4, count -= 4)
 		if (buf[0] == 0x66 && buf[1] == 0x55 && buf[2] == 0x99 &&
 		    buf[3] == 0xaa)
 			return true;
@@ -200,8 +200,11 @@ static int zynq_fpga_ops_write_init(struct fpga_manager *mgr, u32 flags,
 	/* The hardware can only DMA multiples of 4 bytes, and we need at
 	 * least the sync word and something else to do anything.
 	 */
-	if (count <= 4 || (count % 4) != 0)
+	if (count <= 4 || (count % 4) != 0) {
+		dev_err(priv->dev,
+			"Invalid bitstream size, must be multiples of 4 bytes");
 		return -EINVAL;
+	}
 
 	err = clk_enable(priv->clk);
 	if (err)
@@ -210,6 +213,8 @@ static int zynq_fpga_ops_write_init(struct fpga_manager *mgr, u32 flags,
 	/* don't globally reset PL if we're doing partial reconfig */
 	if (!(flags & FPGA_MGR_PARTIAL_RECONFIG)) {
 		if (!zynq_fpga_has_sync(buf, count)) {
+			dev_err(priv->dev,
+				"Invalid bitstream, could not find a sync word. Bitstream must be a byte swaped .bin file");
 			err = -EINVAL;
 			goto out_err;
 		}