为什么RNDIS的MAC地址每次插拔USB都会变?

  • 您好 可以参考这个主题viewtopic.php?p=4098&hilit=mac#p4098,将启动的脚本修改为

    Code: Select all

    ifconfig usb0 down
    ifconfig usb0 hw ether <Your MAC addr>#更正 原本为  ifconfig usb0 hw ethertg <Your MAC addr>
    ifconfig usb0 up
    
  • 我在

    Code: Select all

    /oem/usr/bin/RkLunch.sh
    里这样改了,没用:

    Code: Select all

    network_init()
    {
            ethaddr1=`ifconfig -a | grep "eth.*HWaddr" | awk '{print $5}'`
    
            if [ -f /data/ethaddr.txt ]; then
                    ethaddr2=`cat /data/ethaddr.txt`
                    if [ $ethaddr1 == $ethaddr2 ]; then
                            echo "eth HWaddr cfg ok"
                    else
                            ifconfig eth0 down
                            ifconfig eth0 hw ether $ethaddr2
                    fi
            else
                    echo $ethaddr1 > /data/ethaddr.txt
            fi
            ifconfig eth0 up && udhcpc -i eth0
    
            ifconfig usb0 down
            ifconfig usb0 hw ethertg BA:B1:44:37:D7:AA
            ifconfig usb0 up
    }
    
    
  • 我发现了这个issue:
    https://github.com/milkv-duo/duo-buildr ... /issues/27
    里面人说:
    There is maybe an easier way to do this.

    If you set the kernel params something like g_ether.host_addr=00:11:22:33:44:55 g_ether.dev_addr=55:44:33:22:11:00, the ethernet-over-usb driver (CONFIG_USB_ETH) will not generate random MACs for the host and the device.

    For the buildroot image, this would need to be set in SET_BOOTARGS in various places.
    我在那里设置SET_BOOTARGS呢?
  • 很抱歉,前面提供的参考有误,设置MAC地址的命令应该为

    Code: Select all

    ifconfig usb0 hw ether <Your MAC Address>
    
    usb的初始化在执行RkLunch.sh之后,所以要添加脚本的位置应该在 /etc/init.d/S99usb0config 的 usb0_config 函数下

    Code: Select all

    usb0_config() {                                              
            if [ "$(cat /proc/device-tree/usbdrd/usb@ffb00000/dr_mode)" == "peripheral" ]; then
                            ################################################
            		#################  Add Here   ####################
            		################################################   
                            ifconfig usb0 down                                                       
                            ifconfig usb0 hw ether 2A:EE:82:4A:7C:0C                                 
                            ifconfig usb0 up                      
                           	sleep 0.5                                   
                            ################################################
            		##################### END  ######################
            		################################################       
            
           
                    current_ip=$(ifconfig usb0 | grep -o 'inet addr:[^ ]*' | awk -F ':' '{print $2}')
                    echo "current_ip = $current_ip"                                                  
                    echo "TARGET_IP = $TARGET_IP"                                                    
                  
                                                                                                    
                    while [[ "$current_ip" != "$TARGET_IP" && $retries -lt $MAX_RETRIES ]]; do                                                                        
                            
                            sleep .5                                                          
                            echo "luckfox : set usb0 ip"                                      
                            ifconfig usb0 "$TARGET_IP"                                        
                            current_ip=$(ifconfig usb0 | grep -o 'inet addr:[^ ]*' | awk -F ':' '{print $2}')
                            echo $current_ip                                                                 
                            retries=$((retries + 1))                                                         
                                                                                                             
                                                                                                             
                    done                                                                                     
                                                                                                             
                    if [[ "$current_ip" != "$TARGET_IP" ]]; then                                             
                            echo "usb0 config error"            
                    else                                        
                            echo "usb0 config success"          
                    fi                                          
            else                                                
                    echo "usb0 is using host mode"              
            fi                                                  
    }  
    
    如果需要修改BOOTARG的话直接修改设备树中/choose节点的bootargs,放在luckfox-pico的dts对应的dtsi中
  • 我按照这个方法改了MAC还是无法固定。然后尝试改设备树文件加入

    Code: Select all

    g_ether.dev_addr=12:34:56:78:9a:bc g_ether.host_addr=12:34:56:78:9a:bd

    Code: Select all

    // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
    /*
     * Copyright (c) 2022 Rockchip Electronics Co., Ltd.
     */
    #include "rv1106-amp.dtsi"
    
    / {
    	chosen {
    		bootargs = "earlycon=uart8250,mmio32,0xff4c0000 console=ttyFIQ0 root=/dev/mmcblk1p7 rootwait snd_soc_core.prealloc_buffer_size_kbytes=16 coherent_pool=0 g_ether.dev_addr=12:34:56:78:9a:bc g_ether.host_addr=12:34:56:78:9a:bd";
    	};
    依旧是一点用也没。。。
    Last edited by cocoyuan on 2024-11-25 14:54, edited 1 time in total.
  • Crocodile wrote: 2024-11-25 6:01 很抱歉,前面提供的参考有误,设置MAC地址的命令应该为

    Code: Select all

    ifconfig usb0 hw ether <Your MAC Address>
    
    usb的初始化在执行RkLunch.sh之后,所以要添加脚本的位置应该在 /etc/init.d/S99usb0config 的 usb0_config 函数下

    Code: Select all

    usb0_config() {                                              
            if [ "$(cat /proc/device-tree/usbdrd/usb@ffb00000/dr_mode)" == "peripheral" ]; then
                            ################################################
            		#################  Add Here   ####################
            		################################################   
                            ifconfig usb0 down                                                       
                            ifconfig usb0 hw ether 2A:EE:82:4A:7C:0C                                 
                            ifconfig usb0 up                      
                           	sleep 0.5                                   
                            ################################################
            		##################### END  ######################
            		################################################       
            
           
                    current_ip=$(ifconfig usb0 | grep -o 'inet addr:[^ ]*' | awk -F ':' '{print $2}')
                    echo "current_ip = $current_ip"                                                  
                    echo "TARGET_IP = $TARGET_IP"                                                    
                  
                                                                                                    
                    while [[ "$current_ip" != "$TARGET_IP" && $retries -lt $MAX_RETRIES ]]; do                                                                        
                            
                            sleep .5                                                          
                            echo "luckfox : set usb0 ip"                                      
                            ifconfig usb0 "$TARGET_IP"                                        
                            current_ip=$(ifconfig usb0 | grep -o 'inet addr:[^ ]*' | awk -F ':' '{print $2}')
                            echo $current_ip                                                                 
                            retries=$((retries + 1))                                                         
                                                                                                             
                                                                                                             
                    done                                                                                     
                                                                                                             
                    if [[ "$current_ip" != "$TARGET_IP" ]]; then                                             
                            echo "usb0 config error"            
                    else                                        
                            echo "usb0 config success"          
                    fi                                          
            else                                                
                    echo "usb0 is using host mode"              
            fi                                                  
    }  
    
    如果需要修改BOOTARG的话直接修改设备树中/choose节点的bootargs,放在luckfox-pico的dts对应的dtsi中


    使用该脚本的方法实测是没有问题的,现在修改加入延时确保不与IP设置冲突,您可以再次尝试一下,bootarg直接添加内容如果内核无法解析会传入用户空间,不确定有没有应用来接收处理添加的内容也是无法生效的