summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2017-02-09 16:56:35 (GMT)
committerTom Rini <trini@konsulko.com>2017-02-09 16:56:35 (GMT)
commite1a71f8b339220fa74c9cd5d36ae9c444c492e83 (patch)
tree5eca7ff69f390f4c2f2df3459d3e2db3d91b7af9 /tools
parent6f57b19857b514f6e94e423c9bbe2d4b8973a0db (diff)
parenta5fd13ad1913d9c66c47666dbedac7703a48e502 (diff)
downloadu-boot-e1a71f8b339220fa74c9cd5d36ae9c444c492e83.tar.xz
Merge branch 'master' of git://git.denx.de/u-boot-net
Diffstat (limited to 'tools')
-rw-r--r--tools/.gitignore1
-rw-r--r--tools/Makefile5
-rw-r--r--tools/gen_ethaddr_crc.c75
3 files changed, 81 insertions, 0 deletions
diff --git a/tools/.gitignore b/tools/.gitignore
index cb1e722..0d1f076 100644
--- a/tools/.gitignore
+++ b/tools/.gitignore
@@ -6,6 +6,7 @@
/fit_check_sign
/fit_info
/gen_eth_addr
+/gen_ethaddr_crc
/ifdtool
/img2srec
/kwboot
diff --git a/tools/Makefile b/tools/Makefile
index cbccd4a..5000f4d 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -42,6 +42,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_ethaddr_crc
+gen_ethaddr_crc-objs := gen_ethaddr_crc.o lib/crc8.o
+HOSTCFLAGS_gen_ethaddr_crc.o := -pedantic
+
hostprogs-$(CONFIG_CMD_LOADS) += img2srec
HOSTCFLAGS_img2srec.o := -pedantic
@@ -195,6 +199,7 @@ fdtgrep-objs += $(LIBFDT_OBJS) fdtgrep.o
# that won't build on some weird host compiler -- though there are lots of
# exceptions for files that aren't complaint.
HOSTCFLAGS_crc32.o := -pedantic
+HOSTCFLAGS_crc8.o := -pedantic
HOSTCFLAGS_md5.o := -pedantic
HOSTCFLAGS_sha1.o := -pedantic
HOSTCFLAGS_sha256.o := -pedantic
diff --git a/tools/gen_ethaddr_crc.c b/tools/gen_ethaddr_crc.c
new file mode 100644
index 0000000..fe9896d
--- /dev/null
+++ b/tools/gen_ethaddr_crc.c
@@ -0,0 +1,75 @@
+/*
+ * (C) Copyright 2016
+ * Olliver Schinagl <oliver@schinagl.nl>
+ *
+ * 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>
+
+#define ARP_HLEN 6 /* Length of hardware address */
+#define ARP_HLEN_ASCII (ARP_HLEN * 2) + (ARP_HLEN - 1) /* with separators */
+#define ARP_HLEN_LAZY (ARP_HLEN * 2) /* separatorless hardware address length */
+
+uint8_t nibble_to_hex(const char *nibble, bool lo)
+{
+ return (strtol(nibble, NULL, 16) << (lo ? 0 : 4)) & (lo ? 0x0f : 0xf0);
+}
+
+int process_mac(const char *mac_address)
+{
+ uint8_t ethaddr[ARP_HLEN + 1] = { 0x00 };
+ uint_fast8_t i = 0;
+
+ while (*mac_address != '\0') {
+ char nibble[2] = { 0x00, '\n' }; /* for strtol */
+
+ nibble[0] = *mac_address++;
+ if (isxdigit(nibble[0])) {
+ if (isupper(nibble[0]))
+ nibble[0] = tolower(nibble[0]);
+ ethaddr[i >> 1] |= nibble_to_hex(nibble, (i % 2) != 0);
+ i++;
+ }
+ }
+
+ for (i = 0; i < ARP_HLEN; i++)
+ printf("%.2x", ethaddr[i]);
+ printf("%.2x\n", crc8(0, ethaddr, ARP_HLEN));
+
+ return 0;
+}
+
+void print_usage(char *cmdname)
+{
+ printf("Usage: %s <mac_address>\n", cmdname);
+ puts("<mac_address> may be with or without separators.");
+ puts("Valid seperators are ':' and '-'.");
+ puts("<mac_address> digits are in base 16.\n");
+}
+
+int main(int argc, char *argv[])
+{
+ if (argc < 2) {
+ print_usage(argv[0]);
+ return 1;
+ }
+
+ if (!((strlen(argv[1]) == ARP_HLEN_ASCII) || (strlen(argv[1]) == ARP_HLEN_LAZY))) {
+ puts("The MAC address is not valid.\n");
+ print_usage(argv[0]);
+ return 1;
+ }
+
+ if (process_mac(argv[1])) {
+ puts("Failed to calculate the MAC's checksum.");
+ return 1;
+ }
+
+ return 0;
+}