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