summaryrefslogtreecommitdiffstats
path: root/target/linux/mediatek/mt7623/base-files/lib/preinit/07_set_iface_mac
blob: 7b165df5651443f006a799cc713c5c6876b5ff2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright (C) 2018 OpenWrt.org

. /lib/functions.sh
. /lib/functions/system.sh

set_recovery_mac_address() {
	local RECOVERY_PART="$1"
	local mac

	if [ -b $RECOVERY_PART ]; then
		insmod nls_cp437
		insmod nls_iso8859-1
		insmod fat
		insmod vfat
		mkdir -p /tmp/recovery
		if mount -o rw,noatime $RECOVERY_PART -t vfat /tmp/recovery; then
			if [ -f "/tmp/recovery/mac_addr" ];
			then
				mac=$(cat /tmp/recovery/mac_addr)
			else
				mac=$(cat /sys/class/net/eth0/address)
				echo "$mac" > /tmp/recovery/mac_addr
			fi

			sync
			umount /tmp/recovery
			rmdir /tmp/recovery

			ip link set dev wan address $mac 2> /dev/null

			mac=$(macaddr_add $mac 1)

			ip link set dev lan0 address $mac 2>/dev/null
			ip link set dev lan1 address $mac 2>/dev/null
			ip link set dev lan2 address $mac 2>/dev/null
			ip link set dev lan3 address $mac 2>/dev/null
		fi
	fi
}

preinit_set_mac_address() {
	local rootpart

	case $(board_name) in
	bananapi,bpi-r2|\
	unielec,u7623-02-emmc-512m)
		rootpart=$(cat /proc/cmdline)
		rootpart="${rootpart##*root=}"
		rootpart="${rootpart%% *}"
		case $rootpart in
		/dev/mmcblk0p2) # Legacy U7623 image
			set_recovery_mac_address /dev/mmcblk0p1
			;;
		/dev/mmcblk0p3) # U7623 or Banana Pi R2 eMMC
			set_recovery_mac_address /dev/mmcblk0p2
			;;
		/dev/mmcblk1p3) # Banana Pi R2 SD
			set_recovery_mac_address /dev/mmcblk1p2
			;;
		esac
		;;
	esac
}

boot_hook_add preinit_main preinit_set_mac_address