blob: 8a3a6e3482564b7b1722cf5e81f2c54f19283e7b [file] [log] [blame]
Stefan Roese88dc4092018-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 Glass09140112020-05-10 11:40:03 -06007#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06008#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -06009#include <env_internal.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060010#include <flash.h>
Simon Glass52559322019-11-14 12:57:46 -070011#include <init.h>
Stefan Roeseb1f51fc2018-10-09 08:59:13 +020012#include <led.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Stefan Roese7b3af032018-11-30 07:46:30 +010015#include <net.h>
16#include <spi.h>
17#include <spi_flash.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glass1af3c7f2020-05-10 11:40:09 -060019#include <linux/stringify.h>
Simon Glass3db71102019-11-14 12:57:16 -070020#include <u-boot/crc.h>
Stefan Roese7b3af032018-11-30 07:46:30 +010021#include <uuid.h>
22#include <linux/ctype.h>
Stefan Roese48f8e152018-10-09 08:59:11 +020023#include <linux/io.h>
24
25#define MT76XX_AGPIO_CFG 0x1000003c
Stefan Roese88dc4092018-08-16 15:27:31 +020026
Stefan Roese7b3af032018-11-30 07:46:30 +010027#define FACTORY_DATA_OFFS 0xc0000
28#define FACTORY_DATA_SECT_SIZE 0x10000
Tom Rini3eee45d2019-11-18 20:02:06 -050029#if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
Stefan Roese7b3af032018-11-30 07:46:30 +010030#error "U-Boot image with environment too big (overlapping with factory-data)!"
31#endif
32#define FACTORY_DATA_USER_OFFS 0x140
33#define FACTORY_DATA_SIZE 0x1f0
34#define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
35 FACTORY_DATA_USER_OFFS - sizeof(u32))
36
37#define FACTORY_DATA_MAGIC 0xCAFEBABE
38
39struct factory_data_values {
40 u8 pad_1[4];
41 u8 wifi_mac[6]; /* offs: 0x004: binary value */
42 u8 pad_2[30];
43 u8 eth_mac[6]; /* offs: 0x028: binary value */
44 u8 pad_3[FACTORY_DATA_USER_OFFS - 4 - 6 - 30 - 6];
45 /* User values start here at offset 0x140 */
46 u32 crc;
47 u32 magic;
48 u32 version;
49 char ipr_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
50 char hqv_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
51 char unielec_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
52};
53
Stefan Roese88dc4092018-08-16 15:27:31 +020054int board_early_init_f(void)
55{
Stefan Roese48f8e152018-10-09 08:59:11 +020056 void __iomem *gpio_mode;
57
58 /* Configure digital vs analog GPIOs */
59 gpio_mode = ioremap_nocache(MT76XX_AGPIO_CFG, 0x100);
60 iowrite32(0x00fe01ff, gpio_mode);
61
Stefan Roese88dc4092018-08-16 15:27:31 +020062 return 0;
63}
Stefan Roeseb1f51fc2018-10-09 08:59:13 +020064
Stefan Roese7b3af032018-11-30 07:46:30 +010065static bool prepare_uuid_var(const char *fd_ptr, const char *env_var_name,
66 char errorchar)
67{
68 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
69 bool env_updated = false;
70 char *env;
71 int i;
72
73 memcpy(str, fd_ptr, UUID_STR_LEN);
74
75 /* Convert non-ascii character to 'X' */
76 for (i = 0; i < UUID_STR_LEN; i++) {
77 if (!(isascii(str[i]) && isprint(str[i])))
78 str[i] = errorchar;
79 }
80
81 env = env_get(env_var_name);
82 if (strcmp(env, str)) {
83 env_set(env_var_name, str);
84 env_updated = true;
85 }
86
87 return env_updated;
88}
89
90static void factory_data_env_config(void)
91{
92 struct factory_data_values *fd;
93 struct spi_flash *sf;
94 int env_updated = 0;
95 char str[UUID_STR_LEN + 1]; /* Enough for UUID stuff */
96 char *env;
97 u8 *buf;
98 u32 crc;
99 int ret;
100 u8 *ptr;
101
102 buf = malloc(FACTORY_DATA_SIZE);
103 if (!buf) {
104 printf("F-Data:Unable to allocate buffer\n");
105 return;
106 }
107
108 /*
109 * Get values from factory-data area in SPI NOR
110 */
111 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
112 CONFIG_SF_DEFAULT_CS,
113 CONFIG_SF_DEFAULT_SPEED,
114 CONFIG_SF_DEFAULT_MODE);
115 if (!sf) {
116 printf("F-Data:Unable to access SPI NOR flash\n");
117 goto err_free;
118 }
119
120 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SIZE,
121 (void *)buf);
122 if (ret) {
123 printf("F-Data:Unable to read factory-data from SPI NOR\n");
124 goto err_spi_flash;
125 }
126
127 fd = (struct factory_data_values *)buf;
128
129 if (fd->magic != FACTORY_DATA_MAGIC)
130 printf("F-Data:Magic value not correct\n");
131
132 crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
133 if (crc != fd->crc)
134 printf("F-Data:CRC not correct\n");
135 else
136 printf("F-Data:factory-data version %x detected\n",
137 fd->version);
138
139 /* Handle wifi_mac env variable */
140 ptr = fd->wifi_mac;
141 sprintf(str, "%pM", ptr);
142 if (!is_valid_ethaddr(ptr))
143 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str);
144
145 env = env_get("wifiaddr");
146 if (strcmp(env, str)) {
147 env_set("wifiaddr", str);
148 env_updated = 1;
149 }
150
151 /* Handle eth_mac env variable */
152 ptr = fd->eth_mac;
153 sprintf(str, "%pM", ptr);
154 if (!is_valid_ethaddr(ptr))
155 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str);
156
157 env = env_get("ethaddr");
158 if (strcmp(env, str)) {
159 env_set("ethaddr", str);
160 env_updated = 1;
161 }
162
163 /* Handle UUID env variables */
164 env_updated |= prepare_uuid_var(fd->ipr_id, "linuxmoduleid", 'X');
165 env_updated |= prepare_uuid_var(fd->hqv_id, "linuxmodulehqvid", '\0');
166 env_updated |= prepare_uuid_var(fd->unielec_id,
167 "linuxmoduleunielecid", '\0');
168
169 /* Check if the environment was updated and needs to get stored */
170 if (env_updated != 0) {
171 printf("F-Data:Values don't match env values -> saving\n");
172 env_save();
173 } else {
174 debug("F-Data:Values match current env values\n");
175 }
176
177err_spi_flash:
178 spi_flash_free(sf);
179
180err_free:
181 free(buf);
182}
183
Stefan Roeseb1f51fc2018-10-09 08:59:13 +0200184int board_late_init(void)
185{
186 if (IS_ENABLED(CONFIG_LED))
187 led_default_state();
188
Stefan Roese7b3af032018-11-30 07:46:30 +0100189 factory_data_env_config();
190
Stefan Roeseb1f51fc2018-10-09 08:59:13 +0200191 return 0;
192}
Stefan Roese7b3af032018-11-30 07:46:30 +0100193
194static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
195{
196 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
197 char *env;
198
199 /* Don't use the UUID dest place, as the \0 char won't fit */
200 env = env_get(env_var_name);
201 if (env)
202 strncpy(str, env, UUID_STR_LEN);
203 else
204 gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
205
206 memcpy(fd_ptr, str, UUID_STR_LEN);
207}
208
209/*
210 * Helper function to provide some sane factory-data values for testing
211 * purpose, when these values are not programmed correctly
212 */
Simon Glass09140112020-05-10 11:40:03 -0600213int do_fd_write(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Stefan Roese7b3af032018-11-30 07:46:30 +0100214{
215 struct factory_data_values *fd;
216 struct spi_flash *sf;
217 u8 *buf;
218 int ret = CMD_RET_FAILURE;
219
220 buf = malloc(FACTORY_DATA_SECT_SIZE);
221 if (!buf) {
222 printf("F-Data:Unable to allocate buffer\n");
223 return CMD_RET_FAILURE;
224 }
225
226 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
227 CONFIG_SF_DEFAULT_CS,
228 CONFIG_SF_DEFAULT_SPEED,
229 CONFIG_SF_DEFAULT_MODE);
230 if (!sf) {
231 printf("F-Data:Unable to access SPI NOR flash\n");
232 goto err_free;
233 }
234
235 /* Generate the factory-data struct */
236
237 /* Fist read complete sector into buffer */
238 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
239 (void *)buf);
240 if (ret) {
241 printf("F-Data:spi_flash_read failed (%d)\n", ret);
242 goto err_spi_flash;
243 }
244
245 fd = (struct factory_data_values *)buf;
246 fd->magic = FACTORY_DATA_MAGIC;
247 fd->version = 0x1;
248
249 /* Use existing MAC and UUID values or generate some random ones */
250 if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
251 net_random_ethaddr(fd->wifi_mac);
252 /* to get a different seed value for the MAC address */
253 mdelay(10);
254 }
255
256 if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
257 net_random_ethaddr(fd->eth_mac);
258
259 copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
260 copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
261 copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
262
263 printf("New factory-data values:\n");
264 printf("wifiaddr=%pM\n", fd->wifi_mac);
265 printf("ethaddr=%pM\n", fd->eth_mac);
266
267 /*
268 * We don't have the \0 char at the end, so we need to specify the
269 * length in the printf format instead
270 */
271 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
272 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
273 fd->hqv_id);
274 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
275 fd->unielec_id);
276
277 fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
278
279 ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
280 if (ret) {
281 printf("F-Data:spi_flash_erase failed (%d)\n", ret);
282 goto err_spi_flash;
283 }
284
285 ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
286 buf);
287 if (ret) {
288 printf("F-Data:spi_flash_write failed (%d)\n", ret);
289 goto err_spi_flash;
290 }
291
292 printf("F-Data:factory-data values written to SPI NOR flash\n");
293
294err_spi_flash:
295 spi_flash_free(sf);
296
297err_free:
298 free(buf);
299
300 return ret;
301}
302
Weijie Gao757cbbe2020-04-21 09:28:48 +0200303#ifndef CONFIG_SPL_BUILD
Stefan Roese7b3af032018-11-30 07:46:30 +0100304U_BOOT_CMD(
305 fd_write, 1, 0, do_fd_write,
306 "Write test factory-data values to SPI NOR",
307 "\n"
308);
Weijie Gao757cbbe2020-04-21 09:28:48 +0200309#endif