From 1dc603d3055df7285a5bf606caa1368cb2286e79 Mon Sep 17 00:00:00 2001 From: Chanho Park Date: Fri, 19 Feb 2016 17:25:56 +0900 Subject: [PATCH] common: introduce gen_eth_addr command This patch adds gen_eth_addr command to generate a random ethernet mac address and set the address into ethaddr env. Change-Id: I5823d9ddc380569ee0f3853996f5d1cea641f880 Signed-off-by: Chanho Park --- common/cmd_net.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/common/cmd_net.c b/common/cmd_net.c index a9ade8b92..87e602c41 100644 --- a/common/cmd_net.c +++ b/common/cmd_net.c @@ -459,3 +459,41 @@ U_BOOT_CMD( ); #endif /* CONFIG_CMD_LINK_LOCAL */ + +#if defined(CONFIG_CMD_GEN_ETHADDR) +int do_gen_eth_addr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + unsigned long ethaddr_low, ethaddr_high; + char tmp[18]; + + if (argc != 1) + return -1; + + srand(get_timer(0)); + + /* + * setting the 2nd LSB in the most significant byte of + * the address makes it a locally administered ethernet + * address + */ + ethaddr_high = (rand() & 0xfeff) | 0x0200; + ethaddr_low = rand(); + + sprintf(tmp, "%02lx:%02lx:%02lx:%02lx:%02lx:%02lx", + ethaddr_high >> 8, ethaddr_high & 0xff, + ethaddr_low >> 24, (ethaddr_low >> 16) & 0xff, + (ethaddr_low >> 8) & 0xff, ethaddr_low & 0xff); + + setenv("ethaddr", tmp); + + printf("%s\n", tmp); + + return 0; +} + +U_BOOT_CMD( + gen_eth_addr, 1, 1, do_gen_eth_addr, + "Generate a random ethernet mac address", + "" +); +#endif -- 2.34.1