utils: eth_mac_addr: Refactor ethernet mac addr script am: e0b877ac38

Original change: https://android-review.googlesource.com/c/device/linaro/dragonboard/+/2664199

Change-Id: I393ef788434af4f7264560e437b0c6d8b91c6a2a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/db845c/device.mk b/db845c/device.mk
index 7dac78b..700e339 100644
--- a/db845c/device.mk
+++ b/db845c/device.mk
@@ -47,8 +47,8 @@
 
 # Install scripts to set Ethernet MAC address
 PRODUCT_COPY_FILES += \
-    $(LOCAL_PATH)/eth_mac_addr.rc:/system/etc/init/eth_mac_addr.rc \
-    $(LOCAL_PATH)/eth_mac_addr.sh:/system/bin/eth_mac_addr.sh
+    device/linaro/dragonboard/shared/utils/ethaddr/ethaddr.rc:/system/etc/init/ethaddr.rc \
+    device/linaro/dragonboard/shared/utils/ethaddr/set_ethaddr.sh:/system/bin/set_ethaddr.sh
 
 PRODUCT_VENDOR_PROPERTIES += ro.soc.manufacturer=Qualcomm
 PRODUCT_VENDOR_PROPERTIES += ro.soc.model=SDM845
diff --git a/db845c/eth_mac_addr.rc b/db845c/eth_mac_addr.rc
deleted file mode 100644
index a30c0a5..0000000
--- a/db845c/eth_mac_addr.rc
+++ /dev/null
@@ -1,9 +0,0 @@
-service eth_mac_addr /system/bin/eth_mac_addr.sh
-    class core
-    user root
-    group system
-    disabled
-    oneshot
-
-on post-fs-data && property:vendor.hw=db45c
-    start eth_mac_addr
diff --git a/db845c/eth_mac_addr.sh b/db845c/eth_mac_addr.sh
deleted file mode 100644
index bdc1ba7..0000000
--- a/db845c/eth_mac_addr.sh
+++ /dev/null
@@ -1,15 +0,0 @@
-#! /system/bin/sh
-# Set eth0 mac address.
-#
-# Get the unique board serial number from /proc/cmdline,
-# prepend '0's to the serial number to fill 5 LSBs of the
-# MAC address and prepend "02" as MSB to prepare a 6 byte
-# locally administered unicast MAC address.
-# Format the output in xx:xx:xx:xx:xx:xx format for the
-# "ip" set address command to work.
-
-SERIALNO=`cat /proc/cmdline | grep -o serialno.* | cut -f2 -d'=' | awk '{printf("02%010s\n", $1)}' | sed 's/\(..\)/\1:/g' | sed '$s/:$//'`
-
-/system/bin/ip link set dev eth0 down
-/system/bin/ip link set dev eth0 address "${SERIALNO}"
-/system/bin/ip link set dev eth0 up
diff --git a/sepolicy/eth_mac_addr.te b/sepolicy/eth_mac_addr.te
deleted file mode 100644
index d2e4e1a..0000000
--- a/sepolicy/eth_mac_addr.te
+++ /dev/null
@@ -1,12 +0,0 @@
-type eth_mac_addr, domain, coredomain;
-type eth_mac_addr_exec, exec_type, system_file_type, file_type;
-init_daemon_domain(eth_mac_addr);
-
-allow eth_mac_addr proc_cmdline:file { open read };
-allow eth_mac_addr rootfs:dir { open read };
-allow eth_mac_addr self:capability net_admin;
-allow eth_mac_addr self:netlink_route_socket { bind create getattr nlmsg_readpriv nlmsg_write read setopt write };
-allow eth_mac_addr self:udp_socket { create ioctl };
-allow eth_mac_addr shell_exec:file { execute getattr map read };
-allow eth_mac_addr system_file:file execute_no_trans;
-allow eth_mac_addr toolbox_exec:file { execute execute_no_trans getattr map open read };
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index a74f361..22e9d45 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -49,7 +49,7 @@
 /data/vendor/readwrite(/.*)?								u:object_r:tqftpserv_vendor_data_file:s0
 /data/vendor/readonly(/.*)?								u:object_r:tqftpserv_vendor_data_file:s0
 
-/system/bin/eth_mac_addr\.sh								u:object_r:eth_mac_addr_exec:s0
+/system/bin/set_ethaddr\.sh								u:object_r:set_ethaddr_exec:s0
 /system/bin/tinymix									u:object_r:tinymix_exec:s0
 
 /vendor/bin/grep									u:object_r:vendor_toolbox_exec:s0
diff --git a/sepolicy/set_ethaddr.te b/sepolicy/set_ethaddr.te
new file mode 100644
index 0000000..4b62d7f
--- /dev/null
+++ b/sepolicy/set_ethaddr.te
@@ -0,0 +1,13 @@
+type set_ethaddr, domain, coredomain;
+type set_ethaddr_exec, exec_type, system_file_type, file_type;
+init_daemon_domain(set_ethaddr);
+
+allow set_ethaddr proc_cmdline:file { open read };
+allow set_ethaddr proc_bootconfig:file { open read };
+allow set_ethaddr rootfs:dir { open read };
+allow set_ethaddr self:capability net_admin;
+allow set_ethaddr self:netlink_route_socket { bind create getattr nlmsg_read nlmsg_readpriv nlmsg_write read setopt write };
+allow set_ethaddr self:udp_socket { create ioctl };
+allow set_ethaddr shell_exec:file { execute getattr map read };
+allow set_ethaddr system_file:file execute_no_trans;
+allow set_ethaddr toolbox_exec:file { execute execute_no_trans getattr map open read };
diff --git a/shared/utils/ethaddr/ethaddr.rc b/shared/utils/ethaddr/ethaddr.rc
new file mode 100644
index 0000000..d82f5ee
--- /dev/null
+++ b/shared/utils/ethaddr/ethaddr.rc
@@ -0,0 +1,9 @@
+service set_ethaddr /system/bin/set_ethaddr.sh
+    class core
+    user root
+    group system
+    disabled
+    oneshot
+
+on post-fs-data && property:vendor.hw=db845c
+    start set_ethaddr
diff --git a/shared/utils/ethaddr/set_ethaddr.sh b/shared/utils/ethaddr/set_ethaddr.sh
new file mode 100644
index 0000000..2bfa8d8
--- /dev/null
+++ b/shared/utils/ethaddr/set_ethaddr.sh
@@ -0,0 +1,25 @@
+#! /system/bin/sh
+# Set eth0 mac address.
+#
+# Get the unique board serial number from /proc/cmdline or
+# /proc/bootconfig, prepend '0's to the serial number to
+# fill 5 LSBs of the MAC address and prepend "02" as MSB to
+# prepare a 6 byte locally administered unicast MAC address.
+#
+# Format the output in xx:xx:xx:xx:xx:xx format for the "ip"
+# set address command to work.
+
+ETHADDR=`cat /proc/cmdline | grep -o serialno.* | cut -f2 -d'=' |\
+	 awk '{printf("02%010s\n", $1)}' | sed 's/\(..\)/\1:/g' |\
+	 sed '$s/:$//'`
+if [ -z "${ETHADDR}" ]
+then
+ETHADDR=`cat /proc/bootconfig | grep -o serialno.* |\
+	 cut -f2 -d'=' | cut -c 3-10 |\
+	 awk '{printf("02%010s\n", $1)}' | sed 's/\(..\)/\1:/g' |\
+	 sed '$s/:$//'`
+fi
+
+ip link set dev eth0 down
+ip link set dev eth0 address "${ETHADDR}"
+ip link set dev eth0 up