SoC common: save bl30 version info with blx and dump with build_message [1/5]

PD#SWPL-160577

Problem:
Save bl30 version info with blx and dump with build_message.

Solution:
Save bl30 version info with blx and dump with build_message.

Verify:
a5_av409, s5_ax201, t5m_ay301, a4_ba400, t3x_bc302, txhd2_be319

Change-Id: I9784129e2dba67ad60511f105b0237fecee59062
Signed-off-by: bangzheng.liu <bangzheng.liu@amlogic.com>
diff --git a/at301_t962d4/Kconfig b/at301_t962d4/Kconfig
index b9e76b3..5a6c575 100644
--- a/at301_t962d4/Kconfig
+++ b/at301_t962d4/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/at309_t962d4/Kconfig b/at309_t962d4/Kconfig
index 4d18811..a4b8540 100644
--- a/at309_t962d4/Kconfig
+++ b/at309_t962d4/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/av400_a113x2/Kconfig b/av400_a113x2/Kconfig
index b3f60c5..89805a7 100644
--- a/av400_a113x2/Kconfig
+++ b/av400_a113x2/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/av409_a113x2/Kconfig b/av409_a113x2/Kconfig
index 5d00f10..b6c263d 100644
--- a/av409_a113x2/Kconfig
+++ b/av409_a113x2/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/ax201_s928x/Kconfig b/ax201_s928x/Kconfig
index 2a92b68..ed8b213 100644
--- a/ax201_s928x/Kconfig
+++ b/ax201_s928x/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/ax208_s928x/Kconfig b/ax208_s928x/Kconfig
index 544ed6b..f1a8ba5 100644
--- a/ax208_s928x/Kconfig
+++ b/ax208_s928x/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/ax209_s928x/Kconfig b/ax209_s928x/Kconfig
index c3a5e75..21489c5 100644
--- a/ax209_s928x/Kconfig
+++ b/ax209_s928x/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/ay301_t963d4/Kconfig b/ay301_t963d4/Kconfig
index b987d4a..83d0375 100644
--- a/ay301_t963d4/Kconfig
+++ b/ay301_t963d4/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/ay309_t963d4/Kconfig b/ay309_t963d4/Kconfig
index caed49d..10e3b2c 100644
--- a/ay309_t963d4/Kconfig
+++ b/ay309_t963d4/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/ba400_a113l2/Kconfig b/ba400_a113l2/Kconfig
index af029bc..4fa7b51 100644
--- a/ba400_a113l2/Kconfig
+++ b/ba400_a113l2/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/ba409_a113l2/Kconfig b/ba409_a113l2/Kconfig
index 36cdfae..0acbb21 100644
--- a/ba409_a113l2/Kconfig
+++ b/ba409_a113l2/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/bc302_t968d4/Kconfig b/bc302_t968d4/Kconfig
index 20833ba..a6ab169 100644
--- a/bc302_t968d4/Kconfig
+++ b/bc302_t968d4/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/bc303_t968d4/Kconfig b/bc303_t968d4/Kconfig
index 1d0acf3..eb68c84 100644
--- a/bc303_t968d4/Kconfig
+++ b/bc303_t968d4/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/bc309_t968d4/Kconfig b/bc309_t968d4/Kconfig
index dafdeb2..b0c1500 100644
--- a/bc309_t968d4/Kconfig
+++ b/bc309_t968d4/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC
diff --git a/be301_t950s/Kconfig b/be301_t950s/Kconfig
index 6252fd4..630ca1d 100644
--- a/be301_t950s/Kconfig
+++ b/be301_t950s/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 #	select STICK_MEM
 	select UART
 	select CEC
diff --git a/be309_t950s/Kconfig b/be309_t950s/Kconfig
index 00d02cb..ed8cea4 100644
--- a/be309_t950s/Kconfig
+++ b/be309_t950s/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 #	select STICK_MEM
 	select UART
 	select CEC
diff --git a/be311_t950s/Kconfig b/be311_t950s/Kconfig
index de7af9d..6bd4523 100644
--- a/be311_t950s/Kconfig
+++ b/be311_t950s/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 #	select STICK_MEM
 	select UART
 	select CEC
diff --git a/be319_t950s/Kconfig b/be319_t950s/Kconfig
index 3b0a098..11aa804 100644
--- a/be319_t950s/Kconfig
+++ b/be319_t950s/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 #	select STICK_MEM
 	select UART
 	select CEC
diff --git a/board_version.c b/board_version.c
index 0ad42c1..7b64009 100644
--- a/board_version.c
+++ b/board_version.c
@@ -8,7 +8,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <string.h>
-#include "save_version.h"
+#include <errno.h>
 #endif
 #include "board_version.h"
 #include "sdk_ver.h"
@@ -20,38 +20,55 @@
 }
 
 #ifdef CONFIG_BL30_VERSION_SAVE
+#define BL30_VERSION_LEN_MAX 400
+#ifdef BL2E_VER_BUFF_BASE_ADDR
 #define TAG_NAME "BL30: "
