From c8da065654f4a7f9c15dfcbee7999ad5bd8fc8ae Mon Sep 17 00:00:00 2001
From: Gaurang Rathod <Gaurang.Rathod@infineon.com>
Date: Mon, 20 Feb 2023 03:53:31 -0600
Subject: [PATCH 226/296] brcmfmac: Support secure and non-secure code flow

1. This code change will support legacy(non-secure)
   and secure mode code download flow in fmac which
   which ensures backward compatibility.

	
Signed-off-by: Gaurang Rathod <Gaurang.Rathod@infineon.com>
---
 .../broadcom/brcm80211/brcmfmac/firmware.c    |  2 +-
 .../broadcom/brcm80211/brcmfmac/sdio.c        | 52 +++++++++++++------
 2 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
index aa895df4fe74..1c13137f2d1a 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
@@ -692,7 +692,7 @@ static void brcmf_fw_request_done(const struct firmware *fw, void *ctx)
 			cur->path = alt_path;
 	}
 	if (!fw && cur->type == BRCMF_FW_TYPE_TRXSE) {
-		strlcpy(alt_path, cur->path, BRCMF_FW_NAME_LEN);
+		strscpy(alt_path, cur->path, BRCMF_FW_NAME_LEN);
 		/* strip 'se' from .trxse at the end */
 		alt_path[strlen(alt_path) - 2] = 0;
 		ret = request_firmware(&fw, alt_path, fwctx->dev);
diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
index 158d50051aab..bf54ce0977b4 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
@@ -1268,7 +1268,6 @@ static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
 	 * for host. Host can only read/write (384Kb-512Kb) RAM area.
 	 * Read block is controlled by OTP bit.
 	 */
-
 	if (bus->ci->blhs && (bus->ci->chip == CY_CC_43022_CHIP_ID))
 		shaddr = bus->ci->rambase + CM3_SOCRAM_WRITE_END_LOCATION - 4;
 	else
@@ -3954,21 +3953,30 @@ static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
 			image = fw->data;
 			image = image + TRX_HDR_SZ;
 		}
+		err = brcmf_sdiod_ramrw(bus->sdiodev, true, address,
+					image, fw_size);
+		if (err)
+			brcmf_err("error %d on writing %d membytes at 0x%08x\n",
+				  err, (int)fw_size, address);
+		else if (!brcmf_sdio_verifymemory(bus->sdiodev, address,
+						  image, fw_size))
+			err = -EIO;
 	} else {
 		if (trx->magic == cpu_to_le32(TRX_MAGIC)) {
 			address -= sizeof(struct trx_header_le);
 			fw_size = le32_to_cpu(trx->len);
 		}
-	}
 
-	err = brcmf_sdiod_ramrw(bus->sdiodev, true, address,
-				image, fw_size);
-	if (err)
-		brcmf_err("error %d on writing %d membytes at 0x%08x\n",
-			  err, (int)fw_size, address);
-	else if (!brcmf_sdio_verifymemory(bus->sdiodev, address,
-					  image, fw_size))
-		err = -EIO;
+		err = brcmf_sdiod_ramrw(bus->sdiodev, true, address,
+					(u8 *)fw->data, fw_size);
+
+		if (err)
+			brcmf_err("error %d on writing %d membytes at 0x%08x\n",
+				  err, (int)fw_size, address);
+		else if (!brcmf_sdio_verifymemory(bus->sdiodev, address,
+						  (u8 *)fw->data, fw_size))
+			err = -EIO;
+	}
 
 	return err;
 }
@@ -4066,7 +4074,7 @@ static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
 		}
 	}
 
-	if (bus->ci->chip == CY_CC_43022_CHIP_ID && !(bus->ci->blhs)) {
+	if (!(bus->ci->blhs)) {
 		bcmerror = brcmf_sdio_download_code_file(bus, fw);
 		release_firmware(fw);
 		if (bcmerror) {
@@ -4091,7 +4099,7 @@ static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
 			goto err;
 		}
 	}
-	if (bus->ci->chip == CY_CC_43022_CHIP_ID && !(bus->ci->blhs)) {
+	if (!(bus->ci->blhs)) {
 		bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
 		brcmf_fw_nvram_free(nvram);
 		if (bcmerror) {
@@ -5475,11 +5483,21 @@ static struct brcmf_fw_request *
 brcmf_sdio_prepare_fw_request(struct brcmf_sdio *bus)
 {
 	struct brcmf_fw_request *fwreq;
-	struct brcmf_fw_name fwnames[] = {
-		{ ".trxs", bus->sdiodev->fw_name },
-		{ ".txt", bus->sdiodev->nvram_name },
-		{ ".clm_blob", bus->sdiodev->clm_name },
-	};
+	struct brcmf_fw_name fwnames[3];
+
+	if (bus->ci->blhs && bus->ci->chip == CY_CC_43022_CHIP_ID) {
+		/* 43022 : secure-mode supports .trxs image only */
+		fwnames[0].extension = ".trxs";
+		fwnames[0].path = bus->sdiodev->fw_name;
+	} else {
+		fwnames[0].extension = ".bin";
+		fwnames[0].path = bus->sdiodev->fw_name;
+	}
+	fwnames[1].extension = ".txt";
+	fwnames[1].path = bus->sdiodev->nvram_name;
+	
+	fwnames[2].extension = ".clm_blob";
+	fwnames[2].path = bus->sdiodev->clm_name;
 
 	fwreq = brcmf_fw_alloc_request(bus->ci->chip, bus->ci->chiprev,
 				       brcmf_sdio_fwnames,
-- 
2.25.1

