add first boot set mac script

phosphor-misc will use the meson build system - establish the initial
structure.  Copy the first boot set mac script and systemd unit from
meta-openpower.

Change-Id: I1f7ebbb6b5230aa97b28e9a46ac721925b95a75a
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/firstboot/first-boot-set-mac.sh b/firstboot/first-boot-set-mac.sh
new file mode 100644
index 0000000..63a4ad7
--- /dev/null
+++ b/firstboot/first-boot-set-mac.sh
@@ -0,0 +1,84 @@
+#!/bin/sh -eu
+
+show_error() {
+    if [ -n "${JOURNAL_STREAM-}" ]; then
+        echo "$@" | systemd-cat -t first-boot-set-mac -p emerg
+    else
+        echo "$@" >&2
+    fi
+}
+
+sync_mac() {
+
+    MAPPER_IFACE='xyz.openbmc_project.ObjectMapper'
+    MAPPER_PATH='/xyz/openbmc_project/object_mapper'
+    INVENTORY_PATH='/xyz/openbmc_project/inventory'
+    NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface'
+
+    # Get the NETWORK ITEM count
+    NETWORK_ITEM_PATH_COUNT=$(busctl --no-pager --verbose call \
+                            ${MAPPER_IFACE} \
+                            ${MAPPER_PATH} \
+                            ${MAPPER_IFACE} \
+                            GetSubTree sias \
+                            ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+                        2>/dev/null | grep ${INVENTORY_PATH} | wc -l || true)
+
+    if [ $NETWORK_ITEM_PATH_COUNT -gt 1 ]; then
+        # If there are more than 2 NETOWRK ITEM and path must contain $1
+        # for finding the right NETWORK ITEM
+        NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
+                            ${MAPPER_IFACE} \
+                            ${MAPPER_PATH} \
+                            ${MAPPER_IFACE} \
+                            GetSubTree sias \
+                            ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+                        2>/dev/null | grep ${INVENTORY_PATH} | grep $1 || true)
+    else
+        NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
+                            ${MAPPER_IFACE} \
+                            ${MAPPER_PATH} \
+                            ${MAPPER_IFACE} \
+                            GetSubTree sias \
+                            ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+                        2>/dev/null | grep ${INVENTORY_PATH} || true)
+    fi
+
+    # '     STRING "/xyz/openbmc_project/inventory/system/chassis/ethernet";'
+    NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
+    NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
+
+    NETWORK_ITEM_SERVICE=$(mapper get-service \
+                                ${NETWORK_ITEM_PATH} 2>/dev/null || true)
+
+    if [[ -z "${NETWORK_ITEM_SERVICE}" ]]; then
+        show_error 'No Ethernet interface found in the Inventory. Is VPD EEPROM empty?'
+        return
+    fi
+
+    MAC_ADDR=$(busctl get-property ${NETWORK_ITEM_SERVICE} \
+                                ${NETWORK_ITEM_PATH} \
+                                ${NETWORK_ITEM_IFACE} MACAddress)
+
+    # 's "54:52:01:02:03:04"'
+    MAC_ADDR=${MAC_ADDR#*\"}
+    MAC_ADDR=${MAC_ADDR%\"*}
+
+    if [[ -n "${MAC_ADDR}" ]]; then
+        busctl set-property xyz.openbmc_project.Network \
+                            /xyz/openbmc_project/network/$1 \
+                            xyz.openbmc_project.Network.MACAddress \
+                            MACAddress s ${MAC_ADDR}
+    fi
+}
+
+if [ $# -eq 0 ]; then
+    show_error 'No Ethernet interface name is given'
+    exit 1
+fi
+
+sync_mac $1
+
+# Prevent start at next boot time
+mkdir -p "/var/lib/first-boot-set-mac"
+touch "/var/lib/first-boot-set-mac/${1}"
diff --git a/firstboot/first-boot-set-mac@.service b/firstboot/first-boot-set-mac@.service
new file mode 100644
index 0000000..3ff5fc1
--- /dev/null
+++ b/firstboot/first-boot-set-mac@.service
@@ -0,0 +1,17 @@
+[Unit]
+Description=Init BMC MAC address for NIC %I
+ConditionPathExists=!/var/lib/first-boot-set-mac/%i
+Wants=mapper-wait@-xyz-openbmc_project-inventory.service
+After=mapper-wait@-xyz-openbmc_project-inventory.service
+Wants=mapper-wait@-xyz-openbmc_project-network-%i.service
+After=mapper-wait@-xyz-openbmc_project-network-%i.service
+Wants=op-vpd-parser.service
+After=op-vpd-parser.service
+
+[Service]
+ExecStart=/usr/bin/first-boot-set-mac.sh %i
+Type=oneshot
+RemainAfterExit=Yes
+
+[Install]
+WantedBy=multi-user.target
diff --git a/firstboot/meson.build b/firstboot/meson.build
new file mode 100644
index 0000000..d2d09d7
--- /dev/null
+++ b/firstboot/meson.build
@@ -0,0 +1,17 @@
+set_mac = dependency('systemd', required: get_option('first-boot-set-mac'))
+
+if set_mac.found()
+    install_data(
+        'first-boot-set-mac.sh',
+        install_mode: 'rwxr-xr-x',
+        install_dir: get_option('bindir'),
+    )
+
+    configure_file(
+        input: 'first-boot-set-mac@.service',
+        output: 'first-boot-set-mac@.service',
+        copy: true,
+        install_dir:
+            set_mac.get_pkgconfig_variable('systemdsystemunitdir'),
+    )
+endif
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..49736b5
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,7 @@
+project(
+    'phosphor-misc',
+    license: 'Apache-2.0',
+    version: '1.0',
+)
+
+subdir('firstboot')
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..a946e82
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,3 @@
+option(
+    'first-boot-set-mac', type: 'feature', description: 'Set MAC address on first boot.',
+)