wlan: Set the correct MAC addresses

  - Stop using a fixed OUI from Foxconn and instead use the OUI burned into the
    Ethernet PHY's OTP.
  - Fix the format of the Bluetooth MAC and write it in an endian-aware form.

Change-Id: Ifb9cb16ca532b0c8d08eb7a4fd2a8ff37af5e180
diff --git a/debian/changelog b/debian/changelog
index b0ecd95..bd6e9b9 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+imx-board-wlan (3-1) mendel-chef; urgency=medium
+
+  * Fix to the MAC address vendor script.
+
+ -- Coral <coral-support@google.com>  Tue, 09 Jul 2019 17:08:25 -0700
+
 imx-board-wlan (2-1) mendel-chef; urgency=medium
 
   * Cut for chef release
@@ -7,8 +13,8 @@
 imx-board-wlan (1-1) mendel-beaker; urgency=medium
 
   * Cut for beaker release and normalization of version.
-  
- -- Coral <coral-support@google.com>  Wed, 16 Jan 2019 12:00:00 -0800
+
+  -- Coral <coral-support@google.com>  Wed, 16 Jan 2019 12:00:00 -0800
 
 imx-board-wlan (0.1) UNRELEASED; urgency=medium
 
diff --git a/etc/runonce.d/10-set-mac-addresses b/etc/runonce.d/10-set-mac-addresses
index 83b2c39..e14ba16 100755
--- a/etc/runonce.d/10-set-mac-addresses
+++ b/etc/runonce.d/10-set-mac-addresses
@@ -16,16 +16,14 @@
 import sys
 
 
-MAC_VENDOR_PREFIX_STR = '70:20:84'
-
 BD_ADDR_PATH = '/etc/bluetooth/.bt_nv.bin'
 WIFI_ADDR_PATH = '/lib/firmware/wlan/wlan_mac.bin'
 
 BD_NVITEM = 0x02
 BD_RDWR_PROT = 0x00
 BD_NVITEM_SIZE = 0x06
-BD_ADDR_HEADER_STRUCT = r'BBB'
-BD_ADDR_MAC_STRUCT = r'BBBBBB'
+BD_ADDR_HEADER_STRUCT = r'<BBB'
+BD_ADDR_MAC_STRUCT = r'HHH'
 
 WIFI_CONFIG_TEMPLATE = '''\
 Intf0MacAddress={0}
@@ -43,27 +41,28 @@
     any other checking done.
 
     Parameters:
-      mac_address_str: string. Colon-separated six-octet hexidecimal MAC address.
+      mac_address_str: string. Colon-separated six-octet hexadecimal MAC address.
 
     Returns:
       string. The next MAC address in the sequence.
     '''
+    vendor_prefix = GetMacDevicePrefixString(mac_address_str)
     device_suffix = GetMacDeviceSuffixString(mac_address_str)
     suffix_number = MacDeviceSuffixToNumber(device_suffix)
     suffix_string = MacDeviceSuffixNumberToString(suffix_number + 1)
-    next_mac = MAC_VENDOR_PREFIX_STR + ':' + suffix_string
+    next_mac = vendor_prefix + ':' + suffix_string
     return next_mac
 
 
 def MacDeviceSuffixNumberToString(device_suffix_number):
-    '''Given a device suffix number, generate a colon-separated hexidecimal
+    '''Given a device suffix number, generate a colon-separated hexadecimal
     representation.
 
     Parameters:
       device_suffix_number: integer. The number to convert into a MAC suffix.
 
     Returns:
-      string. The colon-separated hexidecimal version of the number passed in.
+      string. The colon-separated hexadecimal version of the number passed in.
     '''
     suffix_string = '%06x' % device_suffix_number
     suffix_array = []
@@ -75,16 +74,23 @@
 
 
 def MacDeviceSuffixToNumber(mac_suffix):
-    '''Converts a given a three-octet colon separated hexidecimal MAC device suffix
+    '''Converts a given a three-octet colon separated hexadecimal MAC device suffix
     into an integer.'''
     mac_array = mac_suffix.split(':')
     flatmac = ''.join(mac_array)
     return int(flatmac, 16)
 
 
+def GetMacDevicePrefixString(mac_address):
+    '''Return the vendor prefix from a given a full six-octet colon separated
+    hexadecimal MAC.'''
+    mac_array = mac_address.split(':')
+    return ':'.join(mac_array[:3])
+
+
 def GetMacDeviceSuffixString(mac_address):
     '''Strip the vendor prefix from a given a full six-octet colon separated
-    hexidecimal MAC.'''
+    hexadecimal MAC.'''
     mac_array = mac_address.split(':')
     return ':'.join(mac_array[3:])
 
@@ -123,12 +129,17 @@
 def WriteBluetoothMacAddress(next_mac):
     '''Writes the given MAC address string to a binary file readable by Bluez.'''
     mac_bytes = [int(x, 16) for x in next_mac.split(':')]
+    even_bytes = mac_bytes[0::2]
+    odd_bytes = mac_bytes[1::2]
+    zipped_bytes = zip(even_bytes, odd_bytes)
+    mac_bytes = [(x << 8) + y for x, y in zipped_bytes]
+    mac_bytes.reverse()
 
     try:
         with open(BD_ADDR_PATH, 'w') as fp:
-            fp.write(struct.pack(BD_ADDR_HEADER_STRUCT, BD_NVITEM, BD_RDWR_PROT,
-                                 BD_NVITEM_SIZE))
-            fp.write(struct.pack(BD_ADDR_MAC_STRUCT, *mac_bytes))
+            fp.write(struct.pack(BD_ADDR_HEADER_STRUCT + BD_ADDR_MAC_STRUCT,
+                                 BD_NVITEM, BD_RDWR_PROT, BD_NVITEM_SIZE,
+                                 *mac_bytes))
         return True
     except Exception as e:
         print('Error writing bluetooth configuration to %s: %s' % (BD_ADDR_PATH, e))