1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Stefan Roese <sr@denx.de>
8 #include <env_internal.h>
12 #include <spi_flash.h>
14 #include <linux/ctype.h>
17 #define MT76XX_AGPIO_CFG 0x1000003c
19 #define FACTORY_DATA_OFFS 0xc0000
20 #define FACTORY_DATA_SECT_SIZE 0x10000
21 #if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
22 #error "U-Boot image with environment too big (overlapping with factory-data)!"
24 #define FACTORY_DATA_USER_OFFS 0x140
25 #define FACTORY_DATA_SIZE 0x1f0
26 #define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
27 FACTORY_DATA_USER_OFFS - sizeof(u32))
29 #define FACTORY_DATA_MAGIC 0xCAFEBABE
31 struct factory_data_values {
33 u8 wifi_mac[6]; /* offs: 0x004: binary value */
35 u8 eth_mac[6]; /* offs: 0x028: binary value */
36 u8 pad_3[FACTORY_DATA_USER_OFFS - 4 - 6 - 30 - 6];
37 /* User values start here at offset 0x140 */
41 char ipr_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
42 char hqv_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
43 char unielec_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
46 int board_early_init_f(void)
48 void __iomem *gpio_mode;
50 /* Configure digital vs analog GPIOs */
51 gpio_mode = ioremap_nocache(MT76XX_AGPIO_CFG, 0x100);
52 iowrite32(0x00fe01ff, gpio_mode);
57 static bool prepare_uuid_var(const char *fd_ptr, const char *env_var_name,
60 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
61 bool env_updated = false;
65 memcpy(str, fd_ptr, UUID_STR_LEN);
67 /* Convert non-ascii character to 'X' */
68 for (i = 0; i < UUID_STR_LEN; i++) {
69 if (!(isascii(str[i]) && isprint(str[i])))
73 env = env_get(env_var_name);
74 if (strcmp(env, str)) {
75 env_set(env_var_name, str);
82 static void factory_data_env_config(void)
84 struct factory_data_values *fd;
87 char str[UUID_STR_LEN + 1]; /* Enough for UUID stuff */
94 buf = malloc(FACTORY_DATA_SIZE);
96 printf("F-Data:Unable to allocate buffer\n");
101 * Get values from factory-data area in SPI NOR
103 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
104 CONFIG_SF_DEFAULT_CS,
105 CONFIG_SF_DEFAULT_SPEED,
106 CONFIG_SF_DEFAULT_MODE);
108 printf("F-Data:Unable to access SPI NOR flash\n");
112 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SIZE,
115 printf("F-Data:Unable to read factory-data from SPI NOR\n");
119 fd = (struct factory_data_values *)buf;
121 if (fd->magic != FACTORY_DATA_MAGIC)
122 printf("F-Data:Magic value not correct\n");
124 crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
126 printf("F-Data:CRC not correct\n");
128 printf("F-Data:factory-data version %x detected\n",
131 /* Handle wifi_mac env variable */
133 sprintf(str, "%pM", ptr);
134 if (!is_valid_ethaddr(ptr))
135 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str);
137 env = env_get("wifiaddr");
138 if (strcmp(env, str)) {
139 env_set("wifiaddr", str);
143 /* Handle eth_mac env variable */
145 sprintf(str, "%pM", ptr);
146 if (!is_valid_ethaddr(ptr))
147 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str);
149 env = env_get("ethaddr");
150 if (strcmp(env, str)) {
151 env_set("ethaddr", str);
155 /* Handle UUID env variables */
156 env_updated |= prepare_uuid_var(fd->ipr_id, "linuxmoduleid", 'X');
157 env_updated |= prepare_uuid_var(fd->hqv_id, "linuxmodulehqvid", '\0');
158 env_updated |= prepare_uuid_var(fd->unielec_id,
159 "linuxmoduleunielecid", '\0');
161 /* Check if the environment was updated and needs to get stored */
162 if (env_updated != 0) {
163 printf("F-Data:Values don't match env values -> saving\n");
166 debug("F-Data:Values match current env values\n");
176 int board_late_init(void)
178 if (IS_ENABLED(CONFIG_LED))
181 factory_data_env_config();
186 static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
188 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
191 /* Don't use the UUID dest place, as the \0 char won't fit */
192 env = env_get(env_var_name);
194 strncpy(str, env, UUID_STR_LEN);
196 gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
198 memcpy(fd_ptr, str, UUID_STR_LEN);
202 * Helper function to provide some sane factory-data values for testing
203 * purpose, when these values are not programmed correctly
205 int do_fd_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
207 struct factory_data_values *fd;
208 struct spi_flash *sf;
210 int ret = CMD_RET_FAILURE;
212 buf = malloc(FACTORY_DATA_SECT_SIZE);
214 printf("F-Data:Unable to allocate buffer\n");
215 return CMD_RET_FAILURE;
218 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
219 CONFIG_SF_DEFAULT_CS,
220 CONFIG_SF_DEFAULT_SPEED,
221 CONFIG_SF_DEFAULT_MODE);
223 printf("F-Data:Unable to access SPI NOR flash\n");
227 /* Generate the factory-data struct */
229 /* Fist read complete sector into buffer */
230 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
233 printf("F-Data:spi_flash_read failed (%d)\n", ret);
237 fd = (struct factory_data_values *)buf;
238 fd->magic = FACTORY_DATA_MAGIC;
241 /* Use existing MAC and UUID values or generate some random ones */
242 if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
243 net_random_ethaddr(fd->wifi_mac);
244 /* to get a different seed value for the MAC address */
248 if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
249 net_random_ethaddr(fd->eth_mac);
251 copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
252 copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
253 copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
255 printf("New factory-data values:\n");
256 printf("wifiaddr=%pM\n", fd->wifi_mac);
257 printf("ethaddr=%pM\n", fd->eth_mac);
260 * We don't have the \0 char at the end, so we need to specify the
261 * length in the printf format instead
263 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
264 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
266 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
269 fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
271 ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
273 printf("F-Data:spi_flash_erase failed (%d)\n", ret);
277 ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
280 printf("F-Data:spi_flash_write failed (%d)\n", ret);
284 printf("F-Data:factory-data values written to SPI NOR flash\n");
296 fd_write, 1, 0, do_fd_write,
297 "Write test factory-data values to SPI NOR",