Caleb Connolly | daaab94 | 2022-04-07 01:59:50 +0100 | [diff] [blame^] | 1 | #! /system/bin/sh |
| 2 | # Set wlan0 mac address. |
| 3 | # |
| 4 | # Get the unique board serial number from /proc/cmdline, |
| 5 | # prepend '0's to the serial number to fill 5 LSBs of the |
| 6 | # MAC address and prepend "02" as MSB to prepare a 6 byte |
| 7 | # locally administered unicast MAC address. |
| 8 | # Format the output in xx:xx:xx:xx:xx:xx format for the |
| 9 | # "ip" set address command to work. |
| 10 | |
| 11 | DEV="wlan0" |
| 12 | |
| 13 | SERIALNO=`cat /proc/cmdline | grep -o "serialno.*" | cut -d" " -f1` |
| 14 | |
| 15 | # If for some reason the serial number isn't on cmdline, just use random |
| 16 | if [ -z "$SERIALNO" ]; then |
| 17 | SERIALNO="$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM" |
| 18 | fi |
| 19 | |
| 20 | # Do some funky stuff to generate a MAC address from the serial number |
| 21 | # We sha256 it because some devices use non-base 16 characters in their |
| 22 | # serial number (*ahem* SHIFT6mq). |
| 23 | SERIALNO=`echo $SERIALNO | sha256sum | awk '{printf("02%010s\n", $1)}' | sed 's/\(..\)/\1:/g' | sed '$s/:$//' | cut -d: -f1,2,3,4,5,6` |
| 24 | |
| 25 | # If the mac address has already been generated then use that instead |
| 26 | if [ -f /data/wlan_mac.bin ]; then |
| 27 | WLAN_MAC=`cat /data/wlan_mac.bin` |
| 28 | else |
| 29 | WLAN_MAC="$SERIALNO" |
| 30 | echo $WLAN_MAC > /data/wlan_mac.bin |
| 31 | fi |
| 32 | |
| 33 | echo "Setting $DEV mac addr to $WLAN_MAC" |
| 34 | |
| 35 | while ! ip link show $DEV > /dev/null; do |
| 36 | echo "Device not up yet, waiting for it" |
| 37 | sleep 1 |
| 38 | done |
| 39 | |
| 40 | /system/bin/ip link set dev $DEV down |
| 41 | /system/bin/ip link set dev $DEV address "${WLAN_MAC}" |
| 42 | /system/bin/ip link set dev $DEV up |