README.enetaddr 4.8 KB

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