1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016-2020 Toradex
7 #include <asm/global_data.h>
8 #include "tdx-cfg-block.h"
9 #include "tdx-eeprom.h"
12 #include <asm/cache.h>
21 #include <asm/mach-types.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 #define TAG_VALID 0xcf01
26 #define TAG_MAC 0x0000
27 #define TAG_CAR_SERIAL 0x0021
29 #define TAG_INVALID 0xffff
31 #define TAG_FLAG_VALID 0x1
33 #define TDX_EEPROM_ID_MODULE 0
34 #define TDX_EEPROM_ID_CARRIER 1
36 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
37 #define TDX_CFG_BLOCK_MAX_SIZE 512
38 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
39 #define TDX_CFG_BLOCK_MAX_SIZE 64
40 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
41 #define TDX_CFG_BLOCK_MAX_SIZE 64
42 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
43 #define TDX_CFG_BLOCK_MAX_SIZE 64
45 #error Toradex config block location not set
48 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
49 #define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
59 struct toradex_hw tdx_hw_tag;
60 struct toradex_eth_addr tdx_eth_addr;
62 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
64 bool valid_cfgblock_carrier;
65 struct toradex_hw tdx_car_hw_tag;
68 #define TARGET_IS_ENABLED(x) IS_ENABLED(CONFIG_TARGET_ ## x)
70 const struct toradex_som toradex_modules[] = {
71 [0] = { "UNKNOWN MODULE", 0 },
72 [1] = { "Colibri PXA270 312MHz", 0 },
73 [2] = { "Colibri PXA270 520MHz", 0 },
74 [3] = { "Colibri PXA320 806MHz", 0 },
75 [4] = { "Colibri PXA300 208MHz", 0 },
76 [5] = { "Colibri PXA310 624MHz", 0 },
77 [6] = { "Colibri PXA320IT 806MHz", 0 },
78 [7] = { "Colibri PXA300 208MHz XT", 0 },
79 [8] = { "Colibri PXA270 312MHz", 0 },
80 [9] = { "Colibri PXA270 520MHz", 0 },
81 [10] = { "Colibri VF50 128MB", TARGET_IS_ENABLED(COLIBRI_VF) },
82 [11] = { "Colibri VF61 256MB", TARGET_IS_ENABLED(COLIBRI_VF) },
83 [12] = { "Colibri VF61 256MB IT", TARGET_IS_ENABLED(COLIBRI_VF) },
84 [13] = { "Colibri VF50 128MB IT", TARGET_IS_ENABLED(COLIBRI_VF) },
85 [14] = { "Colibri iMX6S 256MB", TARGET_IS_ENABLED(COLIBRI_IMX6) },
86 [15] = { "Colibri iMX6DL 512MB", TARGET_IS_ENABLED(COLIBRI_IMX6) },
87 [16] = { "Colibri iMX6S 256MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6) },
88 [17] = { "Colibri iMX6DL 512MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6) },
89 [18] = { "UNKNOWN MODULE", 0 },
90 [19] = { "UNKNOWN MODULE", 0 },
91 [20] = { "Colibri T20 256MB", TARGET_IS_ENABLED(COLIBRI_T20) },
92 [21] = { "Colibri T20 512MB", TARGET_IS_ENABLED(COLIBRI_T20) },
93 [22] = { "Colibri T20 512MB IT", TARGET_IS_ENABLED(COLIBRI_T20) },
94 [23] = { "Colibri T30 1GB", TARGET_IS_ENABLED(COLIBRI_T30) },
95 [24] = { "Colibri T20 256MB IT", TARGET_IS_ENABLED(COLIBRI_T20) },
96 [25] = { "Apalis T30 2GB", TARGET_IS_ENABLED(APALIS_T30) },
97 [26] = { "Apalis T30 1GB", TARGET_IS_ENABLED(APALIS_T30) },
98 [27] = { "Apalis iMX6Q 1GB", TARGET_IS_ENABLED(APALIS_IMX6) },
99 [28] = { "Apalis iMX6Q 2GB IT", TARGET_IS_ENABLED(APALIS_IMX6) },
100 [29] = { "Apalis iMX6D 512MB", TARGET_IS_ENABLED(APALIS_IMX6) },
101 [30] = { "Colibri T30 1GB IT", TARGET_IS_ENABLED(COLIBRI_T30) },
102 [31] = { "Apalis T30 1GB IT", TARGET_IS_ENABLED(APALIS_T30) },
103 [32] = { "Colibri iMX7S 256MB", TARGET_IS_ENABLED(COLIBRI_IMX7) },
104 [33] = { "Colibri iMX7D 512MB", TARGET_IS_ENABLED(COLIBRI_IMX7) },
105 [34] = { "Apalis TK1 2GB", TARGET_IS_ENABLED(APALIS_TK1) },
106 [35] = { "Apalis iMX6D 1GB IT", TARGET_IS_ENABLED(APALIS_IMX6) },
107 [36] = { "Colibri iMX6ULL 256MB", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
108 [37] = { "Apalis iMX8QM 4GB WB IT", TARGET_IS_ENABLED(APALIS_IMX8) },
109 [38] = { "Colibri iMX8QXP 2GB WB IT", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
110 [39] = { "Colibri iMX7D 1GB", TARGET_IS_ENABLED(COLIBRI_IMX7) },
111 [40] = { "Colibri iMX6ULL 512MB WB IT", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
112 [41] = { "Colibri iMX7D 512MB EPDC", TARGET_IS_ENABLED(COLIBRI_IMX7) },
113 [42] = { "Apalis TK1 4GB", TARGET_IS_ENABLED(APALIS_TK1) },
114 [43] = { "Colibri T20 512MB IT SETEK", TARGET_IS_ENABLED(COLIBRI_T20) },
115 [44] = { "Colibri iMX6ULL 512MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
116 [45] = { "Colibri iMX6ULL 512MB WB", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
117 [46] = { "Apalis iMX8QXP 2GB WB IT", 0 },
118 [47] = { "Apalis iMX8QM 4GB IT", TARGET_IS_ENABLED(APALIS_IMX8) },
119 [48] = { "Apalis iMX8QP 2GB WB", TARGET_IS_ENABLED(APALIS_IMX8) },
120 [49] = { "Apalis iMX8QP 2GB", TARGET_IS_ENABLED(APALIS_IMX8) },
121 [50] = { "Colibri iMX8QXP 2GB IT", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
122 [51] = { "Colibri iMX8DX 1GB WB", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
123 [52] = { "Colibri iMX8DX 1GB", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
124 [53] = { "Apalis iMX8QXP 2GB ECC IT", 0 },
125 [54] = { "Apalis iMX8DXP 1GB", TARGET_IS_ENABLED(APALIS_IMX8) },
126 [55] = { "Verdin iMX8M Mini Quad 2GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
127 [56] = { "Verdin iMX8M Nano Quad 1GB WB", 0 },
128 [57] = { "Verdin iMX8M Mini DualLite 1GB", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
129 [58] = { "Verdin iMX8M Plus Quad 4GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
130 [59] = { "Verdin iMX8M Mini Quad 2GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
131 [60] = { "Verdin iMX8M Mini DualLite 1GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
132 [61] = { "Verdin iMX8M Plus Quad 2GB", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
133 [62] = { "Colibri iMX6ULL 1GB IT", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
134 [63] = { "Verdin iMX8M Plus Quad 4GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
135 [64] = { "Verdin iMX8M Plus Quad 2GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
136 [65] = { "Verdin iMX8M Plus QuadLite 1GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
137 [66] = { "Verdin iMX8M Plus Quad 8GB WB", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
138 [67] = { "Apalis iMX8QM 8GB WB IT", TARGET_IS_ENABLED(APALIS_IMX8) },
139 [68] = { "Verdin iMX8M Mini Quad 2GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
142 const char * const toradex_carrier_boards[] = {
143 [0] = "UNKNOWN CARRIER BOARD",
145 [156] = "Verdin Development Board",
148 const char * const toradex_display_adapters[] = {
149 [0] = "UNKNOWN DISPLAY ADAPTER",
150 [157] = "Verdin DSI to HDMI Adapter",
151 [159] = "Verdin DSI to LVDS Adapter",
154 const u32 toradex_ouis[] = {
159 static u32 get_serial_from_mac(struct toradex_eth_addr *eth_addr)
162 u32 oui = ntohl(eth_addr->oui) >> 8;
163 u32 nic = ntohl(eth_addr->nic) >> 8;
165 for (i = 0; i < ARRAY_SIZE(toradex_ouis); i++) {
166 if (toradex_ouis[i] == oui)
170 return (u32)((i << 24) + nic);
173 void get_mac_from_serial(u32 tdx_serial, struct toradex_eth_addr *eth_addr)
175 u8 oui_index = tdx_serial >> 24;
176 u32 nic = tdx_serial & GENMASK(23, 0);
179 if (oui_index >= ARRAY_SIZE(toradex_ouis)) {
180 puts("Can't find OUI for this serial#\n");
184 oui = toradex_ouis[oui_index];
186 eth_addr->oui = htonl(oui << 8);
187 eth_addr->nic = htonl(nic << 8);
190 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
191 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
194 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
195 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
196 uint part = CONFIG_TDX_CFG_BLOCK_PART;
200 /* Read production parameter config block from eMMC */
201 mmc = find_mmc_device(dev);
203 puts("No MMC card found\n");
208 puts("MMC init failed\n");
211 if (part != mmc_get_blk_desc(mmc)->hwpart) {
212 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
213 puts("MMC partition switch failed\n");
219 offset += mmc->capacity;
220 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
223 /* Careful reads a whole block of 512 bytes into config_block */
224 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
225 (unsigned char *)config_block) != 1) {
230 /* Just writing one 512 byte block */
231 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
232 (unsigned char *)config_block) != 1) {
239 /* Switch back to regular eMMC user partition */
240 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
246 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
247 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
249 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
250 struct mtd_info *mtd = get_nand_dev_by_index(0);
255 /* Read production parameter config block from NAND page */
256 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
257 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
261 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
263 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
265 /* Write production parameter config block to NAND page */
266 return nand_write_skip_bad(get_nand_dev_by_index(0),
267 CONFIG_TDX_CFG_BLOCK_OFFSET,
268 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
269 config_block, WITH_WR_VERIFY);
273 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
274 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
276 /* Read production parameter config block from NOR flash */
277 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
278 TDX_CFG_BLOCK_MAX_SIZE);
282 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
284 /* Write production parameter config block to NOR flash */
285 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
286 TDX_CFG_BLOCK_MAX_SIZE);
290 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
291 static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
293 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
294 TDX_CFG_BLOCK_MAX_SIZE);
297 static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
299 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
300 TDX_CFG_BLOCK_MAX_SIZE);
304 int read_tdx_cfg_block(void)
307 u8 *config_block = NULL;
308 struct toradex_tag *tag;
309 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
312 /* Allocate RAM area for config block */
313 config_block = memalign(ARCH_DMA_MINALIGN, size);
315 printf("Not enough malloc space available!\n");
319 memset(config_block, 0, size);
321 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
322 ret = tdx_cfg_block_mmc_storage(config_block, 0);
323 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
324 ret = read_tdx_cfg_block_from_nand(config_block);
325 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
326 ret = read_tdx_cfg_block_from_nor(config_block);
327 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
328 ret = read_tdx_cfg_block_from_eeprom(config_block);
335 /* Expect a valid tag first */
336 tag = (struct toradex_tag *)config_block;
337 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
338 valid_cfgblock = false;
342 valid_cfgblock = true;
346 * check if there is enough space for storing tag and value of the
349 while (offset + sizeof(struct toradex_tag) +
350 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
351 tag = (struct toradex_tag *)(config_block + offset);
353 if (tag->id == TAG_INVALID)
356 if (tag->flags == TAG_FLAG_VALID) {
359 memcpy(&tdx_eth_addr, config_block + offset,
362 tdx_serial = get_serial_from_mac(&tdx_eth_addr);
365 memcpy(&tdx_hw_tag, config_block + offset, 8);
370 /* Get to next tag according to current tags length */
371 offset += tag->len * 4;
374 /* Cap product id to avoid issues with a yet unknown one */
375 if (tdx_hw_tag.prodid >= ARRAY_SIZE(toradex_modules))
376 tdx_hw_tag.prodid = 0;
383 static int parse_assembly_string(char *string_to_parse, u16 *assembly)
385 if (string_to_parse[3] >= 'A' && string_to_parse[3] <= 'Z')
386 *assembly = string_to_parse[3] - 'A';
387 else if (string_to_parse[3] == '#')
388 *assembly = dectoul(&string_to_parse[4], NULL);
395 static int get_cfgblock_interactive(void)
397 char message[CONFIG_SYS_CBSIZE];
403 printf("Enabled modules:\n");
404 for (i = 0; i < ARRAY_SIZE(toradex_modules); i++) {
405 if (toradex_modules[i].is_enabled)
406 printf(" %04d %s\n", i, toradex_modules[i].name);
409 sprintf(message, "Enter the module ID: ");
410 len = cli_readline(message);
412 prodid = dectoul(console_buffer, NULL);
413 if (prodid >= ARRAY_SIZE(toradex_modules) || !toradex_modules[prodid].is_enabled) {
414 printf("Parsing module id failed\n");
417 tdx_hw_tag.prodid = prodid;
421 sprintf(message, "Enter the module version (e.g. V1.1B or V1.1#26): V");
422 len = cli_readline(message);
425 tdx_hw_tag.ver_major = console_buffer[0] - '0';
426 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
428 ret = parse_assembly_string(console_buffer, &tdx_hw_tag.ver_assembly);
430 printf("Parsing module version failed\n");
435 sprintf(message, "Enter module serial number: ");
436 len = cli_readline(message);
439 tdx_serial = dectoul(console_buffer, NULL);
444 static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
447 char revision[3] = {barcode[6], barcode[7], '\0'};
449 if (strlen(barcode) < 16) {
450 printf("Argument too short, barcode is 16 chars long\n");
454 /* Get hardware information from the first 8 digits */
455 tag->ver_major = barcode[4] - '0';
456 tag->ver_minor = barcode[5] - '0';
457 tag->ver_assembly = dectoul(revision, NULL);
460 tag->prodid = dectoul(barcode, NULL);
462 /* Parse second part of the barcode (serial number */
464 *serial = dectoul(barcode, NULL);
469 static int write_tag(u8 *config_block, int *offset, int tag_id,
470 u8 *tag_data, size_t tag_data_size)
472 struct toradex_tag *tag;
474 if (!offset || !config_block)
477 tag = (struct toradex_tag *)(config_block + *offset);
479 tag->flags = TAG_FLAG_VALID;
480 /* len is provided as number of 32bit values after the tag */
481 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
482 *offset += sizeof(struct toradex_tag);
483 if (tag_data && tag_data_size) {
484 memcpy(config_block + *offset, tag_data,
486 *offset += tag_data_size;
492 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
493 int read_tdx_cfg_block_carrier(void)
496 u8 *config_block = NULL;
497 struct toradex_tag *tag;
498 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
501 /* Allocate RAM area for carrier config block */
502 config_block = memalign(ARCH_DMA_MINALIGN, size);
504 printf("Not enough malloc space available!\n");
508 memset(config_block, 0, size);
510 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
515 /* Expect a valid tag first */
516 tag = (struct toradex_tag *)config_block;
517 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
518 valid_cfgblock_carrier = false;
522 valid_cfgblock_carrier = true;
525 while (offset + sizeof(struct toradex_tag) +
526 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
527 tag = (struct toradex_tag *)(config_block + offset);
529 if (tag->id == TAG_INVALID)
532 if (tag->flags == TAG_FLAG_VALID) {
535 memcpy(&tdx_car_serial, config_block + offset,
536 sizeof(tdx_car_serial));
539 memcpy(&tdx_car_hw_tag, config_block +
545 /* Get to next tag according to current tags length */
546 offset += tag->len * 4;
553 int check_pid8_sanity(char *pid8)
555 char s_carrierid_verdin_dev[5];
556 char s_carrierid_dahlia[5];
558 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
559 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
561 /* sane value check, first 4 chars which represent carrier id */
562 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
565 if (!strncmp(pid8, s_carrierid_dahlia, 4))
571 int try_migrate_tdx_cfg_block_carrier(void)
575 int ret = CMD_RET_SUCCESS;
576 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
579 memset(pid8, 0x0, 8);
580 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
584 if (check_pid8_sanity(pid8))
587 /* Allocate RAM area for config block */
588 config_block = memalign(ARCH_DMA_MINALIGN, size);
590 printf("Not enough malloc space available!\n");
591 return CMD_RET_FAILURE;
594 memset(config_block, 0xff, size);
595 /* we try parse PID8 concatenating zeroed serial number */
596 tdx_car_hw_tag.ver_major = pid8[4] - '0';
597 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
598 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
601 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
604 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
607 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
608 sizeof(tdx_car_hw_tag));
611 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
612 sizeof(tdx_car_serial));
614 memset(config_block + offset, 0, 32 - offset);
615 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
618 printf("Failed to write Toradex Extra config block: %d\n",
620 ret = CMD_RET_FAILURE;
624 printf("Successfully migrated to Toradex Config Block from PID8\n");
631 static int get_cfgblock_carrier_interactive(void)
633 char message[CONFIG_SYS_CBSIZE];
637 printf("Supported carrier boards:\n");
638 printf("CARRIER BOARD NAME\t\t [ID]\n");
639 for (int i = 0; i < ARRAY_SIZE(toradex_carrier_boards); i++)
640 if (toradex_carrier_boards[i])
641 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
643 sprintf(message, "Choose your carrier board (provide ID): ");
644 len = cli_readline(message);
645 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
648 sprintf(message, "Enter carrier board version (e.g. V1.1B or V1.1#26): V");
649 len = cli_readline(message);
652 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
653 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
655 ret = parse_assembly_string(console_buffer, &tdx_car_hw_tag.ver_assembly);
657 printf("Parsing module version failed\n");
662 sprintf(message, "Enter carrier board serial number: ");
663 len = cli_readline(message);
666 tdx_car_serial = dectoul(console_buffer, NULL);
671 static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
675 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
677 int ret = CMD_RET_SUCCESS;
679 int force_overwrite = 0;
682 if (argv[2][0] == '-' && argv[2][1] == 'y')
686 /* Allocate RAM area for config block */
687 config_block = memalign(ARCH_DMA_MINALIGN, size);
689 printf("Not enough malloc space available!\n");
690 return CMD_RET_FAILURE;
693 memset(config_block, 0xff, size);
694 read_tdx_cfg_block_carrier();
695 if (valid_cfgblock_carrier && !force_overwrite) {
696 char message[CONFIG_SYS_CBSIZE];
698 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
700 if (!cli_readline(message))
703 if (console_buffer[0] != 'y' &&
704 console_buffer[0] != 'Y')
708 if (argc < 3 || (force_overwrite && argc < 4)) {
709 err = get_cfgblock_carrier_interactive();
712 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
715 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
720 ret = CMD_RET_FAILURE;
725 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
728 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
729 sizeof(tdx_car_hw_tag));
732 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
733 sizeof(tdx_car_serial));
735 memset(config_block + offset, 0, 32 - offset);
736 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
739 printf("Failed to write Toradex Extra config block: %d\n",
741 ret = CMD_RET_FAILURE;
745 printf("Toradex Extra config block successfully written\n");
752 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
754 static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
758 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
760 int ret = CMD_RET_SUCCESS;
762 int force_overwrite = 0;
765 #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
766 if (!strcmp(argv[2], "carrier"))
767 return do_cfgblock_carrier_create(cmdtp, flag,
769 #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
770 if (argv[2][0] == '-' && argv[2][1] == 'y')
774 /* Allocate RAM area for config block */
775 config_block = memalign(ARCH_DMA_MINALIGN, size);
777 printf("Not enough malloc space available!\n");
778 return CMD_RET_FAILURE;
781 memset(config_block, 0xff, size);
783 read_tdx_cfg_block();
784 if (valid_cfgblock) {
785 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
787 * On NAND devices, recreation is only allowed if the page is
788 * empty (config block invalid...)
790 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
791 CONFIG_TDX_CFG_BLOCK_OFFSET /
792 get_nand_dev_by_index(0)->erasesize);
794 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
796 * On NOR devices, recreation is only allowed if the sector is
797 * empty and write protection is off (config block invalid...)
799 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
800 CONFIG_TDX_CFG_BLOCK_OFFSET);
803 if (!force_overwrite) {
804 char message[CONFIG_SYS_CBSIZE];
807 "A valid Toradex config block is present, still recreate? [y/N] ");
809 if (!cli_readline(message))
812 if (console_buffer[0] != 'y' &&
813 console_buffer[0] != 'Y')
819 /* Parse new Toradex config block data... */
820 if (argc < 3 || (force_overwrite && argc < 4)) {
821 err = get_cfgblock_interactive();
824 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
827 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
831 ret = CMD_RET_FAILURE;
835 /* Convert serial number to MAC address (the storage format) */
836 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
839 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
842 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
846 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
847 sizeof(tdx_eth_addr));
849 memset(config_block + offset, 0, 32 - offset);
850 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
851 err = tdx_cfg_block_mmc_storage(config_block, 1);
852 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
853 err = write_tdx_cfg_block_to_nand(config_block);
854 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
855 err = write_tdx_cfg_block_to_nor(config_block);
856 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
857 err = write_tdx_cfg_block_to_eeprom(config_block);
862 printf("Failed to write Toradex config block: %d\n", ret);
863 ret = CMD_RET_FAILURE;
867 printf("Toradex config block successfully written\n");
874 static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
880 return CMD_RET_USAGE;
882 if (!strcmp(argv[1], "create")) {
883 return do_cfgblock_create(cmdtp, flag, argc, argv);
884 } else if (!strcmp(argv[1], "reload")) {
885 ret = read_tdx_cfg_block();
887 printf("Failed to reload Toradex config block: %d\n",
889 return CMD_RET_FAILURE;
891 return CMD_RET_SUCCESS;
894 return CMD_RET_USAGE;
898 cfgblock, 5, 0, do_cfgblock,
899 "Toradex config block handling commands",
900 "create [-y] [barcode] - (Re-)create Toradex config block\n"
901 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
902 "cfgblock reload - Reload Toradex config block from flash"