Skip to end of metadata
Go to start of metadata

Overview

This Application Note explains a simple method showing how to Store/Retrieve MAC IDs from EEPROM.

Targeted Hardware

Your target hardware may have different sysfs IDs for the EEPROM you are targeting. Please note that you may have to modify the device ID from what is used in the code-blocks below. You may also have different ethernet interfaces on your device. You can use ifconfig to list your interfaces. 

Store MAC IDs in EEPROM using sysfs Node

Format of MAC IDs

Single MAC ID

Beginning at byte 0 of the EEPROM, the data is formatted as follows:

eth0:001122334455

 Multiple MAC IDs

Beginning at byte 0 of the EEPROM, the data is formatted as follows:

eth1:002233445566
eth2:DEEEADBEEEEF
wlan0:502d00cceedd

MAC IDs are contained in a file

Write a file containing MAC IDs to the EEPROM

$ dd if=macids.txt of=/sys/bus/i2c/devices/2-0051/eeprom

MAC IDs are not contained in a file

Create a string of MAC IDs (assuming you have multiple interfaces) and write them to the EEPROM

$ printf "eth1:002233445566\neth2:DEEEADBEEEEF\nwlan0:502d00cceedd\n" > /sys/bus/i2c/devices/2-0051/eeprom

Append MAC IDs

The dd command can also take in stdin instead of an if= argument, and can be used to skip (seek past) # blocks of size bs=. This is useful to append MAC IDs instead of overwriting each one, allowing individual writes.

$ printf "eth2:DEEEADBEEEEF\nwlan0:502d00cceedd\n" | dd of=/sys/bus/i2c/devices/2-0051/eeprom bs=1 seek=18

Retrieve MAC IDs from EEPROM using sysfs Node

Display entire contents of EEPROM

$ cat /sys/bus/i2c/devices/2-0051/eeprom
eth1:002233445566
eth2:DEEEADBEEEEF
wlan0:502d00cceedd

Display a single line from EEPROM

3p directs sed to print the third line.

$ sed -n '3p' /sys/bus/i2c/devices/2-0051/eeprom
wlan0:502d00cceedd

Display a single line from EEPROM (based on the interface)

$ grep -i eth2 /sys/bus/i2c/devices/2-0051/eeprom
eth2:DEEEADBEEEEF

Strip the interface information from the output

A sed command: sed -e 's/^.*://' is used to strip the interface and display only the MAC ID that can be directly passed to ifconfig. It can be combined with the output of any of the display commands to grab the desired MAC IDs.

$ cat /sys/bus/i2c/devices/2-0051/eeprom | sed -e 's/^.*://'
002233445566
DEEEADBEEEEF
502d00cceedd
 
$ sed -n '3p' /sys/bus/i2c/devices/2-0051/eeprom | sed -e 's/^.*://'
502d00cceedd
 
$ grep -i eth2 /sys/bus/i2c/devices/2-0051/eeprom | sed -e 's/^.*://'
DEEEADBEEEEF

Sample script to automate setting MAC IDs using ifconfig

This script uses ifconfig to gather a list of supported ethernet and wlan interfaces then sets the corresponding MAC ID for that interface. This example is done after the interfaces have potentially come up, but can be modified and implemented as part of the network init scripts specific to each device's filesystem.

#!/bin/sh

INTERFACES=(`ifconfig -a | grep -o "eth[0-9]\|wlan[0-9]"`)

for iface in ${INTERFACES[@]}; do
        MACID=`grep -i ${iface} /sys/bus/i2c/devices/2-0051/eeprom | sed -e 's/^
        ifconfig ${iface} down
        ifconfig ${iface} hw ether ${MACID}
        ifconfig ${iface} up
done
exit 0