Skip to contentSkip to conversation

cmd: sf: prevent overwriting the reserved memory

PATCH In review Prasad Kummari 5 participants 11 messages Sep 13, 2024 – Sep 21, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 cmd: sf: prevent overwriting the reserved memory

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

 cmd/sf.c      | 8 ++++++++
 include/lmb.h | 5 +++++
 2 files changed, 13 insertions(+)
cmd/sf.c +8 -0
index f43a2e08b3..08e364e191 100644
@@ -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);
include/lmb.h +5 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -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 */
Signature · 1 line
2.44.1
#1 lore raw
Quoted 99 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

  cmd/sf.c      | 8 ++++++++
  include/lmb.h | 5 +++++
  2 files changed, 13 insertions(+)
cmd/sf.c +8 -0
index f43a2e08b3..08e364e191 100644
@@ -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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
  int lmb_push(struct lmb *store);

include/lmb.h struct lmb *lmb_get(void); · line 121

Quoted 10 lines
include/lmb.h +5 -0
  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 */

It is coming based on discussion with Sughosh in v3 that's why

Thanks,
Michal

#2 lore raw
In reply to Prasad Kummari

Hi Prasad,

Quoted 80 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

  cmd/sf.c      | 8 ++++++++
  include/lmb.h | 5 +++++
  2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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>

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 5 lines
cmd/sf.c +2 -0
@@ -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)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Quoted 13 lines
cmd/sf.c +5 -0
+				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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 6 lines
include/lmb.h +3 -0
@@ -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;

who frees this? can we free this right after checking?

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
  #endif /* __KERNEL__ */
 
  #endif /* _LINUX_LMB_H */
#3 lore raw
Quoted 87 lines · Vaishnav Achath

Hi Prasad,

Quoted 83 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

  cmd/sf.c      | 8 ++++++++
  include/lmb.h | 5 +++++
  2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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;
 