+#endif
 int version_string(char *buf)
 {
 	char *tmp_buf;
 	int size = 0;
 
-	if (buf)
-		tmp_buf = buf;
+	if (!buf) {
+		printf("bl30 version string is NULL\n");
+		return -EINVAL;
+	}
+
+	tmp_buf = buf;
 
 	if (strlen(CONFIG_COMPILE_TIME)) {
+#ifdef BL2E_VER_BUFF_BASE_ADDR
 		size += strlen(TAG_NAME);
+#endif
+		size += strlen(CONFIG_COMPILE_TIME);
+		size += 1;
+		if (size > BL30_VERSION_LEN_MAX)
+			return -ENOMEM;
+#ifdef BL2E_VER_BUFF_BASE_ADDR
 		memcpy((void *)tmp_buf, (void *)TAG_NAME, strlen(TAG_NAME));
 		tmp_buf += strlen(TAG_NAME);
-
-		size += strlen(CONFIG_COMPILE_TIME);
+#endif
 		memcpy((void *)tmp_buf, (void *)CONFIG_COMPILE_TIME, strlen(CONFIG_COMPILE_TIME));
 		tmp_buf += strlen(CONFIG_COMPILE_TIME);
 
 		memcpy((void *)tmp_buf, (void *)"\n", 1);
 		tmp_buf += 1;
-		size += 1;
 	}
 
 	for (int i = 0; i < sizeof(version_map) / sizeof(const char *); i++) {
+		size += 2;
+		size += strlen(version_map[i]);
+		if (size > BL30_VERSION_LEN_MAX)
+			return -ENOMEM;
+
+		memcpy((void *)tmp_buf, (void *)"\t", 1);
+		tmp_buf += 1;
+
 		memcpy((void *)tmp_buf, (void *)version_map[i], strlen(version_map[i]));
 		tmp_buf += strlen(version_map[i]);
-		size += strlen(version_map[i]);
 
 		memcpy((void *)tmp_buf, "\n", 1);
 		tmp_buf += 1;
-		size += 1;
-
 	}
 
 	return size;
@@ -60,10 +77,11 @@
 /*******************************************************************************
  * Save build message
  ******************************************************************************/
+#ifdef BL2E_VER_BUFF_BASE_ADDR
 int bl30_plat_save_version(void)
 {
 	struct build_messages_t *build_info;
-	char rs[400];
+	char rs[BL30_VERSION_LEN_MAX] = {0};
 	int size;
 
 	build_info = (struct build_messages_t *)(uintptr_t)(BL2E_VER_BUFF_BASE_ADDR);
@@ -72,7 +90,7 @@
 		build_info->h.version != VERSION_1 ||
 		build_info->h.size != sizeof(struct build_messages_t)) {
 		printf("invalid build messages info head!\n");
-		return -1;
+		return -EINVAL;
 	}
 
 	if ((sizeof(struct build_messages_t)
@@ -84,7 +102,7 @@
 			+ version_string(rs))
 			> BL2E_VER_BUFF_SIZE) {
 		printf("invalid bl30 build messages size!\n");
-		return -1;
+		return -EINVAL;
 	}
 
 	if (build_info->bl32_message_addr && build_info->bl32_message_size)
@@ -122,4 +140,51 @@
 			build_info->bl30_message_size);
 	return 0;
 }
+#else
+/* Applied to save bl30 version info to ddr with blx version info
+ * when dump build message in uboot console.
+ */
+#include "mailbox-api.h"
+#define GET_BL30_VERSION_SIZE 0x30303000
+#define GET_BL30_VERSION_INFO 0x30303001
+#define BL30_VERSION_LEN_ONCE MHU_DATA_SIZE
+
+static uint32_t ver_get_size;
+static void *xMboxBL30VersionSave(void *msg)
+{
+	char vs[BL30_VERSION_LEN_MAX] = {0};
+	int size;
+
+	size = version_string(vs);
+	if (size == -ENOMEM || size > BL30_VERSION_LEN_MAX ||
+		ver_get_size >= BL30_VERSION_LEN_MAX) {
+		printf("bl30 version message get fail: length overflow\n");
+		return NULL;
+	}
+
+	if (*(uint32_t *)msg == GET_BL30_VERSION_SIZE) {
+		*(uint32_t *)msg = size;
+		ver_get_size = 0;
+	} else if (*(uint32_t *)msg == GET_BL30_VERSION_INFO) {
+		memcpy(msg, &vs[ver_get_size], BL30_VERSION_LEN_ONCE);
+		ver_get_size += BL30_VERSION_LEN_ONCE;
+	}
+
+	return NULL;
+}
+
+int bl30_plat_save_version(void)
+{
+	int ret;
+
+	ret = xInstallRemoteMessageCallbackFeedBack(AOREE_CHANNEL,
+		MBX_CMD_SAVE_BL30_VERSION, xMboxBL30VersionSave, 1);
+	if (ret == MBOX_CALL_MAX) {
+		printf("mbox cmd 0x%x register fail\n", MBX_CMD_SAVE_BL30_VERSION);
+		return -ENOTSUP;
+	}
+
+	return 0;
+}
 #endif
+#endif /* end CONFIG_BL30_VERSION_SAVE */
diff --git a/t3x_sky/Kconfig b/t3x_sky/Kconfig
index ceb37aa..6447468 100644
--- a/t3x_sky/Kconfig
+++ b/t3x_sky/Kconfig
@@ -8,6 +8,7 @@
 	select LIBC_AML
 	select COMMON
 	select GET_VERSION
+	select BL30_VERSION_SAVE
 	select STICK_MEM
 	select UART
 	select CEC