Add mac address script, udev rules

- Add a script to take the wlan's mac address, and inject it into the
EEPROM that the bluetooth driver will load.
- Update the service script to execute the mac address script before
inserting the kernel module.
- Instead of triggering the systemd service to be loaded for the
multi-user target, we use udev to run it when wlan0 is ready (so we can
be sure the mac address is available).

Change-Id: Ic3fbd727ab9cde728a877a6be984a6ed927d9e7e
diff --git a/debian/changelog b/debian/changelog
index 42fd488..d8ee603 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+excelsior-bluetooth (2-3) mendel-eagle; urgency=medium
+
+  * Add MAC address script, update service configuration.
+
+ -- Coral Team <coral-support@google.com>  Wed, 26 Aug 2020 15:34:45 -0700
+
 excelsior-bluetooth (2-2) mendel-day; urgency=medium
 
   * Update to target 4.19.106 kernel.
diff --git a/debian/control b/debian/control
index 97212ca..867d705 100644
--- a/debian/control
+++ b/debian/control
@@ -13,6 +13,7 @@
 Architecture: arm64 armhf
 Priority: optional
 Depends: excelsior-connectivity-fw,
+         python3,
          ${shlibs:Depends},
          ${misc:Depends}
 Description: Excelsior Bluetooth support.
diff --git a/debian/excelsior-bluetooth-mac.py b/debian/excelsior-bluetooth-mac.py
new file mode 100644
index 0000000..0d2a197
--- /dev/null
+++ b/debian/excelsior-bluetooth-mac.py
@@ -0,0 +1,18 @@
+#!/usr/bin/python3
+
+import binascii
+
+# Fetch and trim whitespace from the wlan mac
+with open('/sys/class/net/wlan0/address') as mac_address_file:
+    mac_address = mac_address_file.read().strip()
+
+# Split the address on :, reverse the order, rejoin and convert to binary.
+# This gives it to us in the proper endianness to write into the EEPROM.
+mac_address_bin = binascii.a2b_hex(''.join(reversed(mac_address.split(':'))))
+
+# Overwrite the EEPROM at the correct offset.
+# With this as a real value instead of zeroes, the bluetooth driver
+# will use the address instead of generating a random one.
+with open('/lib/firmware/EEPROM_MT7668.bin', 'r+b') as eeprom:
+    eeprom.seek(0x384)
+    eeprom.write(mac_address_bin)
diff --git a/debian/excelsior-bluetooth.service b/debian/excelsior-bluetooth.service
index 08b2184..c34f215 100644
--- a/debian/excelsior-bluetooth.service
+++ b/debian/excelsior-bluetooth.service
@@ -6,6 +6,3 @@
 [Service]
 Type=simple
 ExecStart=/bin/sh /usr/sbin/excelsior-bluetooth.sh
-
-[Install]
-WantedBy=multi-user.target
diff --git a/debian/excelsior-bluetooth.sh b/debian/excelsior-bluetooth.sh
index 33f8fea..b54b3dd 100644
--- a/debian/excelsior-bluetooth.sh
+++ b/debian/excelsior-bluetooth.sh
@@ -1,3 +1,9 @@
 #!/bin/sh
 
+set -e
+
+# Set BDAddr based on the wlan BDAddr
+python3 /usr/sbin/excelsior-bluetooth-mac.py
+
+# Load module
 insmod /lib/modules/4.19.125-mtk/kernel/misc/mediatek/connectivity/btmtksdio.ko
diff --git a/debian/excelsior-bluetooth.udev b/debian/excelsior-bluetooth.udev
new file mode 100644
index 0000000..0d72ab5
--- /dev/null
+++ b/debian/excelsior-bluetooth.udev
@@ -0,0 +1 @@
+ACTION=="add", SUBSYSTEM=="net", KERNEL=="wlan0", TAG+="systemd", ENV{SYSTEMD_WANTS}="excelsior-bluetooth.service"
diff --git a/debian/rules b/debian/rules
index b08b165..8c7d627 100755
--- a/debian/rules
+++ b/debian/rules
@@ -42,6 +42,7 @@
 	install -g 0 -o 0 $(KERNELMOD) $(CONNECTIVITYMODULESDIR)
 	install -d $(DESTDIR)/usr/sbin
 	install -g 0 -o 0 debian/excelsior-bluetooth.sh $(DESTDIR)/usr/sbin/excelsior-bluetooth.sh
+	install -g 0 -o 0 debian/excelsior-bluetooth-mac.py $(DESTDIR)/usr/sbin/excelsior-bluetooth-mac.py
 	install -d $(DESTDIR)/etc/modprobe.d
 	install -g 0 -o 0 debian/btmtksdio.conf $(DESTDIR)/etc/modprobe.d/btmtksdio.conf