Quoted 9 lines

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 2 lines
cmd/sf.c +2 -0
+             if (CONFIG_IS_ENABLED(LMB)) {
+                     if (lmb_read_check(addr, len)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Like I mentioned in my other reply, using a check for lmb_alloc_addr() allows for memory re-use, which is the behaviour that a large number of use cases rely on -- if you go through the test scripts, it is assumed that memory re-use is allowed. That there is no need to explicitly free up memory, and that has been how the LMB memory has been used historically. So it is allowed to use some address to load an image to that address, and then use the same address to load a different image. The LMB rework series does keep this behaviour consistent. So it would be better to change the behaviour of the tftp command to use the same API. I was planning on working on this cleanup. If you want, you can take it up.

Quoted 18 lines
Quoted 18 lines
cmd/sf.c +5 -0
+                             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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
  int lmb_push(struct lmb *store);
  void lmb_pop(struct lmb *store);
 
Quoted 5 lines

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 3 lines
include/lmb.h +3 -0
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+     return lmb_alloc_addr(addr, len) == addr ? 0 : -1;

who frees this? can we free this right after checking?

It is not required to explicitly free up this memory, as it is not an actual allocation per-se. Why were these functions called alloc something, I am not sure. But the point is, if you change the tftp command code to use this API instead, and then use it after a previous load, it would not fail.

-sughosh

Quoted 9 lines

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
  #endif /* __KERNEL__ */
 
  #endif /* _LINUX_LMB_H */
#4 lore raw

Hi Sughosh,

Quoted 98 lines · Sughosh Ganu
Quoted 87 lines · Vaishnav Achath

Hi Prasad,

Quoted 83 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

   cmd/sf.c      | 8 ++++++++
   include/lmb.h | 5 +++++
   2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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;
 
Quoted 9 lines

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 2 lines
cmd/sf.c +2 -0
+             if (CONFIG_IS_ENABLED(LMB)) {
+                     if (lmb_read_check(addr, len)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Quoted 12 lines

Like I mentioned in my other reply, using a check for lmb_alloc_addr() allows for memory re-use, which is the behaviour that a large number of use cases rely on -- if you go through the test scripts, it is assumed that memory re-use is allowed. That there is no need to explicitly free up memory, and that has been how the LMB memory has been used historically. So it is allowed to use some address to load an image to that address, and then use the same address to load a different image. The LMB rework series does keep this behaviour consistent. So it would be better to change the behaviour of the tftp command to use the same API. I was planning on working on this cleanup. If you want, you can take it up.

Please let me know if you are planning to work on this in the coming few days, otherwise I can pick it up as we have platforms failing due to this.

Quoted 24 lines
Quoted 18 lines
Quoted 18 lines
cmd/sf.c +5 -0
+                             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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
   int lmb_push(struct lmb *store);
   void lmb_pop(struct lmb *store);
 
Quoted 5 lines

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 3 lines
include/lmb.h +3 -0
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+     return lmb_alloc_addr(addr, len) == addr ? 0 : -1;

who frees this? can we free this right after checking?

Quoted 8 lines

It is not required to explicitly free up this memory, as it is not an actual allocation per-se. Why were these functions called alloc something, I am not sure. But the point is, if you change the tftp command code to use this API instead, and then use it after a previous load, it would not fail.

-sughosh

Agreed, but the commit message says to "ensure it does not overwrite reserved memory addresses" but the implementation marks the region as reserved in the global memory map and is visible in lmb_dump_all() , which is not expected, is there really a need to mark the region as reserved.

Thanks and Regards,
Vaishnav

Quoted 9 lines
Quoted 9 lines

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
   #endif /* __KERNEL__ */
 
   #endif /* _LINUX_LMB_H */
#5 lore raw
Quoted 115 lines · Vaishnav Achath

Hi Sughosh,

Quoted 98 lines · Sughosh Ganu
Quoted 87 lines · Vaishnav Achath

Hi Prasad,

Quoted 83 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

   cmd/sf.c      | 8 ++++++++
   include/lmb.h | 5 +++++
   2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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;
 
Quoted 9 lines

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 2 lines
cmd/sf.c +2 -0
+             if (CONFIG_IS_ENABLED(LMB)) {
+                     if (lmb_read_check(addr, len)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Quoted 12 lines

Like I mentioned in my other reply, using a check for lmb_alloc_addr() allows for memory re-use, which is the behaviour that a large number of use cases rely on -- if you go through the test scripts, it is assumed that memory re-use is allowed. That there is no need to explicitly free up memory, and that has been how the LMB memory has been used historically. So it is allowed to use some address to load an image to that address, and then use the same address to load a different image. The LMB rework series does keep this behaviour consistent. So it would be better to change the behaviour of the tftp command to use the same API. I was planning on working on this cleanup. If you want, you can take it up.

Quoted 2 lines

Please let me know if you are planning to work on this in the coming few days, otherwise I can pick it up as we have platforms failing due to this.

I can take this up. Will keep you on Cc so that you can test the patches on your boards.

Quoted 31 lines
Quoted 31 lines
Quoted 19 lines
Quoted 19 lines
cmd/sf.c +5 -0
+                             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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
   int lmb_push(struct lmb *store);
   void lmb_pop(struct lmb *store);
 
Quoted 5 lines

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 3 lines
include/lmb.h +3 -0
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+     return lmb_alloc_addr(addr, len) == addr ? 0 : -1;

who frees this? can we free this right after checking?

It is not required to explicitly free up this memory, as it is not an actual allocation per-se. Why were these functions called alloc something, I am not sure. But the point is, if you change the tftp command code to use this API instead, and then use it after a previous load, it would not fail.

Quoted 8 lines
Quoted 2 lines

-sughosh

Agreed, but the commit message says to "ensure it does not overwrite reserved memory addresses" but the implementation marks the region as reserved in the global memory map and is visible in lmb_dump_all() , which is not expected, is there really a need to mark the region as reserved.

What can be overwritten, and what cannot be can now be determined from the flags. If you check the LMB memory map from the bdinfo command, you will see that the regions which cannot be overwritten are now being marked with the "no-overwrite" flag. The other LMB memory which can be re-used to load multiple different images is being marked with the "none" flag. One issue with all this is that currently there is no document which explains all these concepts. I will work on adding such a document. Thanks.

-sughosh

Quoted 13 lines

Thanks and Regards,
Vaishnav

Quoted 9 lines
Quoted 9 lines

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
   #endif /* __KERNEL__ */
 
   #endif /* _LINUX_LMB_H */
#6 lore raw

Hi Sughosh,

Quoted 116 lines · Sughosh Ganu
Quoted 115 lines · Vaishnav Achath

Hi Sughosh,

Quoted 110 lines · Sughosh Ganu
Quoted 87 lines · Vaishnav Achath

Hi Prasad,

Quoted 83 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

    cmd/sf.c      | 8 ++++++++
    include/lmb.h | 5 +++++
    2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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;
 
Quoted 9 lines

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 2 lines
cmd/sf.c +2 -0
+             if (CONFIG_IS_ENABLED(LMB)) {
+                     if (lmb_read_check(addr, len)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Like I mentioned in my other reply, using a check for lmb_alloc_addr() allows for memory re-use, which is the behaviour that a large number of use cases rely on -- if you go through the test scripts, it is assumed that memory re-use is allowed. That there is no need to explicitly free up memory, and that has been how the LMB memory has been used historically. So it is allowed to use some address to load an image to that address, and then use the same address to load a different image. The LMB rework series does keep this behaviour consistent. So it would be better to change the behaviour of the tftp command to use the same API. I was planning on working on this cleanup. If you want, you can take it up.

Quoted 6 lines
Quoted 2 lines

Please let me know if you are planning to work on this in the coming few days, otherwise I can pick it up as we have platforms failing due to this.

I can take this up. Will keep you on Cc so that you can test the patches on your boards.

Thanks, I will test and report the results once you post the patches.

Quoted 40 lines
Quoted 31 lines
Quoted 31 lines
Quoted 19 lines
Quoted 19 lines
cmd/sf.c +5 -0
+                             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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
    int lmb_push(struct lmb *store);
    void lmb_pop(struct lmb *store);
 
Quoted 5 lines

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 3 lines
include/lmb.h +3 -0
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+     return lmb_alloc_addr(addr, len) == addr ? 0 : -1;

who frees this? can we free this right after checking?

It is not required to explicitly free up this memory, as it is not an actual allocation per-se. Why were these functions called alloc something, I am not sure. But the point is, if you change the tftp command code to use this API instead, and then use it after a previous load, it would not fail.

Quoted 8 lines
Quoted 2 lines

-sughosh

Agreed, but the commit message says to "ensure it does not overwrite reserved memory addresses" but the implementation marks the region as reserved in the global memory map and is visible in lmb_dump_all() , which is not expected, is there really a need to mark the region as reserved.

Quoted 9 lines

What can be overwritten, and what cannot be can now be determined from the flags. If you check the LMB memory map from the bdinfo command, you will see that the regions which cannot be overwritten are now being marked with the "no-overwrite" flag. The other LMB memory which can be re-used to load multiple different images is being marked with the "none" flag. One issue with all this is that currently there is no document which explains all these concepts. I will work on adding such a document. Thanks.

Yes, documenting the behavior will clear the confusion.

Thanks and Regards,
Vaishnav

Quoted 15 lines

-sughosh

Quoted 13 lines

Thanks and Regards,
Vaishnav

Quoted 9 lines
Quoted 9 lines

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
    #endif /* __KERNEL__ */
 
    #endif /* _LINUX_LMB_H */
#7 lore raw
Quoted 120 lines · Vaishnav Achath

Hi Sughosh,

Quoted 116 lines · Sughosh Ganu
Quoted 115 lines · Vaishnav Achath

Hi Sughosh,

Quoted 110 lines · Sughosh Ganu
Quoted 87 lines · Vaishnav Achath

Hi Prasad,

Quoted 83 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

    cmd/sf.c      | 8 ++++++++
    include/lmb.h | 5 +++++
    2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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;
 
Quoted 9 lines

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 2 lines
cmd/sf.c +2 -0
+             if (CONFIG_IS_ENABLED(LMB)) {
+                     if (lmb_read_check(addr, len)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Like I mentioned in my other reply, using a check for lmb_alloc_addr() allows for memory re-use, which is the behaviour that a large number of use cases rely on -- if you go through the test scripts, it is assumed that memory re-use is allowed. That there is no need to explicitly free up memory, and that has been how the LMB memory has been used historically. So it is allowed to use some address to load an image to that address, and then use the same address to load a different image. The LMB rework series does keep this behaviour consistent. So it would be better to change the behaviour of the tftp command to use the same API. I was planning on working on this cleanup. If you want, you can take it up.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Please let me know if you are planning to work on this in the coming few days, otherwise I can pick it up as we have platforms failing due to this.

I can take this up. Will keep you on Cc so that you can test the patches on your boards.

Thanks, I will test and report the results once you post the patches.

I have pushed a couple of patches to my github branch [1]. Can you please try these on your platforms and check if this fixes the issues that you see. I will also set up a board with network access on my end and try it out. Thanks.

-sughosh

[1] - https://github.com/sughoshg/u-boot/tree/tftp_wget_lmb_changes

Quoted 71 lines
Quoted 41 lines
Quoted 32 lines
Quoted 32 lines
Quoted 20 lines
Quoted 20 lines
cmd/sf.c +5 -0
+                             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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
    int lmb_push(struct lmb *store);
    void lmb_pop(struct lmb *store);
 
Quoted 5 lines

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 3 lines
include/lmb.h +3 -0
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+     return lmb_alloc_addr(addr, len) == addr ? 0 : -1;

who frees this? can we free this right after checking?

It is not required to explicitly free up this memory, as it is not an actual allocation per-se. Why were these functions called alloc something, I am not sure. But the point is, if you change the tftp command code to use this API instead, and then use it after a previous load, it would not fail.

Quoted 8 lines
Quoted 2 lines

-sughosh

Agreed, but the commit message says to "ensure it does not overwrite reserved memory addresses" but the implementation marks the region as reserved in the global memory map and is visible in lmb_dump_all() , which is not expected, is there really a need to mark the region as reserved.

Quoted 9 lines

What can be overwritten, and what cannot be can now be determined from the flags. If you check the LMB memory map from the bdinfo command, you will see that the regions which cannot be overwritten are now being marked with the "no-overwrite" flag. The other LMB memory which can be re-used to load multiple different images is being marked with the "none" flag. One issue with all this is that currently there is no document which explains all these concepts. I will work on adding such a document. Thanks.

Yes, documenting the behavior will clear the confusion.

Thanks and Regards,
Vaishnav

Quoted 15 lines

-sughosh

Quoted 13 lines

Thanks and Regards,
Vaishnav

Quoted 9 lines
Quoted 9 lines

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
    #endif /* __KERNEL__ */
 
    #endif /* _LINUX_LMB_H */
#8 lore raw

Hi Sughosh,

Quoted 130 lines · Sughosh Ganu
Quoted 120 lines · Vaishnav Achath

Hi Sughosh,

Quoted 116 lines · Sughosh Ganu
Quoted 115 lines · Vaishnav Achath

Hi Sughosh,

Quoted 110 lines · Sughosh Ganu
Quoted 87 lines · Vaishnav Achath

Hi Prasad,

Quoted 83 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

     cmd/sf.c      | 8 ++++++++
     include/lmb.h | 5 +++++
     2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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;
 
Quoted 9 lines

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 2 lines
cmd/sf.c +2 -0
+             if (CONFIG_IS_ENABLED(LMB)) {
+                     if (lmb_read_check(addr, len)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Like I mentioned in my other reply, using a check for lmb_alloc_addr() allows for memory re-use, which is the behaviour that a large number of use cases rely on -- if you go through the test scripts, it is assumed that memory re-use is allowed. That there is no need to explicitly free up memory, and that has been how the LMB memory has been used historically. So it is allowed to use some address to load an image to that address, and then use the same address to load a different image. The LMB rework series does keep this behaviour consistent. So it would be better to change the behaviour of the tftp command to use the same API. I was planning on working on this cleanup. If you want, you can take it up.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Please let me know if you are planning to work on this in the coming few days, otherwise I can pick it up as we have platforms failing due to this.

I can take this up. Will keep you on Cc so that you can test the patches on your boards.

Thanks, I will test and report the results once you post the patches.

Quoted 9 lines

I have pushed a couple of patches to my github branch [1]. Can you please try these on your platforms and check if this fixes the issues that you see. I will also set up a board with network access on my end and try it out. Thanks.

-sughosh

[1] - https://github.com/sughoshg/u-boot/tree/tftp_wget_lmb_changes

I tested with your patches on our failing platforms and it is working, Thanks, In the logs, initially a mmc load of remoteproc firmware happens and then Kernel/DTB load through TFTP which was failing, now it is fixed with your patches:

https://gist.github.com/vachath/bb7c8c7b5a38a58298026b831db58424

Thanks and Regards,
Vaishnav

Quoted 71 lines
Quoted 71 lines
Quoted 41 lines
Quoted 32 lines
Quoted 32 lines
Quoted 20 lines
Quoted 20 lines
cmd/sf.c +5 -0
+                             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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
     int lmb_push(struct lmb *store);
     void lmb_pop(struct lmb *store);
 
Quoted 5 lines

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 3 lines
include/lmb.h +3 -0
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+     return lmb_alloc_addr(addr, len) == addr ? 0 : -1;

who frees this? can we free this right after checking?

It is not required to explicitly free up this memory, as it is not an actual allocation per-se. Why were these functions called alloc something, I am not sure. But the point is, if you change the tftp command code to use this API instead, and then use it after a previous load, it would not fail.

Quoted 8 lines
Quoted 2 lines

-sughosh

Agreed, but the commit message says to "ensure it does not overwrite reserved memory addresses" but the implementation marks the region as reserved in the global memory map and is visible in lmb_dump_all() , which is not expected, is there really a need to mark the region as reserved.

Quoted 9 lines

What can be overwritten, and what cannot be can now be determined from the flags. If you check the LMB memory map from the bdinfo command, you will see that the regions which cannot be overwritten are now being marked with the "no-overwrite" flag. The other LMB memory which can be re-used to load multiple different images is being marked with the "none" flag. One issue with all this is that currently there is no document which explains all these concepts. I will work on adding such a document. Thanks.

Yes, documenting the behavior will clear the confusion.

Thanks and Regards,
Vaishnav

Quoted 15 lines

-sughosh

Quoted 13 lines

Thanks and Regards,
Vaishnav

Quoted 9 lines
Quoted 9 lines

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
     #endif /* __KERNEL__ */
 
     #endif /* _LINUX_LMB_H */
#9 lore raw
Quoted 141 lines · Vaishnav Achath

Hi Sughosh,

Quoted 137 lines · Sughosh Ganu
Quoted 120 lines · Vaishnav Achath

Hi Sughosh,

Quoted 116 lines · Sughosh Ganu
Quoted 115 lines · Vaishnav Achath

Hi Sughosh,

Quoted 110 lines · Sughosh Ganu
Quoted 87 lines · Vaishnav Achath

Hi Prasad,

Quoted 83 lines · Prasad Kummari

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.

---
Changes in V4:
- Removed do_spi_read_lmb_check().
- Added the lmb_read_check() function in lmb.c, making it reusable for NAND, MMC, etc.
- Addressed review comments.

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.

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...

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...
Versal NET> sf erase 0x0 0x1000000;mw.b 0x8000 aabbccdd 0x1000000;sf write 0x8000 0x0 0x1000000;mw.b 0x8008000 0x0 0x1000000;sf read 0x8008000 0x0 0x1000000;cmp.b 0x8000 0x8008000 0x01000000
SF: 16777216 bytes @ 0x0 Erased: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Written: OK
device 0 offset 0x0, size 0x1000000
SF: 16777216 bytes @ 0x0 Read: OK
Total of 16777216 byte(s) were the same

     cmd/sf.c      | 8 ++++++++
     include/lmb.h | 5 +++++
     2 files changed, 13 insertions(+)
cmd/sf.c +1 -0
index f43a2e08b3..08e364e191 100644
@@ -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;
 
Quoted 9 lines

cmd/sf.c static int do_spi_flash_read_write(int argc, char *const argv[]) · line 322

Quoted 2 lines
cmd/sf.c +2 -0
+             if (CONFIG_IS_ENABLED(LMB)) {
+                     if (lmb_read_check(addr, len)) {

Even though the function is named lmb_read_check(), it performs an alloc which is never freed, thus it makes it difficult for other callers to use the same region for other purposes (some callers use lmb_get_free_size() ), as mentioned in the commit message the check is only to prevent sf from overwriting reserved region, but it looks like this patch makes the load region also as reserved, is this necessary?

Like I mentioned in my other reply, using a check for lmb_alloc_addr() allows for memory re-use, which is the behaviour that a large number of use cases rely on -- if you go through the test scripts, it is assumed that memory re-use is allowed. That there is no need to explicitly free up memory, and that has been how the LMB memory has been used historically. So it is allowed to use some address to load an image to that address, and then use the same address to load a different image. The LMB rework series does keep this behaviour consistent. So it would be better to change the behaviour of the tftp command to use the same API. I was planning on working on this cleanup. If you want, you can take it up.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Please let me know if you are planning to work on this in the coming few days, otherwise I can pick it up as we have platforms failing due to this.

I can take this up. Will keep you on Cc so that you can test the patches on your boards.

Thanks, I will test and report the results once you post the patches.

I have pushed a couple of patches to my github branch [1]. Can you please try these on your platforms and check if this fixes the issues that you see. I will also set up a board with network access on my end and try it out. Thanks.

-sughosh

Quoted 7 lines
Quoted 2 lines

[1] - https://github.com/sughoshg/u-boot/tree/tftp_wget_lmb_changes

I tested with your patches on our failing platforms and it is working, Thanks, In the logs, initially a mmc load of remoteproc firmware happens and then Kernel/DTB load through TFTP which was failing, now it is fixed with your patches:

Thanks much for testing the patches! I have put the patches through CI. Will post them on the list once the CI completes.

-sughosh

Quoted 77 lines

https://gist.github.com/vachath/bb7c8c7b5a38a58298026b831db58424

Thanks and Regards,
Vaishnav

Quoted 71 lines
Quoted 71 lines
Quoted 41 lines
Quoted 32 lines
Quoted 32 lines
Quoted 20 lines
Quoted 20 lines
cmd/sf.c +5 -0
+                             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);
include/lmb.h +0 -0
index fc2daaa7bf..aee2f9fcda 100644
@@ -111,6 +111,11 @@ struct lmb *lmb_get(void);
     int lmb_push(struct lmb *store);
     void lmb_pop(struct lmb *store);
 
Quoted 5 lines

include/lmb.h struct lmb *lmb_get(void); · line 116

Quoted 3 lines
include/lmb.h +3 -0
+static inline int lmb_read_check(phys_addr_t addr, phys_size_t len)
+{
+     return lmb_alloc_addr(addr, len) == addr ? 0 : -1;

who frees this? can we free this right after checking?

It is not required to explicitly free up this memory, as it is not an actual allocation per-se. Why were these functions called alloc something, I am not sure. But the point is, if you change the tftp command code to use this API instead, and then use it after a previous load, it would not fail.

Quoted 8 lines
Quoted 2 lines

-sughosh

Agreed, but the commit message says to "ensure it does not overwrite reserved memory addresses" but the implementation marks the region as reserved in the global memory map and is visible in lmb_dump_all() , which is not expected, is there really a need to mark the region as reserved.

Quoted 9 lines

What can be overwritten, and what cannot be can now be determined from the flags. If you check the LMB memory map from the bdinfo command, you will see that the regions which cannot be overwritten are now being marked with the "no-overwrite" flag. The other LMB memory which can be re-used to load multiple different images is being marked with the "none" flag. One issue with all this is that currently there is no document which explains all these concepts. I will work on adding such a document. Thanks.

Yes, documenting the behavior will clear the confusion.

Thanks and Regards,
Vaishnav

Quoted 15 lines

-sughosh

Quoted 13 lines

Thanks and Regards,
Vaishnav

Quoted 9 lines
Quoted 9 lines

Thanks and Regards,
Vaishnav

Quoted 5 lines
include/lmb.h +2 -0
+}
+
     #endif /* __KERNEL__ */
 
     #endif /* _LINUX_LMB_H */
#10 lore raw
In reply to Prasad Kummari
Quoted 9 lines · Prasad Kummari

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.

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom