aboutsummaryrefslogtreecommitdiffstats
path: root/doc/README.enetaddr
blob: 1eaeaf9416efb5c76a81a202390f4bf110b11adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---------------------------------
 Ethernet Address (MAC) Handling
---------------------------------

There are a variety of places in U-Boot where the MAC address is used, parsed,
and stored.  This document covers proper usage of each location and the moving
of data between them.

-----------
 Locations
-----------

Here are the places where MAC addresses might be stored:

 - board-specific location (eeprom, dedicated flash, ...)
	Note: only used when mandatory due to hardware design etc...

 - environment ("ethaddr", "eth1addr", ...) (see CONFIG_ETHADDR)
	Note: this is the preferred way to permanently store MAC addresses

 - ethernet data (struct eth_device -> enetaddr)
	Note: these are temporary copies of the MAC address which exist only
	      after the respective init steps have run and only to make usage
	      in other places easier (to avoid constant env lookup/parsing)

 - struct bd_info and/or device tree
	Note: these are temporary copies of the MAC address only for the
	      purpose of passing this information to an OS kernel we are about
	      to boot

Correct flow of setting up the MAC address (summarized):

1. Read from hardware in initialize() function
2. Read from environment in net/eth.c after initialize()
3. The environment variable will be compared to the driver initialized
   struct eth_device->enetaddr. If they differ, a warning is printed, and the
   environment variable will be used unchanged.
   If the environment variable is not set, it will be initialized from
   eth_device->enetaddr, and a warning will be printed.
4. Program the address into hardware if the following conditions are met:
	a) The relevant driver has a 'write_addr' function
	b) The user hasn't set an 'ethmacskip' environment variable
	c) The address is valid (unicast, not all-zeros)

Previous behavior had the MAC address always being programmed into hardware
in the device's init() function.

-------
 Usage
-------

If the hardware design mandates that the MAC address is stored in some special
place (like EEPROM etc...), then the board specific init code (such as the
board-specific misc_init_r() function) is responsible for locating the MAC
address(es) and initializing the respective environment variable(s) from it.
Note that this shall be done if, and only if, the environment does not already
contain these environment variables, i.e. existing variable definitions must
not be overwritten.

During runtime, the ethernet layer will use the environment variables to sync
the MAC addresses to the ethernet structures.  All ethernet driver code should
then only use the enetaddr member of the eth_device structure.  This is done
on every network command, so the ethernet copies will stay in sync.

Any other code that wishes to access the MAC address should query the
environment directly.  The helper functions documented below should make
working with this storage much smoother.

---------
 Helpers
---------

To assist in the management of these layers, a few helper functions exist.  You
should use these rather than attempt to do any kind of parsing/manipulation
yourself as many common errors have arisen in the past.

	* void eth_parse_enetaddr(const char *addr, uchar *enetaddr);

Convert a string representation of a MAC address to the binary version.
char *addr = "00:11:22:33:44:55";
uchar enetaddr[6];
eth_parse_enetaddr(addr, enetaddr);
/* enetaddr now equals { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 } */

	* int eth_getenv_enetaddr(char *name, uchar *enetaddr);

Look up an environment variable and convert the stored address.  If the address
is valid, then the function returns 1.  Otherwise, the function returns 0.  In
all cases, the enetaddr memory is initialized.  If the env var is not found,
then it is set to all zeros.  The common function is_valid_ether_addr() is used
to determine address validity.
uchar enetaddr[6];
if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
	/* "ethaddr" is not set in the environment */
	... try and setup "ethaddr" in the env ...
}
/* enetaddr is now set to the value stored in the ethaddr env var */

	* int eth_setenv_enetaddr(char *name, const uchar *enetaddr);

Store the MAC address into the named environment variable.  The return value is
the same as the setenv() function.
uchar enetaddr[6] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
eth_setenv_enetaddr("ethaddr", enetaddr);
/* the "ethaddr" env var should now be set to "00:11:22:33:44:55" */

	* the %pM format modifier

The %pM format modifier can be used with any standard printf function to format
the binary 6 byte array representation of a MAC address.
uchar enetaddr[6] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
printf("The MAC is %pM\n", enetaddr);

char buf[20];
sprintf(buf, "%pM", enetaddr);
/* the buf variable is now set to "00:11:22:33:44:55" */