summaryrefslogtreecommitdiff
path: root/board/Arcturus/ucp1020/cmd_arc.c
blob: 2e8477ed3b7a01a205aa460fc32aeee155291bb2 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
/*
 * Command for accessing Arcturus factory environment.
 *
 * Copyright 2013-2019 Arcturus Networks Inc.
 *           https://www.arcturusnetworks.com/products/
 *           by Oleksandr G Zhadan et al.
 *
 */

#include <common.h>
#include <div64.h>
#include <env.h>
#include <malloc.h>
#include <spi_flash.h>
#include <mmc.h>
#include <version.h>
#include <asm/io.h>

static ulong fwenv_addr[MAX_FWENV_ADDR];
const char mystrerr[] = "ERROR: Failed to save factory info";

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;
}

#if (FWENV_TYPE == FWENV_MMC)

static char smac[29][18] __attribute__ ((aligned(0x200)));	/* 1 MMC block is 512 bytes */

int set_mmc_arc_product(int argc, char *const argv[])
{
	struct mmc *mmc;
	u32 blk, cnt, n;
	int i, err = 1;
	void *addr;
	const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;

	mmc = find_mmc_device(mmc_dev_num);
	if (!mmc) {
		printf("No SD/MMC/eMMC card found\n");
		return 0;
	}
	if (mmc_init(mmc)) {
		printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
		       mmc_dev_num);
		return 0;
	}
	if (mmc_getwp(mmc) == 1) {
		printf("Error: card is write protected!\n");
		return CMD_RET_FAILURE;
	}

	/* Save factory defaults */
	addr = (void *)smac;
	cnt = 1;		/* One 512 bytes block */

	for (i = 0; i < MAX_FWENV_ADDR; i++)
		if (fwenv_addr[i] != -1) {
			blk = fwenv_addr[i] / 512;
			n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
			if (n != cnt)
				printf("%s: %s [%d]\n", __func__, mystrerr, i);
			else
				err = 0;
		}
	if (err)
		return -2;

	return err;
}

static int read_mmc_arc_info(void)
{
	struct mmc *mmc;
	u32 blk, cnt, n;
	int i;
	void *addr;
	const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV;

	mmc = find_mmc_device(mmc_dev_num);
	if (!mmc) {
		printf("No SD/MMC/eMMC card found\n");
		return 0;
	}
	if (mmc_init(mmc)) {
		printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC",
		       mmc_dev_num);
		return 0;
	}

	addr = (void *)smac;
	cnt = 1;		/* One 512 bytes block */

	for (i = 0; i < MAX_FWENV_ADDR; i++)
		if (fwenv_addr[i] != -1) {
			blk = fwenv_addr[i] / 512;
			n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
			flush_cache((ulong) addr, 512);
			if (n == cnt)
				return (i + 1);
		}
	return 0;
}
#endif

#if (FWENV_TYPE == FWENV_SPI_FLASH)

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

int set_spi_arc_product(int argc, char *const argv[])
{
	int i, err = 1;

	flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
				CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
	if (!flash) {
		printf("Failed to initialize SPI flash at %u:%u\n",
		       CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS);
		return -1;
	}

	/* Save factory defaults */
	for (i = 0; i < MAX_FWENV_ADDR; i++)
		if (fwenv_addr[i] != -1)
			if (spi_flash_write
			    (flash, fwenv_addr[i], sizeof(smac), smac))
				printf("%s: %s [%d]\n", __func__, mystrerr, i);
			else
				err = 0;
	if (err)
		return -2;

	return err;
}

static int read_spi_arc_info(void)
{
	int i;

	flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
				CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
	if (!flash) {
		printf("Failed to initialize SPI flash at %u:%u\n",
		       CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS);
		return 0;
	}
	for (i = 0; i < MAX_FWENV_ADDR; i++)
		if (fwenv_addr[i] != -1)
			if (!spi_flash_read
			    (flash, fwenv_addr[i], sizeof(smac), smac))
				return (i + 1);
	return 0;
}
#endif

#if (FWENV_TYPE == FWENV_NOR_FLASH)

static char smac[4][18];

