aboutsummaryrefslogtreecommitdiff
path: root/board/Arcturus/ucp1020/cmd_arc.c
blob: 1c42fee3d6a1b899cacb18be478af58fd2c98fb6 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
/*
 * Command for accessing Arcturus factory environment.
 *
 * Copyright 2013-2015 Arcturus Networks Inc.
 *           http://www.arcturusnetworks.com/products/ucp1020/
 *           by Oleksandr G Zhadan et al.
 *
 */

#include <common.h>
#include <div64.h>
#include <malloc.h>
#include <spi_flash.h>

#include <asm/io.h>

#ifndef CONFIG_SF_DEFAULT_SPEED
#   define CONFIG_SF_DEFAULT_SPEED	1000000
#endif
#ifndef CONFIG_SF_DEFAULT_MODE
#   define CONFIG_SF_DEFAULT_MODE	SPI_MODE0
#endif
#ifndef CONFIG_SF_DEFAULT_CS
#   define CONFIG_SF_DEFAULT_CS		0
#endif
#ifndef CONFIG_SF_DEFAULT_BUS
#   define CONFIG_SF_DEFAULT_BUS	0
#endif

#define MAX_SERIAL_SIZE 15
#define MAX_HWADDR_SIZE 17

#define FIRM_ADDR1 (0x200 - sizeof(smac))
#define FIRM_ADDR2 (0x400 - sizeof(smac))
#define FIRM_ADDR3 (CONFIG_ENV_SECT_SIZE + 0x200 - sizeof(smac))
#define FIRM_ADDR4 (CONFIG_ENV_SECT_SIZE + 0x400 - sizeof(smac))

static struct spi_flash *flash;
char smac[4][18];

static int ishwaddr(char *hwaddr)
{
	if (strlen(hwaddr) == MAX_HWADDR_SIZE)
		if (hwaddr[2] == ':' &&
		    hwaddr[5] == ':' &&
		    hwaddr[8] == ':' &&
		    hwaddr[11] == ':' &&
		    hwaddr[14] == ':')
			return 0;
	return -1;
}

static int set_arc_product(int argc, char *const argv[])
{
	int err = 0;
	char *mystrerr = "ERROR: Failed to save factory info in spi location";

	if (argc != 5)
		return -1;

	/* Check serial number */
	if (strlen(argv[1]) != MAX_SERIAL_SIZE)
		return -1;

	/* Check HWaddrs */
	if (ishwaddr(argv[2]) || ishwaddr(argv[3]) || ishwaddr(argv[4]))
		return -1;

	strcpy(smac[3], argv[1]);
	strcpy(smac[2], argv[2]);
	strcpy(smac[1], argv[3]);
	strcpy(smac[0], argv[4]);

	flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
				CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);

	/*
	 * Save factory defaults
	 */

	if (spi_flash_write(flash, FIRM_ADDR1, sizeof(smac), smac)) {
		printf("%s: %s [1]\n", __func__, mystrerr);
		err++;
	}
	if (spi_flash_write(flash, FIRM_ADDR2, sizeof(smac), smac)) {
		printf("%s: %s [2]\n", __func__, mystrerr);
		err++;
	}

	if (spi_flash_write(flash, FIRM_ADDR3, sizeof(smac), smac)) {
		printf("%s: %s [3]\n", __func__, mystrerr);
		err++;
	}

	if (spi_flash_write(flash, FIRM_ADDR4, sizeof(smac), smac)) {
		printf("%s: %s [4]\n", __func__, mystrerr);
		err++;
	}

	if (err == 4) {
		printf("%s: %s [ALL]\n", __func__, mystrerr);
		return -2;
	}

	return 0;
}

int get_arc_info(void)
{
	int location = 1;
	char *myerr = "ERROR: Failed to read all 4 factory info spi locations";

	flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
				CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);

	if (spi_flash_read(flash, FIRM_ADDR1, sizeof(smac), smac)) {
		location++;
		if (spi_flash_read(flash, FIRM_ADDR2, sizeof(smac), smac)) {
			location++;
			if (spi_flash_read(flash, FIRM_ADDR3, sizeof(smac),
					   smac)) {
				location++;
				if (spi_flash_read(flash, FIRM_ADDR4,
						   sizeof(smac), smac)) {
					printf("%s: %s\n", __func__, myerr);
					return -2;
				}
			}
		}
	}
	if (smac[3][0] != 0) {
		if (location > 1)
			printf("Using region %d\n", location);
		printf("SERIAL: ");
		if (smac[3][0] == 0xFF) {
			printf("\t<not found>\n");
		} else {
			printf("\t%s\n", smac[3]);
			env_set("SERIAL", smac[3]);
		}
	}

	if (strcmp(smac[2], "00:00:00:00:00:00") == 0)
		return 0;

	printf("HWADDR0:");
	if (smac[2][0] == 0xFF) {
		printf("\t<not found>\n");
	} else {
		char *ret = env_get("ethaddr");

		if (strcmp(ret, __stringify(CONFIG_ETHADDR)) == 0) {
			env_set("ethaddr", smac[2]);
			printf("\t%s (factory)\n", smac[2]);
		} else {
			printf("\t%s\n", ret);
		}
	}

	if (strcmp(smac[1], "00:00:00:00:00:00") == 0) {
		env_set("eth1addr", smac[2]);
		env_set("eth2addr", smac[2]);
		return 0;
	}

	printf("HWADDR1:");
	if (smac[1][0] == 0xFF) {
		printf("\t<not found>\n");
	} else {
		char *ret = env_get("eth1addr");

		if (strcmp(ret, __stringify(CONFIG_ETH1ADDR)) == 0) {
			env_set("eth1addr", smac[1]);
			printf("\t%s (factory)\n", smac[1]);
		} else {
			printf("\t%s\n", ret);
		}
	}

	if (strcmp(smac[0], "00:00:00:00:00:00") == 0) {
		env_set("eth2addr", smac[1]);
		return 0;
	}

	printf("HWADDR2:");
	if (smac[0][0] == 0xFF) {
		printf("\t<not found>\n");
	} else {
		char *ret = env_get("eth2addr");

		if (strcmp(ret, __stringify(CONFIG_ETH2ADDR)) == 0) {
			env_set("eth2addr", smac[0]);
			printf("\t%s (factory)\n", smac[0]);
		} else {
			printf("\t%s\n", ret);
		}
	}

	return 0;
}

static int do_arc_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	const char *cmd;
	int ret = -1;

	cmd = argv[1];
	--argc;
	++argv;

	if (strcmp(cmd, "product") == 0) {
		ret = set_arc_product(argc, argv);
		goto done;
	}
	if (strcmp(cmd, "info") == 0) {
		ret = get_arc_info();
		goto done;
	}
done:
	if (ret == -1)
		return CMD_RET_USAGE;

	return ret;
}

U_BOOT_CMD(arc, 6, 1, do_arc_cmd,
	   "Arcturus product command sub-system",
	   "product serial hwaddr0 hwaddr1 hwaddr2    - save Arcturus factory env\n"
	   "info                                      - show Arcturus factory env\n\n");