From ba165a1206cf62e81a83b02f192854318e50c3e4 Mon Sep 17 00:00:00 2001
From: "jeffrey.wu" <jeffrey.wu@sigmastar.com.tw>
Date: Tue, 26 Jan 2021 11:24:57 +0800
Subject: [PATCH] [P3][boot]support batch mac address burning

[rootcause]:NA
[solution]:support batch mac address burning
[sideeffect]:NA
[selftestlist]:
1. build ok
2. function ok
3. NA
[selftestresult]: P
[ticket]: mantis_1694195
[redflag]: N
---
 common/autoboot.c          |  15 ++++-
 common/cmd_mstar.c         | 121 +++++++++++++++++++++++++++++++++++--
 drivers/mstar/Kconfig      |   3 +
 include/configs/pioneer3.h |   1 +
 4 files changed, 135 insertions(+), 5 deletions(-)

diff --git a/common/autoboot.c b/common/autoboot.c
index 6949ccf..2840468 100755
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -371,7 +371,6 @@ void autoboot_command(const char *s)
 #endif
 	}
 
-
 #ifdef CONFIG_MENUKEY
 	if (menukey == CONFIG_MENUKEY) {
 		s = getenv("menucmd");
@@ -379,4 +378,18 @@ void autoboot_command(const char *s)
 			run_command_list(s, -1, 0);
 	}
 #endif /* CONFIG_MENUKEY */
+
+#ifdef CONFIG_MS_MAC_BURN
+    env = getenv("MacReadDev");
+    if(!strcmp(env, "usb") || !strcmp(env, "mmc"))
+    {
+        run_command("macburn", 0);
+    }
+    else
+    {
+        run_command("setenv MacReadDev mmc", 0);
+        run_command("saveenv", 0);
+        run_command("macburn", 0);
+    }
+#endif
 }
diff --git a/common/cmd_mstar.c b/common/cmd_mstar.c
index cde8744..1746020 100755
--- a/common/cmd_mstar.c
+++ b/common/cmd_mstar.c
@@ -20,10 +20,12 @@
 #define ENV_IPADDR                  "ipaddr"
 #define ENV_SERVERIP                "serverip"
 #define ENV_PARTNO                  "partno"
-#define ENV_USB_UPGRADEIMAGE               "UsbUpgradeImage"
-#define ENV_SD_UPGRADEIMAGE               "SdUpgradeImage"
-#define ENV_EMMC_UPGRADEIMAGE               "EmmcUpgradeImage"
-#define ENV_USBUPGRADEPORT               "UpgradePort"
+#define ENV_USB_UPGRADEIMAGE        "UsbUpgradeImage"
+#define ENV_SD_UPGRADEIMAGE         "SdUpgradeImage"
+#define ENV_EMMC_UPGRADEIMAGE       "EmmcUpgradeImage"
+#define ENV_USBUPGRADEPORT          "UpgradePort"
+#define ENV_MAC_READ_DEV            "MacReadDev"
+#define ENV_MAC_INDEX_FILE          "MacIndexFile"
 
 #define DEFAULT_BOOTDELAY           "1"                 // boot delay
 #define DEFAULT_IPADDR              "192.168.0.28"      // target (debug) board IP
