简体   繁体   中英

How to change a mac address using the command line parameters in the linux kernel

I want to change a mac address at the u-boot level like the following.

# setenv bootargs 'console=ttyAMA0,115200n8 root=/dev/ram0 rw initrd=0x40000000 ethaddr=${ethaddr}'
# setenv ethaddr 11:22:33:44:55:66
# saveenv

And at the driver,

static unsigned char my_ethaddr[MAX_ADDR_LEN];

/* need to get the ether addr from armboot */
static int __init ethaddr_setup(char *line)
{
        char *ep;
        int i;

        printk("command line : %s\n", line);

        memset(my_ethaddr, 0, MAX_ADDR_LEN);
        /* there should really be routines to do this stuff */
        for (i = 0; i < 6; i++)
        {
                my_ethaddr[i] = line ? simple_strtoul(line, &ep, 16) : 0;
                if (line)
                        line = (*ep) ? ep+1 : ep;
                printk("mac[%d] = 0x%02Xn", i, my_ethaddr[i]);
        }
        return 0;
}
__setup("ethaddr=", ethaddr_setup);

When booting, the log message is like following.

[    0.000000] Kernel command line: console=ttyAMA0,115200n8 root=/dev/ram0 rw initrd=0x40000000 ethaddr=${ethaddr}
[    0.000000] command line : ${ethaddr}
[    0.000000] mac[0] = 0x00, mac[1] = 0x00, mac[2] = 0x0E, mac[3] = 0x00, mac[4] = 0xDD, mac[5] = 0x00

The command line message is ${ethaddr}, is it right? The mac address isn't correct.

How can I fix it?

You are using single quotes in:

setenv bootargs '... ethaddr=${ethaddr}'

so ${ethaddr} is not expanded and the bootargs variable contains the literal string ethaddr=${ethaddr} , which is then passed into the kernel and is what you see in your debug output. See the U-Boot documentation on How the Command Line Parsing Works for more details.

You could use double quotes, or no quotes at all, in which case ${ethaddr} would be expanded when assigning to bootargs , although you would need to set it first:

# setenv ethaddr 11:22:33:44:55:66
# setenv bootargs console=ttyAMA0,115200n8 root=/dev/ram0 rw initrd=0x40000000 ethaddr=${ethaddr}  
# printenv bootargs
bootargs=console=ttyAMA0,115200n8 root=/dev/ram0 rw initrd=0x40000000 ethaddr=11:22:33:44:55:66

Note that in some systems the ethaddr variable is used by U-Boot itself to configure the MAC address of the first network device, and the Linux network driver may continue to use that address, so you don't need to explicitly pass it into the kernel. See the documentation on U-Boot Environment Variables .

In addition U-Boot may be configured to prevent modification of the ethaddr variable, although that's probably not the case here because when it is U-Boot prints the error message:

Can't overwrite "ethaddr"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM