add set_wlan_mac.sh script to set a consistent wifi mac address

This sets the MAC address on boot using the device serial number or
falling back to a random value. The MAC is saved to userdata so if it is
generated randomly it will be consistent until factory reset
diff --git a/bin/set_wlan_mac.rc b/bin/set_wlan_mac.rc
new file mode 100644
index 0000000..72785b6
--- /dev/null
+++ b/bin/set_wlan_mac.rc
@@ -0,0 +1,9 @@
+service set_wlan_mac /system/bin/set_wlan_mac.sh
+    class core
+    user root
+    group system
+    disabled
+    oneshot
+
+on post-fs-data
+    start set_wlan_mac
diff --git a/bin/set_wlan_mac.sh b/bin/set_wlan_mac.sh
new file mode 100755
index 0000000..0e36bc3
--- /dev/null
+++ b/bin/set_wlan_mac.sh
@@ -0,0 +1,42 @@
+#! /system/bin/sh
+# Set wlan0 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.
+
+DEV="wlan0"
+
+SERIALNO=`cat /proc/cmdline | grep -o "serialno.*" | cut -d" " -f1`
+
+# If for some reason the serial number isn't on cmdline, just use random
+if [ -z "$SERIALNO" ]; then
+    SERIALNO="$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM"
+fi
+
+# Do some funky stuff to generate a MAC address from the serial number
+# We sha256 it because some devices use non-base 16 characters in their
+# serial number (*ahem* SHIFT6mq).
+SERIALNO=`echo $SERIALNO | sha256sum | awk '{printf("02%010s\n", $1)}' | sed 's/\(..\)/\1:/g' | sed '$s/:$//' | cut -d: -f1,2,3,4,5,6`
+
+# If the mac address has already been generated then use that instead
+if [ -f /data/wlan_mac.bin ]; then
+    WLAN_MAC=`cat /data/wlan_mac.bin`
+else
+    WLAN_MAC="$SERIALNO"
+    echo $WLAN_MAC > /data/wlan_mac.bin
+fi
+
+echo "Setting $DEV mac addr to $WLAN_MAC"
+
+while ! ip link show $DEV > /dev/null; do
+        echo "Device not up yet, waiting for it"
+        sleep 1
+done
+
+/system/bin/ip link set dev $DEV down
+/system/bin/ip link set dev $DEV address "${WLAN_MAC}"
+/system/bin/ip link set dev $DEV up
diff --git a/device-common.mk b/device-common.mk
index 9bb31b9..f8c55c4 100644
--- a/device-common.mk
+++ b/device-common.mk
@@ -92,6 +92,11 @@
 PRODUCT_COPY_FILES += \
     $(LOCAL_PATH)/qcom/init.qcom.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.qcom.rc
 
+# Install scripts to set consistent Wifi MAC address
+PRODUCT_COPY_FILES += \
+    $(LOCAL_PATH)/bin/set_wlan_mac.rc:/system/etc/init/set_wlan_mac.rc \
+    $(LOCAL_PATH)/bin/set_wlan_mac.sh:/system/bin/set_wlan_mac.sh
+
 PRODUCT_PROPERTY_OVERRIDES += \
     wifi.interface=wlan0 \
     wifi.supplicant_scan_interval=15
@@ -181,12 +186,8 @@
 # Health
 PRODUCT_PACKAGES += \
     android.hardware.health@2.1-service \
-    android.hardware.health@2.1-impl-gsdm845 \ # Passthrough implementation
+    android.hardware.health@2.1-impl-sdm845
 
-# Cuttlefish health
-# PRODUCT_PACKAGES += \
-#     android.hardware.health@2.1-impl-cuttlefish \
-#     android.hardware.health@2.1-service
 
 # mkbootimg host tool to build boot.img separately
 PRODUCT_HOST_PACKAGES := \
diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 1aeb343..df8fa3c 100644
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -36,6 +36,7 @@
 /data/vendor/readonly(/.*)?								u:object_r:tqftpserv_vendor_data_file:s0
 
 /system/bin/tinymix									u:object_r:tinymix_exec:s0
+/system/bin/set_wlan_mac\.sh								u:object_r:set_wlan_mac_exec:s0
 
 /vendor/bin/hw/android\.hardware\.gatekeeper@1\.0-service\.software			u:object_r:hal_gatekeeper_default_exec:s0
 /vendor/bin/hw/android\.hardware\.graphics\.allocator@4\.0-service\.minigbm_msm		u:object_r:hal_graphics_allocator_default_exec:s0
diff --git a/sepolicy/set_wlan_mac.te b/sepolicy/set_wlan_mac.te
new file mode 100644
index 0000000..ff34638
--- /dev/null
+++ b/sepolicy/set_wlan_mac.te
@@ -0,0 +1,15 @@
+type set_wlan_mac, domain, coredomain;
+type set_wlan_mac_exec, exec_type, system_file_type, file_type;
+init_daemon_domain(set_wlan_mac);
+
+allow set_wlan_mac proc_cmdline:file { open read };
+allow set_wlan_mac rootfs:dir { open read };
+allow set_wlan_mac self:capability net_admin;
+allow set_wlan_mac self:netlink_route_socket { bind create getattr nlmsg_read nlmsg_readpriv nlmsg_write read setopt write };
+allow set_wlan_mac self:udp_socket { create ioctl };
+allow set_wlan_mac shell_exec:file { execute getattr map read };
+allow set_wlan_mac system_file:file execute_no_trans;
+allow set_wlan_mac toolbox_exec:file { execute execute_no_trans getattr map open read };
+allow set_wlan_mac system_data_root_file:file { getattr open read };
+allow set_wlan_mac net_data_file:dir search;
+allow set_wlan_mac self:capability sys_module;
\ No newline at end of file