1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016-2020 Toradex
7 #include "tdx-cfg-block.h"
8 #include "tdx-eeprom.h"
11 #include <asm/cache.h>
13 #if defined(CONFIG_TARGET_APALIS_IMX6) || \
14 defined(CONFIG_TARGET_APALIS_IMX8) || \
15 defined(CONFIG_TARGET_APALIS_IMX8X) || \
16 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
17 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
18 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
19 defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
20 defined(CONFIG_TARGET_VERDIN_IMX8MP)
21 #include <asm/arch/sys_proto.h>
23 #define is_cpu_type(cpu) (0)
25 #if defined(CONFIG_CPU_PXA27X)
26 #include <asm/arch-pxa/pxa.h>
28 #define cpu_is_pxa27x(cpu) (0)
37 #include <asm/mach-types.h>
39 DECLARE_GLOBAL_DATA_PTR;
41 #define TAG_VALID 0xcf01
42 #define TAG_MAC 0x0000
43 #define TAG_CAR_SERIAL 0x0021
45 #define TAG_INVALID 0xffff
47 #define TAG_FLAG_VALID 0x1
49 #define TDX_EEPROM_ID_MODULE 0
50 #define TDX_EEPROM_ID_CARRIER 1
52 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
53 #define TDX_CFG_BLOCK_MAX_SIZE 512
54 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
55 #define TDX_CFG_BLOCK_MAX_SIZE 64
56 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
57 #define TDX_CFG_BLOCK_MAX_SIZE 64
58 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
59 #define TDX_CFG_BLOCK_MAX_SIZE 64
61 #error Toradex config block location not set
64 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
65 #define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
75 struct toradex_hw tdx_hw_tag;
76 struct toradex_eth_addr tdx_eth_addr;
78 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
80 bool valid_cfgblock_carrier;
81 struct toradex_hw tdx_car_hw_tag;
84 const char * const toradex_modules[] = {
85 [0] = "UNKNOWN MODULE",
86 [1] = "Colibri PXA270 312MHz",
87 [2] = "Colibri PXA270 520MHz",
88 [3] = "Colibri PXA320 806MHz",
89 [4] = "Colibri PXA300 208MHz",
90 [5] = "Colibri PXA310 624MHz",
91 [6] = "Colibri PXA320 806MHz IT",
92 [7] = "Colibri PXA300 208MHz XT",
93 [8] = "Colibri PXA270 312MHz",
94 [9] = "Colibri PXA270 520MHz",
95 [10] = "Colibri VF50 128MB", /* not currently on sale */
96 [11] = "Colibri VF61 256MB",
97 [12] = "Colibri VF61 256MB IT",
98 [13] = "Colibri VF50 128MB IT",
99 [14] = "Colibri iMX6 Solo 256MB",
100 [15] = "Colibri iMX6 DualLite 512MB",
101 [16] = "Colibri iMX6 Solo 256MB IT",
102 [17] = "Colibri iMX6 DualLite 512MB IT",
103 [18] = "UNKNOWN MODULE",
104 [19] = "UNKNOWN MODULE",
105 [20] = "Colibri T20 256MB",
106 [21] = "Colibri T20 512MB",
107 [22] = "Colibri T20 512MB IT",
108 [23] = "Colibri T30 1GB",
109 [24] = "Colibri T20 256MB IT",
110 [25] = "Apalis T30 2GB",
111 [26] = "Apalis T30 1GB",
112 [27] = "Apalis iMX6 Quad 1GB",
113 [28] = "Apalis iMX6 Quad 2GB IT",
114 [29] = "Apalis iMX6 Dual 512MB",
115 [30] = "Colibri T30 1GB IT",
116 [31] = "Apalis T30 1GB IT",
117 [32] = "Colibri iMX7 Solo 256MB",
118 [33] = "Colibri iMX7 Dual 512MB",
119 [34] = "Apalis TK1 2GB",
120 [35] = "Apalis iMX6 Dual 1GB IT",
121 [36] = "Colibri iMX6ULL 256MB",
122 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
123 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
124 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
125 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
126 [41] = "Colibri iMX7 Dual 512MB EPDC",
127 [42] = "Apalis TK1 4GB",
128 [43] = "Colibri T20 512MB IT SETEK",
129 [44] = "Colibri iMX6ULL 512MB IT",
130 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
131 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
132 [47] = "Apalis iMX8 QuadMax 4GB IT",
133 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
134 [49] = "Apalis iMX8 QuadPlus 2GB",
135 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
136 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
137 [52] = "Colibri iMX8 DualX 1GB",
138 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
139 [54] = "Apalis iMX8 DualXPlus 1GB",
140 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
141 [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
142 [57] = "Verdin iMX8M Mini DualLite 1GB",
143 [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
144 [59] = "Verdin iMX8M Mini Quad 2GB IT",
145 [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
146 [61] = "Verdin iMX8M Plus Quad 2GB",
149 const char * const toradex_carrier_boards[] = {
150 [0] = "UNKNOWN CARRIER BOARD",
152 [156] = "Verdin Development Board",
155 const char * const toradex_display_adapters[] = {
156 [0] = "UNKNOWN DISPLAY ADAPTER",
157 [157] = "Verdin DSI to HDMI Adapter",
158 [159] = "Verdin DSI to LVDS Adapter",
161 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
162 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
165 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
166 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
167 uint part = CONFIG_TDX_CFG_BLOCK_PART;
171 /* Read production parameter config block from eMMC */
172 mmc = find_mmc_device(dev);
174 puts("No MMC card found\n");
179 puts("MMC init failed\n");
182 if (part != mmc_get_blk_desc(mmc)->hwpart) {
183 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
184 puts("MMC partition switch failed\n");
190 offset += mmc->capacity;
191 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
194 /* Careful reads a whole block of 512 bytes into config_block */
195 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
196 (unsigned char *)config_block) != 1) {
201 /* Just writing one 512 byte block */
202 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
203 (unsigned char *)config_block) != 1) {
210 /* Switch back to regular eMMC user partition */
211 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
217 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
218 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
220 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
221 struct mtd_info *mtd = get_nand_dev_by_index(0);
226 /* Read production parameter config block from NAND page */
227 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
228 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
232 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
234 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
236 /* Write production parameter config block to NAND page */
237 return nand_write_skip_bad(get_nand_dev_by_index(0),
238 CONFIG_TDX_CFG_BLOCK_OFFSET,
239 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
240 config_block, WITH_WR_VERIFY);
244 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
245 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
247 /* Read production parameter config block from NOR flash */
248 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
249 TDX_CFG_BLOCK_MAX_SIZE);
253 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
255 /* Write production parameter config block to NOR flash */
256 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
257 TDX_CFG_BLOCK_MAX_SIZE);
261 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
262 static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
264 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
265 TDX_CFG_BLOCK_MAX_SIZE);
268 static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
270 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
271 TDX_CFG_BLOCK_MAX_SIZE);
275 int read_tdx_cfg_block(void)
278 u8 *config_block = NULL;
279 struct toradex_tag *tag;
280 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
283 /* Allocate RAM area for config block */
284 config_block = memalign(ARCH_DMA_MINALIGN, size);
286 printf("Not enough malloc space available!\n");
290 memset(config_block, 0, size);
292 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
293 ret = tdx_cfg_block_mmc_storage(config_block, 0);
294 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
295 ret = read_tdx_cfg_block_from_nand(config_block);
296 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
297 ret = read_tdx_cfg_block_from_nor(config_block);
298 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
299 ret = read_tdx_cfg_block_from_eeprom(config_block);
306 /* Expect a valid tag first */
307 tag = (struct toradex_tag *)config_block;
308 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
309 valid_cfgblock = false;
313 valid_cfgblock = true;
317 * check if there is enough space for storing tag and value of the
320 while (offset + sizeof(struct toradex_tag) +
321 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
322 tag = (struct toradex_tag *)(config_block + offset);
324 if (tag->id == TAG_INVALID)
327 if (tag->flags == TAG_FLAG_VALID) {
330 memcpy(&tdx_eth_addr, config_block + offset,
333 /* NIC part of MAC address is serial number */
334 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
337 memcpy(&tdx_hw_tag, config_block + offset, 8);
342 /* Get to next tag according to current tags length */
343 offset += tag->len * 4;
346 /* Cap product id to avoid issues with a yet unknown one */
347 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
348 sizeof(toradex_modules[0])))
349 tdx_hw_tag.prodid = 0;
356 static int get_cfgblock_interactive(void)
358 char message[CONFIG_SYS_CBSIZE];
364 /* Unknown module by default */
365 tdx_hw_tag.prodid = 0;
368 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
372 #if defined(CONFIG_TARGET_APALIS_IMX8) || \
373 defined(CONFIG_TARGET_APALIS_IMX8X) || \
374 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
375 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
376 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
377 defined(CONFIG_TARGET_VERDIN_IMX8MP)
378 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
379 len = cli_readline(message);
380 wb = console_buffer[0];
383 soc = env_get("soc");
384 if (!strcmp("mx6", soc)) {
385 #ifdef CONFIG_TARGET_APALIS_IMX6
386 if (it == 'y' || it == 'Y') {
387 if (is_cpu_type(MXC_CPU_MX6Q))
388 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
390 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
392 if (is_cpu_type(MXC_CPU_MX6Q))
393 tdx_hw_tag.prodid = APALIS_IMX6Q;
395 tdx_hw_tag.prodid = APALIS_IMX6D;
397 #elif CONFIG_TARGET_COLIBRI_IMX6
398 if (it == 'y' || it == 'Y') {
399 if (is_cpu_type(MXC_CPU_MX6DL))
400 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
401 else if (is_cpu_type(MXC_CPU_MX6SOLO))
402 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
404 if (is_cpu_type(MXC_CPU_MX6DL))
405 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
406 else if (is_cpu_type(MXC_CPU_MX6SOLO))
407 tdx_hw_tag.prodid = COLIBRI_IMX6S;
409 #elif CONFIG_TARGET_COLIBRI_IMX6ULL
410 if (it == 'y' || it == 'Y') {
411 if (wb == 'y' || wb == 'Y')
412 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
414 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
416 if (wb == 'y' || wb == 'Y')
417 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
419 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
422 } else if (!strcmp("imx7d", soc))
423 tdx_hw_tag.prodid = COLIBRI_IMX7D;
424 else if (!strcmp("imx7s", soc))
425 tdx_hw_tag.prodid = COLIBRI_IMX7S;
426 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
427 if (it == 'y' || it == 'Y') {
428 if (wb == 'y' || wb == 'Y')
429 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
431 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
433 if (wb == 'y' || wb == 'Y')
434 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
436 tdx_hw_tag.prodid = APALIS_IMX8QP;
438 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
439 #ifdef CONFIG_TARGET_APALIS_IMX8X
440 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
441 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
443 if (gd->ram_size == 0x40000000)
444 tdx_hw_tag.prodid = APALIS_IMX8DXP;
446 tdx_hw_tag.prodid = APALIS_IMX8QXP;
448 #elif CONFIG_TARGET_COLIBRI_IMX8X
449 if (it == 'y' || it == 'Y') {
450 if (wb == 'y' || wb == 'Y')
451 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
453 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
455 if (wb == 'y' || wb == 'Y')
456 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
458 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
461 } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
462 if (wb == 'y' || wb == 'Y')
463 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
465 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
466 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
467 if (wb == 'y' || wb == 'Y')
468 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
470 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
471 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
472 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
473 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
474 if (wb == 'y' || wb == 'Y')
475 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
477 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
478 } else if (!strcmp("tegra20", soc)) {
479 if (it == 'y' || it == 'Y')
480 if (gd->ram_size == 0x10000000)
481 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
483 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
485 if (gd->ram_size == 0x10000000)
486 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
488 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
489 } else if (cpu_is_pxa27x()) {
490 if (it == 'y' || it == 'Y')
491 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
493 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
495 #ifdef CONFIG_MACH_TYPE
496 else if (!strcmp("tegra30", soc)) {
497 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
498 if (it == 'y' || it == 'Y')
499 tdx_hw_tag.prodid = APALIS_T30_IT;
501 if (gd->ram_size == 0x40000000)
502 tdx_hw_tag.prodid = APALIS_T30_1GB;
504 tdx_hw_tag.prodid = APALIS_T30_2GB;
506 if (it == 'y' || it == 'Y')
507 tdx_hw_tag.prodid = COLIBRI_T30_IT;
509 tdx_hw_tag.prodid = COLIBRI_T30;
512 #endif /* CONFIG_MACH_TYPE */
513 else if (!strcmp("tegra124", soc)) {
514 tdx_hw_tag.prodid = APALIS_TK1_2GB;
515 } else if (!strcmp("vf500", soc)) {
516 if (it == 'y' || it == 'Y')
517 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
519 tdx_hw_tag.prodid = COLIBRI_VF50;
520 } else if (!strcmp("vf610", soc)) {
521 if (it == 'y' || it == 'Y')
522 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
524 tdx_hw_tag.prodid = COLIBRI_VF61;
527 if (!tdx_hw_tag.prodid) {
528 printf("Module type not detectable due to unknown SoC\n");
533 sprintf(message, "Enter the module version (e.g. V1.1B): V");
534 len = cli_readline(message);
537 tdx_hw_tag.ver_major = console_buffer[0] - '0';
538 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
539 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
541 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
542 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
543 COLIBRI_PXA270_V1_312MHZ);
546 sprintf(message, "Enter module serial number: ");
547 len = cli_readline(message);
550 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
555 static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
558 if (strlen(barcode) < 16) {
559 printf("Argument too short, barcode is 16 chars long\n");
563 /* Get hardware information from the first 8 digits */
564 tag->ver_major = barcode[4] - '0';
565 tag->ver_minor = barcode[5] - '0';
566 tag->ver_assembly = barcode[7] - '0';
569 tag->prodid = simple_strtoul(barcode, NULL, 10);
571 /* Parse second part of the barcode (serial number */
573 *serial = simple_strtoul(barcode, NULL, 10);
578 static int write_tag(u8 *config_block, int *offset, int tag_id,
579 u8 *tag_data, size_t tag_data_size)
581 struct toradex_tag *tag;
583 if (!offset || !config_block)
586 tag = (struct toradex_tag *)(config_block + *offset);
588 tag->flags = TAG_FLAG_VALID;
589 /* len is provided as number of 32bit values after the tag */
590 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
591 *offset += sizeof(struct toradex_tag);
592 if (tag_data && tag_data_size) {
593 memcpy(config_block + *offset, tag_data,
595 *offset += tag_data_size;
601 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
602 int read_tdx_cfg_block_carrier(void)
605 u8 *config_block = NULL;
606 struct toradex_tag *tag;
607 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
610 /* Allocate RAM area for carrier config block */
611 config_block = memalign(ARCH_DMA_MINALIGN, size);
613 printf("Not enough malloc space available!\n");
617 memset(config_block, 0, size);
619 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
624 /* Expect a valid tag first */
625 tag = (struct toradex_tag *)config_block;
626 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
627 valid_cfgblock_carrier = false;
631 valid_cfgblock_carrier = true;
634 while (offset + sizeof(struct toradex_tag) +
635 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
636 tag = (struct toradex_tag *)(config_block + offset);
638 if (tag->id == TAG_INVALID)
641 if (tag->flags == TAG_FLAG_VALID) {
644 memcpy(&tdx_car_serial, config_block + offset,
645 sizeof(tdx_car_serial));
648 memcpy(&tdx_car_hw_tag, config_block +
654 /* Get to next tag according to current tags length */
655 offset += tag->len * 4;
662 int check_pid8_sanity(char *pid8)
664 char s_carrierid_verdin_dev[5];
665 char s_carrierid_dahlia[5];
667 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
668 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
670 /* sane value check, first 4 chars which represent carrier id */
671 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
674 if (!strncmp(pid8, s_carrierid_dahlia, 4))
680 int try_migrate_tdx_cfg_block_carrier(void)
684 int ret = CMD_RET_SUCCESS;
685 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
688 memset(pid8, 0x0, 8);
689 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
693 if (check_pid8_sanity(pid8))
696 /* Allocate RAM area for config block */
697 config_block = memalign(ARCH_DMA_MINALIGN, size);
699 printf("Not enough malloc space available!\n");
700 return CMD_RET_FAILURE;
703 memset(config_block, 0xff, size);
704 /* we try parse PID8 concatenating zeroed serial number */
705 tdx_car_hw_tag.ver_major = pid8[4] - '0';
706 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
707 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
710 tdx_car_hw_tag.prodid = simple_strtoul(pid8, NULL, 10);
713 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
716 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
717 sizeof(tdx_car_hw_tag));
720 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
721 sizeof(tdx_car_serial));
723 memset(config_block + offset, 0, 32 - offset);
724 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
727 printf("Failed to write Toradex Extra config block: %d\n",
729 ret = CMD_RET_FAILURE;
733 printf("Successfully migrated to Toradex Config Block from PID8\n");
740 static int get_cfgblock_carrier_interactive(void)
742 char message[CONFIG_SYS_CBSIZE];
745 printf("Supported carrier boards:\n");
746 printf("CARRIER BOARD NAME\t\t [ID]\n");
747 for (int i = 0; i < sizeof(toradex_carrier_boards) /
748 sizeof(toradex_carrier_boards[0]); i++)
749 if (toradex_carrier_boards[i])
750 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
752 sprintf(message, "Choose your carrier board (provide ID): ");
753 len = cli_readline(message);
754 tdx_car_hw_tag.prodid = simple_strtoul(console_buffer, NULL, 10);
757 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
758 len = cli_readline(message);
761 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
762 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
763 tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
766 sprintf(message, "Enter carrier board serial number: ");
767 len = cli_readline(message);
770 tdx_car_serial = simple_strtoul(console_buffer, NULL, 10);
775 static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
779 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
781 int ret = CMD_RET_SUCCESS;
783 int force_overwrite = 0;
786 if (argv[2][0] == '-' && argv[2][1] == 'y')
790 /* Allocate RAM area for config block */
791 config_block = memalign(ARCH_DMA_MINALIGN, size);
793 printf("Not enough malloc space available!\n");
794 return CMD_RET_FAILURE;
797 memset(config_block, 0xff, size);
798 read_tdx_cfg_block_carrier();
799 if (valid_cfgblock_carrier && !force_overwrite) {
800 char message[CONFIG_SYS_CBSIZE];
802 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
804 if (!cli_readline(message))
807 if (console_buffer[0] != 'y' &&
808 console_buffer[0] != 'Y')
812 if (argc < 3 || (force_overwrite && argc < 4)) {
813 err = get_cfgblock_carrier_interactive();
816 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
819 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
824 ret = CMD_RET_FAILURE;
829 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
832 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
833 sizeof(tdx_car_hw_tag));
836 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
837 sizeof(tdx_car_serial));
839 memset(config_block + offset, 0, 32 - offset);
840 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
843 printf("Failed to write Toradex Extra config block: %d\n",
845 ret = CMD_RET_FAILURE;
849 printf("Toradex Extra config block successfully written\n");
856 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
858 static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
862 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
864 int ret = CMD_RET_SUCCESS;
866 int force_overwrite = 0;
869 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
870 if (!strcmp(argv[2], "carrier"))
871 return do_cfgblock_carrier_create(cmdtp, flag,
873 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
874 if (argv[2][0] == '-' && argv[2][1] == 'y')
878 /* Allocate RAM area for config block */
879 config_block = memalign(ARCH_DMA_MINALIGN, size);
881 printf("Not enough malloc space available!\n");
882 return CMD_RET_FAILURE;
885 memset(config_block, 0xff, size);
887 read_tdx_cfg_block();
888 if (valid_cfgblock) {
889 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
891 * On NAND devices, recreation is only allowed if the page is
892 * empty (config block invalid...)
894 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
895 CONFIG_TDX_CFG_BLOCK_OFFSET /
896 get_nand_dev_by_index(0)->erasesize);
898 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
900 * On NOR devices, recreation is only allowed if the sector is
901 * empty and write protection is off (config block invalid...)
903 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
904 CONFIG_TDX_CFG_BLOCK_OFFSET);
907 if (!force_overwrite) {
908 char message[CONFIG_SYS_CBSIZE];
911 "A valid Toradex config block is present, still recreate? [y/N] ");
913 if (!cli_readline(message))
916 if (console_buffer[0] != 'y' &&
917 console_buffer[0] != 'Y')
923 /* Parse new Toradex config block data... */
924 if (argc < 3 || (force_overwrite && argc < 4)) {
925 err = get_cfgblock_interactive();
928 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
931 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
935 ret = CMD_RET_FAILURE;
939 /* Convert serial number to MAC address (the storage format) */
940 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
941 tdx_eth_addr.nic = htonl(tdx_serial << 8);
944 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
947 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
951 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
952 sizeof(tdx_eth_addr));
954 memset(config_block + offset, 0, 32 - offset);
955 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
956 err = tdx_cfg_block_mmc_storage(config_block, 1);
957 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
958 err = write_tdx_cfg_block_to_nand(config_block);
959 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
960 err = write_tdx_cfg_block_to_nor(config_block);
961 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
962 err = write_tdx_cfg_block_to_eeprom(config_block);
967 printf("Failed to write Toradex config block: %d\n", ret);
968 ret = CMD_RET_FAILURE;
972 printf("Toradex config block successfully written\n");
979 static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
985 return CMD_RET_USAGE;
987 if (!strcmp(argv[1], "create")) {
988 return do_cfgblock_create(cmdtp, flag, argc, argv);
989 } else if (!strcmp(argv[1], "reload")) {
990 ret = read_tdx_cfg_block();
992 printf("Failed to reload Toradex config block: %d\n",
994 return CMD_RET_FAILURE;
996 return CMD_RET_SUCCESS;
999 return CMD_RET_USAGE;
1003 cfgblock, 5, 0, do_cfgblock,
1004 "Toradex config block handling commands",
1005 "create [-y] [barcode] - (Re-)create Toradex config block\n"
1006 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1007 "cfgblock reload - Reload Toradex config block from flash"