blob: 475abf78a7ba018e4499c798cceb5b72c117f18d [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 Ziswilerc0c39782020-01-28 14:42:24 +0100426 else if (is_cpu_type(MXC_CPU_IMX8MM))
427 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
428 else if (is_cpu_type(MXC_CPU_IMX8MMDL))
429 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
430 else if (is_cpu_type(MXC_CPU_IMX8MN))
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +0200431 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
Marcel Ziswiler9d63c7d2019-07-12 12:35:08 +0200432 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
433 if (it == 'y' || it == 'Y') {
434 if (wb == 'y' || wb == 'Y')
435 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
436 else
437 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
438 } else {
439 if (wb == 'y' || wb == 'Y')
440 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
441 else
442 tdx_hw_tag.prodid = APALIS_IMX8QP;
443 }
444 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
Marcel Ziswiler842ddf82020-01-28 14:42:23 +0100445#ifdef CONFIG_TARGET_APALIS_IMX8X
446 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
447 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
448 } else {
449 if (gd->ram_size == 0x40000000)
450 tdx_hw_tag.prodid = APALIS_IMX8DXP;
451 else
452 tdx_hw_tag.prodid = APALIS_IMX8QXP;
453 }
454#elif CONFIG_TARGET_COLIBRI_IMX8X
Marcel Ziswiler6c297ee2019-07-12 12:35:07 +0200455 if (it == 'y' || it == 'Y') {
456 if (wb == 'y' || wb == 'Y')
457 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
458 else
459 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
460 } else {
461 if (wb == 'y' || wb == 'Y')
462 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
463 else
464 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
465 }
Marcel Ziswiler842ddf82020-01-28 14:42:23 +0100466#endif
Marcel Ziswilera5b5ad42020-10-28 11:58:08 +0200467 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
468 if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
469 if (wb == 'y' || wb == 'Y')
470 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
471 else
472 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
473 } else {
474 if (wb == 'y' || wb == 'Y')
475 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
476 else
477 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
478 }
479 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
480 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
481 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
482 if (wb == 'y' || wb == 'Y')
483 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
484 else
485 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
Marcel Ziswiler6c297ee2019-07-12 12:35:07 +0200486 } else if (!strcmp("tegra20", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100487 if (it == 'y' || it == 'Y')
488 if (gd->ram_size == 0x10000000)
489 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
490 else
491 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
492 else
493 if (gd->ram_size == 0x10000000)
494 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
495 else
496 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
497 } else if (cpu_is_pxa27x()) {
498 if (it == 'y' || it == 'Y')
499 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
500 else
501 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella08f80552019-04-09 17:24:07 +0200502 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100503#ifdef CONFIG_MACH_TYPE
Gerard Salvatella08f80552019-04-09 17:24:07 +0200504 else if (!strcmp("tegra30", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100505 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
506 if (it == 'y' || it == 'Y')
507 tdx_hw_tag.prodid = APALIS_T30_IT;
508 else
509 if (gd->ram_size == 0x40000000)
510 tdx_hw_tag.prodid = APALIS_T30_1GB;
511 else
512 tdx_hw_tag.prodid = APALIS_T30_2GB;
513 } else {
514 if (it == 'y' || it == 'Y')
515 tdx_hw_tag.prodid = COLIBRI_T30_IT;
516 else
517 tdx_hw_tag.prodid = COLIBRI_T30;
518 }
Gerard Salvatella08f80552019-04-09 17:24:07 +0200519 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100520#endif /* CONFIG_MACH_TYPE */
Gerard Salvatella08f80552019-04-09 17:24:07 +0200521 else if (!strcmp("tegra124", soc)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100522 tdx_hw_tag.prodid = APALIS_TK1_2GB;
523 } else if (!strcmp("vf500", soc)) {
524 if (it == 'y' || it == 'Y')
525 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
526 else
527 tdx_hw_tag.prodid = COLIBRI_VF50;
528 } else if (!strcmp("vf610", soc)) {
529 if (it == 'y' || it == 'Y')
530 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
531 else
532 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner89315f32019-04-09 17:24:08 +0200533 }
534
535 if (!tdx_hw_tag.prodid) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100536 printf("Module type not detectable due to unknown SoC\n");
537 return -1;
538 }
539
540 while (len < 4) {
541 sprintf(message, "Enter the module version (e.g. V1.1B): V");
542 len = cli_readline(message);
543 }
544
545 tdx_hw_tag.ver_major = console_buffer[0] - '0';
546 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
547 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
548
Gerard Salvatella08f80552019-04-09 17:24:07 +0200549 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100550 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
551 COLIBRI_PXA270_V1_312MHZ);
552
553 while (len < 8) {
554 sprintf(message, "Enter module serial number: ");
555 len = cli_readline(message);
556 }
557
558 tdx_serial = simple_strtoul(console_buffer, NULL, 10);
559
560 return 0;
561}
562
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300563static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
564 u32 *serial)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100565{
566 if (strlen(barcode) < 16) {
567 printf("Argument too short, barcode is 16 chars long\n");
568 return -1;
569 }
570
571 /* Get hardware information from the first 8 digits */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300572 tag->ver_major = barcode[4] - '0';
573 tag->ver_minor = barcode[5] - '0';
574 tag->ver_assembly = barcode[7] - '0';
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100575
576 barcode[4] = '\0';
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300577 tag->prodid = simple_strtoul(barcode, NULL, 10);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100578
579 /* Parse second part of the barcode (serial number */
580 barcode += 8;
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300581 *serial = simple_strtoul(barcode, NULL, 10);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100582
583 return 0;
584}
585
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300586static int write_tag(u8 *config_block, int *offset, int tag_id,
587 u8 *tag_data, size_t tag_data_size)
588{
589 struct toradex_tag *tag;
590
591 if (!offset || !config_block)
592 return -EINVAL;
593
594 tag = (struct toradex_tag *)(config_block + *offset);
595 tag->id = tag_id;
596 tag->flags = TAG_FLAG_VALID;
597 /* len is provided as number of 32bit values after the tag */
598 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
599 *offset += sizeof(struct toradex_tag);
600 if (tag_data && tag_data_size) {
601 memcpy(config_block + *offset, tag_data,
602 tag_data_size);
603 *offset += tag_data_size;
604 }
605
606 return 0;
607}
608
609#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
610int read_tdx_cfg_block_carrier(void)
611{
612 int ret = 0;
613 u8 *config_block = NULL;
614 struct toradex_tag *tag;
615 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
616 int offset;
617
618 /* Allocate RAM area for carrier config block */
619 config_block = memalign(ARCH_DMA_MINALIGN, size);
620 if (!config_block) {
621 printf("Not enough malloc space available!\n");
622 return -ENOMEM;
623 }
624
625 memset(config_block, 0, size);
626
627 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
628 size);
629 if (ret)
630 return ret;
631
632 /* Expect a valid tag first */
633 tag = (struct toradex_tag *)config_block;
634 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
635 valid_cfgblock_carrier = false;
636 ret = -EINVAL;
637 goto out;
638 }
639 valid_cfgblock_carrier = true;
640 offset = 4;
641
642 while (offset + sizeof(struct toradex_tag) +
643 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
644 tag = (struct toradex_tag *)(config_block + offset);
645 offset += 4;
646 if (tag->id == TAG_INVALID)
647 break;
648
649 if (tag->flags == TAG_FLAG_VALID) {
650 switch (tag->id) {
651 case TAG_CAR_SERIAL:
652 memcpy(&tdx_car_serial, config_block + offset,
653 sizeof(tdx_car_serial));
654 break;
655 case TAG_HW:
656 memcpy(&tdx_car_hw_tag, config_block +
657 offset, 8);
658 break;
659 }
660 }
661
662 /* Get to next tag according to current tags length */
663 offset += tag->len * 4;
664 }
665out:
666 free(config_block);
667 return ret;
668}
669
Igor Opaniukdb4ab6d2020-07-15 13:30:56 +0300670int check_pid8_sanity(char *pid8)
671{
672 char s_carrierid_verdin_dev[5];
673 char s_carrierid_dahlia[5];
674
675 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
676 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
677
678 /* sane value check, first 4 chars which represent carrier id */
679 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
680 return 0;
681
682 if (!strncmp(pid8, s_carrierid_dahlia, 4))
683 return 0;
684
685 return -EINVAL;
686}
687
688int try_migrate_tdx_cfg_block_carrier(void)
689{
690 char pid8[8];
691 int offset = 0;
692 int ret = CMD_RET_SUCCESS;
693 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
694 u8 *config_block;
695
696 memset(pid8, 0x0, 8);
697 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
698 if (ret)
699 return ret;
700
701 if (check_pid8_sanity(pid8))
702 return -EINVAL;
703
704 /* Allocate RAM area for config block */
705 config_block = memalign(ARCH_DMA_MINALIGN, size);
706 if (!config_block) {
707 printf("Not enough malloc space available!\n");
708 return CMD_RET_FAILURE;
709 }
710
711 memset(config_block, 0xff, size);
712 /* we try parse PID8 concatenating zeroed serial number */
713 tdx_car_hw_tag.ver_major = pid8[4] - '0';
714 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
715 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
716
717 pid8[4] = '\0';
718 tdx_car_hw_tag.prodid = simple_strtoul(pid8, NULL, 10);
719
720 /* Valid Tag */
721 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
722
723 /* Product Tag */
724 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
725 sizeof(tdx_car_hw_tag));
726
727 /* Serial Tag */
728 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
729 sizeof(tdx_car_serial));
730
731 memset(config_block + offset, 0, 32 - offset);
732 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
733 size);
734 if (ret) {
735 printf("Failed to write Toradex Extra config block: %d\n",
736 ret);
737 ret = CMD_RET_FAILURE;
738 goto out;
739 }
740
741 printf("Successfully migrated to Toradex Config Block from PID8\n");
742
743out:
744 free(config_block);
745 return ret;
746}
747
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300748static int get_cfgblock_carrier_interactive(void)
749{
750 char message[CONFIG_SYS_CBSIZE];
751 int len;
752
753 printf("Supported carrier boards:\n");
754 printf("CARRIER BOARD NAME\t\t [ID]\n");
755 for (int i = 0; i < sizeof(toradex_carrier_boards) /
756 sizeof(toradex_carrier_boards[0]); i++)
757 if (toradex_carrier_boards[i])
758 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
759
760 sprintf(message, "Choose your carrier board (provide ID): ");
761 len = cli_readline(message);
762 tdx_car_hw_tag.prodid = simple_strtoul(console_buffer, NULL, 10);
763
764 do {
765 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
766 len = cli_readline(message);
767 } while (len < 4);
768
769 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
770 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
771 tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
772
773 while (len < 8) {
774 sprintf(message, "Enter carrier board serial number: ");
775 len = cli_readline(message);
776 }
777
778 tdx_car_serial = simple_strtoul(console_buffer, NULL, 10);
779
780 return 0;
781}
782
783static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
784 char * const argv[])
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100785{
786 u8 *config_block;
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300787 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100788 int offset = 0;
789 int ret = CMD_RET_SUCCESS;
790 int err;
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100791 int force_overwrite = 0;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100792
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300793 if (argc >= 3) {
794 if (argv[2][0] == '-' && argv[2][1] == 'y')
795 force_overwrite = 1;
796 }
797
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100798 /* Allocate RAM area for config block */
799 config_block = memalign(ARCH_DMA_MINALIGN, size);
800 if (!config_block) {
801 printf("Not enough malloc space available!\n");
802 return CMD_RET_FAILURE;
803 }
804
805 memset(config_block, 0xff, size);
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300806 read_tdx_cfg_block_carrier();
807 if (valid_cfgblock_carrier && !force_overwrite) {
808 char message[CONFIG_SYS_CBSIZE];
809
810 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
811
812 if (!cli_readline(message))
813 goto out;
814
815 if (console_buffer[0] != 'y' &&
816 console_buffer[0] != 'Y')
817 goto out;
818 }
819
820 if (argc < 3 || (force_overwrite && argc < 4)) {
821 err = get_cfgblock_carrier_interactive();
822 } else {
823 if (force_overwrite)
824 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
825 &tdx_car_serial);
826 else
827 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
828 &tdx_car_serial);
829 }
830
831 if (err) {
832 ret = CMD_RET_FAILURE;
833 goto out;
834 }
835
836 /* Valid Tag */
837 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
838
839 /* Product Tag */
840 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
841 sizeof(tdx_car_hw_tag));
842
843 /* Serial Tag */
844 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
845 sizeof(tdx_car_serial));
846
847 memset(config_block + offset, 0, 32 - offset);
848 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
849 size);
850 if (err) {
851 printf("Failed to write Toradex Extra config block: %d\n",
852 ret);
853 ret = CMD_RET_FAILURE;
854 goto out;
855 }
856
857 printf("Toradex Extra config block successfully written\n");
858
859out:
860 free(config_block);
861 return ret;
862}
863
864#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
865
866static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
867 char * const argv[])
868{
869 u8 *config_block;
870 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
871 int offset = 0;
872 int ret = CMD_RET_SUCCESS;
873 int err;
874 int force_overwrite = 0;
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100875
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100876 if (argc >= 3) {
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300877#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
878 if (!strcmp(argv[2], "carrier"))
879 return do_cfgblock_carrier_create(cmdtp, flag,
880 --argc, ++argv);
881#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100882 if (argv[2][0] == '-' && argv[2][1] == 'y')
883 force_overwrite = 1;
884 }
885
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300886 /* Allocate RAM area for config block */
887 config_block = memalign(ARCH_DMA_MINALIGN, size);
888 if (!config_block) {
889 printf("Not enough malloc space available!\n");
890 return CMD_RET_FAILURE;
891 }
892
893 memset(config_block, 0xff, size);
894
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100895 read_tdx_cfg_block();
896 if (valid_cfgblock) {
897#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
898 /*
899 * On NAND devices, recreation is only allowed if the page is
900 * empty (config block invalid...)
901 */
Marcel Ziswiler9d364eb2019-07-12 12:35:09 +0200902 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobf264cd2017-06-26 19:13:06 -0500903 CONFIG_TDX_CFG_BLOCK_OFFSET /
904 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100905 goto out;
906#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
907 /*
908 * On NOR devices, recreation is only allowed if the sector is
909 * empty and write protection is off (config block invalid...)
910 */
Marcel Ziswiler9d364eb2019-07-12 12:35:09 +0200911 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 +0100912 CONFIG_TDX_CFG_BLOCK_OFFSET);
913 goto out;
914#else
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100915 if (!force_overwrite) {
916 char message[CONFIG_SYS_CBSIZE];
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100917
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100918 sprintf(message,
919 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100920
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100921 if (!cli_readline(message))
922 goto out;
923
924 if (console_buffer[0] != 'y' &&
925 console_buffer[0] != 'Y')
926 goto out;
927 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100928#endif
929 }
930
931 /* Parse new Toradex config block data... */
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100932 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100933 err = get_cfgblock_interactive();
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100934 } else {
935 if (force_overwrite)
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300936 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
937 &tdx_serial);
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100938 else
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300939 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
940 &tdx_serial);
Dominik Sliwa587b13c2019-03-25 17:18:27 +0100941 }
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100942 if (err) {
943 ret = CMD_RET_FAILURE;
944 goto out;
945 }
946
947 /* Convert serial number to MAC address (the storage format) */
948 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
949 tdx_eth_addr.nic = htonl(tdx_serial << 8);
950
951 /* Valid Tag */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300952 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100953
954 /* Product Tag */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300955 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
956 sizeof(tdx_hw_tag));
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100957
958 /* MAC Tag */
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300959 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
960 sizeof(tdx_eth_addr));
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100961
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100962 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100963#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
964 err = tdx_cfg_block_mmc_storage(config_block, 1);
965#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
966 err = write_tdx_cfg_block_to_nand(config_block);
967#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
968 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk0c6b5582020-07-15 13:30:55 +0300969#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
970 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100971#else
972 err = -EINVAL;
973#endif
974 if (err) {
975 printf("Failed to write Toradex config block: %d\n", ret);
976 ret = CMD_RET_FAILURE;
977 goto out;
978 }
979
980 printf("Toradex config block successfully written\n");
981
982out:
983 free(config_block);
984 return ret;
985}
986
Simon Glass09140112020-05-10 11:40:03 -0600987static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
988 char *const argv[])
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100989{
990 int ret;
991
992 if (argc < 2)
993 return CMD_RET_USAGE;
994
995 if (!strcmp(argv[1], "create")) {
996 return do_cfgblock_create(cmdtp, flag, argc, argv);
997 } else if (!strcmp(argv[1], "reload")) {
998 ret = read_tdx_cfg_block();
999 if (ret) {
1000 printf("Failed to reload Toradex config block: %d\n",
1001 ret);
1002 return CMD_RET_FAILURE;
1003 }
1004 return CMD_RET_SUCCESS;
1005 }
1006
1007 return CMD_RET_USAGE;
1008}
1009
Igor Opaniuk0c6b5582020-07-15 13:30:55 +03001010U_BOOT_CMD(
1011 cfgblock, 5, 0, do_cfgblock,
1012 "Toradex config block handling commands",
1013 "create [-y] [barcode] - (Re-)create Toradex config block\n"
1014 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1015 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01001016);