[U-Boot] [PATCH v2] memsize: Fix for bug in memory sizing code

Gerd Hoffmann kraxel at redhat.com
Tue Oct 21 18:49:13 CEST 2014


The original memory sizing code in get_ram_size clobbers the word
at the base address, but forgets to restore it.

Unlike the (reverted) commit b8496cced856ff411f1eb2e4eff20f5abe7080b0
we'll go save and restore the base address value at the start and then
end of the function.  It needs to stay cleared until the detection is
done, otherwise we'll fail to detect the same piece of memory being
mapped multiple times into the address space.

Cc: Wolfgang Denk <wd at denx.de>
Cc: Iwo Mergler <Iwo.Mergler at netcommwireless.com>
Signed-off-by: Gerd Hoffmann <kraxel at redhat.com>
---
 common/memsize.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/common/memsize.c b/common/memsize.c
index 589400d..72d0156 100644
--- a/common/memsize.c
+++ b/common/memsize.c
@@ -27,12 +27,15 @@ DECLARE_GLOBAL_DATA_PTR;
 long get_ram_size(long *base, long maxsize)
 {
 	volatile long *addr;
-	long           save[32];
+	long           save[32], save_base;
 	long           cnt;
 	long           val;
 	long           size;
 	int            i = 0;
 
+	save_base = base[0];
+	sync ();
+
 	for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
 		addr = base + cnt;	/* pointer arith! */
 		sync ();
@@ -43,8 +46,6 @@ long get_ram_size(long *base, long maxsize)
 
 	addr = base;
 	sync ();
-	save[i] = *addr;
-	sync ();
 	*addr = 0;
 
 	sync ();
@@ -52,12 +53,12 @@ long get_ram_size(long *base, long maxsize)
 		/* Restore the original data before leaving the function.
 		 */
 		sync ();
-		*addr = save[i];
 		for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
 			addr  = base + cnt;
 			sync ();
 			*addr = save[--i];
 		}
+		base[0] = save_base;
 		return (0);
 	}
 
@@ -73,10 +74,12 @@ long get_ram_size(long *base, long maxsize)
 				addr  = base + cnt;
 				*addr = save[--i];
 			}
+			base[0] = save_base;
 			return (size);
 		}
 	}
 
+	base[0] = save_base;
 	return (maxsize);
 }
 
-- 
1.8.3.1



More information about the U-Boot mailing list