blob: b6c0fe960eacc85c68aa65a7545469358493b712 [file] [log] [blame]
Stefan Roese115802d2018-08-16 15:27:31 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
4 */
5
Simon Glassed38aef2020-05-10 11:40:03 -06006#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -06008#include <env_internal.h>
Simon Glassa7b51302019-11-14 12:57:46 -07009#include <init.h>
Stefan Roesed0fdd672018-10-09 08:59:13 +020010#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Stefan Roese9c550d62018-11-30 07:46:30 +010013#include <net.h>
14#include <spi.h>
15#include <spi_flash.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Simon Glassfb64e362020-05-10 11:40:09 -060017#include <linux/stringify.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010019#include <u-boot/uuid.h>
Stefan Roese9c550d62018-11-30 07:46:30 +010020#include <linux/ctype.h>
Stefan Roese51cb80c2018-10-09 08:59:11 +020021#include <linux/io.h>
22
23#define MT76XX_AGPIO_CFG 0x1000003c
Stefan Roese115802d2018-08-16 15:27:31 +020024
Stefan Roese9c550d62018-11-30 07:46:30 +010025#define FACTORY_DATA_OFFS 0xc0000
26#define FACTORY_DATA_SECT_SIZE 0x10000
Tom Rinib6c9e0d2019-11-18 20:02:06 -050027#if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
Stefan Roese9c550d62018-11-30 07:46:30 +010028#error "U-Boot image with environment too big (overlapping with factory-data)!"
29#endif
30#define FACTORY_DATA_USER_OFFS 0x140
31#define FACTORY_DATA_SIZE 0x1f0
32#define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
33 FACTORY_DATA_USER_OFFS - sizeof(u32))
34
35#define FACTORY_DATA_MAGIC 0xCAFEBABE
36
37struct factory_data_values {
38 u8 pad_1[4];
39 u8 wifi_mac[6]; /* offs: 0x004: binary value */
40 u8 pad_2[30];
41 u8 eth_mac[6]; /* offs: 0x028: binary value */
42 u8 pad_3[FACTORY_DATA_USER_OFFS - 4 - 6 - 30 - 6];
43 /* User values start here at offset 0x140 */
44 u32 crc;
45 u32 magic;
46 u32 version;
47 char ipr_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
48 char hqv_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
49 char unielec_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
50};
51
Stefan Roese115802d2018-08-16 15:27:31 +020052int board_early_init_f(void)
53{
Stefan Roese51cb80c2018-10-09 08:59:11 +020054 void __iomem *gpio_mode;
55
56 /* Configure digital vs analog GPIOs */
57 gpio_mode = ioremap_nocache(MT76XX_AGPIO_CFG, 0x100);
58 iowrite32(0x00fe01ff, gpio_mode);
59
Stefan Roese115802d2018-08-16 15:27:31 +020060 return 0;
61}
Stefan Roesed0fdd672018-10-09 08:59:13 +020062
Stefan Roese9c550d62018-11-30 07:46:30 +010063static bool prepare_uuid_var(const char *fd_ptr, const char *env_var_name,
64 char errorchar)
65{
66 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
67 bool env_updated = false;
68 char *env;
69 int i;
70
71 memcpy(str, fd_ptr, UUID_STR_LEN);
72
73 /* Convert non-ascii character to 'X' */
74 for (i = 0; i < UUID_STR_LEN; i++) {
75 if (!(isascii(str[i]) && isprint(str[i])))
76 str[i] = errorchar;
77 }
78
79 env = env_get(env_var_name);
80 if (strcmp(env, str)) {
81 env_set(env_var_name, str);
82 env_updated = true;
83 }
84
85 return env_updated;
86}
87
88static void factory_data_env_config(void)
89{
90 struct factory_data_values *fd;
91 struct spi_flash *sf;
92 int env_updated = 0;
93 char str[UUID_STR_LEN + 1]; /* Enough for UUID stuff */
94 char *env;
95 u8 *buf;
96 u32 crc;
97 int ret;
98 u8 *ptr;
99
100 buf = malloc(FACTORY_DATA_SIZE);
101 if (!buf) {
102 printf("F-Data:Unable to allocate buffer\n");
103 return;
104 }
105
106 /*
107 * Get values from factory-data area in SPI NOR
108 */
109 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
110 CONFIG_SF_DEFAULT_CS,
111 CONFIG_SF_DEFAULT_SPEED,
112 CONFIG_SF_DEFAULT_MODE);
113 if (!sf) {
114 printf("F-Data:Unable to access SPI NOR flash\n");
115 goto err_free;
116 }
117
118 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SIZE,
119 (void *)buf);
120 if (ret) {
121 printf("F-Data:Unable to read factory-data from SPI NOR\n");
122 goto err_spi_flash;
123 }
124
125 fd = (struct factory_data_values *)buf;
126
127 if (fd->magic != FACTORY_DATA_MAGIC)
128 printf("F-Data:Magic value not correct\n");
129
130 crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
131 if (crc != fd->crc)
132 printf("F-Data:CRC not correct\n");
133 else
134 printf("F-Data:factory-data version %x detected\n",
135 fd->version);
136
137 /* Handle wifi_mac env variable */
138 ptr = fd->wifi_mac;
139 sprintf(str, "%pM", ptr);
140 if (!is_valid_ethaddr(ptr))
141 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str);
142
143 env = env_get("wifiaddr");
144 if (strcmp(env, str)) {
145 env_set("wifiaddr", str);
146 env_updated = 1;
147 }
148
149 /* Handle eth_mac env variable */
150 ptr = fd->eth_mac;
151 sprintf(str, "%pM", ptr);
152 if (!is_valid_ethaddr(ptr))
153 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str);
154
155 env = env_get("ethaddr");
156 if (strcmp(env, str)) {
157 env_set("ethaddr", str);
158 env_updated = 1;
159 }
160
161 /* Handle UUID env variables */
162 env_updated |= prepare_uuid_var(fd->ipr_id, "linuxmoduleid", 'X');
163 env_updated |= prepare_uuid_var(fd->hqv_id, "linuxmodulehqvid", '\0');
164 env_updated |= prepare_uuid_var(fd->unielec_id,
165 "linuxmoduleunielecid", '\0');
166
167 /* Check if the environment was updated and needs to get stored */
168 if (env_updated != 0) {
169 printf("F-Data:Values don't match env values -> saving\n");
170 env_save();
171 } else {
172 debug("F-Data:Values match current env values\n");
173 }
174
175err_spi_flash:
176 spi_flash_free(sf);
177
178err_free:
179 free(buf);
180}
181
Stefan Roesed0fdd672018-10-09 08:59:13 +0200182int board_late_init(void)
183{
Stefan Roese9c550d62018-11-30 07:46:30 +0100184 factory_data_env_config();
185
Stefan Roesed0fdd672018-10-09 08:59:13 +0200186 return 0;
187}
Stefan Roese9c550d62018-11-30 07:46:30 +0100188
189static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
190{
191 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
192 char *env;
193
194 /* Don't use the UUID dest place, as the \0 char won't fit */
195 env = env_get(env_var_name);
196 if (env)
197 strncpy(str, env, UUID_STR_LEN);
198 else
199 gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
200
201 memcpy(fd_ptr, str, UUID_STR_LEN);
202}
203
204/*
205 * Helper function to provide some sane factory-data values for testing
206 * purpose, when these values are not programmed correctly
207 */
Simon Glassed38aef2020-05-10 11:40:03 -0600208int do_fd_write(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Stefan Roese9c550d62018-11-30 07:46:30 +0100209{
210 struct factory_data_values *fd;
211 struct spi_flash *sf;
212 u8 *buf;
213 int ret = CMD_RET_FAILURE;
214
215 buf = malloc(FACTORY_DATA_SECT_SIZE);
216 if (!buf) {
217 printf("F-Data:Unable to allocate buffer\n");
218 return CMD_RET_FAILURE;
219 }
220
221 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
222 CONFIG_SF_DEFAULT_CS,
223 CONFIG_SF_DEFAULT_SPEED,
224 CONFIG_SF_DEFAULT_MODE);
225 if (!sf) {
226 printf("F-Data:Unable to access SPI NOR flash\n");
227 goto err_free;
228 }
229
230 /* Generate the factory-data struct */
231
232 /* Fist read complete sector into buffer */
233 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
234 (void *)buf);
235 if (ret) {
236 printf("F-Data:spi_flash_read failed (%d)\n", ret);
237 goto err_spi_flash;
238 }
239
240 fd = (struct factory_data_values *)buf;
241 fd->magic = FACTORY_DATA_MAGIC;
242 fd->version = 0x1;
243
244 /* Use existing MAC and UUID values or generate some random ones */
245 if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
246 net_random_ethaddr(fd->wifi_mac);
247 /* to get a different seed value for the MAC address */
248 mdelay(10);
249 }
250
251 if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
252 net_random_ethaddr(fd->eth_mac);
253
254 copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
255 copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
256 copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
257
258 printf("New factory-data values:\n");
259 printf("wifiaddr=%pM\n", fd->wifi_mac);
260 printf("ethaddr=%pM\n", fd->eth_mac);
261
262 /*
263 * We don't have the \0 char at the end, so we need to specify the
264 * length in the printf format instead
265 */
266 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
267 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
268 fd->hqv_id);
269 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
270 fd->unielec_id);
271
272 fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
273
274 ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
275 if (ret) {
276 printf("F-Data:spi_flash_erase failed (%d)\n", ret);
277 goto err_spi_flash;
278 }
279
280 ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
281 buf);
282 if (ret) {
283 printf("F-Data:spi_flash_write failed (%d)\n", ret);
284 goto err_spi_flash;
285 }
286
287 printf("F-Data:factory-data values written to SPI NOR flash\n");
288
289err_spi_flash:
290 spi_flash_free(sf);
291
292err_free:
293 free(buf);
294
295 return ret;
296}
297
Simon Glass49c24a82024-09-29 19:49:47 -0600298#ifndef CONFIG_XPL_BUILD
Stefan Roese9c550d62018-11-30 07:46:30 +0100299U_BOOT_CMD(
300 fd_write, 1, 0, do_fd_write,
301 "Write test factory-data values to SPI NOR",
302 "\n"
303);
developer9720bc32020-04-21 09:28:48 +0200304#endif