blob: 9146cbac94f84cd17bc4a5dad21dc9f35d69a101 [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/*
Gerard Salvatella08f80552019-04-09 17:24:07 +02003 * Copyright (c) 2016-2019 Toradex, Inc.
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
7#include "tdx-cfg-block.h"
8
Marcel Ziswiler4adc9fc2019-04-09 17:25:32 +02009#if defined(CONFIG_TARGET_APALIS_IMX6) || \
10 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
Marcel Ziswiler6c297ee2019-07-12 12:35:07 +020011 defined(CONFIG_TARGET_COLIBRI_IMX8X)
Marcel Ziswilera2777ec2016-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 Glassc62db352017-05-31 19:47:48 -060027#include <asm/mach-types.h>
Marcel Ziswilera2777ec2016-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 Agnerd826b872018-05-30 19:01:47 +020096 [36] = "Colibri iMX6ULL 256MB",
Marcel Ziswiler4adc9fc2019-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 Agnerd826b872018-05-30 19:01:47 +020099 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
Marcel Ziswiler4adc9fc2019-04-09 17:25:32 +0200100 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
Stefan Agnerd826b872018-05-30 19:01:47 +0200101 [41] = "Colibri iMX7 Dual 512MB EPDC",
102 [42] = "Apalis TK1 4GB",
Gerard Salvatella08f80552019-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 Ziswiler6988a3a2019-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 Ziswilera2777ec2016-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 }
Stefan Agner42a4f182019-07-12 12:35:05 +0200132 if (mmc_init(mmc)) {
133 puts("MMC init failed\n");
134 return -EINVAL;
135 }
Simon Glass0e513e72017-04-23 20:02:11 -0600136 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100137 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
138 puts("MMC partition switch failed\n");
139 ret = -ENODEV;
140 goto out;
141 }
142 }
143 if (offset < 0)
144 offset += mmc->capacity;
145 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
146
147 if (!write) {
148 /* Careful reads a whole block of 512 bytes into config_block */
149 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
150 (unsigned char *)config_block) != 1) {
151 ret = -EIO;
152 goto out;
153 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100154 } else {
155 /* Just writing one 512 byte block */
156 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
157 (unsigned char *)config_block) != 1) {
158 ret = -EIO;
159 goto out;
160 }
161 }
162
163out:
164 /* Switch back to regular eMMC user partition */
165 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
166
167 return ret;
168}
169#endif
170
171#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
172static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
173{
174 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agnerbc53fb12018-08-06 09:19:18 +0200175 struct mtd_info *mtd = get_nand_dev_by_index(0);
176
177 if (!mtd)
178 return -ENODEV;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100179
180 /* Read production parameter config block from NAND page */
Stefan Agnerbc53fb12018-08-06 09:19:18 +0200181 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500182 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
183 config_block);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100184}
185
186static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
187{
188 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
189
190 /* Write production parameter config block to NAND page */
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500191 return nand_write_skip_bad(get_nand_dev_by_index(0),
192 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100193 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
194 config_block, WITH_WR_VERIFY);
195}
196#endif
197
198#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
199static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
200{
201 /* Read production parameter config block from NOR flash */
202 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
203 TDX_CFG_BLOCK_MAX_SIZE);
204 return 0;
205}
206
207static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
208{
209 /* Write production parameter config block to NOR flash */
210 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
211 TDX_CFG_BLOCK_MAX_SIZE);
212}
213#endif
214
215int read_tdx_cfg_block(void)
216{
217 int ret = 0;
218 u8 *config_block = NULL;
219 struct toradex_tag *tag;
220 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
221 int offset;
222
223 /* Allocate RAM area for config block */
224 config_block = memalign(ARCH_DMA_MINALIGN, size);
225 if (!config_block) {
226 printf("Not enough malloc space available!\n");
227 return -ENOMEM;
228 }
229
230 memset(config_block, 0, size);
231
232#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
233 ret = tdx_cfg_block_mmc_storage(config_block, 0);
234#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
235 ret = read_tdx_cfg_block_from_nand(config_block);
236#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
237 ret = read_tdx_cfg_block_from_nor(config_block);
238#else
239 ret = -EINVAL;
240#endif
241 if (ret)
242 goto out;
243
244 /* Expect a valid tag first */
245 tag = (struct toradex_tag *)config_block;
246 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
247 valid_cfgblock = false;
248 ret = -EINVAL;
249 goto out;
250 }
251 valid_cfgblock = true;
252 offset = 4;
253
254 while (offset < TDX_CFG_BLOCK_MAX_SIZE) {
255 tag = (struct toradex_tag *)(config_block + offset);
256 offset += 4;
257 if (tag->id == TAG_INVALID)
258 break;
259
260 if (tag->flags == TAG_FLAG_VALID) {
261 switch (tag->id) {
262 case TAG_MAC:
263 memcpy(&tdx_eth_addr, config_block + offset,
264 6);
265
266 /* NIC part of MAC address is serial number */
267 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
268 break;
269 case TAG_HW:
270 memcpy(&tdx_hw_tag, config_block + offset, 8);
271 break;
272 }
273 }
274
275 /* Get to next tag according to current tags length */
276 offset += tag->len * 4;
277 }
278
279 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswilerccdd3712019-03-25 17:18:29 +0100280 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100281 sizeof(toradex_modules[0])))
282 tdx_hw_tag.prodid = 0;
283
284out:
285 free(config_block);
286 return ret;
287}
288
289static int get_cfgblock_interactive(void)
290{
291 char message[CONFIG_SYS_CBSIZE];
292 char *soc;
293 char it = 'n';
Marcel Ziswilerd1aa14442019-07-12 12:35:06 +0200294 char wb = 'n';
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100295 int len;
296
Stefan Agner89315f32019-04-09 17:24:08 +0200297 /* Unknown module by default */
298 tdx_hw_tag.prodid = 0;
299
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100300 if (cpu_is_pxa27x())
301 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
302 else
303 sprintf(message, "Is the module an IT version? [y/N] ");
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100304 len = cli_readline(message);
305 it = console_buffer[0];
306
Marcel Ziswilerd1aa14442019-07-12 12:35:06 +0200307#if defined(CONFIG_TARGET_APALIS_IMX8) || \
308 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
309 defined(CONFIG_TARGET_COLIBRI_IMX8X)
310 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
311 len = cli_readline(message);
312 wb = console_buffer[0];
313#endif
314
Simon Glass00caae62017-08-03 12:22:12 -0600315 soc = env_get("soc");
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100316 if (!strcmp("mx6", soc)) {
Stefan Agner89315f32019-04-09 17:24:08 +0200317#ifdef CONFIG_TARGET_APALIS_IMX6
318 if (it == 'y' || it == 'Y') {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100319 if (is_cpu_type(MXC_CPU_MX6Q))
320 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
321 else
322 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner89315f32019-04-09 17:24:08 +0200323 } else {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100324 if (is_cpu_type(MXC_CPU_MX6Q))
325 tdx_hw_tag.prodid = APALIS_IMX6Q;
326 else
327 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner89315f32019-04-09 17:24:08 +0200328 }
329#elif CONFIG_TARGET_COLIBRI_IMX6
330 if (it == 'y' || it == 'Y') {
331 if (is_cpu_type(MXC_CPU_MX6DL))
332 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
333 else if (is_cpu_type(MXC_CPU_MX6SOLO))
334 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
335 } else {
336 if (is_cpu_type(MXC_CPU_MX6DL))
337 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
338 else if (is_cpu_type(MXC_CPU_MX6SOLO))
339 tdx_hw_tag.prodid = COLIBRI_IMX6S;
340 }
341#elif CONFIG_TARGET_COLIBRI_IMX6ULL
Gerard Salvatella08f80552019-04-09 17:24:07 +0200342 if (it == 'y' || it == 'Y') {
Stefan Agner89315f32019-04-09 17:24:08 +0200343 if (wb == 'y' || wb == 'Y')
344 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
345 else
346 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200347 } else {
Stefan Agner89315f32019-04-09 17:24:08 +0200348 if (wb == 'y' || wb == 'Y')
349 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
350 else
351 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200352 }
Stefan Agner89315f32019-04-09 17:24:08 +0200353#endif
Gerard Salvatella08f80552019-04-09 17:24:07 +0200354 } else if (!strcmp("imx7d", soc))
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100355 tdx_hw_tag.prodid = COLIBRI_IMX7D;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200356 else if (!strcmp("imx7s", soc))
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100357 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Marcel Ziswiler6c297ee2019-07-12 12:35:07 +0200358 else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
359 if (it == 'y' || it == 'Y') {
360 if (wb == 'y' || wb == 'Y')
361 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
362 else
363 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
364 } else {
365 if (wb == 'y' || wb == 'Y')
366 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
367 else
368 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
369 }
370 } else if (!strcmp("tegra20", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100371 if (it == 'y' || it == 'Y')
372 if (gd->ram_size == 0x10000000)
373 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
374 else
375 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
376 else
377 if (gd->ram_size == 0x10000000)
378 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
379 else
380 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
381 } else if (cpu_is_pxa27x()) {
382 if (it == 'y' || it == 'Y')
383 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
384 else
385 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200386 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100387#ifdef CONFIG_MACH_TYPE
Gerard Salvatella08f80552019-04-09 17:24:07 +0200388 else if (!strcmp("tegra30", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100389 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
390 if (it == 'y' || it == 'Y')
391 tdx_hw_tag.prodid = APALIS_T30_IT;
392 else
393 if (gd->ram_size == 0x40000000)
394 tdx_hw_tag.prodid = APALIS_T30_1GB;
395 else
396 tdx_hw_tag.prodid = APALIS_T30_2GB;
397 } else {
398 if (it == 'y' || it == 'Y')
399 tdx_hw_tag.prodid = COLIBRI_T30_IT;
400 else
401 tdx_hw_tag.prodid = COLIBRI_T30;
402 }
Gerard Salvatella08f80552019-04-09 17:24:07 +0200403 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100404#endif /* CONFIG_MACH_TYPE */
Gerard Salvatella08f80552019-04-09 17:24:07 +0200405 else if (!strcmp("tegra124", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100406 tdx_hw_tag.prodid = APALIS_TK1_2GB;
407 } else if (!strcmp("vf500", soc)) {
408 if (it == 'y' || it == 'Y')
409 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
410 else
411 tdx_hw_tag.prodid = COLIBRI_VF50;
412 } else if (!strcmp("vf610", soc)) {
413 if (it == 'y' || it == 'Y')
414 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
415 else
416 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner89315f32019-04-09 17:24:08 +0200417 }
418
419 if (!tdx_hw_tag.prodid) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100420 printf("Module type not detectable due to unknown SoC\n");
421 return -1;
422 }
423
424 while (len < 4) {
425 sprintf(message, "Enter the module version (e.g. V1.1B): V");
426 len = cli_readline(message);
427 }
428
429 tdx_hw_tag.ver_major = console_buffer[0] - '0';
430 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
431 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
432
Gerard Salvatella08f80552019-04-09 17:24:07 +0200433 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100434 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
435 COLIBRI_PXA270_V1_312MHZ);
436
437 while (len < 8) {
438 sprintf(message, "Enter module serial number: ");
439 len = cli_readline(message);
440 }
441
442 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
443
444 return 0;
445}
446
447static int get_cfgblock_barcode(char *barcode)
448{
449 if (strlen(barcode) < 16) {
450 printf("Argument too short, barcode is 16 chars long\n");
451 return -1;
452 }
453
454 /* Get hardware information from the first 8 digits */
455 tdx_hw_tag.ver_major = barcode[4] - '0';
456 tdx_hw_tag.ver_minor = barcode[5] - '0';
457 tdx_hw_tag.ver_assembly = barcode[7] - '0';
458
459 barcode[4] = '\0';
460 tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10);
461
462 /* Parse second part of the barcode (serial number */
463 barcode += 8;
464 tdx_serial = simple_strtoul(barcode, NULL, 10);
465
466 return 0;
467}
468
469static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc,
470 char * const argv[])
471{
472 u8 *config_block;
473 struct toradex_tag *tag;
474 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
475 int offset = 0;
476 int ret = CMD_RET_SUCCESS;
477 int err;
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100478 int force_overwrite = 0;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100479
480 /* Allocate RAM area for config block */
481 config_block = memalign(ARCH_DMA_MINALIGN, size);
482 if (!config_block) {
483 printf("Not enough malloc space available!\n");
484 return CMD_RET_FAILURE;
485 }
486
487 memset(config_block, 0xff, size);
488
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100489 if (argc >= 3) {
490 if (argv[2][0] == '-' && argv[2][1] == 'y')
491 force_overwrite = 1;
492 }
493
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100494 read_tdx_cfg_block();
495 if (valid_cfgblock) {
496#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
497 /*
498 * On NAND devices, recreation is only allowed if the page is
499 * empty (config block invalid...)
500 */
Gerard Salvatella08f80552019-04-09 17:24:07 +0200501 printf("NAND erase block %d need to be erased before creating" \
502 " a Toradex config block\n",
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500503 CONFIG_TDX_CFG_BLOCK_OFFSET /
504 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100505 goto out;
506#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
507 /*
508 * On NOR devices, recreation is only allowed if the sector is
509 * empty and write protection is off (config block invalid...)
510 */
Gerard Salvatella08f80552019-04-09 17:24:07 +0200511 printf("NOR sector at offset 0x%02x need to be erased and " \
512 "unprotected before creating a Toradex config block\n",
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100513 CONFIG_TDX_CFG_BLOCK_OFFSET);
514 goto out;
515#else
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100516 if (!force_overwrite) {
517 char message[CONFIG_SYS_CBSIZE];
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100518
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100519 sprintf(message,
520 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100521
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100522 if (!cli_readline(message))
523 goto out;
524
525 if (console_buffer[0] != 'y' &&
526 console_buffer[0] != 'Y')
527 goto out;
528 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100529#endif
530 }
531
532 /* Parse new Toradex config block data... */
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100533 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100534 err = get_cfgblock_interactive();
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100535 } else {
536 if (force_overwrite)
537 err = get_cfgblock_barcode(argv[3]);
538 else
539 err = get_cfgblock_barcode(argv[2]);
540 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100541 if (err) {
542 ret = CMD_RET_FAILURE;
543 goto out;
544 }
545
546 /* Convert serial number to MAC address (the storage format) */
547 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
548 tdx_eth_addr.nic = htonl(tdx_serial << 8);
549
550 /* Valid Tag */
551 tag = (struct toradex_tag *)config_block;
552 tag->id = TAG_VALID;
553 tag->flags = TAG_FLAG_VALID;
554 tag->len = 0;
555 offset += 4;
556
557 /* Product Tag */
558 tag = (struct toradex_tag *)(config_block + offset);
559 tag->id = TAG_HW;
560 tag->flags = TAG_FLAG_VALID;
561 tag->len = 2;
562 offset += 4;
563
564 memcpy(config_block + offset, &tdx_hw_tag, 8);
565 offset += 8;
566
567 /* MAC Tag */
568 tag = (struct toradex_tag *)(config_block + offset);
569 tag->id = TAG_MAC;
570 tag->flags = TAG_FLAG_VALID;
571 tag->len = 2;
572 offset += 4;
573
574 memcpy(config_block + offset, &tdx_eth_addr, 6);
575 offset += 6;
576 memset(config_block + offset, 0, 32 - offset);
577
578#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
579 err = tdx_cfg_block_mmc_storage(config_block, 1);
580#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
581 err = write_tdx_cfg_block_to_nand(config_block);
582#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
583 err = write_tdx_cfg_block_to_nor(config_block);
584#else
585 err = -EINVAL;
586#endif
587 if (err) {
588 printf("Failed to write Toradex config block: %d\n", ret);
589 ret = CMD_RET_FAILURE;
590 goto out;
591 }
592
593 printf("Toradex config block successfully written\n");
594
595out:
596 free(config_block);
597 return ret;
598}
599
600static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc,
601 char * const argv[])
602{
603 int ret;
604
605 if (argc < 2)
606 return CMD_RET_USAGE;
607
608 if (!strcmp(argv[1], "create")) {
609 return do_cfgblock_create(cmdtp, flag, argc, argv);
610 } else if (!strcmp(argv[1], "reload")) {
611 ret = read_tdx_cfg_block();
612 if (ret) {
613 printf("Failed to reload Toradex config block: %d\n",
614 ret);
615 return CMD_RET_FAILURE;
616 }
617 return CMD_RET_SUCCESS;
618 }
619
620 return CMD_RET_USAGE;
621}
622
623U_BOOT_CMD(
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100624 cfgblock, 4, 0, do_cfgblock,
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100625 "Toradex config block handling commands",
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100626 "create [-y] [barcode] - (Re-)create Toradex config block\n"
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100627 "cfgblock reload - Reload Toradex config block from flash"
628);