bytedance: g220a:read eeprom mac addr

read a byte at one time eg:
for the first byte
i2cset -y 1 0x50 0x00 0x00
i2cget -y 1 0x50

(From meta-bytedance rev: 6389a4f03a518e49241d13843cf005f4a85d7cc8)

Tested: Verify the eeprom mac addr can be read
	use `fw_printenv`,`ip a` can read device
	mac addr
Signed-off-by: Lotus Xu <xuxiaohan@bytedance.com>
Change-Id: I5fbf28e5bb3bb2ccc2a074388f4166e35da48afc
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr.bb b/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr.bb
new file mode 100644
index 0000000..130187d
--- /dev/null
+++ b/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr.bb
@@ -0,0 +1,24 @@
+SUMMARY = "Enforce static MAC addresses"
+DESCRIPTION = "Set a priority on MAC addresses to run with: \
+               factory-specified > u-boot-specified > random"
+
+FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
+
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit allarch
+SRC_URI = "\
+    file://mac-check \
+    file://${PN}.service \
+    "
+
+inherit obmc-phosphor-systemd
+
+SYSTEMD_SERVICE_${PN} += "${PN}.service"
+
+do_install() {
+    install -d ${D}${bindir}
+    install -m 0755 ${WORKDIR}/mac-check ${D}${bindir}
+}
diff --git a/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr/mac-check b/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr/mac-check
new file mode 100644
index 0000000..2580007
--- /dev/null
+++ b/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr/mac-check
@@ -0,0 +1,130 @@
+#!/bin/sh
+# Copyright 2018 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#	http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+SOFS_MNT=/var/sofs
+SOFS_MACDIR=${SOFS_MNT}/factory-settings/network/mac
+
+read_hw_mac() {
+	local iface="$1"
+	cat /sys/class/net/"$iface"/address 2>/dev/null
+}
+
+set_hw_mac() {
+	local iface="$1"
+	local mac="$2"
+	ip link show dev "$iface" | grep -q "${iface}:.*\<UP\>" 2>/dev/null
+	local up=$?
+	[[ $up -eq 0 ]] && ip link set dev "$iface" down
+	ip link set dev "$iface" address "$mac"
+	[[ $up -eq 0 ]] && ip link set dev "$iface" up
+}
+
+read_sofs_mac() {
+	local iface="$1"
+	cat "${SOFS_MACDIR}/${iface}" 2>/dev/null
+}
+
+read_fw_env_mac() {
+	local envname="$1"
+	fw_printenv "$envname" 2>/dev/null | sed "s/^$envname=//"
+}
+
+set_fw_env_mac() {
+	local envname="$1"
+	local mac="$2"
+	fw_setenv "$envname" "$mac"
+}
+read_eeprom_mac() {
+	local iface="$1"
+	a=0
+	if [ $iface = "eth1" ];then
+		a=8
+	fi
+	for  ((i=a;i <= ((7+a));i++));do
+		i2cset -y 1 0x50 0x00 $i
+		tmp=`i2cget -y 1 0x50`
+		local mac[$i]=${tmp:2}
+		local mac_str=$mac_str:${mac[$i]}
+	done
+	mac_str=${mac_str:1:17}
+	echo $mac_str 2>/dev/null
+}
+
+create_macdir() {
+if [ -a ${SOFS_MACDIR} ]; then
+	if [ ! -d ${SOFS_MACDIR} ]; then
+		rm -rf ${SOFS_MACDIR}
+		mkdir -p ${SOFS_MACDIR}
+	fi
+else
+	mkdir -p ${SOFS_MACDIR}
+fi
+return 0
+}
+
+mac_check() {
+	local iface="$1"
+	local envname="$2"
+
+	# Read the MAC address in use by the NIC
+	local hw_mac=$(read_hw_mac "$iface")
+
+	# Read the MAC address stored in the non-volatile file provisioned in
+	# manufacturing.
+	local sofs_mac=$(read_sofs_mac "$iface")
+
+	local eeprom_mac=$(read_eeprom_mac "$iface")
+	if [ -n "$eeprom_mac" ] && [ -z "$sofs_mac"]; then
+		set_hw_mac "$iface" "$eeprom_mac"
+		set_fw_env_mac "$envname" "$eeprom_mac"
+		return $?
+	elif [ -n "$sofs_mac" ] && [ "$hw_mac" != "$sofs_mac" ]; then
+		# A factory assigned address was found, and it is newly assigned.
+		# Update the active interface and save the new value to the u-boot
+		# environment.
+		set_hw_mac "$iface" "$sofs_mac"
+		set_fw_env_mac "$envname" "$sofs_mac"
+		return $?
+	elif [ -n "$hw_mac" ]; then
+		# Read the MAC address stored by U-Boot
+		local fw_env_mac=$(read_fw_env_mac "$envname")
+		if [ -z "$fw_env_mac" ] || [ "$fw_env_mac" != "$hw_mac" ]; then
+			set_fw_env_mac "$envname" "$hw_mac"
+			return $?
+		fi
+	else
+	    # Could not identify a MAC address
+	    return 255
+	fi
+
+	return 0
+}
+
+create_macdir
+
+error=0
+first_error_seen=0
+
+while read IFACE UBDEV; do
+	mac_check "$IFACE" "$UBDEV"
+	error=$?
+	if [ $error -ne 0 ] && [ $first_error_seen -eq 0 ]; then
+		first_error_seen=$error
+	fi
+done <<-END_CONF
+	eth0 eth1addr
+	eth1 ethaddr
+END_CONF
+exit $first_error_seen
diff --git a/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr/static-mac-addr.service b/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr/static-mac-addr.service
new file mode 100644
index 0000000..86371db
--- /dev/null
+++ b/meta-bytedance/meta-g220a/recipes-network/network/static-mac-addr/static-mac-addr.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Enforce Static MAC addr mapping
+
+[Service]
+Type=oneshot
+Restart=no
+ExecStart=/usr/bin/mac-check
+
+[Install]
+WantedBy=network.target
+
diff --git a/meta-bytedance/meta-g220a/recipes-phosphor/images/obmc-phosphor-image.bbappend b/meta-bytedance/meta-g220a/recipes-phosphor/images/obmc-phosphor-image.bbappend
index 8b7bb9e..e38c272 100644
--- a/meta-bytedance/meta-g220a/recipes-phosphor/images/obmc-phosphor-image.bbappend
+++ b/meta-bytedance/meta-g220a/recipes-phosphor/images/obmc-phosphor-image.bbappend
@@ -1,3 +1,4 @@
 OBMC_IMAGE_EXTRA_INSTALL_append_g220a = " webui-vue \
 					  me-time-sync \
+					  static-mac-addr \
 					"