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