blob: 5f3c27ef0c2260427ecdf94e93d7ea4ca65237b4 [file] [log] [blame]
Daniel Schultz03e619d2024-05-21 23:18:25 -07001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2024 PHYTEC Messtechnik GmbH
4 * Author: Daniel Schultz <d.schultz@phytec.de>
5 */
6
7#include <malloc.h>
8#include <u-boot/crc.h>
9#include <net.h>
10#include <vsprintf.h>
11
12#include "phytec_som_detection_blocks.h"
13
14#if IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION_BLOCKS)
15
16struct phytec_api3_element *
17 phytec_blocks_init_mac(struct phytec_api3_block_header *header,
18 uint8_t *payload)
19{
20 struct phytec_api3_element *element;
21 struct phytec_api3_block_mac *mac;
22 unsigned int crc;
23 unsigned int len = sizeof(struct phytec_api3_block_mac);
24
25 if (!header)
26 return NULL;
27 if (!payload)
28 return NULL;
29
30 element = (struct phytec_api3_element *)
31 calloc(8, PHYTEC_API3_ELEMENT_HEADER_SIZE + len);
32 if (!element) {
33 pr_err("%s: Unable to allocate memory\n", __func__);
34 return NULL;
35 }
36 element->block_type = header->block_type;
37 memcpy(&element->block.mac, payload, len);
38 mac = &element->block.mac;
39
40 debug("%s: interface: %i\n", __func__, mac->interface);
41 debug("%s: MAC %pM\n", __func__, mac->address);
42
43 crc = crc8(0, (const unsigned char *)mac, len);
44 debug("%s: crc: %x\n", __func__, crc);
45 if (crc) {
46 pr_err("%s: CRC mismatch. API3 block payload is unusable\n",
47 __func__);
48 return NULL;
49 }
50
51 return element;
52}
53
54int __maybe_unused
55 phytec_blocks_add_mac_to_env(struct phytec_api3_element *element)
56{
57 char enetenv[9] = "ethaddr";
58 char buf[ARP_HLEN_ASCII + 1];
59 struct phytec_api3_block_mac *block = &element->block.mac;
60 int ret;
61
62 if (!is_valid_ethaddr(block->address)) {
63 pr_err("%s: Invalid MAC address in block.\n", __func__);
64 return -1;
65 }
66
67 if (block->interface > 0) {
68 ret = sprintf(enetenv, "eth%iaddr", block->interface);
69 if (ret != 8) {
70 pr_err("%s: Unable to create env string\n", __func__);
71 return -1;
72 }
73 }
74
75 ret = sprintf(buf, "%pM", block->address);
76 if (ret != ARP_HLEN_ASCII) {
77 pr_err("%s: Unable to convert MAC address\n", __func__);
78 return -1;
79 }
80 ret = env_set(enetenv, buf);
81 if (ret) {
82 pr_err("%s: Failed to set MAC address to env.\n", __func__);
83 return -1;
84 }
85
86 debug("%s: Added %s to %s\n", __func__, buf, enetenv);
87 return 0;
88}
89
90#else
91
92inline struct phytec_api3_element *
93 phytec_api3_init_mac_block(struct phytec_api3_block_header *header,
94 uint8_t *payload)
95{
96 return NULL;
97}
98
99inline int __maybe_unused
100 phytec_blocks_add_mac_to_env(struct phytec_api3_element *element)
101{
102 return -1;
103}
104
105#endif