From 5c51b2b597557e8981efe18f136e2304eeb2f364 Mon Sep 17 00:00:00 2001
From: Vyacheslav Bocharov <adeep@lexina.in>
Date: Tue, 5 Jul 2022 19:06:25 +0300
Subject: [PATCH 5/7] Add usid/serial/mac read from emmc

---
 board/amlogic/jethub-j100/jethub-j100.c | 63 +++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 3 deletions(-)

diff --git a/board/amlogic/jethub-j100/jethub-j100.c b/board/amlogic/jethub-j100/jethub-j100.c
index 6a2c4ad4c3..41ef5db493 100644
--- a/board/amlogic/jethub-j100/jethub-j100.c
+++ b/board/amlogic/jethub-j100/jethub-j100.c
@@ -5,6 +5,7 @@
  */
 
 #include <common.h>
+#include <command.h>
 #include <dm.h>
 #include <init.h>
 #include <net.h>
@@ -20,8 +21,65 @@ int misc_init_r(void)
 	u8 mac_addr[ARP_HLEN];
 	char serial[SM_SERIAL_SIZE];
 	u32 sid;
+	int ret;
 
-	if (!meson_sm_get_serial(serial, SM_SERIAL_SIZE)) {
+	char _cmdbuf[96];
+	char keyname[32];
+	char keydata[256];
+	int ver=0;
+
+	memset (mac_addr,0, sizeof(mac_addr));
+	sprintf(_cmdbuf, "store init");
+	if(!run_command(_cmdbuf, 0))
+	{
+		sprintf(_cmdbuf, "keyman init 0x1234");
+		if(!run_command(_cmdbuf, 0))
+		{
+			strcpy(keyname, "usid");
+			memset (keydata, 0, sizeof(keydata));
+			sprintf(_cmdbuf, "keyman read %s %p str", keyname, keydata);
+			ret = run_command(_cmdbuf, 0);
+			if (!ret)
+			{
+			// j100__04012201sw00016142005c
+			// 0123456789
+			  if (keydata[0] == 'j')
+			    {
+			      if (keydata[1] == '1')
+			      {
+				sprintf(_cmdbuf, "%c%c",keydata[6],keydata[7]);
+				env_set("hwrev", _cmdbuf);
+				sprintf(_cmdbuf, "%c%c",keydata[8],keydata[9]);
+				env_set("perev", _cmdbuf);
+			      }
+			    }			
+			}
+			// get serial
+			strcpy(keyname, "serial");
+			memset (keydata, 0, sizeof(keydata));
+			sprintf(_cmdbuf, "keyman read %s %p str", keyname, keydata);
+			ret = run_command(_cmdbuf, 0);
+
+			// get mac
+			strcpy(keyname, "mac");
+			memset (keydata, 0, sizeof(keydata));
+			sprintf(_cmdbuf, "keyman read %s %#p str", keyname, keydata);
+			ret = run_command(_cmdbuf, 0);
+			if (keydata[2]==':') 
+			{
+				keydata[17] = (char) 0x00;
+				sprintf(_cmdbuf,"env set ethaddr %s", keydata);
+				ret = run_command(_cmdbuf, 0);
+				mac_addr[0] = (char) 0x01;
+			} else 
+			{
+				printf("keyman read mac failed\n");
+			}
+		}
+	}
+
+	if (mac_addr[0]==0)
+	  if (!meson_sm_get_serial(serial, SM_SERIAL_SIZE)) {
 		sid = crc32(0, (unsigned char *)serial, SM_SERIAL_SIZE);
 		/* Ensure the NIC specific bytes of the mac are not all 0 */
 		if ((sid & 0xffff) == 0)
@@ -34,9 +92,8 @@ int misc_init_r(void)
 		mac_addr[3] = (sid >> 16) & 0xff;
 		mac_addr[4] = (sid >>  8) & 0xff;
 		mac_addr[5] = (sid >>  0) & 0xff;
-
 		eth_env_set_enetaddr("ethaddr", mac_addr);
-	}
+	  }
 
 	return 0;
 }
-- 
2.34.1

