toradex: tdx-cfg-block: add Apalis iMX8X support
[platform/kernel/u-boot.git] / board / toradex / common / tdx-cfg-block.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016-2020 Toradex
4  */
5
6 #include <common.h>
7 #include "tdx-cfg-block.h"
8
9 #if defined(CONFIG_TARGET_APALIS_IMX6) || \
10         defined(CONFIG_TARGET_APALIS_IMX8) || \
11         defined(CONFIG_TARGET_APALIS_IMX8X) || \
12         defined(CONFIG_TARGET_COLIBRI_IMX6) || \
13         defined(CONFIG_TARGET_COLIBRI_IMX8X)
14 #include <asm/arch/sys_proto.h>
15 #else
16 #define is_cpu_type(cpu) (0)
17 #endif
18 #if defined(CONFIG_CPU_PXA27X)
19 #include <asm/arch-pxa/pxa.h>
20 #else
21 #define cpu_is_pxa27x(cpu) (0)
22 #endif
23 #include <cli.h>
24 #include <console.h>
25 #include <env.h>
26 #include <flash.h>
27 #include <malloc.h>
28 #include <mmc.h>
29 #include <nand.h>
30 #include <asm/mach-types.h>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 #define TAG_VALID       0xcf01
35 #define TAG_MAC         0x0000
36 #define TAG_HW          0x0008
37 #define TAG_INVALID     0xffff
38
39 #define TAG_FLAG_VALID  0x1
40
41 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
42 #define TDX_CFG_BLOCK_MAX_SIZE 512
43 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
44 #define TDX_CFG_BLOCK_MAX_SIZE 64
45 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
46 #define TDX_CFG_BLOCK_MAX_SIZE 64
47 #else
48 #error Toradex config block location not set
49 #endif
50
51 struct toradex_tag {
52         u32 len:14;
53         u32 flags:2;
54         u32 id:16;
55 };
56
57 bool valid_cfgblock;
58 struct toradex_hw tdx_hw_tag;
59 struct toradex_eth_addr tdx_eth_addr;
60 u32 tdx_serial;
61
62 const char * const toradex_modules[] = {
63          [0] = "UNKNOWN MODULE",
64          [1] = "Colibri PXA270 312MHz",
65          [2] = "Colibri PXA270 520MHz",
66          [3] = "Colibri PXA320 806MHz",
67          [4] = "Colibri PXA300 208MHz",
68          [5] = "Colibri PXA310 624MHz",
69          [6] = "Colibri PXA320 806MHz IT",
70          [7] = "Colibri PXA300 208MHz XT",
71          [8] = "Colibri PXA270 312MHz",
72          [9] = "Colibri PXA270 520MHz",
73         [10] = "Colibri VF50 128MB", /* not currently on sale */
74         [11] = "Colibri VF61 256MB",
75         [12] = "Colibri VF61 256MB IT",
76         [13] = "Colibri VF50 128MB IT",
77         [14] = "Colibri iMX6 Solo 256MB",
78         [15] = "Colibri iMX6 DualLite 512MB",
79         [16] = "Colibri iMX6 Solo 256MB IT",
80         [17] = "Colibri iMX6 DualLite 512MB IT",
81         [18] = "UNKNOWN MODULE",
82         [19] = "UNKNOWN MODULE",
83         [20] = "Colibri T20 256MB",
84         [21] = "Colibri T20 512MB",
85         [22] = "Colibri T20 512MB IT",
86         [23] = "Colibri T30 1GB",
87         [24] = "Colibri T20 256MB IT",
88         [25] = "Apalis T30 2GB",
89         [26] = "Apalis T30 1GB",
90         [27] = "Apalis iMX6 Quad 1GB",
91         [28] = "Apalis iMX6 Quad 2GB IT",
92         [29] = "Apalis iMX6 Dual 512MB",
93         [30] = "Colibri T30 1GB IT",
94         [31] = "Apalis T30 1GB IT",
95         [32] = "Colibri iMX7 Solo 256MB",
96         [33] = "Colibri iMX7 Dual 512MB",
97         [34] = "Apalis TK1 2GB",
98         [35] = "Apalis iMX6 Dual 1GB IT",
99         [36] = "Colibri iMX6ULL 256MB",
100         [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
101         [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
102         [39] = "Colibri iMX7 Dual 1GB (eMMC)",
103         [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
104         [41] = "Colibri iMX7 Dual 512MB EPDC",
105         [42] = "Apalis TK1 4GB",
106         [43] = "Colibri T20 512MB IT SETEK",
107         [44] = "Colibri iMX6ULL 512MB IT",
108         [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
109         [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
110         [47] = "Apalis iMX8 QuadMax 4GB IT",
111         [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
112         [49] = "Apalis iMX8 QuadPlus 2GB",
113         [50] = "Colibri iMX8 QuadXPlus 2GB IT",
114         [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
115         [52] = "Colibri iMX8 DualX 1GB",
116         [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
117         [54] = "Apalis iMX8 DualXPlus 1GB",
118 };
119
120 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
121 static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
122 {
123         struct mmc *mmc;
124         int dev = CONFIG_TDX_CFG_BLOCK_DEV;
125         int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
126         uint part = CONFIG_TDX_CFG_BLOCK_PART;
127         uint blk_start;
128         int ret = 0;
129
130         /* Read production parameter config block from eMMC */
131         mmc = find_mmc_device(dev);
132         if (!mmc) {
133                 puts("No MMC card found\n");
134                 ret = -ENODEV;
135                 goto out;
136         }
137         if (mmc_init(mmc)) {
138                 puts("MMC init failed\n");
139                 return -EINVAL;
140         }
141         if (part != mmc_get_blk_desc(mmc)->hwpart) {
142                 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
143                         puts("MMC partition switch failed\n");
144                         ret = -ENODEV;
145                         goto out;
146                 }
147         }
148         if (offset < 0)
149                 offset += mmc->capacity;
150         blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
151
152         if (!write) {
153                 /* Careful reads a whole block of 512 bytes into config_block */
154                 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
155                               (unsigned char *)config_block) != 1) {
156                         ret = -EIO;
157                         goto out;
158                 }
159         } else {
160                 /* Just writing one 512 byte block */
161                 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
162                                (unsigned char *)config_block) != 1) {
163                         ret = -EIO;
164                         goto out;
165                 }
166         }
167
168 out:
169         /* Switch back to regular eMMC user partition */
170         blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
171
172         return ret;
173 }
174 #endif
175
176 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
177 static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
178 {
179         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
180         struct mtd_info *mtd = get_nand_dev_by_index(0);
181
182         if (!mtd)
183                 return -ENODEV;
184
185         /* Read production parameter config block from NAND page */
186         return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
187                                   &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
188                                   config_block);
189 }
190
191 static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
192 {
193         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
194
195         /* Write production parameter config block to NAND page */
196         return nand_write_skip_bad(get_nand_dev_by_index(0),
197                                    CONFIG_TDX_CFG_BLOCK_OFFSET,
198                                    &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
199                                    config_block, WITH_WR_VERIFY);
200 }
201 #endif
202
203 #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
204 static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
205 {
206         /* Read production parameter config block from NOR flash */
207         memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
208                TDX_CFG_BLOCK_MAX_SIZE);
209         return 0;
210 }
211
212 static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
213 {
214         /* Write production parameter config block to NOR flash */
215         return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
216                            TDX_CFG_BLOCK_MAX_SIZE);
217 }
218 #endif
219
220 int read_tdx_cfg_block(void)
221 {
222         int ret = 0;
223         u8 *config_block = NULL;
224         struct toradex_tag *tag;
225         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
226         int offset;
227
228         /* Allocate RAM area for config block */
229         config_block = memalign(ARCH_DMA_MINALIGN, size);
230         if (!config_block) {
231                 printf("Not enough malloc space available!\n");
232                 return -ENOMEM;
233         }
234
235         memset(config_block, 0, size);
236
237 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
238         ret = tdx_cfg_block_mmc_storage(config_block, 0);
239 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
240         ret = read_tdx_cfg_block_from_nand(config_block);
241 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
242         ret = read_tdx_cfg_block_from_nor(config_block);
243 #else
244         ret = -EINVAL;
245 #endif
246         if (ret)
247                 goto out;
248
249         /* Expect a valid tag first */
250         tag = (struct toradex_tag *)config_block;
251         if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
252                 valid_cfgblock = false;
253                 ret = -EINVAL;
254                 goto out;
255         }
256         valid_cfgblock = true;
257         offset = 4;
258
259         while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
260                 tag = (struct toradex_tag *)(config_block + offset);
261                 offset += 4;
262                 if (tag->id == TAG_INVALID)
263                         break;
264
265                 if (tag->flags == TAG_FLAG_VALID) {
266                         switch (tag->id) {
267                         case TAG_MAC:
268                                 memcpy(&tdx_eth_addr, config_block + offset,
269                                        6);
270
271                                 /* NIC part of MAC address is serial number */
272                                 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
273                                 break;
274                         case TAG_HW:
275                                 memcpy(&tdx_hw_tag, config_block + offset, 8);
276                                 break;
277                         }
278                 }
279
280                 /* Get to next tag according to current tags length */
281                 offset += tag->len * 4;
282         }
283
284         /* Cap product id to avoid issues with a yet unknown one */
285         if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
286                                   sizeof(toradex_modules[0])))
287                 tdx_hw_tag.prodid = 0;
288
289 out:
290         free(config_block);
291         return ret;
292 }
293
294 static int get_cfgblock_interactive(void)
295 {
296         char message[CONFIG_SYS_CBSIZE];
297         char *soc;
298         char it = 'n';
299         char wb = 'n';
300         int len;
301
302         /* Unknown module by default */
303         tdx_hw_tag.prodid = 0;
304
305         if (cpu_is_pxa27x())
306                 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
307         else
308                 sprintf(message, "Is the module an IT version? [y/N] ");
309         len = cli_readline(message);
310         it = console_buffer[0];
311
312 #if defined(CONFIG_TARGET_APALIS_IMX8) || \
313                 defined(CONFIG_TARGET_APALIS_IMX8X) || \
314                 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
315                 defined(CONFIG_TARGET_COLIBRI_IMX8X)
316         sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
317         len = cli_readline(message);
318         wb = console_buffer[0];
319 #endif
320
321         soc = env_get("soc");
322         if (!strcmp("mx6", soc)) {
323 #ifdef CONFIG_TARGET_APALIS_IMX6
324                 if (it == 'y' || it == 'Y') {
325                         if (is_cpu_type(MXC_CPU_MX6Q))
326                                 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
327                         else
328                                 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
329                 } else {
330                         if (is_cpu_type(MXC_CPU_MX6Q))
331                                 tdx_hw_tag.prodid = APALIS_IMX6Q;
332                         else
333                                 tdx_hw_tag.prodid = APALIS_IMX6D;
334                 }
335 #elif CONFIG_TARGET_COLIBRI_IMX6
336                 if (it == 'y' || it == 'Y') {
337                         if (is_cpu_type(MXC_CPU_MX6DL))
338                                 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
339                         else if (is_cpu_type(MXC_CPU_MX6SOLO))
340                                 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
341                 } else {
342                         if (is_cpu_type(MXC_CPU_MX6DL))
343                                 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
344                         else if (is_cpu_type(MXC_CPU_MX6SOLO))
345                                 tdx_hw_tag.prodid = COLIBRI_IMX6S;
346                 }
347 #elif CONFIG_TARGET_COLIBRI_IMX6ULL
348                 if (it == 'y' || it == 'Y') {
349                         if (wb == 'y' || wb == 'Y')
350                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
351                         else
352                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
353                 } else {
354                         if (wb == 'y' || wb == 'Y')
355                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
356                         else
357                                 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
358                 }
359 #endif
360         } else if (!strcmp("imx7d", soc))
361                 tdx_hw_tag.prodid = COLIBRI_IMX7D;
362         else if (!strcmp("imx7s", soc))
363                 tdx_hw_tag.prodid = COLIBRI_IMX7S;
364         else if (is_cpu_type(MXC_CPU_IMX8QM)) {
365                 if (it == 'y' || it == 'Y') {
366                         if (wb == 'y' || wb == 'Y')
367                                 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
368                         else
369                                 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
370                 } else {
371                         if (wb == 'y' || wb == 'Y')
372                                 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
373                         else
374                                 tdx_hw_tag.prodid = APALIS_IMX8QP;
375                 }
376         } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
377 #ifdef CONFIG_TARGET_APALIS_IMX8X
378                 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
379                                 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
380                 } else {
381                         if (gd->ram_size == 0x40000000)
382                                 tdx_hw_tag.prodid = APALIS_IMX8DXP;
383                         else
384                                 tdx_hw_tag.prodid = APALIS_IMX8QXP;
385                 }
386 #elif CONFIG_TARGET_COLIBRI_IMX8X
387                 if (it == 'y' || it == 'Y') {
388                         if (wb == 'y' || wb == 'Y')
389                                 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
390                         else
391                                 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
392                 } else {
393                         if (wb == 'y' || wb == 'Y')
394                                 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
395                         else
396                                 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
397                 }
398 #endif
399         } else if (!strcmp("tegra20", soc)) {
400                 if (it == 'y' || it == 'Y')
401                         if (gd->ram_size == 0x10000000)
402                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
403                         else
404                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
405                 else
406                         if (gd->ram_size == 0x10000000)
407                                 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
408                         else
409                                 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
410         } else if (cpu_is_pxa27x()) {
411                 if (it == 'y' || it == 'Y')
412                         tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
413                 else
414                         tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
415         }
416 #ifdef CONFIG_MACH_TYPE
417         else if (!strcmp("tegra30", soc)) {
418                 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
419                         if (it == 'y' || it == 'Y')
420                                 tdx_hw_tag.prodid = APALIS_T30_IT;
421                         else
422                                 if (gd->ram_size == 0x40000000)
423                                         tdx_hw_tag.prodid = APALIS_T30_1GB;
424                                 else
425                                         tdx_hw_tag.prodid = APALIS_T30_2GB;
426                 } else {
427                         if (it == 'y' || it == 'Y')
428                                 tdx_hw_tag.prodid = COLIBRI_T30_IT;
429                         else
430                                 tdx_hw_tag.prodid = COLIBRI_T30;
431                 }
432         }
433 #endif /* CONFIG_MACH_TYPE */
434         else if (!strcmp("tegra124", soc)) {
435                 tdx_hw_tag.prodid = APALIS_TK1_2GB;
436         } else if (!strcmp("vf500", soc)) {
437                 if (it == 'y' || it == 'Y')
438                         tdx_hw_tag.prodid = COLIBRI_VF50_IT;
439                 else
440                         tdx_hw_tag.prodid = COLIBRI_VF50;
441         } else if (!strcmp("vf610", soc)) {
442                 if (it == 'y' || it == 'Y')
443                         tdx_hw_tag.prodid = COLIBRI_VF61_IT;
444                 else
445                         tdx_hw_tag.prodid = COLIBRI_VF61;
446         }
447
448         if (!tdx_hw_tag.prodid) {
449                 printf("Module type not detectable due to unknown SoC\n");
450                 return -1;
451         }
452
453         while (len < 4) {
454                 sprintf(message, "Enter the module version (e.g. V1.1B): V");
455                 len = cli_readline(message);
456         }
457
458         tdx_hw_tag.ver_major = console_buffer[0] - '0';
459         tdx_hw_tag.ver_minor = console_buffer[2] - '0';
460         tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
461
462         if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
463                 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
464                                        COLIBRI_PXA270_V1_312MHZ);
465
466         while (len < 8) {
467                 sprintf(message, "Enter module serial number: ");
468                 len = cli_readline(message);
469         }
470
471         tdx_serial = simple_strtoul(console_buffer, NULL, 10);
472
473         return 0;
474 }
475
476 static int get_cfgblock_barcode(char *barcode)
477 {
478         if (strlen(barcode) < 16) {
479                 printf("Argument too short, barcode is 16 chars long\n");
480                 return -1;
481         }
482
483         /* Get hardware information from the first 8 digits */
484         tdx_hw_tag.ver_major = barcode[4] - '0';
485         tdx_hw_tag.ver_minor = barcode[5] - '0';
486         tdx_hw_tag.ver_assembly = barcode[7] - '0';
487
488         barcode[4] = '\0';
489         tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
490
491         /* Parse second part of the barcode (serial number */
492         barcode += 8;
493         tdx_serial = simple_strtoul(barcode, NULL, 10);
494
495         return 0;
496 }
497
498 static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
499                               char * const argv[])
500 {
501         u8 *config_block;
502         struct toradex_tag *tag;
503         size_t size = TDX_CFG_BLOCK_MAX_SIZE;
504         int offset = 0;
505         int ret = CMD_RET_SUCCESS;
506         int err;
507         int force_overwrite = 0;
508
509         /* Allocate RAM area for config block */
510         config_block = memalign(ARCH_DMA_MINALIGN, size);
511         if (!config_block) {
512                 printf("Not enough malloc space available!\n");
513                 return CMD_RET_FAILURE;
514         }
515
516         memset(config_block, 0xff, size);
517
518         if (argc >= 3) {
519                 if (argv[2][0] == '-' && argv[2][1] == 'y')
520                         force_overwrite = 1;
521         }
522
523         read_tdx_cfg_block();
524         if (valid_cfgblock) {
525 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
526                 /*
527                  * On NAND devices, recreation is only allowed if the page is
528                  * empty (config block invalid...)
529                  */
530                 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
531                        CONFIG_TDX_CFG_BLOCK_OFFSET /
532                        get_nand_dev_by_index(0)->erasesize);
533                 goto out;
534 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
535                 /*
536                  * On NOR devices, recreation is only allowed if the sector is
537                  * empty and write protection is off (config block invalid...)
538                  */
539                 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
540                        CONFIG_TDX_CFG_BLOCK_OFFSET);
541                 goto out;
542 #else
543                 if (!force_overwrite) {
544                         char message[CONFIG_SYS_CBSIZE];
545
546                         sprintf(message,
547                                 "A valid Toradex config block is present, still recreate? [y/N] ");
548
549                         if (!cli_readline(message))
550                                 goto out;
551
552                         if (console_buffer[0] != 'y' &&
553                             console_buffer[0] != 'Y')
554                                 goto out;
555                 }
556 #endif
557         }
558
559         /* Parse new Toradex config block data... */
560         if (argc < 3 || (force_overwrite && argc < 4)) {
561                 err = get_cfgblock_interactive();
562         } else {
563                 if (force_overwrite)
564                         err = get_cfgblock_barcode(argv[3]);
565                 else
566                         err = get_cfgblock_barcode(argv[2]);
567         }
568         if (err) {
569                 ret = CMD_RET_FAILURE;
570                 goto out;
571         }
572
573         /* Convert serial number to MAC address (the storage format) */
574         tdx_eth_addr.oui = htonl(0x00142dUL << 8);
575         tdx_eth_addr.nic = htonl(tdx_serial << 8);
576
577         /* Valid Tag */
578         tag = (struct toradex_tag *)config_block;
579         tag->id = TAG_VALID;
580         tag->flags = TAG_FLAG_VALID;
581         tag->len = 0;
582         offset += 4;
583
584         /* Product Tag */
585         tag = (struct toradex_tag *)(config_block + offset);
586         tag->id = TAG_HW;
587         tag->flags = TAG_FLAG_VALID;
588         tag->len = 2;
589         offset += 4;
590
591         memcpy(config_block + offset, &tdx_hw_tag, 8);
592         offset += 8;
593
594         /* MAC Tag */
595         tag = (struct toradex_tag *)(config_block + offset);
596         tag->id = TAG_MAC;
597         tag->flags = TAG_FLAG_VALID;
598         tag->len = 2;
599         offset += 4;
600
601         memcpy(config_block + offset, &tdx_eth_addr, 6);
602         offset += 6;
603         memset(config_block + offset, 0, 32 - offset);
604
605 #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
606         err = tdx_cfg_block_mmc_storage(config_block, 1);
607 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
608         err = write_tdx_cfg_block_to_nand(config_block);
609 #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
610         err = write_tdx_cfg_block_to_nor(config_block);
611 #else
612         err = -EINVAL;
613 #endif
614         if (err) {
615                 printf("Failed to write Toradex config block: %d\n", ret);
616                 ret = CMD_RET_FAILURE;
617                 goto out;
618         }
619
620         printf("Toradex config block successfully written\n");
621
622 out:
623         free(config_block);
624         return ret;
625 }
626
627 static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
628                        char * const argv[])
629 {
630         int ret;
631
632         if (argc < 2)
633                 return CMD_RET_USAGE;
634
635         if (!strcmp(argv[1], "create")) {
636                 return do_cfgblock_create(cmdtp, flag, argc, argv);
637         } else if (!strcmp(argv[1], "reload")) {
638                 ret = read_tdx_cfg_block();
639                 if (ret) {
640                         printf("Failed to reload Toradex config block: %d\n",
641                                ret);
642                         return CMD_RET_FAILURE;
643                 }
644                 return CMD_RET_SUCCESS;
645         }
646
647         return CMD_RET_USAGE;
648 }
649
650 U_BOOT_CMD(cfgblock, 4, 0, do_cfgblock,
651            "Toradex config block handling commands",
652            "create [-y] [barcode] - (Re-)create Toradex config block\n"
653            "cfgblock reload - Reload Toradex config block from flash"
654 );