blob: 57edb6c5c948cec607d4bcfb5a844b188c7cfc8f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01002/*
3 * Copyright (c) 2016 Toradex, Inc.
Marcel Ziswilera2777ec2016-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 Glassc62db352017-05-31 19:47:48 -060025#include <asm/mach-types.h>
Marcel Ziswilera2777ec2016-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 Agnerd826b872018-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",
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100101};
102
103#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
104static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
105{
106 struct mmc *mmc;
107 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
108 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
109 uint part = CONFIG_TDX_CFG_BLOCK_PART;
110 uint blk_start;
111 int ret = 0;
112
113 /* Read production parameter config block from eMMC */
114 mmc = find_mmc_device(dev);
115 if (!mmc) {
116 puts("No MMC card found\n");
117 ret = -ENODEV;
118 goto out;
119 }
Simon Glass0e513e72017-04-23 20:02:11 -0600120 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100121 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
122 puts("MMC partition switch failed\n");
123 ret = -ENODEV;
124 goto out;
125 }
126 }
127 if (offset < 0)
128 offset += mmc->capacity;
129 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
130
131 if (!write) {
132 /* Careful reads a whole block of 512 bytes into config_block */
133 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
134 (unsigned char *)config_block) != 1) {
135 ret = -EIO;
136 goto out;
137 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100138 } else {
139 /* Just writing one 512 byte block */
140 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
141 (unsigned char *)config_block) != 1) {
142 ret = -EIO;
143 goto out;
144 }
145 }
146
147out:
148 /* Switch back to regular eMMC user partition */
149 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
150
151 return ret;
152}
153#endif
154
155#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
156static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
157{
158 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
159
160 /* Read production parameter config block from NAND page */
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500161 return nand_read_skip_bad(get_nand_dev_by_index(0),
162 CONFIG_TDX_CFG_BLOCK_OFFSET,
163 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
164 config_block);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100165}
166
167static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
168{
169 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
170
171 /* Write production parameter config block to NAND page */
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500172 return nand_write_skip_bad(get_nand_dev_by_index(0),
173 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100174 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
175 config_block, WITH_WR_VERIFY);
176}
177#endif
178
179#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
180static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
181{
182 /* Read production parameter config block from NOR flash */
183 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
184 TDX_CFG_BLOCK_MAX_SIZE);
185 return 0;
186}
187
188static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
189{
190 /* Write production parameter config block to NOR flash */
191 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
192 TDX_CFG_BLOCK_MAX_SIZE);
193}
194#endif
195
196int read_tdx_cfg_block(void)
197{
198 int ret = 0;
199 u8 *config_block = NULL;
200 struct toradex_tag *tag;
201 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
202 int offset;
203
204 /* Allocate RAM area for config block */
205 config_block = memalign(ARCH_DMA_MINALIGN, size);
206 if (!config_block) {
207 printf("Not enough malloc space available!\n");
208 return -ENOMEM;
209 }
210
211 memset(config_block, 0, size);
212
213#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
214 ret = tdx_cfg_block_mmc_storage(config_block, 0);
215#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
216 ret = read_tdx_cfg_block_from_nand(config_block);
217#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
218 ret = read_tdx_cfg_block_from_nor(config_block);
219#else
220 ret = -EINVAL;
221#endif
222 if (ret)
223 goto out;
224
225 /* Expect a valid tag first */
226 tag = (struct toradex_tag *)config_block;
227 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
228 valid_cfgblock = false;
229 ret = -EINVAL;
230 goto out;
231 }
232 valid_cfgblock = true;
233 offset = 4;
234
235 while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
236 tag = (struct toradex_tag *)(config_block + offset);
237 offset += 4;
238 if (tag->id == TAG_INVALID)
239 break;
240
241 if (tag->flags == TAG_FLAG_VALID) {
242 switch (tag->id) {
243 case TAG_MAC:
244 memcpy(&tdx_eth_addr, config_block + offset,
245 6);
246
247 /* NIC part of MAC address is serial number */
248 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
249 break;
250 case TAG_HW:
251 memcpy(&tdx_hw_tag, config_block + offset, 8);
252 break;
253 }
254 }
255
256 /* Get to next tag according to current tags length */
257 offset += tag->len * 4;
258 }
259
260 /* Cap product id to avoid issues with a yet unknown one */
261 if (tdx_hw_tag.prodid > (sizeof(toradex_modules) /
262 sizeof(toradex_modules[0])))
263 tdx_hw_tag.prodid = 0;
264
265out:
266 free(config_block);
267 return ret;
268}
269
270static int get_cfgblock_interactive(void)
271{
272 char message[CONFIG_SYS_CBSIZE];
273 char *soc;
274 char it = 'n';
275 int len;
276
277 if (cpu_is_pxa27x())
278 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
279 else
280 sprintf(message, "Is the module an IT version? [y/N] ");
281
282 len = cli_readline(message);
283 it = console_buffer[0];
284
Simon Glass00caae62017-08-03 12:22:12 -0600285 soc = env_get("soc");
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100286 if (!strcmp("mx6", soc)) {
287#ifdef CONFIG_MACH_TYPE
288 if (it == 'y' || it == 'Y')
289 if (is_cpu_type(MXC_CPU_MX6Q))
290 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
291 else
292 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
293 else
294 if (is_cpu_type(MXC_CPU_MX6Q))
295 tdx_hw_tag.prodid = APALIS_IMX6Q;
296 else
297 tdx_hw_tag.prodid = APALIS_IMX6D;
298#else
299 if (it == 'y' || it == 'Y')
300 if (is_cpu_type(MXC_CPU_MX6DL))
301 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
302 else
303 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
304 else
305 if (is_cpu_type(MXC_CPU_MX6DL))
306 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
307 else
308 tdx_hw_tag.prodid = COLIBRI_IMX6S;
309#endif /* CONFIG_MACH_TYPE */
310 } else if (!strcmp("imx7d", soc)) {
311 tdx_hw_tag.prodid = COLIBRI_IMX7D;
312 } else if (!strcmp("imx7s", soc)) {
313 tdx_hw_tag.prodid = COLIBRI_IMX7S;
314 } else if (!strcmp("tegra20", soc)) {
315 if (it == 'y' || it == 'Y')
316 if (gd->ram_size == 0x10000000)
317 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
318 else
319 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
320 else
321 if (gd->ram_size == 0x10000000)
322 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
323 else
324 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
325 } else if (cpu_is_pxa27x()) {
326 if (it == 'y' || it == 'Y')
327 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
328 else
329 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
330#ifdef CONFIG_MACH_TYPE
331 } else if (!strcmp("tegra30", soc)) {
332 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
333 if (it == 'y' || it == 'Y')
334 tdx_hw_tag.prodid = APALIS_T30_IT;
335 else
336 if (gd->ram_size == 0x40000000)
337 tdx_hw_tag.prodid = APALIS_T30_1GB;
338 else
339 tdx_hw_tag.prodid = APALIS_T30_2GB;
340 } else {
341 if (it == 'y' || it == 'Y')
342 tdx_hw_tag.prodid = COLIBRI_T30_IT;
343 else
344 tdx_hw_tag.prodid = COLIBRI_T30;
345 }
346#endif /* CONFIG_MACH_TYPE */
347 } else if (!strcmp("tegra124", soc)) {
348 tdx_hw_tag.prodid = APALIS_TK1_2GB;
349 } else if (!strcmp("vf500", soc)) {
350 if (it == 'y' || it == 'Y')
351 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
352 else
353 tdx_hw_tag.prodid = COLIBRI_VF50;
354 } else if (!strcmp("vf610", soc)) {
355 if (it == 'y' || it == 'Y')
356 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
357 else
358 tdx_hw_tag.prodid = COLIBRI_VF61;
359 } else {
360 printf("Module type not detectable due to unknown SoC\n");
361 return -1;
362 }
363
364 while (len < 4) {
365 sprintf(message, "Enter the module version (e.g. V1.1B): V");
366 len = cli_readline(message);
367 }
368
369 tdx_hw_tag.ver_major = console_buffer[0] - '0';
370 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
371 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
372
373 if (cpu_is_pxa27x() && (tdx_hw_tag.ver_major == 1))
374 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
375 COLIBRI_PXA270_V1_312MHZ);
376
377 while (len < 8) {
378 sprintf(message, "Enter module serial number: ");
379 len = cli_readline(message);
380 }
381
382 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
383
384 return 0;
385}
386
387static int get_cfgblock_barcode(char *barcode)
388{
389 if (strlen(barcode) < 16) {
390 printf("Argument too short, barcode is 16 chars long\n");
391 return -1;
392 }
393
394 /* Get hardware information from the first 8 digits */
395 tdx_hw_tag.ver_major = barcode[4] - '0';
396 tdx_hw_tag.ver_minor = barcode[5] - '0';
397 tdx_hw_tag.ver_assembly = barcode[7] - '0';
398
399 barcode[4] = '\0';
400 tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
401
402 /* Parse second part of the barcode (serial number */
403 barcode += 8;
404 tdx_serial = simple_strtoul(barcode, NULL, 10);
405
406 return 0;
407}
408
409static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
410 char * const argv[])
411{
412 u8 *config_block;
413 struct toradex_tag *tag;
414 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
415 int offset = 0;
416 int ret = CMD_RET_SUCCESS;
417 int err;
418
419 /* Allocate RAM area for config block */
420 config_block = memalign(ARCH_DMA_MINALIGN, size);
421 if (!config_block) {
422 printf("Not enough malloc space available!\n");
423 return CMD_RET_FAILURE;
424 }
425
426 memset(config_block, 0xff, size);
427
428 read_tdx_cfg_block();
429 if (valid_cfgblock) {
430#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
431 /*
432 * On NAND devices, recreation is only allowed if the page is
433 * empty (config block invalid...)
434 */
435 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500436 CONFIG_TDX_CFG_BLOCK_OFFSET /
437 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100438 goto out;
439#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
440 /*
441 * On NOR devices, recreation is only allowed if the sector is
442 * empty and write protection is off (config block invalid...)
443 */
444 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
445 CONFIG_TDX_CFG_BLOCK_OFFSET);
446 goto out;
447#else
448 char message[CONFIG_SYS_CBSIZE];
449 sprintf(message,
450 "A valid Toradex config block is present, still recreate? [y/N] ");
451
452 if (!cli_readline(message))
453 goto out;
454
455 if (console_buffer[0] != 'y' && console_buffer[0] != 'Y')
456 goto out;
457#endif
458 }
459
460 /* Parse new Toradex config block data... */
461 if (argc < 3)
462 err = get_cfgblock_interactive();
463 else
464 err = get_cfgblock_barcode(argv[2]);
465
466 if (err) {
467 ret = CMD_RET_FAILURE;
468 goto out;
469 }
470
471 /* Convert serial number to MAC address (the storage format) */
472 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
473 tdx_eth_addr.nic = htonl(tdx_serial << 8);
474
475 /* Valid Tag */
476 tag = (struct toradex_tag *)config_block;
477 tag->id = TAG_VALID;
478 tag->flags = TAG_FLAG_VALID;
479 tag->len = 0;
480 offset += 4;
481
482 /* Product Tag */
483 tag = (struct toradex_tag *)(config_block + offset);
484 tag->id = TAG_HW;
485 tag->flags = TAG_FLAG_VALID;
486 tag->len = 2;
487 offset += 4;
488
489 memcpy(config_block + offset, &tdx_hw_tag, 8);
490 offset += 8;
491
492 /* MAC Tag */
493 tag = (struct toradex_tag *)(config_block + offset);
494 tag->id = TAG_MAC;
495 tag->flags = TAG_FLAG_VALID;
496 tag->len = 2;
497 offset += 4;
498
499 memcpy(config_block + offset, &tdx_eth_addr, 6);
500 offset += 6;
501 memset(config_block + offset, 0, 32 - offset);
502
503#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
504 err = tdx_cfg_block_mmc_storage(config_block, 1);
505#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
506 err = write_tdx_cfg_block_to_nand(config_block);
507#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
508 err = write_tdx_cfg_block_to_nor(config_block);
509#else
510 err = -EINVAL;
511#endif
512 if (err) {
513 printf("Failed to write Toradex config block: %d\n", ret);
514 ret = CMD_RET_FAILURE;
515 goto out;
516 }
517
518 printf("Toradex config block successfully written\n");
519
520out:
521 free(config_block);
522 return ret;
523}
524
525static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
526 char * const argv[])
527{
528 int ret;
529
530 if (argc < 2)
531 return CMD_RET_USAGE;
532
533 if (!strcmp(argv[1], "create")) {
534 return do_cfgblock_create(cmdtp, flag, argc, argv);
535 } else if (!strcmp(argv[1], "reload")) {
536 ret = read_tdx_cfg_block();
537 if (ret) {
538 printf("Failed to reload Toradex config block: %d\n",
539 ret);
540 return CMD_RET_FAILURE;
541 }
542 return CMD_RET_SUCCESS;
543 }
544
545 return CMD_RET_USAGE;
546}
547
548U_BOOT_CMD(
549 cfgblock, 3, 0, do_cfgblock,
550 "Toradex config block handling commands",
551 "create [barcode] - (Re-)create Toradex config block\n"
552 "cfgblock reload - Reload Toradex config block from flash"
553);