Skip to contentSkip to conversation

loads: Block writes into LMB reserved areas of U-Boot

PATCH In review [email protected] 4 participants 6 messages Oct 10, 2021 – Oct 26, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 loads: Block writes into LMB reserved areas of U-Boot

From: Marek Vasut <[email protected]>

The loads srec loading may overwrite piece of U-Boot accidentally. Prevent that by using LMB to detect whether upcoming write would overwrite piece of reserved U-Boot code, and if that is the case, abort the srec loading.

---
 cmd/load.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
cmd/load.c +12 -0
index 249ebd4ae0..7e4a552d90 100644
@@ -16,6 +16,7 @@
 #include <exports.h>
 #include <flash.h>
 #include <image.h>
+#include <lmb.h>
 #include <mapmem.h>
 #include <net.h>
 #include <s_record.h>
@@ -137,6 +138,7 @@ static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
 
 static ulong load_serial(long offset)
 {
+	struct lmb lmb;
 	char	record[SREC_MAXRECLEN + 1];	/* buffer for one S-Record	*/
 	char	binbuf[SREC_MAXBINLEN];		/* buffer for binary data	*/
 	int	binlen;				/* no. of data bytes in S-Rec.	*/
@@ -147,6 +149,9 @@ static ulong load_serial(long offset)
 	ulong	start_addr = ~0;
 	ulong	end_addr   =  0;
 	int	line_count =  0;
+	long ret;
+
+	lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
 
 	while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
 		type = srec_decode(record, &binlen, &addr, binbuf);
@@ -172,7 +177,14 @@ static ulong load_serial(long offset)
 		    } else
 #endif
 		    {
+			ret = lmb_reserve(&lmb, store_addr, binlen);
+			if (ret) {
+				printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
+					store_addr, store_addr + binlen);
+				return ret;
+			}
 			memcpy((char *)(store_addr), binbuf, binlen);
+			lmb_free(&lmb, store_addr, binlen);
 		    }
 		    if ((store_addr) < start_addr)
 			start_addr = store_addr;
Signature · 1 line
2.33.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Marek,

Quoted 61 lines · On Sun, 10 Oct 2021 at 15:52, <[email protected]> wrote:

From: Marek Vasut <[email protected]>

The loads srec loading may overwrite piece of U-Boot accidentally. Prevent that by using LMB to detect whether upcoming write would overwrite piece of reserved U-Boot code, and if that is the case, abort the srec loading.

---
 cmd/load.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
cmd/load.c +12 -1
index 249ebd4ae0..7e4a552d90 100644
@@ -16,6 +16,7 @@
 #include <exports.h>
 #include <flash.h>
 #include <image.h>
+#include <lmb.h>
 #include <mapmem.h>
 #include <net.h>
 #include <s_record.h>
@@ -137,6 +138,7 @@ static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
 
 static ulong load_serial(long offset)
 {
+       struct lmb lmb;
        char    record[SREC_MAXRECLEN + 1];     /* buffer for one S-Record      */
        char    binbuf[SREC_MAXBINLEN];         /* buffer for binary data       */
        int     binlen;                         /* no. of data bytes in S-Rec.  */
@@ -147,6 +149,9 @@ static ulong load_serial(long offset)
        ulong   start_addr = ~0;
        ulong   end_addr   =  0;
        int     line_count =  0;
+       long ret;
+
+       lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
 
        while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
                type = srec_decode(record, &binlen, &addr, binbuf);
@@ -172,7 +177,14 @@ static ulong load_serial(long offset)
                    } else
 #endif
                    {
+                       ret = lmb_reserve(&lmb, store_addr, binlen);
+                       if (ret) {
+                               printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
+                                       store_addr, store_addr + binlen);
+                               return ret;
+                       }
                        memcpy((char *)(store_addr), binbuf, binlen);
+                       lmb_free(&lmb, store_addr, binlen);
                    }
                    if ((store_addr) < start_addr)
                        start_addr = store_addr;
--
Quoted 2 lines

2.33.0

This code looks OK but I don't know what lmb_reserve() and lmb_free() do. Can you add comments to the header file?

Regards,
Simon

#2 lore raw

On 10/14/21 5:10 PM, Simon Glass wrote:
[...]

