3 * Olliver Schinagl <oliver@schinagl.nl>
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <u-boot/crc.h>
16 #define ARP_HLEN 6 /* Length of hardware address */
17 #define ARP_HLEN_ASCII (ARP_HLEN * 2) + (ARP_HLEN - 1) /* with separators */
18 #define ARP_HLEN_LAZY (ARP_HLEN * 2) /* separatorless hardware address length */
20 uint8_t nibble_to_hex(const char *nibble, bool lo)
22 return (strtol(nibble, NULL, 16) << (lo ? 0 : 4)) & (lo ? 0x0f : 0xf0);
25 int process_mac(const char *mac_address)
27 uint8_t ethaddr[ARP_HLEN + 1] = { 0x00 };
30 while (*mac_address != '\0') {
31 char nibble[2] = { 0x00, '\n' }; /* for strtol */
33 nibble[0] = *mac_address++;
34 if (isxdigit(nibble[0])) {
35 if (isupper(nibble[0]))
36 nibble[0] = tolower(nibble[0]);
37 ethaddr[i >> 1] |= nibble_to_hex(nibble, (i % 2) != 0);
42 for (i = 0; i < ARP_HLEN; i++)
43 printf("%.2x", ethaddr[i]);
44 printf("%.2x\n", crc8(0, ethaddr, ARP_HLEN));
49 void print_usage(char *cmdname)
51 printf("Usage: %s <mac_address>\n", cmdname);
52 puts("<mac_address> may be with or without separators.");
53 puts("Valid seperators are ':' and '-'.");
54 puts("<mac_address> digits are in base 16.\n");
57 int main(int argc, char *argv[])
64 if (!((strlen(argv[1]) == ARP_HLEN_ASCII) || (strlen(argv[1]) == ARP_HLEN_LAZY))) {
65 puts("The MAC address is not valid.\n");
70 if (process_mac(argv[1])) {
71 puts("Failed to calculate the MAC's checksum.");