blob: 6fd771a2e2044da8a16736aad631df02d244b9d4 [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
6#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -06009#include <env_internal.h>
Simon Glass8e201882020-05-10 11:39:54 -060010#include <flash.h>
Simon Glassa7b51302019-11-14 12:57:46 -070011#include <init.h>
Stefan Roesed0fdd672018-10-09 08:59:13 +020012#include <led.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Stefan Roese9c550d62018-11-30 07:46:30 +010015#include <net.h>
16#include <spi.h>
17#include <spi_flash.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Stefan Roese9c550d62018-11-30 07:46:30 +010019#include <uuid.h>
20#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{
184 if (IS_ENABLED(CONFIG_LED))
185 led_default_state();
186
Stefan Roese9c550d62018-11-30 07:46:30 +0100187 factory_data_env_config();
188
Stefan Roesed0fdd672018-10-09 08:59:13 +0200189 return 0;
190}
Stefan Roese9c550d62018-11-30 07:46:30 +0100191
192static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
193{
194 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
195 char *env;
196
197 /* Don't use the UUID dest place, as the \0 char won't fit */
198 env = env_get(env_var_name);
199 if (env)
200 strncpy(str, env, UUID_STR_LEN);
201 else
202 gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
203
204 memcpy(fd_ptr, str, UUID_STR_LEN);
205}
206
207/*
208 * Helper function to provide some sane factory-data values for testing
209 * purpose, when these values are not programmed correctly
210 */
Simon Glassed38aef2020-05-10 11:40:03 -0600211int do_fd_write(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Stefan Roese9c550d62018-11-30 07:46:30 +0100212{
213 struct factory_data_values *fd;
214 struct spi_flash *sf;
215 u8 *buf;
216 int ret = CMD_RET_FAILURE;
217
218 buf = malloc(FACTORY_DATA_SECT_SIZE);
219 if (!buf) {
220 printf("F-Data:Unable to allocate buffer\n");
221 return CMD_RET_FAILURE;
222 }
223
224 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
225 CONFIG_SF_DEFAULT_CS,
226 CONFIG_SF_DEFAULT_SPEED,
227 CONFIG_SF_DEFAULT_MODE);
228 if (!sf) {
229 printf("F-Data:Unable to access SPI NOR flash\n");
230 goto err_free;
231 }
232
233 /* Generate the factory-data struct */
234
235 /* Fist read complete sector into buffer */
236 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
237 (void *)buf);
238 if (ret) {
239 printf("F-Data:spi_flash_read failed (%d)\n", ret);
240 goto err_spi_flash;
241 }
242
243 fd = (struct factory_data_values *)buf;
244 fd->magic = FACTORY_DATA_MAGIC;
245 fd->version = 0x1;
246
247 /* Use existing MAC and UUID values or generate some random ones */
248 if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
249 net_random_ethaddr(fd->wifi_mac);
250 /* to get a different seed value for the MAC address */
251 mdelay(10);
252 }
253
254 if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
255 net_random_ethaddr(fd->eth_mac);
256
257 copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
258 copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
259 copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
260
261 printf("New factory-data values:\n");
262 printf("wifiaddr=%pM\n", fd->wifi_mac);
263 printf("ethaddr=%pM\n", fd->eth_mac);
264
265 /*
266 * We don't have the \0 char at the end, so we need to specify the
267 * length in the printf format instead
268 */
269 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
270 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
271 fd->hqv_id);
272 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
273 fd->unielec_id);
274
275 fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
276
277 ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
278 if (ret) {
279 printf("F-Data:spi_flash_erase failed (%d)\n", ret);
280 goto err_spi_flash;
281 }
282
283 ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
284 buf);
285 if (ret) {
286 printf("F-Data:spi_flash_write failed (%d)\n", ret);
287 goto err_spi_flash;
288 }
289
290 printf("F-Data:factory-data values written to SPI NOR flash\n");
291
292err_spi_flash:
293 spi_flash_free(sf);
294
295err_free:
296 free(buf);
297
298 return ret;
299}
300
developer9720bc32020-04-21 09:28:48 +0200301#ifndef CONFIG_SPL_BUILD
Stefan Roese9c550d62018-11-30 07:46:30 +0100302U_BOOT_CMD(
303 fd_write, 1, 0, do_fd_write,
304 "Write test factory-data values to SPI NOR",
305 "\n"
306);
developer9720bc32020-04-21 09:28:48 +0200307#endif