From 18bed41c6600ed556acf8d60fd0f04f78274de74 Mon Sep 17 00:00:00 2001 From: Lethja <150316953+Lethja@users.noreply.github.com> Date: Wed, 10 Sep 2025 16:41:03 +1000 Subject: [PATCH] Mac Address (#123) * Initial implementation of mac_address.sh * Added manual MAC address entry. * Fixed wrong variable in update_mac() function. * Added clear commands before exiting. * Spelling and grammar. * Added some comments. * Added shebang. * Corrected terminal output of dialog commands. * Allow startup dialogs to show on any console. * Corrected valid MAC addresses being marked as invalid. Improved manual MAC address entry. * Replaced hardcoded redirects to '/dev/tty2' with redirects to scripts stderr and swap where applicable * Make MAC address discovery optional. * Modified sed regex to improve finding and replacing ethaddr variable in update_uboot() * Code cleanup. * Check important commands exist before running the script. * Generate locally administered, unicast addresses in a smarter way. * Simplify script * Code cleanup and minor experence changes * Moved mac_address.sh from other_authors to repository root --- mac_address.sh | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100755 mac_address.sh diff --git a/mac_address.sh b/mac_address.sh new file mode 100755 index 0000000..b97601c --- /dev/null +++ b/mac_address.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash + +# Hardcoded for MiSTer +iface="eth0" +file="/media/fat/linux/u-boot.txt" + +update_uboot() { + if [ -n "$1" ]; then + if [ ! -e "$file" ]; then + echo "ethaddr=${1^^}" > "$file" + elif grep -q 'ethaddr=' "$file"; then + sed -i "s/\(ethaddr=\)[0-9A-Fa-f:]\+/\1${1^^}/" "$file" + else + echo "ethaddr=${1^^}" >> "$file" + fi + fi +} + +valid() { + # Convert to a locally administistored unicast MAC address + local byte=$(( 16#${1:0:2} )) + byte=$(( (byte & ~1) | 2 )) + byte=$(printf "%02x" $byte) + + local mac="${byte}${1:2}" + echo "${mac^^}" + + if [[ "${1^^}" != "${mac^^}" ]]; then + return 0 # Input was not valid + fi + + return 1 # Input was already valid +} + +finished() { + if dialog --yesno "The MAC address has been changed to '$1'.\nYou will need to do a cold reboot for this to take effect.\n\nWould you like to cold reboot now?" 10 38 1>&2; then + dialog --infobox "Rebooting please wait..." 3 28 1>&2 + reboot + else + dialog --clear + fi + exit 0 +} + +auto() { + local mac + mac=$(tr -dc '0-9a-f' &1 1>&2 2>&3); then + return + fi + + # Format MAC address + input=${input^^} + input=${input//-/:} + + # Validate user input is a MAC address + if [[ ! "$input" =~ ^([0-9A-F]{2}([-:])){5}([0-9A-Fa-f]{2})$ ]]; then + dialog --msgbox "Not a valid MAC address." 5 52 1>&2 + continue + fi + + local mac sta + mac=$(valid "$input") + sta=$? + + if [ $sta -eq 0 ]; then + if dialog --yesno "'${input^^}' is not a suitable MAC address.\n\nWould you like to use '$mac' instead?" 7 64 1>&2; then + input="$mac" + break + fi + else + if [ "$input" = "$current" ]; then + dialog --msgbox "'$input' is the current MAC address." 5 52 1>&2 + continue + fi + break + fi + done + + update_uboot "$mac" + finished "$mac" +} + +while true; do + local addr + local choice + addr=$(cat "/sys/class/net/$iface/address") + choice=$(dialog --menu "The current MAC address for $iface is '${addr^^}'.\n\nWhat do you want to do?" 12 44 3 \ + A "Automatically generate MAC address" M "Manually set MAC address..." 3>&1 1>&2 2>&3) + + case $choice in + A) + auto + ;; + M) + manual + ;; + *) + exit 0 + ;; + esac +done