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