如何使用Linux内核中的命令行参数更改mac地址

问题描述 投票:-1回答:1

我想像下面那样在u-boot级别更改mac地址。

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

在司机那里,

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);

启动时,日志消息如下。

[    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

命令行消息是$ {ethaddr},对吗?Mac地址不正确。

我该如何解决?

command-line embedded-linux mac-address
1个回答
1
投票

您在以下位置使用单引号:

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

所以${ethaddr}不会展开,并且bootargs变量包含文字字符串ethaddr=${ethaddr},然后将其传递到内核中,这就是您在调试输出中看到的内容。有关更多详细信息,请参见How the Command Line Parsing Works上的U-Boot文档。

您可以使用双引号,或者根本不使用双引号,在这种情况下,${ethaddr}会在分配给bootargs时进行扩展,尽管您需要先设置它:

# 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

[请注意,在某些系统中,U-Boot本身使用ethaddr变量来配置第一个网络设备的MAC地址,并且Linux网络驱动程序可能会继续使用该地址,因此您无需显式将其传递到内核。请参阅U-Boot Environment Variables上的文档。

另外,U-Boot可能被配置为防止修改ethaddr变量,尽管这里可能不是这样,因为当它是U-Boot时会显示错误消息:

Can't overwrite "ethaddr"
© www.soinside.com 2019 - 2024. All rights reserved.