Derive MAC Address from board unique_id

This commit is contained in:
Adam Comley 2024-03-17 14:05:18 -05:00 committed by Xiang Xiao
parent b311c46778
commit c15a6970c9
2 changed files with 18 additions and 2 deletions

View File

@ -474,6 +474,14 @@ choice
address to the hardware before it brings the network up. This address to the hardware before it brings the network up. This
choice allows you select the source of that MAC address. choice allows you select the source of that MAC address.
config NETINIT_UIDMAC
bool "Board Unique ID"
depends on BOARDCTL_UNIQUEID
---help---
Assign a MAC address based on the board unique ID.
CONFIG_BOARDCTL_UNIQUEID_SIZE must be at least 6 bytes for
Ethernet, 8 bytes for 6LOWPAN.
config NETINIT_SWMAC config NETINIT_SWMAC
bool "Fixed address" bool "Fixed address"
---help--- ---help---

View File

@ -48,6 +48,7 @@
#include <unistd.h> #include <unistd.h>
#include <nuttx/net/mii.h> #include <nuttx/net/mii.h>
#include <sys/boardctl.h>
#include "netutils/netlib.h" #include "netutils/netlib.h"
#if defined(CONFIG_NETUTILS_DHCPC) || defined(CONFIG_NETINIT_DNS) #if defined(CONFIG_NETUTILS_DHCPC) || defined(CONFIG_NETINIT_DNS)
@ -289,7 +290,9 @@ static const uint16_t g_ipv6_netmask[8] =
defined(HAVE_MAC) defined(HAVE_MAC)
static void netinit_set_macaddr(void) static void netinit_set_macaddr(void)
{ {
#if defined(CONFIG_NET_ETHERNET) #if defined(CONFIG_NETINIT_UIDMAC)
uint8_t uid[CONFIG_BOARDCTL_UNIQUEID_SIZE];
#elif defined(CONFIG_NET_ETHERNET)
uint8_t mac[IFHWADDRLEN]; uint8_t mac[IFHWADDRLEN];
#elif defined(HAVE_EADDR) #elif defined(HAVE_EADDR)
uint8_t eaddr[8]; uint8_t eaddr[8];
@ -297,7 +300,12 @@ static void netinit_set_macaddr(void)
/* Many embedded network interfaces must have a software assigned MAC */ /* Many embedded network interfaces must have a software assigned MAC */
#if defined(CONFIG_NET_ETHERNET) #if defined(CONFIG_NETINIT_UIDMAC)
boardctl(BOARDIOC_UNIQUEID, (uintptr_t)&uid);
uid[0] = (uid[0] & 0b11110000) | 2; /* Locally Administered MAC */
netlib_setmacaddr(NET_DEVNAME, uid);
#elif defined(CONFIG_NET_ETHERNET)
/* Use the configured, fixed MAC address */ /* Use the configured, fixed MAC address */
mac[0] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 1)) & 0xff; mac[0] = (CONFIG_NETINIT_MACADDR_2 >> (8 * 1)) & 0xff;