blob: 6c61817da3986b5fa9ba543b331adb7d6cec250d [file] [log] [blame]
Mike Frysinger6ff41372009-02-11 14:12:34 -05001---------------------------------
2 Ethernet Address (MAC) Handling
3---------------------------------
4
5There are a variety of places in U-Boot where the MAC address is used, parsed,
6and stored. This document covers proper usage of each location and the moving
7of data between them.
8
9-----------
10 Locations
11-----------
12
13Here are the places where MAC addresses might be stored:
14
15 - board-specific location (eeprom, dedicated flash, ...)
16 Note: only used when mandatory due to hardware design etc...
17
18 - environment ("ethaddr", "eth1addr", ...) (see CONFIG_ETHADDR)
19 Note: this is the preferred way to permanently store MAC addresses
20
21 - ethernet data (struct eth_device -> enetaddr)
22 Note: these are temporary copies of the MAC address which exist only
23 after the respective init steps have run and only to make usage
24 in other places easier (to avoid constant env lookup/parsing)
25
26 - struct bd_info and/or device tree
27 Note: these are temporary copies of the MAC address only for the
28 purpose of passing this information to an OS kernel we are about
29 to boot
30
Heiko Schocher8e64d6e2010-03-31 08:34:51 +020031Correct flow of setting up the MAC address (summarized):
32
331. Read from hardware in initialize() function
342. Read from environment in net/eth.c after initialize()
Rob Herring9eab6932011-12-06 11:15:04 -0600353. Write value to environment if setup in struct eth_device->enetaddr by driver
36 initialize() function. Give priority to the value in the environment if a
37 conflict.
Ben Warrenecee9322010-04-26 11:11:46 -0700384. Program the address into hardware if the following conditions are met:
39 a) The relevant driver has a 'write_addr' function
40 b) The user hasn't set an 'ethmacskip' environment variable
41 c) The address is valid (unicast, not all-zeros)
Heiko Schocher8e64d6e2010-03-31 08:34:51 +020042
Ben Warrenecee9322010-04-26 11:11:46 -070043Previous behavior had the MAC address always being programmed into hardware
44in the device's init() function.
Heiko Schocher8e64d6e2010-03-31 08:34:51 +020045
Mike Frysinger6ff41372009-02-11 14:12:34 -050046-------
47 Usage
48-------
49
50If the hardware design mandates that the MAC address is stored in some special
51place (like EEPROM etc...), then the board specific init code (such as the
52board-specific misc_init_r() function) is responsible for locating the MAC
53address(es) and initializing the respective environment variable(s) from it.
54Note that this shall be done if, and only if, the environment does not already
55contain these environment variables, i.e. existing variable definitions must
56not be overwritten.
57
58During runtime, the ethernet layer will use the environment variables to sync
59the MAC addresses to the ethernet structures. All ethernet driver code should
60then only use the enetaddr member of the eth_device structure. This is done
61on every network command, so the ethernet copies will stay in sync.
62
63Any other code that wishes to access the MAC address should query the
64environment directly. The helper functions documented below should make
65working with this storage much smoother.
66
67---------
68 Helpers
69---------
70
71To assist in the management of these layers, a few helper functions exist. You
72should use these rather than attempt to do any kind of parsing/manipulation
73yourself as many common errors have arisen in the past.
74
75 * void eth_parse_enetaddr(const char *addr, uchar *enetaddr);
76
77Convert a string representation of a MAC address to the binary version.
78char *addr = "00:11:22:33:44:55";
79uchar enetaddr[6];
80eth_parse_enetaddr(addr, enetaddr);
81/* enetaddr now equals { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 } */
82
83 * int eth_getenv_enetaddr(char *name, uchar *enetaddr);
84
85Look up an environment variable and convert the stored address. If the address
86is valid, then the function returns 1. Otherwise, the function returns 0. In
87all cases, the enetaddr memory is initialized. If the env var is not found,
88then it is set to all zeros. The common function is_valid_ether_addr() is used
89to determine address validity.
90uchar enetaddr[6];
91if (!eth_getenv_enetaddr("ethaddr", enetaddr)) {
92 /* "ethaddr" is not set in the environment */
93 ... try and setup "ethaddr" in the env ...
94}
95/* enetaddr is now set to the value stored in the ethaddr env var */
96
97 * int eth_setenv_enetaddr(char *name, const uchar *enetaddr);
98
99Store the MAC address into the named environment variable. The return value is
100the same as the setenv() function.
101uchar enetaddr[6] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
102eth_setenv_enetaddr("ethaddr", enetaddr);
103/* the "ethaddr" env var should now be set to "00:11:22:33:44:55" */
104
105 * the %pM format modifier
106
107The %pM format modifier can be used with any standard printf function to format
108the binary 6 byte array representation of a MAC address.
109uchar enetaddr[6] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
110printf("The MAC is %pM\n", enetaddr);
111
112char buf[20];
113sprintf(buf, "%pM", enetaddr);
114/* the buf variable is now set to "00:11:22:33:44:55" */