1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2016-2020 Toradex
4 */
5
6 #include <common.h>
7 #include <asm/global_data.h>
8 #include "tdx-cfg-block.h"
9 #include "tdx-eeprom.h"
10
11 #include <command.h>
12 #include <asm/cache.h>
13
14 #if defined(CONFIG_TARGET_APALIS_IMX6) || \
15 defined(CONFIG_TARGET_APALIS_IMX8) || \
16 defined(CONFIG_TARGET_APALIS_IMX8X) || \
17 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
18 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
19 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
20 defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
21 defined(CONFIG_TARGET_VERDIN_IMX8MP)
22 #include <asm/arch/sys_proto.h>
23 #else
24 #define is_cpu_type(cpu) (0)
25 #endif
26 #if defined(CONFIG_CPU_PXA27X)
27 #include <asm/arch-pxa/pxa.h>
28 #else
29 #define cpu_is_pxa27x(cpu) (0)
30 #endif
31 #include <cli.h>
32 #include <console.h>
33 #include <env.h>
34 #include <flash.h>
35 #include <malloc.h>
36 #include <mmc.h>
37 #include <nand.h>
38 #include <asm/mach-types.h>
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 #define TAG_VALID 0xcf01
43 #define TAG_MAC 0x0000
44 #define TAG_CAR_SERIAL 0x0021
45 #define TAG_HW 0x0008
46 #define TAG_INVALID 0xffff
47
48 #define TAG_FLAG_VALID 0x1
49
50 #define TDX_EEPROM_ID_MODULE 0
51 #define TDX_EEPROM_ID_CARRIER 1
52
53 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
54 #define TDX_CFG_BLOCK_MAX_SIZE 512
55 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
56 #define TDX_CFG_BLOCK_MAX_SIZE 64
57 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
58 #define TDX_CFG_BLOCK_MAX_SIZE 64
59 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
60 #define TDX_CFG_BLOCK_MAX_SIZE 64
61 #else
62 #error Toradex config block location not set
63 #endif
64
65 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
66 #define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
67 #endif
68
69 struct toradex_tag {
70 u32 len:14;
71 u32 flags:2;
72 u32 id:16;
73 };
74
75 bool valid_cfgblock;
76 struct toradex_hw tdx_hw_tag;
77 struct toradex_eth_addr tdx_eth_addr;
78 u32 tdx_serial;
79 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
80 u32 tdx_car_serial;
81 bool valid_cfgblock_carrier;
82 struct toradex_hw tdx_car_hw_tag;
83 #endif
84
85 const char * const toradex_modules[] = {
86 [0] = "UNKNOWN MODULE",
87 [1] = "Colibri PXA270 312MHz",
88 [2] = "Colibri PXA270 520MHz",
89 [3] = "Colibri PXA320 806MHz",
90 [4] = "Colibri PXA300 208MHz",
91 [5] = "Colibri PXA310 624MHz",
92 [6] = "Colibri PXA320 806MHz IT",
93 [7] = "Colibri PXA300 208MHz XT",
94 [8] = "Colibri PXA270 312MHz",
95 [9] = "Colibri PXA270 520MHz",
96 [10] = "Colibri VF50 128MB", /* not currently on sale */
97 [11] = "Colibri VF61 256MB",
98 [12] = "Colibri VF61 256MB IT",
99 [13] = "Colibri VF50 128MB IT",
100 [14] = "Colibri iMX6 Solo 256MB",
101 [15] = "Colibri iMX6 DualLite 512MB",
102 [16] = "Colibri iMX6 Solo 256MB IT",
103 [17] = "Colibri iMX6 DualLite 512MB IT",
104 [18] = "UNKNOWN MODULE",
105 [19] = "UNKNOWN MODULE",
106 [20] = "Colibri T20 256MB",
107 [21] = "Colibri T20 512MB",
108 [22] = "Colibri T20 512MB IT",
109 [23] = "Colibri T30 1GB",
110 [24] = "Colibri T20 256MB IT",
111 [25] = "Apalis T30 2GB",
112 [26] = "Apalis T30 1GB",
113 [27] = "Apalis iMX6 Quad 1GB",
114 [28] = "Apalis iMX6 Quad 2GB IT",
115 [29] = "Apalis iMX6 Dual 512MB",
116 [30] = "Colibri T30 1GB IT",
117 [31] = "Apalis T30 1GB IT",
118 [32] = "Colibri iMX7 Solo 256MB",
119 [33] = "Colibri iMX7 Dual 512MB",
120 [34] = "Apalis TK1 2GB",
121 [35] = "Apalis iMX6 Dual 1GB IT",
122 [36] = "Colibri iMX6ULL 256MB",
123 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
124 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
125 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
126 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
127 [41] = "Colibri iMX7 Dual 512MB EPDC",
128 [42] = "Apalis TK1 4GB",
129 [43] = "Colibri T20 512MB IT SETEK",
130 [44] = "Colibri iMX6ULL 512MB IT",
131 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
132 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
133 [47] = "Apalis iMX8 QuadMax 4GB IT",
134 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
135 [49] = "Apalis iMX8 QuadPlus 2GB",
136 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
137 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
138 [52] = "Colibri iMX8 DualX 1GB",
139 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
140 [54] = "Apalis iMX8 DualXPlus 1GB",
141 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
142 [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
143 [57] = "Verdin iMX8M Mini DualLite 1GB",
144 [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
145 [59] = "Verdin iMX8M Mini Quad 2GB IT",
146 [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
147 [61] = "Verdin iMX8M Plus Quad 2GB",
148 };
149
150 const char * const toradex_carrier_boards[] = {
151 [0] = "UNKNOWN CARRIER BOARD",
152 [155] = "Dahlia",
153 [156] = "Verdin Development Board",
154 };
155
156 const char * const toradex_display_adapters[] = {
157 [0] = "UNKNOWN DISPLAY ADAPTER",
158 [157] = "Verdin DSI to HDMI Adapter",
159 [159] = "Verdin DSI to LVDS Adapter",
160 };
161
162 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
tdx_cfg_block_mmc_storage(u8 * config_block,int write)163 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
164 {
165 struct mmc *mmc;
166 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
167 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
168 uint part = CONFIG_TDX_CFG_BLOCK_PART;
169 uint blk_start;
170 int ret = 0;
171
172 /* Read production parameter config block from eMMC */
173 mmc = find_mmc_device(dev);
174 if (!mmc) {
175 puts("No MMC card found\n");
176 ret = -ENODEV;
177 goto out;
178 }
179 if (mmc_init(mmc)) {
180 puts("MMC init failed\n");
181 return -EINVAL;
182 }
183 if (part != mmc_get_blk_desc(mmc)->hwpart) {
184 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
185 puts("MMC partition switch failed\n");
186 ret = -ENODEV;
187 goto out;
188 }
189 }
190 if (offset < 0)
191 offset += mmc->capacity;
192 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
193
194 if (!write) {
195 /* Careful reads a whole block of 512 bytes into config_block */
196 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
197 (unsigned char *)config_block) != 1) {
198 ret = -EIO;
199 goto out;
200 }
201 } else {
202 /* Just writing one 512 byte block */
203 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
204 (unsigned char *)config_block) != 1) {
205 ret = -EIO;
206 goto out;
207 }
208 }
209
210 out:
211 /* Switch back to regular eMMC user partition */
212 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
213
214 return ret;
215 }
216 #endif
217
218 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
read_tdx_cfg_block_from_nand(unsigned char * config_block)219 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
220 {
221 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
222 struct mtd_info *mtd = get_nand_dev_by_index(0);
223
224 if (!mtd)
225 return -ENODEV;
226
227 /* Read production parameter config block from NAND page */
228 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
229 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
230 config_block);
231 }
232
write_tdx_cfg_block_to_nand(unsigned char * config_block)233 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
234 {
235 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
236
237 /* Write production parameter config block to NAND page */
238 return nand_write_skip_bad(get_nand_dev_by_index(0),
239 CONFIG_TDX_CFG_BLOCK_OFFSET,
240 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
241 config_block, WITH_WR_VERIFY);
242 }
243 #endif
244
245 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
read_tdx_cfg_block_from_nor(unsigned char * config_block)246 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
247 {
248 /* Read production parameter config block from NOR flash */
249 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
250 TDX_CFG_BLOCK_MAX_SIZE);
251 return 0;
252 }
253
write_tdx_cfg_block_to_nor(unsigned char * config_block)254 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
255 {
256 /* Write production parameter config block to NOR flash */
257 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
258 TDX_CFG_BLOCK_MAX_SIZE);
259 }
260 #endif
261
262 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
read_tdx_cfg_block_from_eeprom(unsigned char * config_block)263 static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
264 {
265 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
266 TDX_CFG_BLOCK_MAX_SIZE);
267 }
268
write_tdx_cfg_block_to_eeprom(unsigned char * config_block)269 static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
270 {
271 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
272 TDX_CFG_BLOCK_MAX_SIZE);
273 }
274 #endif
275
read_tdx_cfg_block(void)276 int read_tdx_cfg_block(void)
277 {
278 int ret = 0;
279 u8 *config_block = NULL;
280 struct toradex_tag *tag;
281 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
282 int offset;
283
284 /* Allocate RAM area for config block */
285 config_block = memalign(ARCH_DMA_MINALIGN, size);
286 if (!config_block) {
287 printf("Not enough malloc space available!\n");
288 return -ENOMEM;
289 }
290
291 memset(config_block, 0, size);
292
293 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
294 ret = tdx_cfg_block_mmc_storage(config_block, 0);
295 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
296 ret = read_tdx_cfg_block_from_nand(config_block);
297 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
298 ret = read_tdx_cfg_block_from_nor(config_block);
299 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
300 ret = read_tdx_cfg_block_from_eeprom(config_block);
301 #else
302 ret = -EINVAL;
303 #endif
304 if (ret)
305 goto out;
306
307 /* Expect a valid tag first */
308 tag = (struct toradex_tag *)config_block;
309 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
310 valid_cfgblock = false;
311 ret = -EINVAL;
312 goto out;
313 }
314 valid_cfgblock = true;
315 offset = 4;
316
317 /*
318 * check if there is enough space for storing tag and value of the
319 * biggest element
320 */
321 while (offset + sizeof(struct toradex_tag) +
322 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
323 tag = (struct toradex_tag *)(config_block + offset);
324 offset += 4;
325 if (tag->id == TAG_INVALID)
326 break;
327
328 if (tag->flags == TAG_FLAG_VALID) {
329 switch (tag->id) {
330 case TAG_MAC:
331 memcpy(&tdx_eth_addr, config_block + offset,
332 6);
333
334 /* NIC part of MAC address is serial number */
335 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
336 break;
337 case TAG_HW:
338 memcpy(&tdx_hw_tag, config_block + offset, 8);
339 break;
340 }
341 }
342
343 /* Get to next tag according to current tags length */
344 offset += tag->len * 4;
345 }
346
347 /* Cap product id to avoid issues with a yet unknown one */
348 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
349 sizeof(toradex_modules[0])))
350 tdx_hw_tag.prodid = 0;
351
352 out:
353 free(config_block);
354 return ret;
355 }
356
get_cfgblock_interactive(void)357 static int get_cfgblock_interactive(void)
358 {
359 char message[CONFIG_SYS_CBSIZE];
360 char *soc;
361 char it = 'n';
362 char wb = 'n';
363 int len = 0;
364
365 /* Unknown module by default */
366 tdx_hw_tag.prodid = 0;
367
368 if (cpu_is_pxa27x())
369 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
370 else
371 it = 'y';
372
373 #if defined(CONFIG_TARGET_APALIS_IMX8) || \
374 defined(CONFIG_TARGET_APALIS_IMX8X) || \
375 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
376 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
377 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
378 defined(CONFIG_TARGET_VERDIN_IMX8MP)
379 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
380 len = cli_readline(message);
381 wb = console_buffer[0];
382 #endif
383
384 soc = env_get("soc");
385 if (!strcmp("mx6", soc)) {
386 #ifdef CONFIG_TARGET_APALIS_IMX6
387 if (it == 'y' || it == 'Y') {
388 if (is_cpu_type(MXC_CPU_MX6Q))
389 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
390 else
391 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
392 } else {
393 if (is_cpu_type(MXC_CPU_MX6Q))
394 tdx_hw_tag.prodid = APALIS_IMX6Q;
395 else
396 tdx_hw_tag.prodid = APALIS_IMX6D;
397 }
398 #elif CONFIG_TARGET_COLIBRI_IMX6
399 if (it == 'y' || it == 'Y') {
400 if (is_cpu_type(MXC_CPU_MX6DL))
401 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
402 else if (is_cpu_type(MXC_CPU_MX6SOLO))
403 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
404 } else {
405 if (is_cpu_type(MXC_CPU_MX6DL))
406 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
407 else if (is_cpu_type(MXC_CPU_MX6SOLO))
408 tdx_hw_tag.prodid = COLIBRI_IMX6S;
409 }
410 #elif CONFIG_TARGET_COLIBRI_IMX6ULL
411 if (it == 'y' || it == 'Y') {
412 if (wb == 'y' || wb == 'Y')
413 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
414 else
415 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
416 } else {
417 if (wb == 'y' || wb == 'Y')
418 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
419 else
420 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
421 }
422 #endif
423 } else if (!strcmp("imx7d", soc))
424 tdx_hw_tag.prodid = COLIBRI_IMX7D;
425 else if (!strcmp("imx7s", soc))
426 tdx_hw_tag.prodid = COLIBRI_IMX7S;
427 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
428 if (it == 'y' || it == 'Y') {
429 if (wb == 'y' || wb == 'Y')
430 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
431 else
432 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
433 } else {
434 if (wb == 'y' || wb == 'Y')
435 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
436 else
437 tdx_hw_tag.prodid = APALIS_IMX8QP;
438 }
439 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
440 #ifdef CONFIG_TARGET_APALIS_IMX8X
441 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
442 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
443 } else {
444 if (gd->ram_size == 0x40000000)
445 tdx_hw_tag.prodid = APALIS_IMX8DXP;
446 else
447 tdx_hw_tag.prodid = APALIS_IMX8QXP;
448 }
449 #elif CONFIG_TARGET_COLIBRI_IMX8X
450 if (it == 'y' || it == 'Y') {
451 if (wb == 'y' || wb == 'Y')
452 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
453 else
454 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
455 } else {
456 if (wb == 'y' || wb == 'Y')
457 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
458 else
459 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
460 }
461 #endif
462 } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
463 if (wb == 'y' || wb == 'Y')
464 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
465 else
466 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
467 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
468 if (wb == 'y' || wb == 'Y')
469 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
470 else
471 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
472 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
473 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
474 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
475 if (wb == 'y' || wb == 'Y')
476 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
477 else
478 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
479 } else if (!strcmp("tegra20", soc)) {
480 if (it == 'y' || it == 'Y')
481 if (gd->ram_size == 0x10000000)
482 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
483 else
484 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
485 else
486 if (gd->ram_size == 0x10000000)
487 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
488 else
489 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
490 } else if (cpu_is_pxa27x()) {
491 if (it == 'y' || it == 'Y')
492 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
493 else
494 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
495 }
496 #ifdef CONFIG_MACH_TYPE
497 else if (!strcmp("tegra30", soc)) {
498 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
499 if (it == 'y' || it == 'Y')
500 tdx_hw_tag.prodid = APALIS_T30_IT;
501 else
502 if (gd->ram_size == 0x40000000)
503 tdx_hw_tag.prodid = APALIS_T30_1GB;
504 else
505 tdx_hw_tag.prodid = APALIS_T30_2GB;
506 } else {
507 if (it == 'y' || it == 'Y')
508 tdx_hw_tag.prodid = COLIBRI_T30_IT;
509 else
510 tdx_hw_tag.prodid = COLIBRI_T30;
511 }
512 }
513 #endif /* CONFIG_MACH_TYPE */
514 else if (!strcmp("tegra124", soc)) {
515 tdx_hw_tag.prodid = APALIS_TK1_2GB;
516 } else if (!strcmp("vf500", soc)) {
517 if (it == 'y' || it == 'Y')
518 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
519 else
520 tdx_hw_tag.prodid = COLIBRI_VF50;
521 } else if (!strcmp("vf610", soc)) {
522 if (it == 'y' || it == 'Y')
523 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
524 else
525 tdx_hw_tag.prodid = COLIBRI_VF61;
526 }
527
528 if (!tdx_hw_tag.prodid) {
529 printf("Module type not detectable due to unknown SoC\n");
530 return -1;
531 }
532
533 while (len < 4) {
534 sprintf(message, "Enter the module version (e.g. V1.1B): V");
535 len = cli_readline(message);
536 }
537
538 tdx_hw_tag.ver_major = console_buffer[0] - '0';
539 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
540 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
541
542 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
543 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
544 COLIBRI_PXA270_V1_312MHZ);
545
546 while (len < 8) {
547 sprintf(message, "Enter module serial number: ");
548 len = cli_readline(message);
549 }
550
551 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
552
553 return 0;
554 }
555
get_cfgblock_barcode(char * barcode,struct toradex_hw * tag,u32 * serial)556 static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
557 u32 *serial)
558 {
559 if (strlen(barcode) < 16) {
560 printf("Argument too short, barcode is 16 chars long\n");
561 return -1;
562 }
563
564 /* Get hardware information from the first 8 digits */
565 tag->ver_major = barcode[4] - '0';
566 tag->ver_minor = barcode[5] - '0';
567 tag->ver_assembly = barcode[7] - '0';
568
569 barcode[4] = '\0';
570 tag->prodid = simple_strtoul(barcode, NULL, 10);
571
572 /* Parse second part of the barcode (serial number */
573 barcode += 8;
574 *serial = simple_strtoul(barcode, NULL, 10);
575
576 return 0;
577 }
578
write_tag(u8 * config_block,int * offset,int tag_id,u8 * tag_data,size_t tag_data_size)579 static int write_tag(u8 *config_block, int *offset, int tag_id,
580 u8 *tag_data, size_t tag_data_size)
581 {
582 struct toradex_tag *tag;
583
584 if (!offset || !config_block)
585 return -EINVAL;
586
587 tag = (struct toradex_tag *)(config_block + *offset);
588 tag->id = tag_id;
589 tag->flags = TAG_FLAG_VALID;
590 /* len is provided as number of 32bit values after the tag */
591 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
592 *offset += sizeof(struct toradex_tag);
593 if (tag_data && tag_data_size) {
594 memcpy(config_block + *offset, tag_data,
595 tag_data_size);
596 *offset += tag_data_size;
597 }
598
599 return 0;
600 }
601
602 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
read_tdx_cfg_block_carrier(void)603 int read_tdx_cfg_block_carrier(void)
604 {
605 int ret = 0;
606 u8 *config_block = NULL;
607 struct toradex_tag *tag;
608 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
609 int offset;
610
611 /* Allocate RAM area for carrier config block */
612 config_block = memalign(ARCH_DMA_MINALIGN, size);
613 if (!config_block) {
614 printf("Not enough malloc space available!\n");
615 return -ENOMEM;
616 }
617
618 memset(config_block, 0, size);
619
620 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
621 size);
622 if (ret)
623 return ret;
624
625 /* Expect a valid tag first */
626 tag = (struct toradex_tag *)config_block;
627 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
628 valid_cfgblock_carrier = false;
629 ret = -EINVAL;
630 goto out;
631 }
632 valid_cfgblock_carrier = true;
633 offset = 4;
634
635 while (offset + sizeof(struct toradex_tag) +
636 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
637 tag = (struct toradex_tag *)(config_block + offset);
638 offset += 4;
639 if (tag->id == TAG_INVALID)
640 break;
641
642 if (tag->flags == TAG_FLAG_VALID) {
643 switch (tag->id) {
644 case TAG_CAR_SERIAL:
645 memcpy(&tdx_car_serial, config_block + offset,
646 sizeof(tdx_car_serial));
647 break;
648 case TAG_HW:
649 memcpy(&tdx_car_hw_tag, config_block +
650 offset, 8);
651 break;
652 }
653 }
654
655 /* Get to next tag according to current tags length */
656 offset += tag->len * 4;
657 }
658 out:
659 free(config_block);
660 return ret;
661 }
662
check_pid8_sanity(char * pid8)663 int check_pid8_sanity(char *pid8)
664 {
665 char s_carrierid_verdin_dev[5];
666 char s_carrierid_dahlia[5];
667
668 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
669 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
670
671 /* sane value check, first 4 chars which represent carrier id */
672 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
673 return 0;
674
675 if (!strncmp(pid8, s_carrierid_dahlia, 4))
676 return 0;
677
678 return -EINVAL;
679 }
680
try_migrate_tdx_cfg_block_carrier(void)681 int try_migrate_tdx_cfg_block_carrier(void)
682 {
683 char pid8[8];
684 int offset = 0;
685 int ret = CMD_RET_SUCCESS;
686 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
687 u8 *config_block;
688
689 memset(pid8, 0x0, 8);
690 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
691 if (ret)
692 return ret;
693
694 if (check_pid8_sanity(pid8))
695 return -EINVAL;
696
697 /* Allocate RAM area for config block */
698 config_block = memalign(ARCH_DMA_MINALIGN, size);
699 if (!config_block) {
700 printf("Not enough malloc space available!\n");
701 return CMD_RET_FAILURE;
702 }
703
704 memset(config_block, 0xff, size);
705 /* we try parse PID8 concatenating zeroed serial number */
706 tdx_car_hw_tag.ver_major = pid8[4] - '0';
707 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
708 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
709
710 pid8[4] = '\0';
711 tdx_car_hw_tag.prodid = simple_strtoul(pid8, NULL, 10);
712
713 /* Valid Tag */
714 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
715
716 /* Product Tag */
717 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
718 sizeof(tdx_car_hw_tag));
719
720 /* Serial Tag */
721 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
722 sizeof(tdx_car_serial));
723
724 memset(config_block + offset, 0, 32 - offset);
725 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
726 size);
727 if (ret) {
728 printf("Failed to write Toradex Extra config block: %d\n",
729 ret);
730 ret = CMD_RET_FAILURE;
731 goto out;
732 }
733
734 printf("Successfully migrated to Toradex Config Block from PID8\n");
735
736 out:
737 free(config_block);
738 return ret;
739 }
740
get_cfgblock_carrier_interactive(void)741 static int get_cfgblock_carrier_interactive(void)
742 {
743 char message[CONFIG_SYS_CBSIZE];
744 int len;
745
746 printf("Supported carrier boards:\n");
747 printf("CARRIER BOARD NAME\t\t [ID]\n");
748 for (int i = 0; i < sizeof(toradex_carrier_boards) /
749 sizeof(toradex_carrier_boards[0]); i++)
750 if (toradex_carrier_boards[i])
751 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
752
753 sprintf(message, "Choose your carrier board (provide ID): ");
754 len = cli_readline(message);
755 tdx_car_hw_tag.prodid = simple_strtoul(console_buffer, NULL, 10);
756
757 do {
758 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
759 len = cli_readline(message);
760 } while (len < 4);
761
762 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
763 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
764 tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
765
766 while (len < 8) {
767 sprintf(message, "Enter carrier board serial number: ");
768 len = cli_readline(message);
769 }
770
771 tdx_car_serial = simple_strtoul(console_buffer, NULL, 10);
772
773 return 0;
774 }
775
do_cfgblock_carrier_create(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])776 static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
777 char * const argv[])
778 {
779 u8 *config_block;
780 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
781 int offset = 0;
782 int ret = CMD_RET_SUCCESS;
783 int err;
784 int force_overwrite = 0;
785
786 if (argc >= 3) {
787 if (argv[2][0] == '-' && argv[2][1] == 'y')
788 force_overwrite = 1;
789 }
790
791 /* Allocate RAM area for config block */
792 config_block = memalign(ARCH_DMA_MINALIGN, size);
793 if (!config_block) {
794 printf("Not enough malloc space available!\n");
795 return CMD_RET_FAILURE;
796 }
797
798 memset(config_block, 0xff, size);
799 read_tdx_cfg_block_carrier();
800 if (valid_cfgblock_carrier && !force_overwrite) {
801 char message[CONFIG_SYS_CBSIZE];
802
803 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
804
805 if (!cli_readline(message))
806 goto out;
807
808 if (console_buffer[0] != 'y' &&
809 console_buffer[0] != 'Y')
810 goto out;
811 }
812
813 if (argc < 3 || (force_overwrite && argc < 4)) {
814 err = get_cfgblock_carrier_interactive();
815 } else {
816 if (force_overwrite)
817 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
818 &tdx_car_serial);
819 else
820 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
821 &tdx_car_serial);
822 }
823
824 if (err) {
825 ret = CMD_RET_FAILURE;
826 goto out;
827 }
828
829 /* Valid Tag */
830 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
831
832 /* Product Tag */
833 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
834 sizeof(tdx_car_hw_tag));
835
836 /* Serial Tag */
837 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
838 sizeof(tdx_car_serial));
839
840 memset(config_block + offset, 0, 32 - offset);
841 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
842 size);
843 if (err) {
844 printf("Failed to write Toradex Extra config block: %d\n",
845 ret);
846 ret = CMD_RET_FAILURE;
847 goto out;
848 }
849
850 printf("Toradex Extra config block successfully written\n");
851
852 out:
853 free(config_block);
854 return ret;
855 }
856
857 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
858
do_cfgblock_create(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])859 static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
860 char * const argv[])
861 {
862 u8 *config_block;
863 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
864 int offset = 0;
865 int ret = CMD_RET_SUCCESS;
866 int err;
867 int force_overwrite = 0;
868
869 if (argc >= 3) {
870 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
871 if (!strcmp(argv[2], "carrier"))
872 return do_cfgblock_carrier_create(cmdtp, flag,
873 --argc, ++argv);
874 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
875 if (argv[2][0] == '-' && argv[2][1] == 'y')
876 force_overwrite = 1;
877 }
878
879 /* Allocate RAM area for config block */
880 config_block = memalign(ARCH_DMA_MINALIGN, size);
881 if (!config_block) {
882 printf("Not enough malloc space available!\n");
883 return CMD_RET_FAILURE;
884 }
885
886 memset(config_block, 0xff, size);
887
888 read_tdx_cfg_block();
889 if (valid_cfgblock) {
890 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
891 /*
892 * On NAND devices, recreation is only allowed if the page is
893 * empty (config block invalid...)
894 */
895 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
896 CONFIG_TDX_CFG_BLOCK_OFFSET /
897 get_nand_dev_by_index(0)->erasesize);
898 goto out;
899 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
900 /*
901 * On NOR devices, recreation is only allowed if the sector is
902 * empty and write protection is off (config block invalid...)
903 */
904 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
905 CONFIG_TDX_CFG_BLOCK_OFFSET);
906 goto out;
907 #else
908 if (!force_overwrite) {
909 char message[CONFIG_SYS_CBSIZE];
910
911 sprintf(message,
912 "A valid Toradex config block is present, still recreate? [y/N] ");
913
914 if (!cli_readline(message))
915 goto out;
916
917 if (console_buffer[0] != 'y' &&
918 console_buffer[0] != 'Y')
919 goto out;
920 }
921 #endif
922 }
923
924 /* Parse new Toradex config block data... */
925 if (argc < 3 || (force_overwrite && argc < 4)) {
926 err = get_cfgblock_interactive();
927 } else {
928 if (force_overwrite)
929 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
930 &tdx_serial);
931 else
932 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
933 &tdx_serial);
934 }
935 if (err) {
936 ret = CMD_RET_FAILURE;
937 goto out;
938 }
939
940 /* Convert serial number to MAC address (the storage format) */
941 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
942 tdx_eth_addr.nic = htonl(tdx_serial << 8);
943
944 /* Valid Tag */
945 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
946
947 /* Product Tag */
948 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
949 sizeof(tdx_hw_tag));
950
951 /* MAC Tag */
952 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
953 sizeof(tdx_eth_addr));
954
955 memset(config_block + offset, 0, 32 - offset);
956 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
957 err = tdx_cfg_block_mmc_storage(config_block, 1);
958 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
959 err = write_tdx_cfg_block_to_nand(config_block);
960 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
961 err = write_tdx_cfg_block_to_nor(config_block);
962 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
963 err = write_tdx_cfg_block_to_eeprom(config_block);
964 #else
965 err = -EINVAL;
966 #endif
967 if (err) {
968 printf("Failed to write Toradex config block: %d\n", ret);
969 ret = CMD_RET_FAILURE;
970 goto out;
971 }
972
973 printf("Toradex config block successfully written\n");
974
975 out:
976 free(config_block);
977 return ret;
978 }
979
do_cfgblock(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])980 static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
981 char *const argv[])
982 {
983 int ret;
984
985 if (argc < 2)
986 return CMD_RET_USAGE;
987
988 if (!strcmp(argv[1], "create")) {
989 return do_cfgblock_create(cmdtp, flag, argc, argv);
990 } else if (!strcmp(argv[1], "reload")) {
991 ret = read_tdx_cfg_block();
992 if (ret) {
993 printf("Failed to reload Toradex config block: %d\n",
994 ret);
995 return CMD_RET_FAILURE;
996 }
997 return CMD_RET_SUCCESS;
998 }
999
1000 return CMD_RET_USAGE;
1001 }
1002
1003 U_BOOT_CMD(
1004 cfgblock, 5, 0, do_cfgblock,
1005 "Toradex config block handling commands",
1006 "create [-y] [barcode] - (Re-)create Toradex config block\n"
1007 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1008 "cfgblock reload - Reload Toradex config block from flash"
1009 );
1010