Avoid initrd and logbuffer area overlaps

Add logbuffer to reserved LMB areas to prevent initrd allocation
from overlaping with it.

Make sure to use correct logbuffer base address.

Signed-off-by: Marian Balakowicz <m8@semihalf.com>
diff --git a/common/cmd_log.c b/common/cmd_log.c
index c6e72ac..fdcc575 100644
--- a/common/cmd_log.c
+++ b/common/cmd_log.c
@@ -66,6 +66,12 @@
 #endif
 static char *lbuf;
 
+unsigned long __logbuffer_base(void)
+{
+	return CFG_SDRAM_BASE + gd->bd->bi_memsize - LOGBUFF_LEN;
+}
+unsigned long logbuffer_base (void) __attribute__((weak, alias("__logbuffer_base")));
+
 void logbuff_init_ptrs (void)
 {
 	unsigned long tag, post_word;
@@ -75,7 +81,7 @@
 	log = (logbuff_t *)CONFIG_ALT_LH_ADDR;
 	lbuf = (char *)CONFIG_ALT_LB_ADDR;
 #else
-	log = (logbuff_t *)(gd->bd->bi_memsize-LOGBUFF_LEN) - 1;
+	log = (logbuff_t *)(logbuffer_base ()) - 1;
 	lbuf = (char *)log->buf;
 #endif
 
diff --git a/common/image.c b/common/image.c
index 67e594d..9188024 100644
--- a/common/image.c
+++ b/common/image.c
@@ -35,6 +35,10 @@
 #include <dataflash.h>
 #endif
 
+#ifdef CONFIG_LOGBUFFER
+#include <logbuff.h>
+#endif
+
 #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
 #include <rtc.h>
 #endif
@@ -1013,6 +1017,12 @@
 		initrd_high = ~0;
 	}
 
+
+#ifdef CONFIG_LOGBUFFER
+	/* Prevent initrd from overwriting logbuffer */
+	lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
+#endif
+
 	debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
 			initrd_high, initrd_copy_to_ram);
 
diff --git a/include/logbuff.h b/include/logbuff.h
index d06d208..ae7908c 100644
--- a/include/logbuff.h
+++ b/include/logbuff.h
@@ -58,6 +58,7 @@
 void logbuff_init_ptrs (void);
 void logbuff_log(char *msg);
 void logbuff_reset (void);
+unsigned long logbuffer_base (void);
 
 #endif /* CONFIG_LOGBUFFER */
 
diff --git a/lib_ppc/board.c b/lib_ppc/board.c
index 6f7242d..a908831 100644
--- a/lib_ppc/board.c
+++ b/lib_ppc/board.c
@@ -396,6 +396,13 @@
  ************************************************************************
  */
 
+#ifdef CONFIG_LOGBUFFER
+unsigned long logbuffer_base(void)
+{
+	return CFG_SDRAM_BASE + get_effective_memsize() - LOGBUFF_LEN;
+}
+#endif
+
 void board_init_f (ulong bootflag)
 {
 	bd_t *bd;