]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
cmd: sf: prevent overwriting the reserved memory
authorPrasad Kummari <prasad.kummari@amd.com>
Fri, 13 Sep 2024 07:32:52 +0000 (13:02 +0530)
committerTom Rini <trini@konsulko.com>
Fri, 20 Sep 2024 23:38:16 +0000 (17:38 -0600)
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>
Suggested-by: Sughosh Ganu <sughosh.ganu@linaro.org>
cmd/sf.c
include/lmb.h

index f43a2e08b318ca3a6ddc4f8af791d46a455d7611..08e364e19140f673399b8d377a0369519bde13d7 100644 (file)
--- 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>
@@ -317,6 +318,13 @@ static int do_spi_flash_read_write(int argc, char *const argv[])
                        strncmp(argv[0], "write", 5) == 0) {
                int read;
 
+               if (CONFIG_IS_ENABLED(LMB)) {
+                       if (lmb_read_check(addr, len)) {
+                               printf("ERROR: trying to overwrite reserved memory...\n");
+                               return CMD_RET_FAILURE;
+                       }
+               }
+
                read = strncmp(argv[0], "read", 4) == 0;
                if (read)
                        ret = spi_flash_read(flash, offset, len, buf);
index fc2daaa7bfcdf64e305782d25520310cecf83d62..aee2f9fcdaa0a0378dcdea8d8135cda7136fc253 100644 (file)
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
 int lmb_push(struct lmb *store);
 void lmb_pop(struct lmb *store);
 
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+       return lmb_alloc_addr(addr, len) == addr ? 0 : -1;
+}
+
 #endif /* __KERNEL__ */
 
 #endif /* _LINUX_LMB_H */