Quoted 39 lines
Quoted 34 lines
cmd/load.c +11 -1
@@ -137,6 +138,7 @@ static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
 
  static ulong load_serial(long offset)
  {
+       struct lmb lmb;
         char    record[SREC_MAXRECLEN + 1];     /* buffer for one S-Record      */
         char    binbuf[SREC_MAXBINLEN];         /* buffer for binary data       */
         int     binlen;                         /* no. of data bytes in S-Rec.  */
@@ -147,6 +149,9 @@ static ulong load_serial(long offset)
         ulong   start_addr = ~0;
         ulong   end_addr   =  0;
         int     line_count =  0;
+       long ret;
+
+       lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
 
         while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
                 type = srec_decode(record, &binlen, &addr, binbuf);
@@ -172,7 +177,14 @@ static ulong load_serial(long offset)
                     } else
  #endif
                     {
+                       ret = lmb_reserve(&lmb, store_addr, binlen);
+                       if (ret) {
+                               printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
+                                       store_addr, store_addr + binlen);
+                               return ret;
+                       }
                         memcpy((char *)(store_addr), binbuf, binlen);
+                       lmb_free(&lmb, store_addr, binlen);
                     }
                     if ((store_addr) < start_addr)
                         start_addr = store_addr;
--
Quoted 2 lines

2.33.0

Quoted 2 lines

This code looks OK but I don't know what lmb_reserve() and lmb_free() do. Can you add comments to the header file?

Not here, the entire LMB stuff needs (better) documentation, that's where (all) such clarification should go.

#3 lore raw
Quoted 41 lines · Marek Vasut

On 10/14/21 5:10 PM, Simon Glass wrote:
[...]

Quoted 39 lines
Quoted 34 lines
cmd/load.c +11 -1
@@ -137,6 +138,7 @@ static int do_load_serial(struct cmd_tbl *cmdtp, int flag, int argc,
 
  static ulong load_serial(long offset)
  {
+       struct lmb lmb;
         char    record[SREC_MAXRECLEN + 1];     /* buffer for one S-Record      */
         char    binbuf[SREC_MAXBINLEN];         /* buffer for binary data       */
         int     binlen;                         /* no. of data bytes in S-Rec.  */
@@ -147,6 +149,9 @@ static ulong load_serial(long offset)
         ulong   start_addr = ~0;
         ulong   end_addr   =  0;
         int     line_count =  0;
+       long ret;
+
+       lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
 
         while (read_record(record, SREC_MAXRECLEN + 1) >= 0) {
                 type = srec_decode(record, &binlen, &addr, binbuf);
@@ -172,7 +177,14 @@ static ulong load_serial(long offset)
                     } else
  #endif
                     {
+                       ret = lmb_reserve(&lmb, store_addr, binlen);
+                       if (ret) {
+                               printf("\nCannot overwrite reserved area (%08lx..%08lx)\n",
+                                       store_addr, store_addr + binlen);
+                               return ret;
+                       }
                         memcpy((char *)(store_addr), binbuf, binlen);
+                       lmb_free(&lmb, store_addr, binlen);
                     }
                     if ((store_addr) < start_addr)
                         start_addr = store_addr;
--
Quoted 2 lines

2.33.0

Quoted 5 lines
Quoted 2 lines

This code looks OK but I don't know what lmb_reserve() and lmb_free() do. Can you add comments to the header file?

Not here, the entire LMB stuff needs (better) documentation, that's where (all) such clarification should go.

Is that you saying you'll do such a follow-up patch, given you've touched this code the most of late?

Signature · 1 line
Tom
#4 lore raw

On 10/15/21 6:09 PM, Tom Rini wrote:

[...]

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This code looks OK but I don't know what lmb_reserve() and lmb_free() do. Can you add comments to the header file?

Not here, the entire LMB stuff needs (better) documentation, that's where (all) such clarification should go.

Is that you saying you'll do such a follow-up patch, given you've touched this code the most of late?

Maybe, I won't promise you anything except I add it to my roll of todo paper.

#5 lore raw
In reply to [email protected]
Quoted 11 lines · On Sun, Oct 10, 2021 at 11:52:41PM +0200, [email protected] wrote:

From: Marek Vasut <[email protected]>

The loads srec loading may overwrite piece of U-Boot accidentally. Prevent that by using LMB to detect whether upcoming write would overwrite piece of reserved U-Boot code, and if that is the case, abort the srec loading.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom