meta-ethanolx: Add service to transfer BMC IP to FPGA

If switch SW2-7 is on, FPGA shows BMC IP address on the 7-segment
displays. For that functionality to work correctly BMC needs to write
its own IP address to the paticular FPGA I2C registers.
Add a service that performs this action on every IP change.

Change-Id: Iead978ef3651448f4c32936402607fbb3267ee18
Signed-off-by: Konstantin Aladyshev <aladyshev22@gmail.com>
diff --git a/meta-amd/meta-ethanolx/recipes-amd/packagegroups/packagegroup-amd-apps.bbappend b/meta-amd/meta-ethanolx/recipes-amd/packagegroups/packagegroup-amd-apps.bbappend
new file mode 100644
index 0000000..f4cc3c1
--- /dev/null
+++ b/meta-amd/meta-ethanolx/recipes-amd/packagegroups/packagegroup-amd-apps.bbappend
@@ -0,0 +1 @@
+RDEPENDS:${PN}-system:append:ethanolx = " ip-to-fpga"
diff --git a/meta-amd/recipes-amd/amd-fpga/files/ip-to-fpga.service b/meta-amd/recipes-amd/amd-fpga/files/ip-to-fpga.service
new file mode 100644
index 0000000..18fbca8
--- /dev/null
+++ b/meta-amd/recipes-amd/amd-fpga/files/ip-to-fpga.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Transfer IP address to the FPGA
+BindsTo=sys-subsystem-net-devices-eth0.device
+After=sys-subsystem-net-devices-eth0.device
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/ip-to-fpga.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-amd/recipes-amd/amd-fpga/files/ip-to-fpga.sh b/meta-amd/recipes-amd/amd-fpga/files/ip-to-fpga.sh
new file mode 100644
index 0000000..a4d0a98
--- /dev/null
+++ b/meta-amd/recipes-amd/amd-fpga/files/ip-to-fpga.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+OLD_IP=""
+
+while true
+do
+  IP=$(ip a | awk '/inet.*global/ {split ($2,A,"/"); print A[1]}')
+
+  if [ "${IP}" != "${OLD_IP}" ]
+  then
+    if [ -n "${IP}" ]
+    then
+      IP_1=$(echo "${IP}" | cut -d "." -f 1)
+      IP_2=$(echo "${IP}" | cut -d "." -f 2)
+      IP_3=$(echo "${IP}" | cut -d "." -f 3)
+      IP_4=$(echo "${IP}" | cut -d "." -f 4)
+    else
+      IP_1=0
+      IP_2=0
+      IP_3=0
+      IP_4=0
+    fi
+
+    echo "Transfer current IP address (${IP_1}.${IP_2}.${IP_3}.${IP_4}) to the FPGA"
+
+    i2cset -y 2 0x50 0 "${IP_1}"
+    i2cset -y 2 0x50 1 "${IP_2}"
+    i2cset -y 2 0x50 2 "${IP_3}"
+    i2cset -y 2 0x50 3 "${IP_4}"
+    OLD_IP=${IP}
+  fi
+  sleep 5
+done
diff --git a/meta-amd/recipes-amd/amd-fpga/ip-to-fpga.bb b/meta-amd/recipes-amd/amd-fpga/ip-to-fpga.bb
new file mode 100644
index 0000000..2dd76c9
--- /dev/null
+++ b/meta-amd/recipes-amd/amd-fpga/ip-to-fpga.bb
@@ -0,0 +1,25 @@
+DESCRIPTION = "Transfer BMC IP address to the FPGA"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit systemd
+
+SRC_URI = " file://ip-to-fpga.sh \
+            file://ip-to-fpga.service \
+          "
+
+S = "${WORKDIR}"
+
+DEPENDS = "systemd"
+RDEPENDS:${PN} = "bash"
+
+SYSTEMD_SERVICE:${PN} = "ip-to-fpga.service"
+
+do_install() {
+    install -d ${D}${bindir}
+    install -m 0755 ${S}/ip-to-fpga.sh ${D}${bindir}/
+
+    install -d ${D}${systemd_system_unitdir}
+    install -m 0644 ${S}/ip-to-fpga.service ${D}${systemd_system_unitdir}
+}