使用ethtool命令对EEPROM进行读写
Android系统默认未支持ethtool命令,需增加工具。
参考:http://blog.csdn.net/u013686019/article/details/51491364
下载ethtool源码进行编译。
源码地址:https://www.kernel.org/pub/software/network/ethtool/
增加Android.mk进行编译如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
define all-c-files-under
$(patsubst ./%,%, $(shell find $(LOCAL_PATH) -name “platform” -prune -o -name ".c" -and -not -name "."))
endef
define all-subdir-c-files
$(call all-c-files-under,.)
endef
C_FILES := $(call all-subdir-c-files)
LOCAL_SRC_FILES :=
(
C
F
I
L
E
S
:
(C_FILES:
(CFILES:(LOCAL_PATH)/%=%)
LOCAL_SHARED_LIBRARIES := libcutils libutils libcrypto libc
#LOCAL_C_INCLUDES := $(KERNEL_HEADERS) //此处修改网站参考内容
LOCAL_CFLAGS := -DANDROID_CHANGES -DCHAPMS=1 -DMPPE=1 -Iexternal/openssl/include
LOCAL_MODULE :=ethtool
LOCAL_MODULE_TAGS := optional
include $(BUILD_EXECUTABLE)
生成ethtool push到system/bin中。命令如下:
ethtool -e|–eeprom-dump DEVNAME Do a EEPROM dump
[ raw on|off ]
[ offset N ]
[ length N ]
ethtool -E|–change-eeprom DEVNAME Change bytes in device EEPROM
[ magic N ]
[ offset N ]
[ length N ]
[ value N ]
示例
本次使用ethtool实现对LAN9514的Mac地址烧写
MAC地址写号命令如下:
ethtool -E eth0 magic 0x9500 offset 0x0000 length 1 value 0xff
ethtool -E eth0 magic 0x9500 offset 0x0001 length 1 value 0x00
ethtool -E eth0 magic 0x9500 offset 0x0002 length 1 value 0x1a
ethtool -E eth0 magic 0x9500 offset 0x0003 length 1 value 0x13
ethtool -E eth0 magic 0x9500 offset 0x0004 length 1 value 0x14
ethtool -E eth0 magic 0x9500 offset 0x0005 length 1 value 0x56
ethtool -E eth0 magic 0x9500 offset 0x0006 length 1 value 0x78
0x9500是需要写入的EEPROM地址
写入后回读情况如下:
回读EEPROM内容正确,但需重启才会更新MAC地址,否则还是原来随机分配的地址。
批量操作
使用adb push ethtool后对输入的mac地址进行解析处理,再通过命令写号并回读。
脚本参考ethtool_mac.bat
@set mac=
@set /p mac=MAC:
@echo %mac%
@set mac1=%mac:~0,2%
@set mac2=%mac:~2,2%
@set mac3=%mac:~4,2%
@set mac4=%mac:~6,2%
@set mac5=%mac:~8,2%
@set mac6=%mac:~10,2%
@tools\adb.exe shell /data/ethtool -E eth0 magic 0x9500 offset 0x0001 length 1 value 0x%mac1%
@tools\adb.exe shell /data/ethtool -E eth0 magic 0x9500 offset 0x0002 length 1 value 0x%mac2%
@tools\adb.exe shell /data/ethtool -E eth0 magic 0x9500 offset 0x0003 length 1 value 0x%mac3%
@tools\adb.exe shell /data/ethtool -E eth0 magic 0x9500 offset 0x0004 length 1 value 0x%mac4%
@tools\adb.exe shell /data/ethtool -E eth0 magic 0x9500 offset 0x0005 length 1 value 0x%mac5%
@tools\adb.exe shell /data/ethtool -E eth0 magic 0x9500 offset 0x0006 length 1 value 0x%mac6%