2 * Command for accessing Arcturus factory environment.
4 * Copyright 2013-2015 Arcturus Networks Inc.
5 * http://www.arcturusnetworks.com/products/ucp1020/
6 * by Oleksandr G Zhadan et al.
8 * SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
15 #include <spi_flash.h>
19 #ifndef CONFIG_SF_DEFAULT_SPEED
20 # define CONFIG_SF_DEFAULT_SPEED 1000000
22 #ifndef CONFIG_SF_DEFAULT_MODE
23 # define CONFIG_SF_DEFAULT_MODE SPI_MODE0
25 #ifndef CONFIG_SF_DEFAULT_CS
26 # define CONFIG_SF_DEFAULT_CS 0
28 #ifndef CONFIG_SF_DEFAULT_BUS
29 # define CONFIG_SF_DEFAULT_BUS 0
32 #define MAX_SERIAL_SIZE 15
33 #define MAX_HWADDR_SIZE 17
35 #define FIRM_ADDR1 (0x200 - sizeof(smac))
36 #define FIRM_ADDR2 (0x400 - sizeof(smac))
37 #define FIRM_ADDR3 (CONFIG_ENV_SECT_SIZE + 0x200 - sizeof(smac))
38 #define FIRM_ADDR4 (CONFIG_ENV_SECT_SIZE + 0x400 - sizeof(smac))
40 static struct spi_flash *flash;
43 static int ishwaddr(char *hwaddr)
45 if (strlen(hwaddr) == MAX_HWADDR_SIZE)
46 if (hwaddr[2] == ':' &&
55 static int set_arc_product(int argc, char *const argv[])
58 char *mystrerr = "ERROR: Failed to save factory info in spi location";
63 /* Check serial number */
64 if (strlen(argv[1]) != MAX_SERIAL_SIZE)
68 if (ishwaddr(argv[2]) || ishwaddr(argv[3]) || ishwaddr(argv[4]))
71 strcpy(smac[3], argv[1]);
72 strcpy(smac[2], argv[2]);
73 strcpy(smac[1], argv[3]);
74 strcpy(smac[0], argv[4]);
76 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
77 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
80 * Save factory defaults
83 if (spi_flash_write(flash, FIRM_ADDR1, sizeof(smac), smac)) {
84 printf("%s: %s [1]\n", __func__, mystrerr);
87 if (spi_flash_write(flash, FIRM_ADDR2, sizeof(smac), smac)) {
88 printf("%s: %s [2]\n", __func__, mystrerr);
92 if (spi_flash_write(flash, FIRM_ADDR3, sizeof(smac), smac)) {
93 printf("%s: %s [3]\n", __func__, mystrerr);
97 if (spi_flash_write(flash, FIRM_ADDR4, sizeof(smac), smac)) {
98 printf("%s: %s [4]\n", __func__, mystrerr);
103 printf("%s: %s [ALL]\n", __func__, mystrerr);
110 int get_arc_info(void)
113 char *myerr = "ERROR: Failed to read all 4 factory info spi locations";
115 flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
116 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
118 if (spi_flash_read(flash, FIRM_ADDR1, sizeof(smac), smac)) {
120 if (spi_flash_read(flash, FIRM_ADDR2, sizeof(smac), smac)) {
122 if (spi_flash_read(flash, FIRM_ADDR3, sizeof(smac),
125 if (spi_flash_read(flash, FIRM_ADDR4,
126 sizeof(smac), smac)) {
127 printf("%s: %s\n", __func__, myerr);
133 if (smac[3][0] != 0) {
135 printf("Using region %d\n", location);
137 if (smac[3][0] == 0xFF) {
138 printf("\t<not found>\n");
140 printf("\t%s\n", smac[3]);
141 setenv("SERIAL", smac[3]);
145 if (strcmp(smac[2], "00:00:00:00:00:00") == 0)
149 if (smac[2][0] == 0xFF) {
150 printf("\t<not found>\n");
152 char *ret = getenv("ethaddr");
154 if (strcmp(ret, __stringify(CONFIG_ETHADDR)) == 0) {
155 setenv("ethaddr", smac[2]);
156 printf("\t%s (factory)\n", smac[2]);
158 printf("\t%s\n", ret);
162 if (strcmp(smac[1], "00:00:00:00:00:00") == 0) {
163 setenv("eth1addr", smac[2]);
164 setenv("eth2addr", smac[2]);
169 if (smac[1][0] == 0xFF) {
170 printf("\t<not found>\n");
172 char *ret = getenv("eth1addr");
174 if (strcmp(ret, __stringify(CONFIG_ETH1ADDR)) == 0) {
175 setenv("eth1addr", smac[1]);
176 printf("\t%s (factory)\n", smac[1]);
178 printf("\t%s\n", ret);
182 if (strcmp(smac[0], "00:00:00:00:00:00") == 0) {
183 setenv("eth2addr", smac[1]);
188 if (smac[0][0] == 0xFF) {
189 printf("\t<not found>\n");
191 char *ret = getenv("eth2addr");
193 if (strcmp(ret, __stringify(CONFIG_ETH2ADDR)) == 0) {
194 setenv("eth2addr", smac[0]);
195 printf("\t%s (factory)\n", smac[0]);
197 printf("\t%s\n", ret);
204 static int do_arc_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
213 if (strcmp(cmd, "product") == 0) {
214 ret = set_arc_product(argc, argv);
217 if (strcmp(cmd, "info") == 0) {
218 ret = get_arc_info();
223 return CMD_RET_USAGE;
228 U_BOOT_CMD(arc, 6, 1, do_arc_cmd,
229 "Arcturus product command sub-system",
230 "product serial hwaddr0 hwaddr1 hwaddr2 - save Arcturus factory env\n"
231 "info - show Arcturus factory env\n\n");