blob: 71ff40cfad6fc604e3ba5fd86d43177430a920c0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01002/*
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +02003 * Copyright (c) 2016-2019 Toradex, Inc.
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
7#include "tdx-cfg-block.h"
8
9#if defined(CONFIG_TARGET_APALIS_IMX6) || defined(CONFIG_TARGET_COLIBRI_IMX6)
10#include <asm/arch/sys_proto.h>
11#else
12#define is_cpu_type(cpu) (0)
13#endif
14#if defined(CONFIG_CPU_PXA27X)
15#include <asm/arch-pxa/pxa.h>
16#else
17#define cpu_is_pxa27x(cpu) (0)
18#endif
19#include <cli.h>
20#include <console.h>
21#include <flash.h>
22#include <malloc.h>
23#include <mmc.h>
24#include <nand.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060025#include <asm/mach-types.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010026
27DECLARE_GLOBAL_DATA_PTR;
28
29#define TAG_VALID 0xcf01
30#define TAG_MAC 0x0000
31#define TAG_HW 0x0008
32#define TAG_INVALID 0xffff
33
34#define TAG_FLAG_VALID 0x1
35
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#else
43#error Toradex config block location not set
44#endif
45
46struct toradex_tag {
47 u32 len:14;
48 u32 flags:2;
49 u32 id:16;
50};
51
52bool valid_cfgblock;
53struct toradex_hw tdx_hw_tag;
54struct toradex_eth_addr tdx_eth_addr;
55u32 tdx_serial;
56
57const char * const toradex_modules[] = {
58 [0] = "UNKNOWN MODULE",
59 [1] = "Colibri PXA270 312MHz",
60 [2] = "Colibri PXA270 520MHz",
61 [3] = "Colibri PXA320 806MHz",
62 [4] = "Colibri PXA300 208MHz",
63 [5] = "Colibri PXA310 624MHz",
64 [6] = "Colibri PXA320 806MHz IT",
65 [7] = "Colibri PXA300 208MHz XT",
66 [8] = "Colibri PXA270 312MHz",
67 [9] = "Colibri PXA270 520MHz",
68 [10] = "Colibri VF50 128MB", /* not currently on sale */
69 [11] = "Colibri VF61 256MB",
70 [12] = "Colibri VF61 256MB IT",
71 [13] = "Colibri VF50 128MB IT",
72 [14] = "Colibri iMX6 Solo 256MB",
73 [15] = "Colibri iMX6 DualLite 512MB",
74 [16] = "Colibri iMX6 Solo 256MB IT",
75 [17] = "Colibri iMX6 DualLite 512MB IT",
76 [18] = "UNKNOWN MODULE",
77 [19] = "UNKNOWN MODULE",
78 [20] = "Colibri T20 256MB",
79 [21] = "Colibri T20 512MB",
80 [22] = "Colibri T20 512MB IT",
81 [23] = "Colibri T30 1GB",
82 [24] = "Colibri T20 256MB IT",
83 [25] = "Apalis T30 2GB",
84 [26] = "Apalis T30 1GB",
85 [27] = "Apalis iMX6 Quad 1GB",
86 [28] = "Apalis iMX6 Quad 2GB IT",
87 [29] = "Apalis iMX6 Dual 512MB",
88 [30] = "Colibri T30 1GB IT",
89 [31] = "Apalis T30 1GB IT",
90 [32] = "Colibri iMX7 Solo 256MB",
91 [33] = "Colibri iMX7 Dual 512MB",
92 [34] = "Apalis TK1 2GB",
93 [35] = "Apalis iMX6 Dual 1GB IT",
Stefan Agner01875e92018-05-30 19:01:47 +020094 [36] = "Colibri iMX6ULL 256MB",
95 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / Bluetooth",
96 [38] = "Colibri iMX8X",
97 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
98 [40] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth IT",
99 [41] = "Colibri iMX7 Dual 512MB EPDC",
100 [42] = "Apalis TK1 4GB",
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200101 [43] = "Colibri T20 512MB IT SETEK",
102 [44] = "Colibri iMX6ULL 512MB IT",
103 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100104};
105
106#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
107static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
108{
109 struct mmc *mmc;
110 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
111 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
112 uint part = CONFIG_TDX_CFG_BLOCK_PART;
113 uint blk_start;
114 int ret = 0;
115
116 /* Read production parameter config block from eMMC */
117 mmc = find_mmc_device(dev);
118 if (!mmc) {
119 puts("No MMC card found\n");
120 ret = -ENODEV;
121 goto out;
122 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600123 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100124 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
125 puts("MMC partition switch failed\n");
126 ret = -ENODEV;
127 goto out;
128 }
129 }
130 if (offset < 0)
131 offset += mmc->capacity;
132 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
133
134 if (!write) {
135 /* Careful reads a whole block of 512 bytes into config_block */
136 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
137 (unsigned char *)config_block) != 1) {
138 ret = -EIO;
139 goto out;
140 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100141 } else {
142 /* Just writing one 512 byte block */
143 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
144 (unsigned char *)config_block) != 1) {
145 ret = -EIO;
146 goto out;
147 }
148 }
149
150out:
151 /* Switch back to regular eMMC user partition */
152 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
153
154 return ret;
155}
156#endif
157
158#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
159static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
160{
161 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200162 struct mtd_info *mtd = get_nand_dev_by_index(0);
163
164 if (!mtd)
165 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100166
167 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200168 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500169 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
170 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100171}
172
173static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
174{
175 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
176
177 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500178 return nand_write_skip_bad(get_nand_dev_by_index(0),
179 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100180 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
181 config_block, WITH_WR_VERIFY);
182}
183#endif
184
185#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
186static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
187{
188 /* Read production parameter config block from NOR flash */
189 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
190 TDX_CFG_BLOCK_MAX_SIZE);
191 return 0;
192}
193
194static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
195{
196 /* Write production parameter config block to NOR flash */
197 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
198 TDX_CFG_BLOCK_MAX_SIZE);
199}
200#endif
201
202int read_tdx_cfg_block(void)
203{
204 int ret = 0;
205 u8 *config_block = NULL;
206 struct toradex_tag *tag;
207 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
208 int offset;
209
210 /* Allocate RAM area for config block */
211 config_block = memalign(ARCH_DMA_MINALIGN, size);
212 if (!config_block) {
213 printf("Not enough malloc space available!\n");
214 return -ENOMEM;
215 }
216
217 memset(config_block, 0, size);
218
219#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
220 ret = tdx_cfg_block_mmc_storage(config_block, 0);
221#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
222 ret = read_tdx_cfg_block_from_nand(config_block);
223#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
224 ret = read_tdx_cfg_block_from_nor(config_block);
225#else
226 ret = -EINVAL;
227#endif
228 if (ret)
229 goto out;
230
231 /* Expect a valid tag first */
232 tag = (struct toradex_tag *)config_block;
233 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
234 valid_cfgblock = false;
235 ret = -EINVAL;
236 goto out;
237 }
238 valid_cfgblock = true;
239 offset = 4;
240
241 while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
242 tag = (struct toradex_tag *)(config_block + offset);
243 offset += 4;
244 if (tag->id == TAG_INVALID)
245 break;
246
247 if (tag->flags == TAG_FLAG_VALID) {
248 switch (tag->id) {
249 case TAG_MAC:
250 memcpy(&tdx_eth_addr, config_block + offset,
251 6);
252
253 /* NIC part of MAC address is serial number */
254 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
255 break;
256 case TAG_HW:
257 memcpy(&tdx_hw_tag, config_block + offset, 8);
258 break;
259 }
260 }
261
262 /* Get to next tag according to current tags length */
263 offset += tag->len * 4;
264 }
265
266 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswiler8c9127c2019-03-25 17:18:29 +0100267 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100268 sizeof(toradex_modules[0])))
269 tdx_hw_tag.prodid = 0;
270
271out:
272 free(config_block);
273 return ret;
274}
275
276static int get_cfgblock_interactive(void)
277{
278 char message[CONFIG_SYS_CBSIZE];
279 char *soc;
280 char it = 'n';
281 int len;
282
Stefan Agner68f58782019-04-09 17:24:08 +0200283 /* Unknown module by default */
284 tdx_hw_tag.prodid = 0;
285
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100286 if (cpu_is_pxa27x())
287 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
288 else
289 sprintf(message, "Is the module an IT version? [y/N] ");
290
291 len = cli_readline(message);
292 it = console_buffer[0];
293
Simon Glass64b723f2017-08-03 12:22:12 -0600294 soc = env_get("soc");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100295 if (!strcmp("mx6", soc)) {
Stefan Agner68f58782019-04-09 17:24:08 +0200296#ifdef CONFIG_TARGET_APALIS_IMX6
297 if (it == 'y' || it == 'Y') {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100298 if (is_cpu_type(MXC_CPU_MX6Q))
299 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
300 else
301 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200302 } else {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100303 if (is_cpu_type(MXC_CPU_MX6Q))
304 tdx_hw_tag.prodid = APALIS_IMX6Q;
305 else
306 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner68f58782019-04-09 17:24:08 +0200307 }
308#elif CONFIG_TARGET_COLIBRI_IMX6
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200309 if (it == 'y' || it == 'Y') {
Stefan Agner68f58782019-04-09 17:24:08 +0200310 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100311 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200312 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100313 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200314 } else {
Stefan Agner68f58782019-04-09 17:24:08 +0200315 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100316 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
Stefan Agner68f58782019-04-09 17:24:08 +0200317 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100318 tdx_hw_tag.prodid = COLIBRI_IMX6S;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200319 }
Stefan Agner68f58782019-04-09 17:24:08 +0200320#elif CONFIG_TARGET_COLIBRI_IMX6ULL
321 char wb = 'n';
322
323 sprintf(message, "Does the module have Wi-Fi / Bluetooth? " \
324 "[y/N] ");
325 len = cli_readline(message);
326 wb = console_buffer[0];
327 if (it == 'y' || it == 'Y') {
328 if (wb == 'y' || wb == 'Y')
329 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
330 else
331 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
332 } else {
333 if (wb == 'y' || wb == 'Y')
334 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
335 else
336 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
337 }
338#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200339 } else if (!strcmp("imx7d", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100340 tdx_hw_tag.prodid = COLIBRI_IMX7D;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200341 else if (!strcmp("imx7s", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100342 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200343 else if (!strcmp("tegra20", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100344 if (it == 'y' || it == 'Y')
345 if (gd->ram_size == 0x10000000)
346 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
347 else
348 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
349 else
350 if (gd->ram_size == 0x10000000)
351 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
352 else
353 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
354 } else if (cpu_is_pxa27x()) {
355 if (it == 'y' || it == 'Y')
356 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
357 else
358 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200359 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100360#ifdef CONFIG_MACH_TYPE
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200361 else if (!strcmp("tegra30", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100362 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
363 if (it == 'y' || it == 'Y')
364 tdx_hw_tag.prodid = APALIS_T30_IT;
365 else
366 if (gd->ram_size == 0x40000000)
367 tdx_hw_tag.prodid = APALIS_T30_1GB;
368 else
369 tdx_hw_tag.prodid = APALIS_T30_2GB;
370 } else {
371 if (it == 'y' || it == 'Y')
372 tdx_hw_tag.prodid = COLIBRI_T30_IT;
373 else
374 tdx_hw_tag.prodid = COLIBRI_T30;
375 }
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200376 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100377#endif /* CONFIG_MACH_TYPE */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200378 else if (!strcmp("tegra124", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100379 tdx_hw_tag.prodid = APALIS_TK1_2GB;
380 } else if (!strcmp("vf500", soc)) {
381 if (it == 'y' || it == 'Y')
382 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
383 else
384 tdx_hw_tag.prodid = COLIBRI_VF50;
385 } else if (!strcmp("vf610", soc)) {
386 if (it == 'y' || it == 'Y')
387 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
388 else
389 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner68f58782019-04-09 17:24:08 +0200390 }
391
392 if (!tdx_hw_tag.prodid) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100393 printf("Module type not detectable due to unknown SoC\n");
394 return -1;
395 }
396
397 while (len < 4) {
398 sprintf(message, "Enter the module version (e.g. V1.1B): V");
399 len = cli_readline(message);
400 }
401
402 tdx_hw_tag.ver_major = console_buffer[0] - '0';
403 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
404 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
405
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200406 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100407 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
408 COLIBRI_PXA270_V1_312MHZ);
409
410 while (len < 8) {
411 sprintf(message, "Enter module serial number: ");
412 len = cli_readline(message);
413 }
414
415 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
416
417 return 0;
418}
419
420static int get_cfgblock_barcode(char *barcode)
421{
422 if (strlen(barcode) < 16) {
423 printf("Argument too short, barcode is 16 chars long\n");
424 return -1;
425 }
426
427 /* Get hardware information from the first 8 digits */
428 tdx_hw_tag.ver_major = barcode[4] - '0';
429 tdx_hw_tag.ver_minor = barcode[5] - '0';
430 tdx_hw_tag.ver_assembly = barcode[7] - '0';
431
432 barcode[4] = '\0';
433 tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
434
435 /* Parse second part of the barcode (serial number */
436 barcode += 8;
437 tdx_serial = simple_strtoul(barcode, NULL, 10);
438
439 return 0;
440}
441
442static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
443 char * const argv[])
444{
445 u8 *config_block;
446 struct toradex_tag *tag;
447 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
448 int offset = 0;
449 int ret = CMD_RET_SUCCESS;
450 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100451 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100452
453 /* Allocate RAM area for config block */
454 config_block = memalign(ARCH_DMA_MINALIGN, size);
455 if (!config_block) {
456 printf("Not enough malloc space available!\n");
457 return CMD_RET_FAILURE;
458 }
459
460 memset(config_block, 0xff, size);
461
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100462 if (argc >= 3) {
463 if (argv[2][0] == '-' && argv[2][1] == 'y')
464 force_overwrite = 1;
465 }
466
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100467 read_tdx_cfg_block();
468 if (valid_cfgblock) {
469#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
470 /*
471 * On NAND devices, recreation is only allowed if the page is
472 * empty (config block invalid...)
473 */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200474 printf("NAND erase block %d need to be erased before creating" \
475 " a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500476 CONFIG_TDX_CFG_BLOCK_OFFSET /
477 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100478 goto out;
479#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
480 /*
481 * On NOR devices, recreation is only allowed if the sector is
482 * empty and write protection is off (config block invalid...)
483 */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200484 printf("NOR sector at offset 0x%02x need to be erased and " \
485 "unprotected before creating a Toradex config block\n",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100486 CONFIG_TDX_CFG_BLOCK_OFFSET);
487 goto out;
488#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100489 if (!force_overwrite) {
490 char message[CONFIG_SYS_CBSIZE];
491
492 sprintf(message,
493 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100494
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100495 if (!cli_readline(message))
496 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100497
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100498 if (console_buffer[0] != 'y' &&
499 console_buffer[0] != 'Y')
500 goto out;
501 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100502#endif
503 }
504
505 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100506 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100507 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100508 } else {
509 if (force_overwrite)
510 err = get_cfgblock_barcode(argv[3]);
511 else
512 err = get_cfgblock_barcode(argv[2]);
513 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100514 if (err) {
515 ret = CMD_RET_FAILURE;
516 goto out;
517 }
518
519 /* Convert serial number to MAC address (the storage format) */
520 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
521 tdx_eth_addr.nic = htonl(tdx_serial << 8);
522
523 /* Valid Tag */
524 tag = (struct toradex_tag *)config_block;
525 tag->id = TAG_VALID;
526 tag->flags = TAG_FLAG_VALID;
527 tag->len = 0;
528 offset += 4;
529
530 /* Product Tag */
531 tag = (struct toradex_tag *)(config_block + offset);
532 tag->id = TAG_HW;
533 tag->flags = TAG_FLAG_VALID;
534 tag->len = 2;
535 offset += 4;
536
537 memcpy(config_block + offset, &tdx_hw_tag, 8);
538 offset += 8;
539
540 /* MAC Tag */
541 tag = (struct toradex_tag *)(config_block + offset);
542 tag->id = TAG_MAC;
543 tag->flags = TAG_FLAG_VALID;
544 tag->len = 2;
545 offset += 4;
546
547 memcpy(config_block + offset, &tdx_eth_addr, 6);
548 offset += 6;
549 memset(config_block + offset, 0, 32 - offset);
550
551#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
552 err = tdx_cfg_block_mmc_storage(config_block, 1);
553#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
554 err = write_tdx_cfg_block_to_nand(config_block);
555#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
556 err = write_tdx_cfg_block_to_nor(config_block);
557#else
558 err = -EINVAL;
559#endif
560 if (err) {
561 printf("Failed to write Toradex config block: %d\n", ret);
562 ret = CMD_RET_FAILURE;
563 goto out;
564 }
565
566 printf("Toradex config block successfully written\n");
567
568out:
569 free(config_block);
570 return ret;
571}
572
573static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
574 char * const argv[])
575{
576 int ret;
577
578 if (argc < 2)
579 return CMD_RET_USAGE;
580
581 if (!strcmp(argv[1], "create")) {
582 return do_cfgblock_create(cmdtp, flag, argc, argv);
583 } else if (!strcmp(argv[1], "reload")) {
584 ret = read_tdx_cfg_block();
585 if (ret) {
586 printf("Failed to reload Toradex config block: %d\n",
587 ret);
588 return CMD_RET_FAILURE;
589 }
590 return CMD_RET_SUCCESS;
591 }
592
593 return CMD_RET_USAGE;
594}
595
596U_BOOT_CMD(
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100597 cfgblock, 4, 0, do_cfgblock,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100598 "Toradex config block handling commands",
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100599 "create [-y] [barcode] - (Re-)create Toradex config block\n"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100600 "cfgblock reload - Reload Toradex config block from flash"
601);