Set MAC-address when building firmware

Hi, how do I set MAC address when building the firmware?

hard, or random ?

Hard. I have the model NanoPi R4S, not the Enterprise, with unique MAC (I know the MAC address) but it can’t be read by OpenWrt so it’s the actual MAC of the board I want to use instead of the random address it gets each boot.

Just copy and paste current random mac into interface....

try uci set network.wan.macaddr="aa:bb:cc:dd:ee" in the script window in https://firmware-selector.openwrt.org/.
not sure about the " " though.

1 Like

Thank you. I’m building OpenWrt from source, not using the Firmware Selector, but I’ll look into how to combine your suggestion with my own build. I think I can use a uci-defaults script in the files/ directory to set the MAC address automatically at first boot.

This is the way to go indeed ...

1 Like

So now I’ve tried both using uci-default and also patching dts and the the static MAC I want is changed in etc/config/network in both cases but it appears it’s changed too late in boot process since log says can’t read MAC address, setting random one and the random one is used not the hard coded one I want. Is there another option I could try?

that could be irrelevant depending on where/what is reporting it. What matters is the mac/s are correct once the device has fully booted. The error message provided appears to be from uboot (though is hard to say without the log) .. which is common for many devices because macs can be set in numerous ways, and not all use (or can use) uboot to harvest the mac/s, which leads to these types of toothless errors.

This is the desired outcome ..no ?

The problem is that the hard coded MAC in etc/config/network is not used, the interfaces are using the random MAC. When checking ip a the random MAC is used. That’s why I’m thinking that the correct MAC is changed too late during boot and the random MAC is generated earlier and therefore being used?

It would not be timing related if you used the dts method. I would try setting them in 02_network to see if it make a difference.

1 Like

I’ll try that. Thanks

Another thing, did you set the label mac device alias in the dts when you tried the dts method ?

I would suggest setting the local mac for the main interface, then assigning ethernet alias and label-mac-device alias

to use my device dts as an example

aliases {
		ethernet1 = &dp2;
		label-mac-device = &dp2;
		led-boot = &led_system_blue;
		led-failsafe = &led_status_white;
		led-running = &led_status_white;
		led-upgrade = &led_system_blue;
		serial0 = &blsp1_uart1;
};

...

&dp2 {
	status = "okay";
	local-mac-address = [ 00 00 12 34 56 78 ];  <--- set dersired mac

	fixed-link {
		speed = <1000>;
		full-duplex;
	};
};

then in your 02_network you can reference label mac like

#!/bin/sh

. /lib/functions/uci-defaults.sh
. /lib/functions/system.sh

<target>_setup_interfaces()  # --> change target to your target name
{
	local board="$1"
	case $board in
	<boardname>)
		ucidef_set_interfaces_lan_wan "lan1 lan2" "wan" # --> change according to device 
		;;
	esac
}

<target>_setup_macs() # --> change target to your target name
{
	local board="$1"
	local lan_mac=""
	local wan_mac=""
	local label_mac=""

	case "$board" in
	
	<boardname>) # --> your boardname
			label_mac=$(get_mac_label_dt)
			lan_mac=$(macaddr_add $label_mac 1)
			wan_mac=$(macaddr_add $label_mac 2)
			ucidef_set_network_device_mac eth0 $label_mac # --> may be eth1
		;;
	esac

	[ -n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" $lan_mac
	[ -n "$wan_mac" ] && ucidef_set_interface_macaddr "wan" $wan_mac
	[ -n "$label_mac" ] && ucidef_set_label_macaddr $label_mac
}

board_config_update
board=$(board_name)
<target>_setup_interfaces $board  # --> change target to your target name
<target>_setup_macs $board  # --> change target to your target name
board_config_flush

exit 0

Could the problem be that I choose to keep settings when upgrading via LuCi?

I sent my other message before I saw this reply. I’ll check the config and reply later.

yes absolutely... i made the same mistake while trying to fix the nvmem macs for my device.

Thet’s great to hear. I’ll try that first then :+1: