blob: 0e36bc3d2fb82a5fa082e7a2348ef1a7740d0314 [file] [log] [blame]
Caleb Connollydaaab942022-04-07 01:59:50 +01001#! /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
11DEV="wlan0"
12
13SERIALNO=`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
16if [ -z "$SERIALNO" ]; then
17 SERIALNO="$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM"
18fi
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).
23SERIALNO=`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
26if [ -f /data/wlan_mac.bin ]; then
27 WLAN_MAC=`cat /data/wlan_mac.bin`
28else
29 WLAN_MAC="$SERIALNO"
30 echo $WLAN_MAC > /data/wlan_mac.bin
31fi
32
33echo "Setting $DEV mac addr to $WLAN_MAC"
34
35while ! ip link show $DEV > /dev/null; do
36 echo "Device not up yet, waiting for it"
37 sleep 1
38done
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