int set_nor_arc_product(int argc, char *const argv[])
{
	int i, err = 1;

	/* Save factory defaults */
	for (i = 0; i < MAX_FWENV_ADDR; i++)
		if (fwenv_addr[i] != -1) {
			ulong fwenv_end = fwenv_addr[i] + 4;

			flash_sect_roundb(&fwenv_end);
			flash_sect_protect(0, fwenv_addr[i], fwenv_end);
			if (flash_write
			    ((char *)smac, fwenv_addr[i], sizeof(smac)))
				printf("%s: %s [%d]\n", __func__, mystrerr, i);
			else
				err = 0;
			flash_sect_protect(1, fwenv_addr[i], fwenv_end);
		}
	if (err)
		return -2;

	return err;
}

static int read_nor_arc_info(void)
{
	int i;

	for (i = 0; i < MAX_FWENV_ADDR; i++)
		if (fwenv_addr[i] != -1) {
			memcpy(smac, (void *)fwenv_addr[i], sizeof(smac));
			return (i + 1);
		}

	return 0;
}
#endif

int set_arc_product(int argc, char *const argv[])
{
	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[0], argv[1]);
	strcpy(smac[1], argv[2]);
	strcpy(smac[2], argv[3]);
	strcpy(smac[3], argv[4]);

#if (FWENV_TYPE == FWENV_NOR_FLASH)
	return set_nor_arc_product(argc, argv);
#endif
#if (FWENV_TYPE == FWENV_SPI_FLASH)
	return set_spi_arc_product(argc, argv);
#endif
#if (FWENV_TYPE == FWENV_MMC)
	return set_mmc_arc_product(argc, argv);
#endif
	return -2;
}

static int read_arc_info(void)
{
#if (FWENV_TYPE == FWENV_NOR_FLASH)
	return read_nor_arc_info();
#endif
#if (FWENV_TYPE == FWENV_SPI_FLASH)
	return read_spi_arc_info();
#endif
#if (FWENV_TYPE == FWENV_MMC)
	return read_mmc_arc_info();
#endif
	return 0;
}

static int do_get_arc_info(void)
{
	int l = read_arc_info();
	char *oldserial = env_get("SERIAL");
	char *oldversion = env_get("VERSION");

	if (oldversion != NULL)
		if (strcmp(oldversion, U_BOOT_VERSION) != 0)
			oldversion = NULL;

	if (l == 0) {
		printf("%s: failed to read factory info\n", __func__);
		return -2;
	}

	printf("\rSERIAL:  ");
	if (smac[0][0] == EMPY_CHAR) {
		printf("<not found>\n");
	} else {
		printf("%s\n", smac[0]);
		env_set("SERIAL", smac[0]);
	}

	if (strcmp(smac[1], "00:00:00:00:00:00") == 0) {
		env_set("ethaddr", NULL);
		env_set("eth1addr", NULL);
		env_set("eth2addr", NULL);
		goto done;
	}

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

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

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

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

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

	if (strcmp(smac[3], "00:00:00:00:00:00") == 0) {
		env_set("eth2addr", NULL);
		goto done;
	}

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

		if (ret == NULL) {
			env_set("ethaddr", smac[3]);
			printf("%s\n", smac[3]);
		} else if (strcmp(ret, __stringify(CONFIG_ETH2ADDR)) == 0) {
			env_set("eth2addr", smac[3]);
			printf("%s (factory)\n", smac[3]);
		} else {
			printf("%s\n", ret);
		}
	}
done:
	if (oldserial == NULL || oldversion == NULL) {
		if (oldversion == NULL)
			env_set("VERSION", U_BOOT_VERSION);
		env_save();
	}

	return 0;
}

static int init_fwenv(void)
{
	int i, ret = -1;

	fwenv_addr[0] = FWENV_ADDR1;
	fwenv_addr[1] = FWENV_ADDR2;
	fwenv_addr[2] = FWENV_ADDR3;
	fwenv_addr[3] = FWENV_ADDR4;

	for (i = 0; i < MAX_FWENV_ADDR; i++)
		if (fwenv_addr[i] != -1)
			ret = 0;
	if (ret)
		printf("%s: No firmfare info storage address is defined\n",
		       __func__);
	return ret;
}

void get_arc_info(void)
{
	if (!init_fwenv())
		do_get_arc_info();
}

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 (init_fwenv())
		return ret;

	if (strcmp(cmd, "product") == 0)
		ret = set_arc_product(argc, argv);
	else if (strcmp(cmd, "info") == 0)
		ret = do_get_arc_info();

	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");