@@ -1067,6 +1069,117 @@ U_BOOT_CMD(
 
 #endif
 
+#if defined(CONFIG_MS_MAC_BURN)
+int do_macburn (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+    int ret = 0;
+    int cur_index = 0, next_index = 0;
+    int usb_dev = 0;
+    char tmp[64];
+    char mac_index[4] = {0};
+    char mac_addr[18] = {0};
+    char* mac_dev = NULL;
+    char* index_file = NULL;
+    char* UpgradePort = NULL;
+
+    mac_dev = getenv(ENV_MAC_READ_DEV);
+    if(mac_dev == NULL) {
+        printf("ENV_MAC_READ_DEV env is null!\n");
+        run_command("setenv MacReadDev mmc", 0);
+        run_command("saveenv",0);
+        return -1;
+    }
+
+    if (!strcmp(mac_dev, "mmc")) {
+        printf("read mac addr from mmc device!\n");
+        usb_dev = 0;
+    } else if (!strcmp(mac_dev, "usb")) {
+        printf("read mac addr from usb device!\n");
+        usb_dev = 1;
+    }else {
+        printf("not find valid device!\n");
+        return -1;
+    }
+
+    //check usb init
+    if(usb_dev && !init_usb_flag) {
+        UpgradePort = getenv(ENV_USBUPGRADEPORT);
+        if(UpgradePort == NULL) {
+            run_command("usb start 0", 0);
+        } else{
+            snprintf(tmp, sizeof(tmp) - 1,"usb start %s", UpgradePort);
+            run_command(tmp, 0);
+        }
+        init_usb_flag=1;
+    }
+
+    index_file = getenv(ENV_MAC_INDEX_FILE);
+    if(index_file == NULL)
+    {
+        printf("ENV_MAC_INDEX_FILE is null, use default MAC_INDEX\n");
+        index_file = "MAC_INDEX";
+        run_command("setenv MacIndexFile MAC_INDEX", 0);
+        run_command("saveenv", 0);
+    }
+    memset(tmp,0,sizeof(tmp));
+    snprintf(tmp, sizeof(tmp) - 1, "fatload %s 0 %X %s 0x4 0x0", mac_dev, (U32)mac_index, index_file);
+    ret = run_command(tmp, 0);
+    if (ret != 0) {
+        printf("read mac index error!\n");
+        return -1;
+    }
+
+    for (int i = 0; i < 4; i ++) {
+        if (mac_index[i] >= '0' && mac_index[i] <= '9') {
+            cur_index = cur_index * 10 + mac_index[i] - '0';
+        }
+    }
+
+    memset(tmp,0,sizeof(tmp));
+    snprintf(tmp, sizeof(tmp) - 1, "fatload %s 0 %X MAC_%d 0x11 0x0", mac_dev, (U32)mac_addr, cur_index);
+    ret = run_command(tmp, 0);
+    if (ret != 0) {
+        printf("read MAC_%d file error!\n", cur_index);
+        return -1;
+    }
+    mac_addr[17] = '\0';
+    printf("mac_%d addr value = [%s]\n", cur_index, mac_addr);
+
+    memset(tmp,0,sizeof(tmp));
+    snprintf(tmp, sizeof(tmp) - 1, "setenv -f ethaddr %s", mac_addr);
+    ret = run_command(tmp, 0);
+    if (ret != 0) {
+        printf("setenv ethaddr error!\n");
+        return -1;
+    }
+    run_command("saveenv", 0);
+
+    next_index = cur_index + 1;
+    sprintf(mac_index, "%04d", next_index);
+    //printf("cur_index = [%d], next_index = [%d]\n", cur_index, next_index);
+
+    memset(tmp,0,sizeof(tmp));
+    snprintf(tmp, sizeof(tmp) - 1, "fatwrite %s 0 %X %s 0x4", mac_dev, (U32)mac_index, index_file);
+    ret = run_command(tmp, 0);
+    if (ret != 0) {
+        printf("write next mac index error!\n");
+        return -1;
+    }
+
+    //printf("read mac_%d:[%s] success, next mac index = [%d]\n", cur_index, mac_addr, next_index);
+
+    //init_usb_flag = 0;
+
+    return 0;
+}
+
+U_BOOT_CMD(
+    macburn,  CONFIG_SYS_MAXARGS,    1,    do_macburn,
+    "script via MAC BURN",
+    ""
+);
+#endif
+
 #ifdef CONFIG_CMD_NET
 int do_estar (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
diff --git a/drivers/mstar/Kconfig b/drivers/mstar/Kconfig
index 2324c00..5c55a7e 100755
--- a/drivers/mstar/Kconfig
+++ b/drivers/mstar/Kconfig
@@ -56,6 +56,9 @@ config MS_EMAC1
     bool "MSTAR EMAC 1"
     depends on MS_EMAC
 
+config MS_MAC_BURN
+    bool "MSTAR MAC BRUN"
+
 config ETHERNET_ALBANY
     bool "Supply to internel PHY"
     depends on MS_EMAC
diff --git a/include/configs/pioneer3.h b/include/configs/pioneer3.h
index c596b81..dde9224 100755
--- a/include/configs/pioneer3.h
+++ b/include/configs/pioneer3.h
@@ -31,6 +31,7 @@
 
 #define CONFIG_WDT_CLOCK    CONFIG_PIUTIMER_CLOCK
 
+#define CONFIG_FAT_WRITE
 
 /* define baud rate */
 #define CONFIG_SYS_BAUDRATE_TABLE	{ 9600, 19200, 38400, 57600, 115200 }
-- 
2.17.1

