All of lore.kernel.org
 help / color / mirror / Atom feed
From: Prasad Kummari <prasad.kummari@amd.com>
To: <u-boot@lists.denx.de>, <git@amd.com>
Cc: <michal.simek@amd.com>, <venkatesh.abbarapu@amd.com>,
	<padmarao.begari@amd.com>, <git@xilinx.com>,
	<jagan@amarulasolutions.com>, <n-francis@ti.com>, <d-gole@ti.com>,
	Prasad Kummari <prasad.kummari@amd.com>
Subject: [PATCH v3] cmd: sf: prevent overwriting the reserved memory
Date: Tue, 10 Sep 2024 21:13:45 +0530	[thread overview]
Message-ID: <20240910154344.1837906-2-prasad.kummari@amd.com> (raw)

Added LMB API to prevent SF command from overwriting reserved
memory areas. The current SPI code does not use LMB APIs for
loading data into memory addresses. To resolve this, LMB APIs
were added to check the load address of an SF command and ensure it
does not overwrite reserved memory addresses. Similar checks are
used in TFTP, serial load, and boot code to prevent overwriting
reserved memory.

Signed-off-by: Prasad Kummari <prasad.kummari@amd.com>
---

Changes in V3:
- Removed lmb_init_and_reserve() as part of latest LMB series.
- Error message moved to one place.
- lmb_alloc_addr() is not required because the given memory address is
  being checked to ensure it is free or not. 

Changes in V2:
- Rebased the code changes on top of the next branch.

UT:
Tested on Versal NET board

Versal NET> fdt print /reserved-memory                                          
reserved-memory {                                                               
        ranges;                                                                 
        #size-cells = <0x00000002>;                                             
        #address-cells = <0x00000002>;                                          
        tf-a {                                                                  
                reg = <0x00000000 0x70000000 0x00000000 0x00050000>;            
                no-map;                                                         
        };                                                                      
};                                                                              
Versal NET> sf read 0x70000000 0x0 0x40
device 0 offset 0x0, size 0x40                                                  
ERROR: trying to overwrite reserved memory...

Versal NET> sf write 0x70000000 0x0 0x40                                        
device 0 offset 0x0, size 0x40                                                  
ERROR: trying to overwrite reserved memory...

relocaddr   = 0x000000007febc000

Versal NET> sf read 0x000000007febc000 0x0 0x40                                 
device 0 offset 0x0, size 0x40 
ERROR: trying to overwrite reserved memory...         

Versal NET> sf write 0x000000007febc000 0x0 0x40                                
device 0 offset 0x0, size 0x40                                                  
ERROR: trying to overwrite reserved memory... 

 cmd/sf.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/cmd/sf.c b/cmd/sf.c
index f43a2e08b3..7bb8bcfce2 100644
--- a/cmd/sf.c
+++ b/cmd/sf.c
@@ -10,6 +10,7 @@
 #include <div64.h>
 #include <dm.h>
 #include <log.h>
+#include <lmb.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <spi.h>
@@ -272,6 +273,31 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
 	return 0;
 }
 
+#ifdef CONFIG_LMB
+static int do_spi_read_lmb_check(ulong start_addr, loff_t len)
+{
+	phys_size_t max_size;
+	ulong end_addr;
+
+	lmb_dump_all();
+
+	max_size = lmb_get_free_size(start_addr);
+	if (!max_size) {
+		return CMD_RET_FAILURE;
+	}
+
+	end_addr = start_addr + max_size;
+	if (!end_addr)
+		end_addr = ULONG_MAX;
+
+	if ((start_addr + len) > end_addr) {
+		return CMD_RET_FAILURE;
+	}
+
+	return 0;
+}
+#endif
+
 static int do_spi_flash_read_write(int argc, char *const argv[])
 {
 	unsigned long addr;
@@ -315,7 +341,15 @@ static int do_spi_flash_read_write(int argc, char *const argv[])
 		ret = spi_flash_update(flash, offset, len, buf);
 	} else if (strncmp(argv[0], "read", 4) == 0 ||
 			strncmp(argv[0], "write", 5) == 0) {
-		int read;
+		int read, ret;
+
+#ifdef CONFIG_LMB
+		ret = do_spi_read_lmb_check(addr, len);
+		if (ret) {
+			printf("ERROR: trying to overwrite reserved memory...\n");
+			return ret;
+		}
+#endif
 
 		read = strncmp(argv[0], "read", 4) == 0;
 		if (read)
-- 
2.25.1


             reply	other threads:[~2024-09-10 15:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-10 15:43 Prasad Kummari [this message]
2024-09-11 11:20 ` [PATCH v3] cmd: sf: prevent overwriting the reserved memory Sughosh Ganu
2024-09-11 11:23   ` Michal Simek
2024-09-11 11:29     ` Sughosh Ganu
2024-09-11 11:32       ` Michal Simek
2024-09-11 11:56         ` Sughosh Ganu
2024-09-13  7:38           ` Kummari, Prasad

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240910154344.1837906-2-prasad.kummari@amd.com \
    --to=prasad.kummari@amd.com \
    --cc=d-gole@ti.com \
    --cc=git@amd.com \
    --cc=git@xilinx.com \
    --cc=jagan@amarulasolutions.com \
    --cc=michal.simek@amd.com \
    --cc=n-francis@ti.com \
    --cc=padmarao.begari@amd.com \
    --cc=u-boot@lists.denx.de \
    --cc=venkatesh.abbarapu@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.