[U-Boot] [PATCH v2 5/5] tools: Add tool to add crc8 to a mac address

Olliver Schinagl o.schinagl at ultimaker.com
Mon Dec 14 13:41:12 CET 2015


This patch adds a little tool that takes a generic MAC address and
generates a CRC byte for it. The output is the full MAC address without
any separators, ready written into an EEPROM.

Signed-off-by: Olliver Schinagl <o.schinagl at ultimaker.com>
---
 tools/Makefile       |  4 ++++
 tools/gen_mac_addr.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 tools/gen_mac_addr.c

diff --git a/tools/Makefile b/tools/Makefile
index 4a50744..6191c26 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -43,6 +43,10 @@ envcrc-objs := envcrc.o lib/crc32.o common/env_embedded.o lib/sha1.o
 hostprogs-$(CONFIG_CMD_NET) += gen_eth_addr
 HOSTCFLAGS_gen_eth_addr.o := -pedantic
 
+hostprogs-$(CONFIG_CMD_NET) += gen_mac_addr
+gen_mac_addr-objs := gen_mac_addr.o lib/crc8.o
+HOSTCFLAGS_gen_mac_addr.o := -pedantic
+
 hostprogs-$(CONFIG_CMD_LOADS) += img2srec
 HOSTCFLAGS_img2srec.o := -pedantic
 
diff --git a/tools/gen_mac_addr.c b/tools/gen_mac_addr.c
new file mode 100644
index 0000000..bd8688f
--- /dev/null
+++ b/tools/gen_mac_addr.c
@@ -0,0 +1,51 @@
+/*
+ * (C) Copyright 2016
+ * Olliver Schinagl <o.schinagl at ultimaker.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <ctype.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <u-boot/crc.h>
+
+
+int main(int argc, char *argv[])
+{
+	uint_fast8_t i;
+	uint8_t mac_addr[7] = { 0x00 };
+
+	if (argc < 2) {
+		puts("Please supply a MAC address.");
+		return -1;
+	}
+
+	if (!((strlen(argv[1]) == 12) || (strlen(argv[1]) == 17))) {
+		puts("Please supply a valid MAC address with optionaly\n"
+		     "dashes (-) or colons (:) as seperators.");
+		return -1;
+	}
+
+	i = 0;
+	while (*argv[1] != '\0') {
+		char nibble[2] = { 0x00, '\n' }; /* for strtol */
+
+		nibble[0] = *argv[1]++;
+		if (isxdigit(nibble[0])) {
+			if (isupper(nibble[0]))
+				nibble[0] = tolower(nibble[0]);
+			mac_addr[i >> 1] |= strtol(nibble, NULL, 16) << ((i % 2) ? 0 : 4) & ((i % 2) ? 0x0f : 0xf0);
+			i++;
+		}
+	}
+	mac_addr[6] = crc8(mac_addr, 6);
+
+	for (i = 0; i < 7; i++)
+		printf("%.2x", mac_addr[i]);
+	putchar('\n');
+
+	return 0;
+}
-- 
2.6.2



More information about the U-Boot mailing list