简体   繁体   English

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

[英]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.我想在 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

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?命令行消息是${ethaddr},对吗? The mac address isn't correct. mac地址不对。

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.所以${ethaddr}没有被扩展并且bootargs变量包含文字字符串ethaddr=${ethaddr} ,然后它被传递到内核并且是你在调试输出中看到的。 See the U-Boot documentation on How the Command Line Parsing Works for more details.有关更多详细信息,请参阅有关命令行解析如何工作的 U-Boot 文档。

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:您可以使用双引号,或者根本不使用引号,在这种情况下, ${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

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.请注意,在某些系统中, ethaddr变量由 U-Boot 本身用于配置第一个网络设备的 MAC 地址,Linux 网络驱动程序可能会继续使用该地址,因此您无需将其显式传递到内核。 See the documentation on U-Boot Environment Variables .请参阅有关U-Boot 环境变量的文档。

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:此外,U-Boot 可能被配置为防止修改ethaddr变量,尽管这里可能不是这种情况,因为当 U-Boot 打印错误消息时:

Can't overwrite "ethaddr"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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