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>
13 #include <u-boot/crc.h>
15 #include <linux/ctype.h>
18 #define MT76XX_AGPIO_CFG 0x1000003c
20 #define FACTORY_DATA_OFFS 0xc0000
21 #define FACTORY_DATA_SECT_SIZE 0x10000
22 #if ((CONFIG_ENV_OFFSET_REDUND + CONFIG_ENV_SIZE) > FACTORY_DATA_OFFS)
23 #error "U-Boot image with environment too big (overlapping with factory-data)!"
25 #define FACTORY_DATA_USER_OFFS 0x140
26 #define FACTORY_DATA_SIZE 0x1f0
27 #define FACTORY_DATA_CRC_LEN (FACTORY_DATA_SIZE - \
28 FACTORY_DATA_USER_OFFS - sizeof(u32))
30 #define FACTORY_DATA_MAGIC 0xCAFEBABE
32 struct factory_data_values {
34 u8 wifi_mac[6]; /* offs: 0x004: binary value */
36 u8 eth_mac[6]; /* offs: 0x028: binary value */
37 u8 pad_3[FACTORY_DATA_USER_OFFS - 4 - 6 - 30 - 6];
38 /* User values start here at offset 0x140 */
42 char ipr_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
43 char hqv_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
44 char unielec_id[UUID_STR_LEN]; /* UUID as string w/o ending \0 */
47 int board_early_init_f(void)
49 void __iomem *gpio_mode;
51 /* Configure digital vs analog GPIOs */
52 gpio_mode = ioremap_nocache(MT76XX_AGPIO_CFG, 0x100);
53 iowrite32(0x00fe01ff, gpio_mode);
58 static bool prepare_uuid_var(const char *fd_ptr, const char *env_var_name,
61 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
62 bool env_updated = false;
66 memcpy(str, fd_ptr, UUID_STR_LEN);
68 /* Convert non-ascii character to 'X' */
69 for (i = 0; i < UUID_STR_LEN; i++) {
70 if (!(isascii(str[i]) && isprint(str[i])))
74 env = env_get(env_var_name);
75 if (strcmp(env, str)) {
76 env_set(env_var_name, str);
83 static void factory_data_env_config(void)
85 struct factory_data_values *fd;
88 char str[UUID_STR_LEN + 1]; /* Enough for UUID stuff */
95 buf = malloc(FACTORY_DATA_SIZE);
97 printf("F-Data:Unable to allocate buffer\n");
102 * Get values from factory-data area in SPI NOR
104 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
105 CONFIG_SF_DEFAULT_CS,
106 CONFIG_SF_DEFAULT_SPEED,
107 CONFIG_SF_DEFAULT_MODE);
109 printf("F-Data:Unable to access SPI NOR flash\n");
113 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SIZE,
116 printf("F-Data:Unable to read factory-data from SPI NOR\n");
120 fd = (struct factory_data_values *)buf;
122 if (fd->magic != FACTORY_DATA_MAGIC)
123 printf("F-Data:Magic value not correct\n");
125 crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
127 printf("F-Data:CRC not correct\n");
129 printf("F-Data:factory-data version %x detected\n",
132 /* Handle wifi_mac env variable */
134 sprintf(str, "%pM", ptr);
135 if (!is_valid_ethaddr(ptr))
136 printf("F-Data:Invalid MAC addr: wifi_mac %s\n", str);
138 env = env_get("wifiaddr");
139 if (strcmp(env, str)) {
140 env_set("wifiaddr", str);
144 /* Handle eth_mac env variable */
146 sprintf(str, "%pM", ptr);
147 if (!is_valid_ethaddr(ptr))
148 printf("F-Data:Invalid MAC addr: eth_mac %s\n", str);
150 env = env_get("ethaddr");
151 if (strcmp(env, str)) {
152 env_set("ethaddr", str);
156 /* Handle UUID env variables */
157 env_updated |= prepare_uuid_var(fd->ipr_id, "linuxmoduleid", 'X');
158 env_updated |= prepare_uuid_var(fd->hqv_id, "linuxmodulehqvid", '\0');
159 env_updated |= prepare_uuid_var(fd->unielec_id,
160 "linuxmoduleunielecid", '\0');
162 /* Check if the environment was updated and needs to get stored */
163 if (env_updated != 0) {
164 printf("F-Data:Values don't match env values -> saving\n");
167 debug("F-Data:Values match current env values\n");
177 int board_late_init(void)
179 if (IS_ENABLED(CONFIG_LED))
182 factory_data_env_config();
187 static void copy_or_generate_uuid(char *fd_ptr, const char *env_var_name)
189 char str[UUID_STR_LEN + 1] = { 0 }; /* Enough for UUID stuff */
192 /* Don't use the UUID dest place, as the \0 char won't fit */
193 env = env_get(env_var_name);
195 strncpy(str, env, UUID_STR_LEN);
197 gen_rand_uuid_str(str, UUID_STR_FORMAT_STD);
199 memcpy(fd_ptr, str, UUID_STR_LEN);
203 * Helper function to provide some sane factory-data values for testing
204 * purpose, when these values are not programmed correctly
206 int do_fd_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
208 struct factory_data_values *fd;
209 struct spi_flash *sf;
211 int ret = CMD_RET_FAILURE;
213 buf = malloc(FACTORY_DATA_SECT_SIZE);
215 printf("F-Data:Unable to allocate buffer\n");
216 return CMD_RET_FAILURE;
219 sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
220 CONFIG_SF_DEFAULT_CS,
221 CONFIG_SF_DEFAULT_SPEED,
222 CONFIG_SF_DEFAULT_MODE);
224 printf("F-Data:Unable to access SPI NOR flash\n");
228 /* Generate the factory-data struct */
230 /* Fist read complete sector into buffer */
231 ret = spi_flash_read(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
234 printf("F-Data:spi_flash_read failed (%d)\n", ret);
238 fd = (struct factory_data_values *)buf;
239 fd->magic = FACTORY_DATA_MAGIC;
242 /* Use existing MAC and UUID values or generate some random ones */
243 if (!eth_env_get_enetaddr("wifiaddr", fd->wifi_mac)) {
244 net_random_ethaddr(fd->wifi_mac);
245 /* to get a different seed value for the MAC address */
249 if (!eth_env_get_enetaddr("ethaddr", fd->eth_mac))
250 net_random_ethaddr(fd->eth_mac);
252 copy_or_generate_uuid(fd->ipr_id, "linuxmoduleid");
253 copy_or_generate_uuid(fd->hqv_id, "linuxmodulehqvid");
254 copy_or_generate_uuid(fd->unielec_id, "linuxmoduleunielecid");
256 printf("New factory-data values:\n");
257 printf("wifiaddr=%pM\n", fd->wifi_mac);
258 printf("ethaddr=%pM\n", fd->eth_mac);
261 * We don't have the \0 char at the end, so we need to specify the
262 * length in the printf format instead
264 printf("linuxmoduleid=%." __stringify(UUID_STR_LEN) "s\n", fd->ipr_id);
265 printf("linuxmodulehqvid=%." __stringify(UUID_STR_LEN) "s\n",
267 printf("linuxmoduleunielecid=%." __stringify(UUID_STR_LEN) "s\n",
270 fd->crc = crc32(0, (u8 *)&fd->magic, FACTORY_DATA_CRC_LEN);
272 ret = spi_flash_erase(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE);
274 printf("F-Data:spi_flash_erase failed (%d)\n", ret);
278 ret = spi_flash_write(sf, FACTORY_DATA_OFFS, FACTORY_DATA_SECT_SIZE,
281 printf("F-Data:spi_flash_write failed (%d)\n", ret);
285 printf("F-Data:factory-data values written to SPI NOR flash\n");
297 fd_write, 1, 0, do_fd_write,
298 "Write test factory-data values to SPI NOR",