Skip to content
Snippets Groups Projects
Unverified Commit c209cf29 authored by Dylan Van Assche's avatar Dylan Van Assche
Browse files

bootmac: set MAC from serialno or machine-id

Bootmac configures the MAC addresses of WLAN and Bluetooth interfaces at boot.
Bootmac can be invoked in various ways at boot, but currently only udev rules are tested.
Bootmac generates MAC addresses from the `serialno` provided by Android bootloaders
through /proc/cmdline or from /etc/machine-id. The Organizationally Unique Identifier (OUI)
is retrieved from a lookup database macdb by matching deviceinfo_manufacturer
from /etc/deviceinfo with the entries in the database.
parent 10625cd5
No related branches found
No related tags found
No related merge requests found
bootmac 0 → 100644
#!/bin/sh
set -e
#
# Configure WLAN and Bluetooth MAC addresses for SDM845 devices on boot.
# This script is triggered through an udev rule when the WLAN or Bluetooth
# interface comes up.
#
# Copyright (c) Dylan Van Assche (2022)
# SPDX-License-Identifier: GPL-3.0-or-later
#
MACDB_PATH="/etc/macdb"
WLAN_MAC=""
BT_MAC=""
WLAN_INTERFACE="wlan0"
BT_INTERFACE="hci0"
source /etc/deviceinfo
log() {
echo "$@" | logger -t "qmac"
}
mac_generate() {
# Try to retrieve the serial number from the cmdline
log "retrieving device serial number"
SERIAL_NUMBER=$(cat /proc/cmdline | grep -o "serialno.*" | cut -d" " -f1)
# If the serial number is missing, fallback to /etc/machine-id
# While 'machine-id' ensures the MAC address is consistent across reboot,
# it will be changed each time the device is flashed since the machine-id
# is generated on first boot.
if [ -z "$SERIAL_NUMBER" ]; then
log "serial number not available, falling back to machine-id"
SERIAL_NUMBER=$(cat /etc/machine-id)
fi
if [ -z "$SERIAL_NUMBER" ]; then
log "no unique ID available, cannot generate MAC address"
exit 1
fi
# Manufacturers get a range of MAC addresses with a prefix called
# Organizationally Unique Identifier (OUI). Read the OUI from /etc/macdb
MAC_OUI=$(cat $MACDB_PATH | grep $deviceinfo_manufacturer | cut -f1)
MAC_CTRL=$(echo "$SERIAL_NUMBER" | sha256sum | awk '{printf("%010s\n", $1)}')
MAC=$(echo "${MAC_OUI:0:6}${MAC_CTRL:0:6}")
# Generate WLAN from extracted SERIAL_NUMBER
# Increment WLAN MAC by 1 to have a valid Bluetooth MAC address
log "generating WLAN and Bluetooth MAC addresses"
WLAN_MAC=$(echo "$MAC" | sed 's/\(..\)/\1:/g' | sed '$s/:$//')
BT_MAC=$(printf "%x\n" $((0x$MAC + 1)) | sed 's/\(..\)/\1:/g' | sed '$s/:$//')
}
mac_bluetooth() {
# Bring Bluetooth down, set the generated MAC and bring it up again
log "setting Bluetooth MAC to $BT_MAC"
btmgmt -i "$BT_INTERFACE" power off
btmgmt -i "$BT_INTERFACE" public-addr $BT_MAC
rfkill unblock bluetooth
btmgmt -i "$BT_INTERFACE" power on
}
mac_wlan() {
# Bring WLAN down, set the extracted MAC and bring it up again
log "setting WLAN MAC to $WLAN_MAC"
ip link set dev "$WLAN_INTERFACE" down
ip link set dev "$WLAN_INTERFACE" address "$WLAN_MAC"
rfkill unblock wlan
ip link set dev "$WLAN_INTERFACE" up
}
log "configuring MAC addresses"
mac_generate
mac_wlan
mac_bluetooth
log "MAC addresses configured successfully"
#
# Configure the MAC addresses of the WLAN and Bluetooth interfaces when they appear.
# Copyright (c) Dylan Van Assche (2022)
# SPDX-License-Identifier: GPL-3.0-or-later
#
ACTION=="add", SUBSYSTEM=="net", KERNEL=="wlan0", RUN+="/usr/bin/qmac"
ACTION=="add", SUBSYSTEM=="bluetooth", KERNEL=="hci0", RUN+="/usr/bin/qmac"
macdb 0 → 100644
OUI MANUFACTURER
64A2F9 OnePlus
785364 SHIFT
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment