blob: adab0a080209e6346b8718accc329abf12ad8cf9 [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/*
Marcel Ziswiler842ddf82020-01-28 14:42:23 +01003 * Copyright (c) 2016-2020 Toradex
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
7#include "tdx-cfg-block.h"
Igor Opaniuk0c6b5582020-07-15 13:30:55 +03008#include "tdx-eeprom.h"
9
Simon Glass09140112020-05-10 11:40:03 -060010#include <command.h>
Simon Glass90526e92020-05-10 11:39:56 -060011#include <asm/cache.h>
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010012
Marcel Ziswiler4adc9fc2019-04-09 17:25:32 +020013#if defined(CONFIG_TARGET_APALIS_IMX6) || \
Marcel Ziswiler9d63c7d2019-07-12 12:35:08 +020014 defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler842ddf82020-01-28 14:42:23 +010015 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswiler4adc9fc2019-04-09 17:25:32 +020016 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
Marcel Ziswilerc0c39782020-01-28 14:42:24 +010017 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
18 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +020019 defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
20 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010021#include <asm/arch/sys_proto.h>
22#else
23#define is_cpu_type(cpu) (0)
24#endif
25#if defined(CONFIG_CPU_PXA27X)
26#include <asm/arch-pxa/pxa.h>
27#else
28#define cpu_is_pxa27x(cpu) (0)
29#endif
30#include <cli.h>
31#include <console.h>
Simon Glass7b51b572019-08-01 09:46:52 -060032#include <env.h>
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010033#include <flash.h>
34#include <malloc.h>
35#include <mmc.h>
36#include <nand.h>
Simon Glassc62db352017-05-31 19:47:48 -060037#include <asm/mach-types.h>
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010038
39DECLARE_GLOBAL_DATA_PTR;
40
41#define TAG_VALID 0xcf01
42#define TAG_MAC 0x0000
Igor Opaniuk0c6b5582020-07-15 13:30:55 +030043#define TAG_CAR_SERIAL 0x0021
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010044#define TAG_HW 0x0008
45#define TAG_INVALID 0xffff
46
47#define TAG_FLAG_VALID 0x1
48
Igor Opaniuk0c6b5582020-07-15 13:30:55 +030049#define TDX_EEPROM_ID_MODULE 0
50#define TDX_EEPROM_ID_CARRIER 1
51
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010052#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
53#define TDX_CFG_BLOCK_MAX_SIZE 512
54#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
55#define TDX_CFG_BLOCK_MAX_SIZE 64
56#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
57#define TDX_CFG_BLOCK_MAX_SIZE 64
Igor Opaniuk0c6b5582020-07-15 13:30:55 +030058#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
59#define TDX_CFG_BLOCK_MAX_SIZE 64
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010060#else
61#error Toradex config block location not set
62#endif
63
Igor Opaniuk0c6b5582020-07-15 13:30:55 +030064#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
65#define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
66#endif
67
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010068struct toradex_tag {
69 u32 len:14;
70 u32 flags:2;
71 u32 id:16;
72};
73
74bool valid_cfgblock;
75struct toradex_hw tdx_hw_tag;
76struct toradex_eth_addr tdx_eth_addr;
77u32 tdx_serial;
Igor Opaniuk0c6b5582020-07-15 13:30:55 +030078#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
79u32 tdx_car_serial;
80bool valid_cfgblock_carrier;
81struct toradex_hw tdx_car_hw_tag;
82#endif
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010083
84const char * const toradex_modules[] = {
85 [0] = "UNKNOWN MODULE",
86 [1] = "Colibri PXA270 312MHz",
87 [2] = "Colibri PXA270 520MHz",
88 [3] = "Colibri PXA320 806MHz",
89 [4] = "Colibri PXA300 208MHz",
90 [5] = "Colibri PXA310 624MHz",
91 [6] = "Colibri PXA320 806MHz IT",
92 [7] = "Colibri PXA300 208MHz XT",
93 [8] = "Colibri PXA270 312MHz",
94 [9] = "Colibri PXA270 520MHz",
95 [10] = "Colibri VF50 128MB", /* not currently on sale */
96 [11] = "Colibri VF61 256MB",
97 [12] = "Colibri VF61 256MB IT",
98 [13] = "Colibri VF50 128MB IT",
99 [14] = "Colibri iMX6 Solo 256MB",
100 [15] = "Colibri iMX6 DualLite 512MB",
101 [16] = "Colibri iMX6 Solo 256MB IT",
102 [17] = "Colibri iMX6 DualLite 512MB IT",
103 [18] = "UNKNOWN MODULE",
104 [19] = "UNKNOWN MODULE",
105 [20] = "Colibri T20 256MB",
106 [21] = "Colibri T20 512MB",
107 [22] = "Colibri T20 512MB IT",
108 [23] = "Colibri T30 1GB",
109 [24] = "Colibri T20 256MB IT",
110 [25] = "Apalis T30 2GB",
111 [26] = "Apalis T30 1GB",
112 [27] = "Apalis iMX6 Quad 1GB",
113 [28] = "Apalis iMX6 Quad 2GB IT",
114 [29] = "Apalis iMX6 Dual 512MB",
115 [30] = "Colibri T30 1GB IT",
116 [31] = "Apalis T30 1GB IT",
117 [32] = "Colibri iMX7 Solo 256MB",
118 [33] = "Colibri iMX7 Dual 512MB",
119 [34] = "Apalis TK1 2GB",
120 [35] = "Apalis iMX6 Dual 1GB IT",
Stefan Agnerd826b872018-05-30 19:01:47 +0200121 [36] = "Colibri iMX6ULL 256MB",
Marcel Ziswiler4adc9fc2019-04-09 17:25:32 +0200122 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
123 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
Stefan Agnerd826b872018-05-30 19:01:47 +0200124 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
Marcel Ziswiler4adc9fc2019-04-09 17:25:32 +0200125 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
Stefan Agnerd826b872018-05-30 19:01:47 +0200126 [41] = "Colibri iMX7 Dual 512MB EPDC",
127 [42] = "Apalis TK1 4GB",
Gerard Salvatella08f80552019-04-09 17:24:07 +0200128 [43] = "Colibri T20 512MB IT SETEK",
129 [44] = "Colibri iMX6ULL 512MB IT",
130 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
Marcel Ziswiler6988a3a2019-04-09 17:25:33 +0200131 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
132 [47] = "Apalis iMX8 QuadMax 4GB IT",
133 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
134 [49] = "Apalis iMX8 QuadPlus 2GB",
135 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
136 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
137 [52] = "Colibri iMX8 DualX 1GB",
Marcel Ziswiler842ddf82020-01-28 14:42:23 +0100138 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
139 [54] = "Apalis iMX8 DualXPlus 1GB",
Marcel Ziswilerc0c39782020-01-28 14:42:24 +0100140 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +0200141 [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
Marcel Ziswilerc0c39782020-01-28 14:42:24 +0100142 [57] = "Verdin iMX8M Mini DualLite 1GB",
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +0200143 [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
144 [59] = "Verdin iMX8M Mini Quad 2GB IT",
145 [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
146 [61] = "Verdin iMX8M Plus Quad 2GB",
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100147};
148
Igor Opaniuk26921f52020-07-15 13:30:54 +0300149const char * const toradex_carrier_boards[] = {
150 [0] = "UNKNOWN CARRIER BOARD",
151 [155] = "Dahlia",
152 [156] = "Verdin Development Board",
153};
154
155const char * const toradex_display_adapters[] = {
156 [0] = "UNKNOWN DISPLAY ADAPTER",
157 [157] = "Verdin DSI to HDMI Adapter",
158 [159] = "Verdin DSI to LVDS Adapter",
159};
160
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100161#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
162static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
163{
164 struct mmc *mmc;
165 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
166 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
167 uint part = CONFIG_TDX_CFG_BLOCK_PART;
168 uint blk_start;
169 int ret = 0;
170
171 /* Read production parameter config block from eMMC */
172 mmc = find_mmc_device(dev);
173 if (!mmc) {
174 puts("No MMC card found\n");
175 ret = -ENODEV;
176 goto out;
177 }
Stefan Agner42a4f182019-07-12 12:35:05 +0200178 if (mmc_init(mmc)) {
179 puts("MMC init failed\n");
180 return -EINVAL;
181 }
Simon Glass0e513e72017-04-23 20:02:11 -0600182 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100183 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
184 puts("MMC partition switch failed\n");
185 ret = -ENODEV;
186 goto out;
187 }
188 }
189 if (offset < 0)
190 offset += mmc->capacity;
191 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
192
193 if (!write) {
194 /* Careful reads a whole block of 512 bytes into config_block */
195 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
196 (unsigned char *)config_block) != 1) {
197 ret = -EIO;
198 goto out;
199 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100200 } else {
201 /* Just writing one 512 byte block */
202 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
203 (unsigned char *)config_block) != 1) {
204 ret = -EIO;
205 goto out;
206 }
207 }
208
209out:
210 /* Switch back to regular eMMC user partition */
211 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
212
213 return ret;
214}
215#endif
216
217#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
218static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
219{
220 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agnerbc53fb12018-08-06 09:19:18 +0200221 struct mtd_info *mtd = get_nand_dev_by_index(0);
222
223 if (!mtd)
224 return -ENODEV;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100225
226 /* Read production parameter config block from NAND page */
Stefan Agnerbc53fb12018-08-06 09:19:18 +0200227 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500228 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
229 config_block);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100230}
231
232static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
233{
234 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
235
236 /* Write production parameter config block to NAND page */
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500237 return nand_write_skip_bad(get_nand_dev_by_index(0),
238 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100239 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
240 config_block, WITH_WR_VERIFY);
241}
242#endif
243
244#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
245static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
246{
247 /* Read production parameter config block from NOR flash */
248 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
249 TDX_CFG_BLOCK_MAX_SIZE);
250 return 0;
251}
252
253static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
254{
255 /* Write production parameter config block to NOR flash */
256 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
257 TDX_CFG_BLOCK_MAX_SIZE);
258}
259#endif
260
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300261#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
262static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
263{
264 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
265 TDX_CFG_BLOCK_MAX_SIZE);
266}
267
268static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
269{
270 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
271 TDX_CFG_BLOCK_MAX_SIZE);
272}
273#endif
274
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100275int read_tdx_cfg_block(void)
276{
277 int ret = 0;
278 u8 *config_block = NULL;
279 struct toradex_tag *tag;
280 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
281 int offset;
282
283 /* Allocate RAM area for config block */
284 config_block = memalign(ARCH_DMA_MINALIGN, size);
285 if (!config_block) {
286 printf("Not enough malloc space available!\n");
287 return -ENOMEM;
288 }
289
290 memset(config_block, 0, size);
291
292#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
293 ret = tdx_cfg_block_mmc_storage(config_block, 0);
294#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
295 ret = read_tdx_cfg_block_from_nand(config_block);
296#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
297 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300298#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
299 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100300#else
301 ret = -EINVAL;
302#endif
303 if (ret)
304 goto out;
305
306 /* Expect a valid tag first */
307 tag = (struct toradex_tag *)config_block;
308 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
309 valid_cfgblock = false;
310 ret = -EINVAL;
311 goto out;
312 }
313 valid_cfgblock = true;
314 offset = 4;
315
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300316 /*
317 * check if there is enough space for storing tag and value of the
318 * biggest element
319 */
320 while (offset + sizeof(struct toradex_tag) +
321 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100322 tag = (struct toradex_tag *)(config_block + offset);
323 offset += 4;
324 if (tag->id == TAG_INVALID)
325 break;
326
327 if (tag->flags == TAG_FLAG_VALID) {
328 switch (tag->id) {
329 case TAG_MAC:
330 memcpy(&tdx_eth_addr, config_block + offset,
331 6);
332
333 /* NIC part of MAC address is serial number */
334 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
335 break;
336 case TAG_HW:
337 memcpy(&tdx_hw_tag, config_block + offset, 8);
338 break;
339 }
340 }
341
342 /* Get to next tag according to current tags length */
343 offset += tag->len * 4;
344 }
345
346 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswilerccdd3712019-03-25 17:18:29 +0100347 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100348 sizeof(toradex_modules[0])))
349 tdx_hw_tag.prodid = 0;
350
351out:
352 free(config_block);
353 return ret;
354}
355
356static int get_cfgblock_interactive(void)
357{
358 char message[CONFIG_SYS_CBSIZE];
359 char *soc;
360 char it = 'n';
Marcel Ziswilerd1aa14442019-07-12 12:35:06 +0200361 char wb = 'n';
Marcel Ziswilerc0c39782020-01-28 14:42:24 +0100362 int len = 0;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100363
Stefan Agner89315f32019-04-09 17:24:08 +0200364 /* Unknown module by default */
365 tdx_hw_tag.prodid = 0;
366
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100367 if (cpu_is_pxa27x())
368 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
Marcel Ziswilerc0c39782020-01-28 14:42:24 +0100369 else
370 it = 'y';
Marcel Ziswilerc0c39782020-01-28 14:42:24 +0100371
Marcel Ziswilerd1aa14442019-07-12 12:35:06 +0200372#if defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler842ddf82020-01-28 14:42:23 +0100373 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswilerd1aa14442019-07-12 12:35:06 +0200374 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +0200375 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
376 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
377 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswilerd1aa14442019-07-12 12:35:06 +0200378 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
379 len = cli_readline(message);
380 wb = console_buffer[0];
381#endif
382
Simon Glass00caae62017-08-03 12:22:12 -0600383 soc = env_get("soc");
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100384 if (!strcmp("mx6", soc)) {
Stefan Agner89315f32019-04-09 17:24:08 +0200385#ifdef CONFIG_TARGET_APALIS_IMX6
386 if (it == 'y' || it == 'Y') {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100387 if (is_cpu_type(MXC_CPU_MX6Q))
388 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
389 else
390 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner89315f32019-04-09 17:24:08 +0200391 } else {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100392 if (is_cpu_type(MXC_CPU_MX6Q))
393 tdx_hw_tag.prodid = APALIS_IMX6Q;
394 else
395 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner89315f32019-04-09 17:24:08 +0200396 }
397#elif CONFIG_TARGET_COLIBRI_IMX6
398 if (it == 'y' || it == 'Y') {
399 if (is_cpu_type(MXC_CPU_MX6DL))
400 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
401 else if (is_cpu_type(MXC_CPU_MX6SOLO))
402 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
403 } else {
404 if (is_cpu_type(MXC_CPU_MX6DL))
405 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
406 else if (is_cpu_type(MXC_CPU_MX6SOLO))
407 tdx_hw_tag.prodid = COLIBRI_IMX6S;
408 }
409#elif CONFIG_TARGET_COLIBRI_IMX6ULL
Gerard Salvatella08f80552019-04-09 17:24:07 +0200410 if (it == 'y' || it == 'Y') {
Stefan Agner89315f32019-04-09 17:24:08 +0200411 if (wb == 'y' || wb == 'Y')
412 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
413 else
414 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200415 } else {
Stefan Agner89315f32019-04-09 17:24:08 +0200416 if (wb == 'y' || wb == 'Y')
417 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
418 else
419 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200420 }
Stefan Agner89315f32019-04-09 17:24:08 +0200421#endif
Gerard Salvatella08f80552019-04-09 17:24:07 +0200422 } else if (!strcmp("imx7d", soc))
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100423 tdx_hw_tag.prodid = COLIBRI_IMX7D;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200424 else if (!strcmp("imx7s", soc))
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100425 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Marcel Ziswiler9d63c7d2019-07-12 12:35:08 +0200426 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
427 if (it == 'y' || it == 'Y') {
428 if (wb == 'y' || wb == 'Y')
429 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
430 else
431 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
432 } else {
433 if (wb == 'y' || wb == 'Y')
434 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
435 else
436 tdx_hw_tag.prodid = APALIS_IMX8QP;
437 }
438 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
Marcel Ziswiler842ddf82020-01-28 14:42:23 +0100439#ifdef CONFIG_TARGET_APALIS_IMX8X
440 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
441 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
442 } else {
443 if (gd->ram_size == 0x40000000)
444 tdx_hw_tag.prodid = APALIS_IMX8DXP;
445 else
446 tdx_hw_tag.prodid = APALIS_IMX8QXP;
447 }
448#elif CONFIG_TARGET_COLIBRI_IMX8X
Marcel Ziswiler6c297ee2019-07-12 12:35:07 +0200449 if (it == 'y' || it == 'Y') {
450 if (wb == 'y' || wb == 'Y')
451 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
452 else
453 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
454 } else {
455 if (wb == 'y' || wb == 'Y')
456 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
457 else
458 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
459 }
Marcel Ziswiler842ddf82020-01-28 14:42:23 +0100460#endif
Marcel Ziswiler2a1a2552020-10-28 11:58:15 +0200461 } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
462 if (wb == 'y' || wb == 'Y')
463 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
464 else
465 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +0200466 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
Marcel Ziswiler2a1a2552020-10-28 11:58:15 +0200467 if (wb == 'y' || wb == 'Y')
468 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
469 else
470 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +0200471 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
472 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
473 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
474 if (wb == 'y' || wb == 'Y')
475 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
476 else
477 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
Marcel Ziswiler6c297ee2019-07-12 12:35:07 +0200478 } else if (!strcmp("tegra20", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100479 if (it == 'y' || it == 'Y')
480 if (gd->ram_size == 0x10000000)
481 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
482 else
483 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
484 else
485 if (gd->ram_size == 0x10000000)
486 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
487 else
488 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
489 } else if (cpu_is_pxa27x()) {
490 if (it == 'y' || it == 'Y')
491 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
492 else
493 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200494 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100495#ifdef CONFIG_MACH_TYPE
Gerard Salvatella08f80552019-04-09 17:24:07 +0200496 else if (!strcmp("tegra30", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100497 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
498 if (it == 'y' || it == 'Y')
499 tdx_hw_tag.prodid = APALIS_T30_IT;
500 else
501 if (gd->ram_size == 0x40000000)
502 tdx_hw_tag.prodid = APALIS_T30_1GB;
503 else
504 tdx_hw_tag.prodid = APALIS_T30_2GB;
505 } else {
506 if (it == 'y' || it == 'Y')
507 tdx_hw_tag.prodid = COLIBRI_T30_IT;
508 else
509 tdx_hw_tag.prodid = COLIBRI_T30;
510 }
Gerard Salvatella08f80552019-04-09 17:24:07 +0200511 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100512#endif /* CONFIG_MACH_TYPE */
Gerard Salvatella08f80552019-04-09 17:24:07 +0200513 else if (!strcmp("tegra124", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100514 tdx_hw_tag.prodid = APALIS_TK1_2GB;
515 } else if (!strcmp("vf500", soc)) {
516 if (it == 'y' || it == 'Y')
517 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
518 else
519 tdx_hw_tag.prodid = COLIBRI_VF50;
520 } else if (!strcmp("vf610", soc)) {
521 if (it == 'y' || it == 'Y')
522 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
523 else
524 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner89315f32019-04-09 17:24:08 +0200525 }
526
527 if (!tdx_hw_tag.prodid) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100528 printf("Module type not detectable due to unknown SoC\n");
529 return -1;
530 }
531
532 while (len < 4) {
533 sprintf(message, "Enter the module version (e.g. V1.1B): V");
534 len = cli_readline(message);
535 }
536
537 tdx_hw_tag.ver_major = console_buffer[0] - '0';
538 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
539 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
540
Gerard Salvatella08f80552019-04-09 17:24:07 +0200541 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100542 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
543 COLIBRI_PXA270_V1_312MHZ);
544
545 while (len < 8) {
546 sprintf(message, "Enter module serial number: ");
547 len = cli_readline(message);
548 }
549
550 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
551
552 return 0;
553}
554
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300555static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
556 u32 *serial)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100557{
558 if (strlen(barcode) < 16) {
559 printf("Argument too short, barcode is 16 chars long\n");
560 return -1;
561 }
562
563 /* Get hardware information from the first 8 digits */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300564 tag->ver_major = barcode[4] - '0';
565 tag->ver_minor = barcode[5] - '0';
566 tag->ver_assembly = barcode[7] - '0';
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100567
568 barcode[4] = '\0';
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300569 tag->prodid = simple_strtoul(barcode, NULL, 10);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100570
571 /* Parse second part of the barcode (serial number */
572 barcode += 8;
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300573 *serial = simple_strtoul(barcode, NULL, 10);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100574
575 return 0;
576}
577
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300578static int write_tag(u8 *config_block, int *offset, int tag_id,
579 u8 *tag_data, size_t tag_data_size)
580{
581 struct toradex_tag *tag;
582
583 if (!offset || !config_block)
584 return -EINVAL;
585
586 tag = (struct toradex_tag *)(config_block + *offset);
587 tag->id = tag_id;
588 tag->flags = TAG_FLAG_VALID;
589 /* len is provided as number of 32bit values after the tag */
590 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
591 *offset += sizeof(struct toradex_tag);
592 if (tag_data && tag_data_size) {
593 memcpy(config_block + *offset, tag_data,
594 tag_data_size);
595 *offset += tag_data_size;
596 }
597
598 return 0;
599}
600
601#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
602int read_tdx_cfg_block_carrier(void)
603{
604 int ret = 0;
605 u8 *config_block = NULL;
606 struct toradex_tag *tag;
607 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
608 int offset;
609
610 /* Allocate RAM area for carrier config block */
611 config_block = memalign(ARCH_DMA_MINALIGN, size);
612 if (!config_block) {
613 printf("Not enough malloc space available!\n");
614 return -ENOMEM;
615 }
616
617 memset(config_block, 0, size);
618
619 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
620 size);
621 if (ret)
622 return ret;
623
624 /* Expect a valid tag first */
625 tag = (struct toradex_tag *)config_block;
626 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
627 valid_cfgblock_carrier = false;
628 ret = -EINVAL;
629 goto out;
630 }
631 valid_cfgblock_carrier = true;
632 offset = 4;
633
634 while (offset + sizeof(struct toradex_tag) +
635 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
636 tag = (struct toradex_tag *)(config_block + offset);
637 offset += 4;
638 if (tag->id == TAG_INVALID)
639 break;
640
641 if (tag->flags == TAG_FLAG_VALID) {
642 switch (tag->id) {
643 case TAG_CAR_SERIAL:
644 memcpy(&tdx_car_serial, config_block + offset,
645 sizeof(tdx_car_serial));
646 break;
647 case TAG_HW:
648 memcpy(&tdx_car_hw_tag, config_block +
649 offset, 8);
650 break;
651 }
652 }
653
654 /* Get to next tag according to current tags length */
655 offset += tag->len * 4;
656 }
657out:
658 free(config_block);
659 return ret;
660}
661
Igor Opaniukdb4ab6d2020-07-15 13:30:56 +0300662int check_pid8_sanity(char *pid8)
663{
664 char s_carrierid_verdin_dev[5];
665 char s_carrierid_dahlia[5];
666
667 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
668 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
669
670 /* sane value check, first 4 chars which represent carrier id */
671 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
672 return 0;
673
674 if (!strncmp(pid8, s_carrierid_dahlia, 4))
675 return 0;
676
677 return -EINVAL;
678}
679
680int try_migrate_tdx_cfg_block_carrier(void)
681{
682 char pid8[8];
683 int offset = 0;
684 int ret = CMD_RET_SUCCESS;
685 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
686 u8 *config_block;
687
688 memset(pid8, 0x0, 8);
689 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
690 if (ret)
691 return ret;
692
693 if (check_pid8_sanity(pid8))
694 return -EINVAL;
695
696 /* Allocate RAM area for config block */
697 config_block = memalign(ARCH_DMA_MINALIGN, size);
698 if (!config_block) {
699 printf("Not enough malloc space available!\n");
700 return CMD_RET_FAILURE;
701 }
702
703 memset(config_block, 0xff, size);
704 /* we try parse PID8 concatenating zeroed serial number */
705 tdx_car_hw_tag.ver_major = pid8[4] - '0';
706 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
707 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
708
709 pid8[4] = '\0';
710 tdx_car_hw_tag.prodid = simple_strtoul(pid8, NULL, 10);
711
712 /* Valid Tag */
713 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
714
715 /* Product Tag */
716 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
717 sizeof(tdx_car_hw_tag));
718
719 /* Serial Tag */
720 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
721 sizeof(tdx_car_serial));
722
723 memset(config_block + offset, 0, 32 - offset);
724 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
725 size);
726 if (ret) {
727 printf("Failed to write Toradex Extra config block: %d\n",
728 ret);
729 ret = CMD_RET_FAILURE;
730 goto out;
731 }
732
733 printf("Successfully migrated to Toradex Config Block from PID8\n");
734
735out:
736 free(config_block);
737 return ret;
738}
739
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300740static int get_cfgblock_carrier_interactive(void)
741{
742 char message[CONFIG_SYS_CBSIZE];
743 int len;
744
745 printf("Supported carrier boards:\n");
746 printf("CARRIER BOARD NAME\t\t [ID]\n");
747 for (int i = 0; i < sizeof(toradex_carrier_boards) /
748 sizeof(toradex_carrier_boards[0]); i++)
749 if (toradex_carrier_boards[i])
750 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
751
752 sprintf(message, "Choose your carrier board (provide ID): ");
753 len = cli_readline(message);
754 tdx_car_hw_tag.prodid = simple_strtoul(console_buffer, NULL, 10);
755
756 do {
757 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
758 len = cli_readline(message);
759 } while (len < 4);
760
761 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
762 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
763 tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
764
765 while (len < 8) {
766 sprintf(message, "Enter carrier board serial number: ");
767 len = cli_readline(message);
768 }
769
770 tdx_car_serial = simple_strtoul(console_buffer, NULL, 10);
771
772 return 0;
773}
774
775static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
776 char * const argv[])
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100777{
778 u8 *config_block;
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300779 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100780 int offset = 0;
781 int ret = CMD_RET_SUCCESS;
782 int err;
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100783 int force_overwrite = 0;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100784
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300785 if (argc >= 3) {
786 if (argv[2][0] == '-' && argv[2][1] == 'y')
787 force_overwrite = 1;
788 }
789
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100790 /* Allocate RAM area for config block */
791 config_block = memalign(ARCH_DMA_MINALIGN, size);
792 if (!config_block) {
793 printf("Not enough malloc space available!\n");
794 return CMD_RET_FAILURE;
795 }
796
797 memset(config_block, 0xff, size);
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300798 read_tdx_cfg_block_carrier();
799 if (valid_cfgblock_carrier && !force_overwrite) {
800 char message[CONFIG_SYS_CBSIZE];
801
802 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
803
804 if (!cli_readline(message))
805 goto out;
806
807 if (console_buffer[0] != 'y' &&
808 console_buffer[0] != 'Y')
809 goto out;
810 }
811
812 if (argc < 3 || (force_overwrite && argc < 4)) {
813 err = get_cfgblock_carrier_interactive();
814 } else {
815 if (force_overwrite)
816 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
817 &tdx_car_serial);
818 else
819 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
820 &tdx_car_serial);
821 }
822
823 if (err) {
824 ret = CMD_RET_FAILURE;
825 goto out;
826 }
827
828 /* Valid Tag */
829 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
830
831 /* Product Tag */
832 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
833 sizeof(tdx_car_hw_tag));
834
835 /* Serial Tag */
836 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
837 sizeof(tdx_car_serial));
838
839 memset(config_block + offset, 0, 32 - offset);
840 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
841 size);
842 if (err) {
843 printf("Failed to write Toradex Extra config block: %d\n",
844 ret);
845 ret = CMD_RET_FAILURE;
846 goto out;
847 }
848
849 printf("Toradex Extra config block successfully written\n");
850
851out:
852 free(config_block);
853 return ret;
854}
855
856#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
857
858static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
859 char * const argv[])
860{
861 u8 *config_block;
862 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
863 int offset = 0;
864 int ret = CMD_RET_SUCCESS;
865 int err;
866 int force_overwrite = 0;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100867
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100868 if (argc >= 3) {
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300869#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
870 if (!strcmp(argv[2], "carrier"))
871 return do_cfgblock_carrier_create(cmdtp, flag,
872 --argc, ++argv);
873#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100874 if (argv[2][0] == '-' && argv[2][1] == 'y')
875 force_overwrite = 1;
876 }
877
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300878 /* Allocate RAM area for config block */
879 config_block = memalign(ARCH_DMA_MINALIGN, size);
880 if (!config_block) {
881 printf("Not enough malloc space available!\n");
882 return CMD_RET_FAILURE;
883 }
884
885 memset(config_block, 0xff, size);
886
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100887 read_tdx_cfg_block();
888 if (valid_cfgblock) {
889#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
890 /*
891 * On NAND devices, recreation is only allowed if the page is
892 * empty (config block invalid...)
893 */
Marcel Ziswiler9d364eb2019-07-12 12:35:09 +0200894 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500895 CONFIG_TDX_CFG_BLOCK_OFFSET /
896 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100897 goto out;
898#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
899 /*
900 * On NOR devices, recreation is only allowed if the sector is
901 * empty and write protection is off (config block invalid...)
902 */
Marcel Ziswiler9d364eb2019-07-12 12:35:09 +0200903 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100904 CONFIG_TDX_CFG_BLOCK_OFFSET);
905 goto out;
906#else
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100907 if (!force_overwrite) {
908 char message[CONFIG_SYS_CBSIZE];
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100909
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100910 sprintf(message,
911 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100912
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100913 if (!cli_readline(message))
914 goto out;
915
916 if (console_buffer[0] != 'y' &&
917 console_buffer[0] != 'Y')
918 goto out;
919 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100920#endif
921 }
922
923 /* Parse new Toradex config block data... */
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100924 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100925 err = get_cfgblock_interactive();
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100926 } else {
927 if (force_overwrite)
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300928 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
929 &tdx_serial);
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100930 else
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300931 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
932 &tdx_serial);
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100933 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100934 if (err) {
935 ret = CMD_RET_FAILURE;
936 goto out;
937 }
938
939 /* Convert serial number to MAC address (the storage format) */
940 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
941 tdx_eth_addr.nic = htonl(tdx_serial << 8);
942
943 /* Valid Tag */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300944 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100945
946 /* Product Tag */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300947 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
948 sizeof(tdx_hw_tag));
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100949
950 /* MAC Tag */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300951 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
952 sizeof(tdx_eth_addr));
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100953
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100954 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100955#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
956 err = tdx_cfg_block_mmc_storage(config_block, 1);
957#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
958 err = write_tdx_cfg_block_to_nand(config_block);
959#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
960 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300961#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
962 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100963#else
964 err = -EINVAL;
965#endif
966 if (err) {
967 printf("Failed to write Toradex config block: %d\n", ret);
968 ret = CMD_RET_FAILURE;
969 goto out;
970 }
971
972 printf("Toradex config block successfully written\n");
973
974out:
975 free(config_block);
976 return ret;
977}
978
Simon Glass09140112020-05-10 11:40:03 -0600979static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
980 char *const argv[])
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100981{
982 int ret;
983
984 if (argc < 2)
985 return CMD_RET_USAGE;
986
987 if (!strcmp(argv[1], "create")) {
988 return do_cfgblock_create(cmdtp, flag, argc, argv);
989 } else if (!strcmp(argv[1], "reload")) {
990 ret = read_tdx_cfg_block();
991 if (ret) {
992 printf("Failed to reload Toradex config block: %d\n",
993 ret);
994 return CMD_RET_FAILURE;
995 }
996 return CMD_RET_SUCCESS;
997 }
998
999 return CMD_RET_USAGE;
1000}
1001
Igor Opaniuk0c6b5582020-07-15 13:30:55 +03001002U_BOOT_CMD(
1003 cfgblock, 5, 0, do_cfgblock,
1004 "Toradex config block handling commands",
1005 "create [-y] [barcode] - (Re-)create Toradex config block\n"
1006 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1007 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01001008);