blob: f69c4433b24376398724e155cf38e3677e278888 [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
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +02009#if defined(CONFIG_TARGET_APALIS_IMX6) || \
10 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
11 defined(CONFIG_TARGET_COLIBRI_IMX8QXP)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010012#include <asm/arch/sys_proto.h>
13#else
14#define is_cpu_type(cpu) (0)
15#endif
16#if defined(CONFIG_CPU_PXA27X)
17#include <asm/arch-pxa/pxa.h>
18#else
19#define cpu_is_pxa27x(cpu) (0)
20#endif
21#include <cli.h>
22#include <console.h>
23#include <flash.h>
24#include <malloc.h>
25#include <mmc.h>
26#include <nand.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060027#include <asm/mach-types.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010028
29DECLARE_GLOBAL_DATA_PTR;
30
31#define TAG_VALID 0xcf01
32#define TAG_MAC 0x0000
33#define TAG_HW 0x0008
34#define TAG_INVALID 0xffff
35
36#define TAG_FLAG_VALID 0x1
37
38#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
39#define TDX_CFG_BLOCK_MAX_SIZE 512
40#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
41#define TDX_CFG_BLOCK_MAX_SIZE 64
42#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
43#define TDX_CFG_BLOCK_MAX_SIZE 64
44#else
45#error Toradex config block location not set
46#endif
47
48struct toradex_tag {
49 u32 len:14;
50 u32 flags:2;
51 u32 id:16;
52};
53
54bool valid_cfgblock;
55struct toradex_hw tdx_hw_tag;
56struct toradex_eth_addr tdx_eth_addr;
57u32 tdx_serial;
58
59const char * const toradex_modules[] = {
60 [0] = "UNKNOWN MODULE",
61 [1] = "Colibri PXA270 312MHz",
62 [2] = "Colibri PXA270 520MHz",
63 [3] = "Colibri PXA320 806MHz",
64 [4] = "Colibri PXA300 208MHz",
65 [5] = "Colibri PXA310 624MHz",
66 [6] = "Colibri PXA320 806MHz IT",
67 [7] = "Colibri PXA300 208MHz XT",
68 [8] = "Colibri PXA270 312MHz",
69 [9] = "Colibri PXA270 520MHz",
70 [10] = "Colibri VF50 128MB", /* not currently on sale */
71 [11] = "Colibri VF61 256MB",
72 [12] = "Colibri VF61 256MB IT",
73 [13] = "Colibri VF50 128MB IT",
74 [14] = "Colibri iMX6 Solo 256MB",
75 [15] = "Colibri iMX6 DualLite 512MB",
76 [16] = "Colibri iMX6 Solo 256MB IT",
77 [17] = "Colibri iMX6 DualLite 512MB IT",
78 [18] = "UNKNOWN MODULE",
79 [19] = "UNKNOWN MODULE",
80 [20] = "Colibri T20 256MB",
81 [21] = "Colibri T20 512MB",
82 [22] = "Colibri T20 512MB IT",
83 [23] = "Colibri T30 1GB",
84 [24] = "Colibri T20 256MB IT",
85 [25] = "Apalis T30 2GB",
86 [26] = "Apalis T30 1GB",
87 [27] = "Apalis iMX6 Quad 1GB",
88 [28] = "Apalis iMX6 Quad 2GB IT",
89 [29] = "Apalis iMX6 Dual 512MB",
90 [30] = "Colibri T30 1GB IT",
91 [31] = "Apalis T30 1GB IT",
92 [32] = "Colibri iMX7 Solo 256MB",
93 [33] = "Colibri iMX7 Dual 512MB",
94 [34] = "Apalis TK1 2GB",
95 [35] = "Apalis iMX6 Dual 1GB IT",
Stefan Agner01875e92018-05-30 19:01:47 +020096 [36] = "Colibri iMX6ULL 256MB",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +020097 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
98 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +020099 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200100 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200101 [41] = "Colibri iMX7 Dual 512MB EPDC",
102 [42] = "Apalis TK1 4GB",
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200103 [43] = "Colibri T20 512MB IT SETEK",
104 [44] = "Colibri iMX6ULL 512MB IT",
105 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
Marcel Ziswilerf60ffec2019-04-09 17:25:33 +0200106 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
107 [47] = "Apalis iMX8 QuadMax 4GB IT",
108 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
109 [49] = "Apalis iMX8 QuadPlus 2GB",
110 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
111 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
112 [52] = "Colibri iMX8 DualX 1GB",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100113};
114
115#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
116static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
117{
118 struct mmc *mmc;
119 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
120 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
121 uint part = CONFIG_TDX_CFG_BLOCK_PART;
122 uint blk_start;
123 int ret = 0;
124
125 /* Read production parameter config block from eMMC */
126 mmc = find_mmc_device(dev);
127 if (!mmc) {
128 puts("No MMC card found\n");
129 ret = -ENODEV;
130 goto out;
131 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600132 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100133 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
134 puts("MMC partition switch failed\n");
135 ret = -ENODEV;
136 goto out;
137 }
138 }
139 if (offset < 0)
140 offset += mmc->capacity;
141 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
142
143 if (!write) {
144 /* Careful reads a whole block of 512 bytes into config_block */
145 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
146 (unsigned char *)config_block) != 1) {
147 ret = -EIO;
148 goto out;
149 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100150 } else {
151 /* Just writing one 512 byte block */
152 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
153 (unsigned char *)config_block) != 1) {
154 ret = -EIO;
155 goto out;
156 }
157 }
158
159out:
160 /* Switch back to regular eMMC user partition */
161 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
162
163 return ret;
164}
165#endif
166
167#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
168static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
169{
170 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200171 struct mtd_info *mtd = get_nand_dev_by_index(0);
172
173 if (!mtd)
174 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100175
176 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200177 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500178 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
179 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100180}
181
182static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
183{
184 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
185
186 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500187 return nand_write_skip_bad(get_nand_dev_by_index(0),
188 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100189 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
190 config_block, WITH_WR_VERIFY);
191}
192#endif
193
194#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
195static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
196{
197 /* Read production parameter config block from NOR flash */
198 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
199 TDX_CFG_BLOCK_MAX_SIZE);
200 return 0;
201}
202
203static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
204{
205 /* Write production parameter config block to NOR flash */
206 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
207 TDX_CFG_BLOCK_MAX_SIZE);
208}
209#endif
210
211int read_tdx_cfg_block(void)
212{
213 int ret = 0;
214 u8 *config_block = NULL;
215 struct toradex_tag *tag;
216 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
217 int offset;
218
219 /* Allocate RAM area for config block */
220 config_block = memalign(ARCH_DMA_MINALIGN, size);
221 if (!config_block) {
222 printf("Not enough malloc space available!\n");
223 return -ENOMEM;
224 }
225
226 memset(config_block, 0, size);
227
228#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
229 ret = tdx_cfg_block_mmc_storage(config_block, 0);
230#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
231 ret = read_tdx_cfg_block_from_nand(config_block);
232#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
233 ret = read_tdx_cfg_block_from_nor(config_block);
234#else
235 ret = -EINVAL;
236#endif
237 if (ret)
238 goto out;
239
240 /* Expect a valid tag first */
241 tag = (struct toradex_tag *)config_block;
242 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
243 valid_cfgblock = false;
244 ret = -EINVAL;
245 goto out;
246 }
247 valid_cfgblock = true;
248 offset = 4;
249
250 while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
251 tag = (struct toradex_tag *)(config_block + offset);
252 offset += 4;
253 if (tag->id == TAG_INVALID)
254 break;
255
256 if (tag->flags == TAG_FLAG_VALID) {
257 switch (tag->id) {
258 case TAG_MAC:
259 memcpy(&tdx_eth_addr, config_block + offset,
260 6);
261
262 /* NIC part of MAC address is serial number */
263 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
264 break;
265 case TAG_HW:
266 memcpy(&tdx_hw_tag, config_block + offset, 8);
267 break;
268 }
269 }
270
271 /* Get to next tag according to current tags length */
272 offset += tag->len * 4;
273 }
274
275 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswiler8c9127c2019-03-25 17:18:29 +0100276 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100277 sizeof(toradex_modules[0])))
278 tdx_hw_tag.prodid = 0;
279
280out:
281 free(config_block);
282 return ret;
283}
284
285static int get_cfgblock_interactive(void)
286{
287 char message[CONFIG_SYS_CBSIZE];
288 char *soc;
289 char it = 'n';
290 int len;
291
Stefan Agner68f58782019-04-09 17:24:08 +0200292 /* Unknown module by default */
293 tdx_hw_tag.prodid = 0;
294
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100295 if (cpu_is_pxa27x())
296 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
297 else
298 sprintf(message, "Is the module an IT version? [y/N] ");
299
300 len = cli_readline(message);
301 it = console_buffer[0];
302
Simon Glass64b723f2017-08-03 12:22:12 -0600303 soc = env_get("soc");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100304 if (!strcmp("mx6", soc)) {
Stefan Agner68f58782019-04-09 17:24:08 +0200305#ifdef CONFIG_TARGET_APALIS_IMX6
306 if (it == 'y' || it == 'Y') {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100307 if (is_cpu_type(MXC_CPU_MX6Q))
308 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
309 else
310 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200311 } else {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100312 if (is_cpu_type(MXC_CPU_MX6Q))
313 tdx_hw_tag.prodid = APALIS_IMX6Q;
314 else
315 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner68f58782019-04-09 17:24:08 +0200316 }
317#elif CONFIG_TARGET_COLIBRI_IMX6
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200318 if (it == 'y' || it == 'Y') {
Stefan Agner68f58782019-04-09 17:24:08 +0200319 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100320 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200321 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100322 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200323 } else {
Stefan Agner68f58782019-04-09 17:24:08 +0200324 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100325 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
Stefan Agner68f58782019-04-09 17:24:08 +0200326 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100327 tdx_hw_tag.prodid = COLIBRI_IMX6S;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200328 }
Stefan Agner68f58782019-04-09 17:24:08 +0200329#elif CONFIG_TARGET_COLIBRI_IMX6ULL
330 char wb = 'n';
331
332 sprintf(message, "Does the module have Wi-Fi / Bluetooth? " \
333 "[y/N] ");
334 len = cli_readline(message);
335 wb = console_buffer[0];
336 if (it == 'y' || it == 'Y') {
337 if (wb == 'y' || wb == 'Y')
338 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
339 else
340 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
341 } else {
342 if (wb == 'y' || wb == 'Y')
343 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
344 else
345 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
346 }
347#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200348 } else if (!strcmp("imx7d", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100349 tdx_hw_tag.prodid = COLIBRI_IMX7D;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200350 else if (!strcmp("imx7s", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100351 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200352 else if (is_cpu_type(MXC_CPU_IMX8QXP))
353 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200354 else if (!strcmp("tegra20", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100355 if (it == 'y' || it == 'Y')
356 if (gd->ram_size == 0x10000000)
357 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
358 else
359 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
360 else
361 if (gd->ram_size == 0x10000000)
362 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
363 else
364 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
365 } else if (cpu_is_pxa27x()) {
366 if (it == 'y' || it == 'Y')
367 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
368 else
369 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200370 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100371#ifdef CONFIG_MACH_TYPE
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200372 else if (!strcmp("tegra30", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100373 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
374 if (it == 'y' || it == 'Y')
375 tdx_hw_tag.prodid = APALIS_T30_IT;
376 else
377 if (gd->ram_size == 0x40000000)
378 tdx_hw_tag.prodid = APALIS_T30_1GB;
379 else
380 tdx_hw_tag.prodid = APALIS_T30_2GB;
381 } else {
382 if (it == 'y' || it == 'Y')
383 tdx_hw_tag.prodid = COLIBRI_T30_IT;
384 else
385 tdx_hw_tag.prodid = COLIBRI_T30;
386 }
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200387 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100388#endif /* CONFIG_MACH_TYPE */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200389 else if (!strcmp("tegra124", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100390 tdx_hw_tag.prodid = APALIS_TK1_2GB;
391 } else if (!strcmp("vf500", soc)) {
392 if (it == 'y' || it == 'Y')
393 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
394 else
395 tdx_hw_tag.prodid = COLIBRI_VF50;
396 } else if (!strcmp("vf610", soc)) {
397 if (it == 'y' || it == 'Y')
398 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
399 else
400 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner68f58782019-04-09 17:24:08 +0200401 }
402
403 if (!tdx_hw_tag.prodid) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100404 printf("Module type not detectable due to unknown SoC\n");
405 return -1;
406 }
407
408 while (len < 4) {
409 sprintf(message, "Enter the module version (e.g. V1.1B): V");
410 len = cli_readline(message);
411 }
412
413 tdx_hw_tag.ver_major = console_buffer[0] - '0';
414 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
415 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
416
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200417 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100418 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
419 COLIBRI_PXA270_V1_312MHZ);
420
421 while (len < 8) {
422 sprintf(message, "Enter module serial number: ");
423 len = cli_readline(message);
424 }
425
426 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
427
428 return 0;
429}
430
431static int get_cfgblock_barcode(char *barcode)
432{
433 if (strlen(barcode) < 16) {
434 printf("Argument too short, barcode is 16 chars long\n");
435 return -1;
436 }
437
438 /* Get hardware information from the first 8 digits */
439 tdx_hw_tag.ver_major = barcode[4] - '0';
440 tdx_hw_tag.ver_minor = barcode[5] - '0';
441 tdx_hw_tag.ver_assembly = barcode[7] - '0';
442
443 barcode[4] = '\0';
444 tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
445
446 /* Parse second part of the barcode (serial number */
447 barcode += 8;
448 tdx_serial = simple_strtoul(barcode, NULL, 10);
449
450 return 0;
451}
452
453static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
454 char * const argv[])
455{
456 u8 *config_block;
457 struct toradex_tag *tag;
458 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
459 int offset = 0;
460 int ret = CMD_RET_SUCCESS;
461 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100462 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100463
464 /* Allocate RAM area for config block */
465 config_block = memalign(ARCH_DMA_MINALIGN, size);
466 if (!config_block) {
467 printf("Not enough malloc space available!\n");
468 return CMD_RET_FAILURE;
469 }
470
471 memset(config_block, 0xff, size);
472
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100473 if (argc >= 3) {
474 if (argv[2][0] == '-' && argv[2][1] == 'y')
475 force_overwrite = 1;
476 }
477
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100478 read_tdx_cfg_block();
479 if (valid_cfgblock) {
480#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
481 /*
482 * On NAND devices, recreation is only allowed if the page is
483 * empty (config block invalid...)
484 */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200485 printf("NAND erase block %d need to be erased before creating" \
486 " a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500487 CONFIG_TDX_CFG_BLOCK_OFFSET /
488 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100489 goto out;
490#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
491 /*
492 * On NOR devices, recreation is only allowed if the sector is
493 * empty and write protection is off (config block invalid...)
494 */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200495 printf("NOR sector at offset 0x%02x need to be erased and " \
496 "unprotected before creating a Toradex config block\n",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100497 CONFIG_TDX_CFG_BLOCK_OFFSET);
498 goto out;
499#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100500 if (!force_overwrite) {
501 char message[CONFIG_SYS_CBSIZE];
502
503 sprintf(message,
504 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100505
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100506 if (!cli_readline(message))
507 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100508
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100509 if (console_buffer[0] != 'y' &&
510 console_buffer[0] != 'Y')
511 goto out;
512 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100513#endif
514 }
515
516 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100517 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100518 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100519 } else {
520 if (force_overwrite)
521 err = get_cfgblock_barcode(argv[3]);
522 else
523 err = get_cfgblock_barcode(argv[2]);
524 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100525 if (err) {
526 ret = CMD_RET_FAILURE;
527 goto out;
528 }
529
530 /* Convert serial number to MAC address (the storage format) */
531 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
532 tdx_eth_addr.nic = htonl(tdx_serial << 8);
533
534 /* Valid Tag */
535 tag = (struct toradex_tag *)config_block;
536 tag->id = TAG_VALID;
537 tag->flags = TAG_FLAG_VALID;
538 tag->len = 0;
539 offset += 4;
540
541 /* Product Tag */
542 tag = (struct toradex_tag *)(config_block + offset);
543 tag->id = TAG_HW;
544 tag->flags = TAG_FLAG_VALID;
545 tag->len = 2;
546 offset += 4;
547
548 memcpy(config_block + offset, &tdx_hw_tag, 8);
549 offset += 8;
550
551 /* MAC Tag */
552 tag = (struct toradex_tag *)(config_block + offset);
553 tag->id = TAG_MAC;
554 tag->flags = TAG_FLAG_VALID;
555 tag->len = 2;
556 offset += 4;
557
558 memcpy(config_block + offset, &tdx_eth_addr, 6);
559 offset += 6;
560 memset(config_block + offset, 0, 32 - offset);
561
562#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
563 err = tdx_cfg_block_mmc_storage(config_block, 1);
564#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
565 err = write_tdx_cfg_block_to_nand(config_block);
566#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
567 err = write_tdx_cfg_block_to_nor(config_block);
568#else
569 err = -EINVAL;
570#endif
571 if (err) {
572 printf("Failed to write Toradex config block: %d\n", ret);
573 ret = CMD_RET_FAILURE;
574 goto out;
575 }
576
577 printf("Toradex config block successfully written\n");
578
579out:
580 free(config_block);
581 return ret;
582}
583
584static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
585 char * const argv[])
586{
587 int ret;
588
589 if (argc < 2)
590 return CMD_RET_USAGE;
591
592 if (!strcmp(argv[1], "create")) {
593 return do_cfgblock_create(cmdtp, flag, argc, argv);
594 } else if (!strcmp(argv[1], "reload")) {
595 ret = read_tdx_cfg_block();
596 if (ret) {
597 printf("Failed to reload Toradex config block: %d\n",
598 ret);
599 return CMD_RET_FAILURE;
600 }
601 return CMD_RET_SUCCESS;
602 }
603
604 return CMD_RET_USAGE;
605}
606
607U_BOOT_CMD(
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100608 cfgblock, 4, 0, do_cfgblock,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100609 "Toradex config block handling commands",
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100610 "create [-y] [barcode] - (Re-)create Toradex config block\n"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100611 "cfgblock reload - Reload Toradex config block from flash"
612);