1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
9 #include <env_internal.h>
16 #include <spi_flash.h>
17 #include <linux/delay.h>
18 #include <linux/stringify.h>
19 #include <u-boot/crc.h>
21 #include <linux/ctype.h>
24 #define MT76XX_AGPIO_CFG 0x1000003c
26 #define FACTORY_DATA_OFFS 0xc0000
27 #define FACTORY_DATA_SECT_SIZE 0x10000
28 #if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
29 #error "U-Boot image with environment too big (overlapping with factory-data)!"
31 #define FACTORY_DATA_USER_OFFS 0x140
32 #define FACTORY_DATA_SIZE 0x1f0
33 #define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
34 FACTORY_DATA_USER_OFFS - sizeof(u32))
36 #define FACTORY_DATA_MAGIC 0xCAFEBABE
38 struct factory_data_values {
40 u8 wifi_mac[6]; /* offs: 0x004: binary value */
42 u8 eth_mac[6]; /* offs: 0x028: binary value */
43 u8 pad_3[FACTORY_DATA_USER_OFFS - 4 - 6 - 30 - 6];
44 /* User values start here at offset 0x140 */
48 char ipr_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
49 char hqv_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
50 char unielec_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
53 int board_early_init_f(void)
55 void __iomem *gpio_mode;
57 /* Configure digital vs analog GPIOs */
58 gpio_mode = ioremap_nocache(MT76XX_AGPIO_CFG, 0x100);
59 iowrite32(0x00fe01ff, gpio_mode);
64 static bool prepare_uuid_var(const char *fd_ptr, const char *env_var_name,
67 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
68 bool env_updated = false;
72 memcpy(str, fd_ptr, UUID_STR_LEN);
74 /* Convert non-ascii character to 'X' */
75 for (i = 0; i < UUID_STR_LEN; i++) {
76 if (!(isascii(str[i]) && isprint(str[i])))
80 env = env_get(env_var_name);
81 if (strcmp(env, str)) {
82 env_set(env_var_name, str);
89 static void factory_data_env_config(void)
91 struct factory_data_values *fd;
94 char str[UUID_STR_LEN + 1]; /* Enough for UUID stuff */
101 buf = malloc(FACTORY_DATA_SIZE);
103 printf("F-Data:Unable to allocate buffer\n");
108 * Get values from factory-data area in SPI NOR
110 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
111 CONFIG_SF_DEFAULT_CS,
112 CONFIG_SF_DEFAULT_SPEED,
113 CONFIG_SF_DEFAULT_MODE);
115 printf("F-Data:Unable to access SPI NOR flash\n");
119 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SIZE,
122 printf("F-Data:Unable to read factory-data from SPI NOR\n");
126 fd = (struct factory_data_values *)buf;
128 if (fd->magic != FACTORY_DATA_MAGIC)
129 printf("F-Data:Magic value not correct\n");
131 crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
133 printf("F-Data:CRC not correct\n");
135 printf("F-Data:factory-data version %x detected\n",
138 /* Handle wifi_mac env variable */
140 sprintf(str, "%pM", ptr);
141 if (!is_valid_ethaddr(ptr))
142 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str);
144 env = env_get("wifiaddr");
145 if (strcmp(env, str)) {
146 env_set("wifiaddr", str);
150 /* Handle eth_mac env variable */
152 sprintf(str, "%pM", ptr);
153 if (!is_valid_ethaddr(ptr))
154 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str);
156 env = env_get("ethaddr");
157 if (strcmp(env, str)) {
158 env_set("ethaddr", str);
162 /* Handle UUID env variables */
163 env_updated |= prepare_uuid_var(fd->ipr_id, "linuxmoduleid", 'X');
164 env_updated |= prepare_uuid_var(fd->hqv_id, "linuxmodulehqvid", '\0');
165 env_updated |= prepare_uuid_var(fd->unielec_id,
166 "linuxmoduleunielecid", '\0');
168 /* Check if the environment was updated and needs to get stored */
169 if (env_updated != 0) {
170 printf("F-Data:Values don't match env values -> saving\n");
173 debug("F-Data:Values match current env values\n");
183 int board_late_init(void)
185 factory_data_env_config();
190 static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
192 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
195 /* Don't use the UUID dest place, as the \0 char won't fit */
196 env = env_get(env_var_name);
198 strncpy(str, env, UUID_STR_LEN);
200 gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
202 memcpy(fd_ptr, str, UUID_STR_LEN);
206 * Helper function to provide some sane factory-data values for testing
207 * purpose, when these values are not programmed correctly
209 int do_fd_write(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
211 struct factory_data_values *fd;
212 struct spi_flash *sf;
214 int ret = CMD_RET_FAILURE;
216 buf = malloc(FACTORY_DATA_SECT_SIZE);
218 printf("F-Data:Unable to allocate buffer\n");
219 return CMD_RET_FAILURE;
222 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
223 CONFIG_SF_DEFAULT_CS,
224 CONFIG_SF_DEFAULT_SPEED,
225 CONFIG_SF_DEFAULT_MODE);
227 printf("F-Data:Unable to access SPI NOR flash\n");
231 /* Generate the factory-data struct */
233 /* Fist read complete sector into buffer */
234 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
237 printf("F-Data:spi_flash_read failed (%d)\n", ret);
241 fd = (struct factory_data_values *)buf;
242 fd->magic = FACTORY_DATA_MAGIC;
245 /* Use existing MAC and UUID values or generate some random ones */
246 if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
247 net_random_ethaddr(fd->wifi_mac);
248 /* to get a different seed value for the MAC address */
252 if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
253 net_random_ethaddr(fd->eth_mac);
255 copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
256 copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
257 copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
259 printf("New factory-data values:\n");
260 printf("wifiaddr=%pM\n", fd->wifi_mac);
261 printf("ethaddr=%pM\n", fd->eth_mac);
264 * We don't have the \0 char at the end, so we need to specify the
265 * length in the printf format instead
267 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
268 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
270 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
273 fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
275 ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
277 printf("F-Data:spi_flash_erase failed (%d)\n", ret);
281 ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
284 printf("F-Data:spi_flash_write failed (%d)\n", ret);
288 printf("F-Data:factory-data values written to SPI NOR flash\n");
299 #ifndef CONFIG_SPL_BUILD
301 fd_write, 1, 0, do_fd_write,
302 "Write test factory-data values to SPI NOR",