summaryrefslogtreecommitdiff
path: root/board/phytec/common/phytec_som_detection_blocks.c
blob: b44ff85972f9d702ab739990cd692a131fd2f1f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2024 PHYTEC Messtechnik GmbH
 * Author: Daniel Schultz <d.schultz@phytec.de>
 */

#include <env.h>
#include <malloc.h>
#include <u-boot/crc.h>
#include <net.h>
#include <vsprintf.h>

#include "phytec_som_detection_blocks.h"

#if IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION_BLOCKS)

struct phytec_api3_element *
	phytec_blocks_init_mac(struct phytec_api3_block_header *header,
			       uint8_t *payload)
{
	struct phytec_api3_element *element;
	struct phytec_api3_block_mac *mac;
	unsigned int crc;
	unsigned int len = sizeof(struct phytec_api3_block_mac);

	if (!header)
		return NULL;
	if (!payload)
		return NULL;

	element = (struct phytec_api3_element *)
			calloc(8, PHYTEC_API3_ELEMENT_HEADER_SIZE + len);
	if (!element) {
		pr_err("%s: Unable to allocate memory\n", __func__);
		return NULL;
	}
	element->block_type = header->block_type;
	memcpy(&element->block.mac, payload, len);
	mac = &element->block.mac;

	debug("%s: interface: %i\n", __func__, mac->interface);
	debug("%s: MAC %pM\n", __func__, mac->address);

	crc = crc8(0, (const unsigned char *)mac, len);
	debug("%s: crc: %x\n", __func__, crc);
	if (crc) {
		pr_err("%s: CRC mismatch. API3 block payload is unusable\n",
		       __func__);
		return NULL;
	}

	return element;
}

int __maybe_unused
	phytec_blocks_add_mac_to_env(struct phytec_api3_element *element)
{
	char enetenv[9] = "ethaddr";
	char buf[ARP_HLEN_ASCII + 1];
	struct phytec_api3_block_mac *block = &element->block.mac;
	int ret;

	if (!is_valid_ethaddr(block->address)) {
		pr_err("%s: Invalid MAC address in block.\n", __func__);
		return -1;
	}

	if (block->interface > 0) {
		ret = sprintf(enetenv, "eth%iaddr", block->interface);
		if (ret != 8) {
			pr_err("%s: Unable to create env string\n", __func__);
			return -1;
		}
	}

	ret = sprintf(buf, "%pM", block->address);
	if (ret != ARP_HLEN_ASCII) {
		pr_err("%s: Unable to convert MAC address\n", __func__);
		return -1;
	}
	ret = env_set(enetenv, buf);
	if (ret) {
		pr_err("%s: Failed to set MAC address to env.\n", __func__);
		return -1;
	}

	debug("%s: Added %s to %s\n", __func__, buf, enetenv);
	return 0;
}

#else

inline struct phytec_api3_element *
	phytec_api3_init_mac_block(struct phytec_api3_block_header *header,
				   uint8_t *payload)
{
	return NULL;
}

inline int __maybe_unused
	phytec_blocks_add_mac_to_env(struct phytec_api3_element *element)
{
	return -1;
}

#endif