blob: e4f9a0db9141f5528ba0a2de228ee0113f294b30 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01002/*
Marcel Ziswiler75b93272020-01-28 14:42:23 +01003 * Copyright (c) 2016-2020 Toradex
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06007#include <asm/global_data.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01008#include "tdx-cfg-block.h"
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03009#include "tdx-eeprom.h"
10
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <asm/cache.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010013
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +020014#if defined(CONFIG_TARGET_APALIS_IMX6) || \
Marcel Ziswiler00320612019-07-12 12:35:08 +020015 defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler75b93272020-01-28 14:42:23 +010016 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +020017 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
Marcel Ziswilereca26ba2020-01-28 14:42:24 +010018 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
19 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +020020 defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
21 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010022#include <asm/arch/sys_proto.h>
23#else
24#define is_cpu_type(cpu) (0)
25#endif
26#if defined(CONFIG_CPU_PXA27X)
27#include <asm/arch-pxa/pxa.h>
28#else
29#define cpu_is_pxa27x(cpu) (0)
30#endif
31#include <cli.h>
32#include <console.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060033#include <env.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010034#include <flash.h>
35#include <malloc.h>
36#include <mmc.h>
37#include <nand.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060038#include <asm/mach-types.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010039
40DECLARE_GLOBAL_DATA_PTR;
41
42#define TAG_VALID 0xcf01
43#define TAG_MAC 0x0000
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030044#define TAG_CAR_SERIAL 0x0021
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010045#define TAG_HW 0x0008
46#define TAG_INVALID 0xffff
47
48#define TAG_FLAG_VALID 0x1
49
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030050#define TDX_EEPROM_ID_MODULE 0
51#define TDX_EEPROM_ID_CARRIER 1
52
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010053#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
54#define TDX_CFG_BLOCK_MAX_SIZE 512
55#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
56#define TDX_CFG_BLOCK_MAX_SIZE 64
57#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
58#define TDX_CFG_BLOCK_MAX_SIZE 64
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030059#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
60#define TDX_CFG_BLOCK_MAX_SIZE 64
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010061#else
62#error Toradex config block location not set
63#endif
64
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030065#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
66#define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
67#endif
68
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010069struct toradex_tag {
70 u32 len:14;
71 u32 flags:2;
72 u32 id:16;
73};
74
75bool valid_cfgblock;
76struct toradex_hw tdx_hw_tag;
77struct toradex_eth_addr tdx_eth_addr;
78u32 tdx_serial;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030079#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
80u32 tdx_car_serial;
81bool valid_cfgblock_carrier;
82struct toradex_hw tdx_car_hw_tag;
83#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010084
85const char * const toradex_modules[] = {
86 [0] = "UNKNOWN MODULE",
87 [1] = "Colibri PXA270 312MHz",
88 [2] = "Colibri PXA270 520MHz",
89 [3] = "Colibri PXA320 806MHz",
90 [4] = "Colibri PXA300 208MHz",
91 [5] = "Colibri PXA310 624MHz",
92 [6] = "Colibri PXA320 806MHz IT",
93 [7] = "Colibri PXA300 208MHz XT",
94 [8] = "Colibri PXA270 312MHz",
95 [9] = "Colibri PXA270 520MHz",
96 [10] = "Colibri VF50 128MB", /* not currently on sale */
97 [11] = "Colibri VF61 256MB",
98 [12] = "Colibri VF61 256MB IT",
99 [13] = "Colibri VF50 128MB IT",
100 [14] = "Colibri iMX6 Solo 256MB",
101 [15] = "Colibri iMX6 DualLite 512MB",
102 [16] = "Colibri iMX6 Solo 256MB IT",
103 [17] = "Colibri iMX6 DualLite 512MB IT",
104 [18] = "UNKNOWN MODULE",
105 [19] = "UNKNOWN MODULE",
106 [20] = "Colibri T20 256MB",
107 [21] = "Colibri T20 512MB",
108 [22] = "Colibri T20 512MB IT",
109 [23] = "Colibri T30 1GB",
110 [24] = "Colibri T20 256MB IT",
111 [25] = "Apalis T30 2GB",
112 [26] = "Apalis T30 1GB",
113 [27] = "Apalis iMX6 Quad 1GB",
114 [28] = "Apalis iMX6 Quad 2GB IT",
115 [29] = "Apalis iMX6 Dual 512MB",
116 [30] = "Colibri T30 1GB IT",
117 [31] = "Apalis T30 1GB IT",
118 [32] = "Colibri iMX7 Solo 256MB",
119 [33] = "Colibri iMX7 Dual 512MB",
120 [34] = "Apalis TK1 2GB",
121 [35] = "Apalis iMX6 Dual 1GB IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200122 [36] = "Colibri iMX6ULL 256MB",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200123 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
124 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200125 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200126 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200127 [41] = "Colibri iMX7 Dual 512MB EPDC",
128 [42] = "Apalis TK1 4GB",
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200129 [43] = "Colibri T20 512MB IT SETEK",
130 [44] = "Colibri iMX6ULL 512MB IT",
131 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
Marcel Ziswilerf60ffec2019-04-09 17:25:33 +0200132 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
133 [47] = "Apalis iMX8 QuadMax 4GB IT",
134 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
135 [49] = "Apalis iMX8 QuadPlus 2GB",
136 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
137 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
138 [52] = "Colibri iMX8 DualX 1GB",
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100139 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
140 [54] = "Apalis iMX8 DualXPlus 1GB",
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100141 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200142 [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100143 [57] = "Verdin iMX8M Mini DualLite 1GB",
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200144 [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
145 [59] = "Verdin iMX8M Mini Quad 2GB IT",
146 [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
147 [61] = "Verdin iMX8M Plus Quad 2GB",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100148};
149
Igor Opaniuk414df352020-07-15 13:30:54 +0300150const char * const toradex_carrier_boards[] = {
151 [0] = "UNKNOWN CARRIER BOARD",
152 [155] = "Dahlia",
153 [156] = "Verdin Development Board",
154};
155
156const char * const toradex_display_adapters[] = {
157 [0] = "UNKNOWN DISPLAY ADAPTER",
158 [157] = "Verdin DSI to HDMI Adapter",
159 [159] = "Verdin DSI to LVDS Adapter",
160};
161
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100162#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
163static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
164{
165 struct mmc *mmc;
166 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
167 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
168 uint part = CONFIG_TDX_CFG_BLOCK_PART;
169 uint blk_start;
170 int ret = 0;
171
172 /* Read production parameter config block from eMMC */
173 mmc = find_mmc_device(dev);
174 if (!mmc) {
175 puts("No MMC card found\n");
176 ret = -ENODEV;
177 goto out;
178 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200179 if (mmc_init(mmc)) {
180 puts("MMC init failed\n");
181 return -EINVAL;
182 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600183 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100184 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
185 puts("MMC partition switch failed\n");
186 ret = -ENODEV;
187 goto out;
188 }
189 }
190 if (offset < 0)
191 offset += mmc->capacity;
192 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
193
194 if (!write) {
195 /* Careful reads a whole block of 512 bytes into config_block */
196 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
197 (unsigned char *)config_block) != 1) {
198 ret = -EIO;
199 goto out;
200 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100201 } else {
202 /* Just writing one 512 byte block */
203 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
204 (unsigned char *)config_block) != 1) {
205 ret = -EIO;
206 goto out;
207 }
208 }
209
210out:
211 /* Switch back to regular eMMC user partition */
212 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
213
214 return ret;
215}
216#endif
217
218#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
219static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
220{
221 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200222 struct mtd_info *mtd = get_nand_dev_by_index(0);
223
224 if (!mtd)
225 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100226
227 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200228 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500229 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
230 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100231}
232
233static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
234{
235 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
236
237 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500238 return nand_write_skip_bad(get_nand_dev_by_index(0),
239 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100240 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
241 config_block, WITH_WR_VERIFY);
242}
243#endif
244
245#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
246static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
247{
248 /* Read production parameter config block from NOR flash */
249 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
250 TDX_CFG_BLOCK_MAX_SIZE);
251 return 0;
252}
253
254static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
255{
256 /* Write production parameter config block to NOR flash */
257 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
258 TDX_CFG_BLOCK_MAX_SIZE);
259}
260#endif
261
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300262#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
263static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
264{
265 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
266 TDX_CFG_BLOCK_MAX_SIZE);
267}
268
269static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
270{
271 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
272 TDX_CFG_BLOCK_MAX_SIZE);
273}
274#endif
275
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100276int read_tdx_cfg_block(void)
277{
278 int ret = 0;
279 u8 *config_block = NULL;
280 struct toradex_tag *tag;
281 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
282 int offset;
283
284 /* Allocate RAM area for config block */
285 config_block = memalign(ARCH_DMA_MINALIGN, size);
286 if (!config_block) {
287 printf("Not enough malloc space available!\n");
288 return -ENOMEM;
289 }
290
291 memset(config_block, 0, size);
292
293#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
294 ret = tdx_cfg_block_mmc_storage(config_block, 0);
295#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
296 ret = read_tdx_cfg_block_from_nand(config_block);
297#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
298 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300299#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
300 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100301#else
302 ret = -EINVAL;
303#endif
304 if (ret)
305 goto out;
306
307 /* Expect a valid tag first */
308 tag = (struct toradex_tag *)config_block;
309 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
310 valid_cfgblock = false;
311 ret = -EINVAL;
312 goto out;
313 }
314 valid_cfgblock = true;
315 offset = 4;
316
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300317 /*
318 * check if there is enough space for storing tag and value of the
319 * biggest element
320 */
321 while (offset + sizeof(struct toradex_tag) +
322 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100323 tag = (struct toradex_tag *)(config_block + offset);
324 offset += 4;
325 if (tag->id == TAG_INVALID)
326 break;
327
328 if (tag->flags == TAG_FLAG_VALID) {
329 switch (tag->id) {
330 case TAG_MAC:
331 memcpy(&tdx_eth_addr, config_block + offset,
332 6);
333
334 /* NIC part of MAC address is serial number */
335 tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
336 break;
337 case TAG_HW:
338 memcpy(&tdx_hw_tag, config_block + offset, 8);
339 break;
340 }
341 }
342
343 /* Get to next tag according to current tags length */
344 offset += tag->len * 4;
345 }
346
347 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswiler8c9127c2019-03-25 17:18:29 +0100348 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100349 sizeof(toradex_modules[0])))
350 tdx_hw_tag.prodid = 0;
351
352out:
353 free(config_block);
354 return ret;
355}
356
357static int get_cfgblock_interactive(void)
358{
359 char message[CONFIG_SYS_CBSIZE];
360 char *soc;
361 char it = 'n';
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200362 char wb = 'n';
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100363 int len = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100364
Stefan Agner68f58782019-04-09 17:24:08 +0200365 /* Unknown module by default */
366 tdx_hw_tag.prodid = 0;
367
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100368 if (cpu_is_pxa27x())
369 sprintf(message, "Is the module the 312 MHz version? [y/N] ");
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100370 else
371 it = 'y';
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100372
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200373#if defined(CONFIG_TARGET_APALIS_IMX8) || \
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100374 defined(CONFIG_TARGET_APALIS_IMX8X) || \
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200375 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200376 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
377 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
378 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200379 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
380 len = cli_readline(message);
381 wb = console_buffer[0];
382#endif
383
Simon Glass64b723f2017-08-03 12:22:12 -0600384 soc = env_get("soc");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100385 if (!strcmp("mx6", soc)) {
Stefan Agner68f58782019-04-09 17:24:08 +0200386#ifdef CONFIG_TARGET_APALIS_IMX6
387 if (it == 'y' || it == 'Y') {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100388 if (is_cpu_type(MXC_CPU_MX6Q))
389 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
390 else
391 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200392 } else {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100393 if (is_cpu_type(MXC_CPU_MX6Q))
394 tdx_hw_tag.prodid = APALIS_IMX6Q;
395 else
396 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner68f58782019-04-09 17:24:08 +0200397 }
398#elif CONFIG_TARGET_COLIBRI_IMX6
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200399 if (it == 'y' || it == 'Y') {
Stefan Agner68f58782019-04-09 17:24:08 +0200400 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100401 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200402 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100403 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200404 } else {
Stefan Agner68f58782019-04-09 17:24:08 +0200405 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100406 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
Stefan Agner68f58782019-04-09 17:24:08 +0200407 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100408 tdx_hw_tag.prodid = COLIBRI_IMX6S;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200409 }
Stefan Agner68f58782019-04-09 17:24:08 +0200410#elif CONFIG_TARGET_COLIBRI_IMX6ULL
Stefan Agner68f58782019-04-09 17:24:08 +0200411 if (it == 'y' || it == 'Y') {
412 if (wb == 'y' || wb == 'Y')
413 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
414 else
415 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
416 } else {
417 if (wb == 'y' || wb == 'Y')
418 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
419 else
420 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
421 }
422#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200423 } else if (!strcmp("imx7d", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100424 tdx_hw_tag.prodid = COLIBRI_IMX7D;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200425 else if (!strcmp("imx7s", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100426 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Marcel Ziswiler00320612019-07-12 12:35:08 +0200427 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
428 if (it == 'y' || it == 'Y') {
429 if (wb == 'y' || wb == 'Y')
430 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
431 else
432 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
433 } else {
434 if (wb == 'y' || wb == 'Y')
435 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
436 else
437 tdx_hw_tag.prodid = APALIS_IMX8QP;
438 }
439 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100440#ifdef CONFIG_TARGET_APALIS_IMX8X
441 if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
442 tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
443 } else {
444 if (gd->ram_size == 0x40000000)
445 tdx_hw_tag.prodid = APALIS_IMX8DXP;
446 else
447 tdx_hw_tag.prodid = APALIS_IMX8QXP;
448 }
449#elif CONFIG_TARGET_COLIBRI_IMX8X
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200450 if (it == 'y' || it == 'Y') {
451 if (wb == 'y' || wb == 'Y')
452 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
453 else
454 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
455 } else {
456 if (wb == 'y' || wb == 'Y')
457 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
458 else
459 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
460 }
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100461#endif
Marcel Ziswiler3834f662020-10-28 11:58:15 +0200462 } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
463 if (wb == 'y' || wb == 'Y')
464 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
465 else
466 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200467 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
Marcel Ziswiler3834f662020-10-28 11:58:15 +0200468 if (wb == 'y' || wb == 'Y')
469 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
470 else
471 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200472 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
473 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
474 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
475 if (wb == 'y' || wb == 'Y')
476 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
477 else
478 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200479 } else if (!strcmp("tegra20", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100480 if (it == 'y' || it == 'Y')
481 if (gd->ram_size == 0x10000000)
482 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
483 else
484 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
485 else
486 if (gd->ram_size == 0x10000000)
487 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
488 else
489 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
490 } else if (cpu_is_pxa27x()) {
491 if (it == 'y' || it == 'Y')
492 tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
493 else
494 tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200495 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100496#ifdef CONFIG_MACH_TYPE
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200497 else if (!strcmp("tegra30", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100498 if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
499 if (it == 'y' || it == 'Y')
500 tdx_hw_tag.prodid = APALIS_T30_IT;
501 else
502 if (gd->ram_size == 0x40000000)
503 tdx_hw_tag.prodid = APALIS_T30_1GB;
504 else
505 tdx_hw_tag.prodid = APALIS_T30_2GB;
506 } else {
507 if (it == 'y' || it == 'Y')
508 tdx_hw_tag.prodid = COLIBRI_T30_IT;
509 else
510 tdx_hw_tag.prodid = COLIBRI_T30;
511 }
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200512 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100513#endif /* CONFIG_MACH_TYPE */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200514 else if (!strcmp("tegra124", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100515 tdx_hw_tag.prodid = APALIS_TK1_2GB;
516 } else if (!strcmp("vf500", soc)) {
517 if (it == 'y' || it == 'Y')
518 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
519 else
520 tdx_hw_tag.prodid = COLIBRI_VF50;
521 } else if (!strcmp("vf610", soc)) {
522 if (it == 'y' || it == 'Y')
523 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
524 else
525 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner68f58782019-04-09 17:24:08 +0200526 }
527
528 if (!tdx_hw_tag.prodid) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100529 printf("Module type not detectable due to unknown SoC\n");
530 return -1;
531 }
532
533 while (len < 4) {
534 sprintf(message, "Enter the module version (e.g. V1.1B): V");
535 len = cli_readline(message);
536 }
537
538 tdx_hw_tag.ver_major = console_buffer[0] - '0';
539 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
540 tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
541
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200542 if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100543 tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
544 COLIBRI_PXA270_V1_312MHZ);
545
546 while (len < 8) {
547 sprintf(message, "Enter module serial number: ");
548 len = cli_readline(message);
549 }
550
Simon Glassff9b9032021-07-24 09:03:30 -0600551 tdx_serial = dectoul(console_buffer, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100552
553 return 0;
554}
555
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300556static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
557 u32 *serial)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100558{
Denys Drozdov3a1c6572021-04-07 15:28:24 +0300559 char revision[3] = {barcode[6], barcode[7], '\0'};
560
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100561 if (strlen(barcode) < 16) {
562 printf("Argument too short, barcode is 16 chars long\n");
563 return -1;
564 }
565
566 /* Get hardware information from the first 8 digits */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300567 tag->ver_major = barcode[4] - '0';
568 tag->ver_minor = barcode[5] - '0';
Simon Glassff9b9032021-07-24 09:03:30 -0600569 tag->ver_assembly = dectoul(revision, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100570
571 barcode[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600572 tag->prodid = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100573
574 /* Parse second part of the barcode (serial number */
575 barcode += 8;
Simon Glassff9b9032021-07-24 09:03:30 -0600576 *serial = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100577
578 return 0;
579}
580
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300581static int write_tag(u8 *config_block, int *offset, int tag_id,
582 u8 *tag_data, size_t tag_data_size)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100583{
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100584 struct toradex_tag *tag;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300585
586 if (!offset || !config_block)
587 return -EINVAL;
588
589 tag = (struct toradex_tag *)(config_block + *offset);
590 tag->id = tag_id;
591 tag->flags = TAG_FLAG_VALID;
592 /* len is provided as number of 32bit values after the tag */
593 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
594 *offset += sizeof(struct toradex_tag);
595 if (tag_data && tag_data_size) {
596 memcpy(config_block + *offset, tag_data,
597 tag_data_size);
598 *offset += tag_data_size;
599 }
600
601 return 0;
602}
603
604#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
605int read_tdx_cfg_block_carrier(void)
606{
607 int ret = 0;
608 u8 *config_block = NULL;
609 struct toradex_tag *tag;
610 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
611 int offset;
612
613 /* Allocate RAM area for carrier config block */
614 config_block = memalign(ARCH_DMA_MINALIGN, size);
615 if (!config_block) {
616 printf("Not enough malloc space available!\n");
617 return -ENOMEM;
618 }
619
620 memset(config_block, 0, size);
621
622 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
623 size);
624 if (ret)
625 return ret;
626
627 /* Expect a valid tag first */
628 tag = (struct toradex_tag *)config_block;
629 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
630 valid_cfgblock_carrier = false;
631 ret = -EINVAL;
632 goto out;
633 }
634 valid_cfgblock_carrier = true;
635 offset = 4;
636
637 while (offset + sizeof(struct toradex_tag) +
638 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
639 tag = (struct toradex_tag *)(config_block + offset);
640 offset += 4;
641 if (tag->id == TAG_INVALID)
642 break;
643
644 if (tag->flags == TAG_FLAG_VALID) {
645 switch (tag->id) {
646 case TAG_CAR_SERIAL:
647 memcpy(&tdx_car_serial, config_block + offset,
648 sizeof(tdx_car_serial));
649 break;
650 case TAG_HW:
651 memcpy(&tdx_car_hw_tag, config_block +
652 offset, 8);
653 break;
654 }
655 }
656
657 /* Get to next tag according to current tags length */
658 offset += tag->len * 4;
659 }
660out:
661 free(config_block);
662 return ret;
663}
664
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300665int check_pid8_sanity(char *pid8)
666{
667 char s_carrierid_verdin_dev[5];
668 char s_carrierid_dahlia[5];
669
670 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
671 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
672
673 /* sane value check, first 4 chars which represent carrier id */
674 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
675 return 0;
676
677 if (!strncmp(pid8, s_carrierid_dahlia, 4))
678 return 0;
679
680 return -EINVAL;
681}
682
683int try_migrate_tdx_cfg_block_carrier(void)
684{
685 char pid8[8];
686 int offset = 0;
687 int ret = CMD_RET_SUCCESS;
688 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
689 u8 *config_block;
690
691 memset(pid8, 0x0, 8);
692 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
693 if (ret)
694 return ret;
695
696 if (check_pid8_sanity(pid8))
697 return -EINVAL;
698
699 /* Allocate RAM area for config block */
700 config_block = memalign(ARCH_DMA_MINALIGN, size);
701 if (!config_block) {
702 printf("Not enough malloc space available!\n");
703 return CMD_RET_FAILURE;
704 }
705
706 memset(config_block, 0xff, size);
707 /* we try parse PID8 concatenating zeroed serial number */
708 tdx_car_hw_tag.ver_major = pid8[4] - '0';
709 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
710 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
711
712 pid8[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600713 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300714
715 /* Valid Tag */
716 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
717
718 /* Product Tag */
719 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
720 sizeof(tdx_car_hw_tag));
721
722 /* Serial Tag */
723 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
724 sizeof(tdx_car_serial));
725
726 memset(config_block + offset, 0, 32 - offset);
727 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
728 size);
729 if (ret) {
730 printf("Failed to write Toradex Extra config block: %d\n",
731 ret);
732 ret = CMD_RET_FAILURE;
733 goto out;
734 }
735
736 printf("Successfully migrated to Toradex Config Block from PID8\n");
737
738out:
739 free(config_block);
740 return ret;
741}
742
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300743static int get_cfgblock_carrier_interactive(void)
744{
745 char message[CONFIG_SYS_CBSIZE];
746 int len;
747
748 printf("Supported carrier boards:\n");
749 printf("CARRIER BOARD NAME\t\t [ID]\n");
750 for (int i = 0; i < sizeof(toradex_carrier_boards) /
751 sizeof(toradex_carrier_boards[0]); i++)
752 if (toradex_carrier_boards[i])
753 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
754
755 sprintf(message, "Choose your carrier board (provide ID): ");
756 len = cli_readline(message);
Simon Glassff9b9032021-07-24 09:03:30 -0600757 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300758
759 do {
760 sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
761 len = cli_readline(message);
762 } while (len < 4);
763
764 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
765 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
766 tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
767
768 while (len < 8) {
769 sprintf(message, "Enter carrier board serial number: ");
770 len = cli_readline(message);
771 }
772
Simon Glassff9b9032021-07-24 09:03:30 -0600773 tdx_car_serial = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300774
775 return 0;
776}
777
778static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
779 char * const argv[])
780{
781 u8 *config_block;
782 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100783 int offset = 0;
784 int ret = CMD_RET_SUCCESS;
785 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100786 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100787
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300788 if (argc >= 3) {
789 if (argv[2][0] == '-' && argv[2][1] == 'y')
790 force_overwrite = 1;
791 }
792
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100793 /* Allocate RAM area for config block */
794 config_block = memalign(ARCH_DMA_MINALIGN, size);
795 if (!config_block) {
796 printf("Not enough malloc space available!\n");
797 return CMD_RET_FAILURE;
798 }
799
800 memset(config_block, 0xff, size);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300801 read_tdx_cfg_block_carrier();
802 if (valid_cfgblock_carrier && !force_overwrite) {
803 char message[CONFIG_SYS_CBSIZE];
804
805 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
806
807 if (!cli_readline(message))
808 goto out;
809
810 if (console_buffer[0] != 'y' &&
811 console_buffer[0] != 'Y')
812 goto out;
813 }
814
815 if (argc < 3 || (force_overwrite && argc < 4)) {
816 err = get_cfgblock_carrier_interactive();
817 } else {
818 if (force_overwrite)
819 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
820 &tdx_car_serial);
821 else
822 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
823 &tdx_car_serial);
824 }
825
826 if (err) {
827 ret = CMD_RET_FAILURE;
828 goto out;
829 }
830
831 /* Valid Tag */
832 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
833
834 /* Product Tag */
835 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
836 sizeof(tdx_car_hw_tag));
837
838 /* Serial Tag */
839 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
840 sizeof(tdx_car_serial));
841
842 memset(config_block + offset, 0, 32 - offset);
843 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
844 size);
845 if (err) {
846 printf("Failed to write Toradex Extra config block: %d\n",
847 ret);
848 ret = CMD_RET_FAILURE;
849 goto out;
850 }
851
852 printf("Toradex Extra config block successfully written\n");
853
854out:
855 free(config_block);
856 return ret;
857}
858
859#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
860
861static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
862 char * const argv[])
863{
864 u8 *config_block;
865 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
866 int offset = 0;
867 int ret = CMD_RET_SUCCESS;
868 int err;
869 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100870
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100871 if (argc >= 3) {
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300872#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
873 if (!strcmp(argv[2], "carrier"))
874 return do_cfgblock_carrier_create(cmdtp, flag,
875 --argc, ++argv);
876#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100877 if (argv[2][0] == '-' && argv[2][1] == 'y')
878 force_overwrite = 1;
879 }
880
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300881 /* Allocate RAM area for config block */
882 config_block = memalign(ARCH_DMA_MINALIGN, size);
883 if (!config_block) {
884 printf("Not enough malloc space available!\n");
885 return CMD_RET_FAILURE;
886 }
887
888 memset(config_block, 0xff, size);
889
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100890 read_tdx_cfg_block();
891 if (valid_cfgblock) {
892#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
893 /*
894 * On NAND devices, recreation is only allowed if the page is
895 * empty (config block invalid...)
896 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200897 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500898 CONFIG_TDX_CFG_BLOCK_OFFSET /
899 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100900 goto out;
901#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
902 /*
903 * On NOR devices, recreation is only allowed if the sector is
904 * empty and write protection is off (config block invalid...)
905 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200906 printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100907 CONFIG_TDX_CFG_BLOCK_OFFSET);
908 goto out;
909#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100910 if (!force_overwrite) {
911 char message[CONFIG_SYS_CBSIZE];
912
913 sprintf(message,
914 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100915
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100916 if (!cli_readline(message))
917 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100918
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100919 if (console_buffer[0] != 'y' &&
920 console_buffer[0] != 'Y')
921 goto out;
922 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100923#endif
924 }
925
926 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100927 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100928 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100929 } else {
930 if (force_overwrite)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300931 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
932 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100933 else
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300934 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
935 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100936 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100937 if (err) {
938 ret = CMD_RET_FAILURE;
939 goto out;
940 }
941
942 /* Convert serial number to MAC address (the storage format) */
943 tdx_eth_addr.oui = htonl(0x00142dUL << 8);
944 tdx_eth_addr.nic = htonl(tdx_serial << 8);
945
946 /* Valid Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300947 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100948
949 /* Product Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300950 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
951 sizeof(tdx_hw_tag));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100952
953 /* MAC Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300954 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
955 sizeof(tdx_eth_addr));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100956
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100957 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100958#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
959 err = tdx_cfg_block_mmc_storage(config_block, 1);
960#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
961 err = write_tdx_cfg_block_to_nand(config_block);
962#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
963 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300964#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
965 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100966#else
967 err = -EINVAL;
968#endif
969 if (err) {
970 printf("Failed to write Toradex config block: %d\n", ret);
971 ret = CMD_RET_FAILURE;
972 goto out;
973 }
974
975 printf("Toradex config block successfully written\n");
976
977out:
978 free(config_block);
979 return ret;
980}
981
Simon Glassed38aef2020-05-10 11:40:03 -0600982static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
983 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100984{
985 int ret;
986
987 if (argc < 2)
988 return CMD_RET_USAGE;
989
990 if (!strcmp(argv[1], "create")) {
991 return do_cfgblock_create(cmdtp, flag, argc, argv);
992 } else if (!strcmp(argv[1], "reload")) {
993 ret = read_tdx_cfg_block();
994 if (ret) {
995 printf("Failed to reload Toradex config block: %d\n",
996 ret);
997 return CMD_RET_FAILURE;
998 }
999 return CMD_RET_SUCCESS;
1000 }
1001
1002 return CMD_RET_USAGE;
1003}
1004
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001005U_BOOT_CMD(
1006 cfgblock, 5, 0, do_cfgblock,
1007 "Toradex config block handling commands",
1008 "create [-y] [barcode] - (Re-)create Toradex config block\n"
1009 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1010 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001011);