mirror of
https://github.com/AsahiLinux/u-boot
synced 2024-11-16 17:58:23 +00:00
83d290c56f
When U-Boot started using SPDX tags we were among the early adopters and there weren't a lot of other examples to borrow from. So we picked the area of the file that usually had a full license text and replaced it with an appropriate SPDX-License-Identifier: entry. Since then, the Linux Kernel has adopted SPDX tags and they place it as the very first line in a file (except where shebangs are used, then it's second line) and with slightly different comment styles than us. In part due to community overlap, in part due to better tag visibility and in part for other minor reasons, switch over to that style. This commit changes all instances where we have a single declared license in the tag as both the before and after are identical in tag contents. There's also a few places where I found we did not have a tag and have introduced one. Signed-off-by: Tom Rini <trini@konsulko.com>
75 lines
1.7 KiB
C
75 lines
1.7 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* (C) Copyright 2016
|
|
* Olliver Schinagl <oliver@schinagl.nl>
|
|
*/
|
|
|
|
#include <ctype.h>
|
|
#include <stdbool.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;
|
|
}
|