Merge branch '2019-10-28-azure-ci-support'
[oweals/u-boot.git] / common / memsize.c
index 0fb9ba57b62b21d99df1fc8214c67a0016d1e63b..13b004778667f98d15b6f8dfbcbdea808ab0e0a8 100644 (file)
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2004
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -27,7 +26,8 @@ DECLARE_GLOBAL_DATA_PTR;
 long get_ram_size(long *base, long maxsize)
 {
        volatile long *addr;
-       long           save[32];
+       long           save[BITS_PER_LONG - 1];
+       long           save_base;
        long           cnt;
        long           val;
        long           size;
@@ -43,7 +43,7 @@ long get_ram_size(long *base, long maxsize)
 
        addr = base;
        sync();
-       save[i] = *addr;
+       save_base = *addr;
        sync();
        *addr = 0;
 
@@ -51,7 +51,7 @@ long get_ram_size(long *base, long maxsize)
        if ((val = *addr) != 0) {
                /* Restore the original data before leaving the function. */
                sync();
-               *addr = save[i];
+               *base = save_base;
                for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
                        addr  = base + cnt;
                        sync();
@@ -76,9 +76,16 @@ long get_ram_size(long *base, long maxsize)
                                addr  = base + cnt;
                                *addr = save[--i];
                        }
+                       /* warning: don't restore save_base in this case,
+                        * it is already done in the loop because
+                        * base and base+size share the same physical memory
+                        * and *base is saved after *(base+size) modification
+                        * in first loop
+                        */
                        return (size);
                }
        }
+       *base = save_base;
 
        return (maxsize);
 }