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