blob: 9305709a3c0dc12aebdcbde702772d008e41ffc6 [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 Ziswiler2658c8e2019-04-09 17:25:32 +020016 defined(CONFIG_TARGET_COLIBRI_IMX6) || \
Marcel Ziswilereca26ba2020-01-28 14:42:24 +010017 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
18 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +020019 defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
20 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010021#include <asm/arch/sys_proto.h>
22#else
23#define is_cpu_type(cpu) (0)
24#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010025#include <cli.h>
26#include <console.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060027#include <env.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010028#include <flash.h>
29#include <malloc.h>
30#include <mmc.h>
31#include <nand.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060032#include <asm/mach-types.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010033
34DECLARE_GLOBAL_DATA_PTR;
35
36#define TAG_VALID 0xcf01
37#define TAG_MAC 0x0000
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030038#define TAG_CAR_SERIAL 0x0021
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010039#define TAG_HW 0x0008
40#define TAG_INVALID 0xffff
41
42#define TAG_FLAG_VALID 0x1
43
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030044#define TDX_EEPROM_ID_MODULE 0
45#define TDX_EEPROM_ID_CARRIER 1
46
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010047#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
48#define TDX_CFG_BLOCK_MAX_SIZE 512
49#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
50#define TDX_CFG_BLOCK_MAX_SIZE 64
51#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
52#define TDX_CFG_BLOCK_MAX_SIZE 64
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030053#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
54#define TDX_CFG_BLOCK_MAX_SIZE 64
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010055#else
56#error Toradex config block location not set
57#endif
58
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030059#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
60#define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
61#endif
62
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010063struct toradex_tag {
64 u32 len:14;
65 u32 flags:2;
66 u32 id:16;
67};
68
69bool valid_cfgblock;
70struct toradex_hw tdx_hw_tag;
71struct toradex_eth_addr tdx_eth_addr;
72u32 tdx_serial;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030073#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
74u32 tdx_car_serial;
75bool valid_cfgblock_carrier;
76struct toradex_hw tdx_car_hw_tag;
77#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010078
79const char * const toradex_modules[] = {
80 [0] = "UNKNOWN MODULE",
81 [1] = "Colibri PXA270 312MHz",
82 [2] = "Colibri PXA270 520MHz",
83 [3] = "Colibri PXA320 806MHz",
84 [4] = "Colibri PXA300 208MHz",
85 [5] = "Colibri PXA310 624MHz",
86 [6] = "Colibri PXA320 806MHz IT",
87 [7] = "Colibri PXA300 208MHz XT",
88 [8] = "Colibri PXA270 312MHz",
89 [9] = "Colibri PXA270 520MHz",
90 [10] = "Colibri VF50 128MB", /* not currently on sale */
91 [11] = "Colibri VF61 256MB",
92 [12] = "Colibri VF61 256MB IT",
93 [13] = "Colibri VF50 128MB IT",
94 [14] = "Colibri iMX6 Solo 256MB",
95 [15] = "Colibri iMX6 DualLite 512MB",
96 [16] = "Colibri iMX6 Solo 256MB IT",
97 [17] = "Colibri iMX6 DualLite 512MB IT",
98 [18] = "UNKNOWN MODULE",
99 [19] = "UNKNOWN MODULE",
100 [20] = "Colibri T20 256MB",
101 [21] = "Colibri T20 512MB",
102 [22] = "Colibri T20 512MB IT",
103 [23] = "Colibri T30 1GB",
104 [24] = "Colibri T20 256MB IT",
105 [25] = "Apalis T30 2GB",
106 [26] = "Apalis T30 1GB",
107 [27] = "Apalis iMX6 Quad 1GB",
108 [28] = "Apalis iMX6 Quad 2GB IT",
109 [29] = "Apalis iMX6 Dual 512MB",
110 [30] = "Colibri T30 1GB IT",
111 [31] = "Apalis T30 1GB IT",
112 [32] = "Colibri iMX7 Solo 256MB",
113 [33] = "Colibri iMX7 Dual 512MB",
114 [34] = "Apalis TK1 2GB",
115 [35] = "Apalis iMX6 Dual 1GB IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200116 [36] = "Colibri iMX6ULL 256MB",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200117 [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
118 [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200119 [39] = "Colibri iMX7 Dual 1GB (eMMC)",
Marcel Ziswiler2658c8e2019-04-09 17:25:32 +0200120 [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
Stefan Agner01875e92018-05-30 19:01:47 +0200121 [41] = "Colibri iMX7 Dual 512MB EPDC",
122 [42] = "Apalis TK1 4GB",
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200123 [43] = "Colibri T20 512MB IT SETEK",
124 [44] = "Colibri iMX6ULL 512MB IT",
125 [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
Marcel Ziswilerf60ffec2019-04-09 17:25:33 +0200126 [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
127 [47] = "Apalis iMX8 QuadMax 4GB IT",
128 [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
129 [49] = "Apalis iMX8 QuadPlus 2GB",
130 [50] = "Colibri iMX8 QuadXPlus 2GB IT",
131 [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
132 [52] = "Colibri iMX8 DualX 1GB",
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100133 [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
134 [54] = "Apalis iMX8 DualXPlus 1GB",
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100135 [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200136 [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100137 [57] = "Verdin iMX8M Mini DualLite 1GB",
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200138 [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
139 [59] = "Verdin iMX8M Mini Quad 2GB IT",
140 [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
141 [61] = "Verdin iMX8M Plus Quad 2GB",
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200142 [62] = "Colibri iMX6ULL 1GB IT (eMMC)",
143 [63] = "Verdin iMX8M Plus Quad 4GB IT",
144 [64] = "Verdin iMX8M Plus Quad 2GB Wi-Fi / BT IT",
145 [65] = "Verdin iMX8M Plus QuadLite 1GB IT",
146 [66] = "Verdin iMX8M Plus Quad 8GB Wi-Fi / BT",
Philippe Schenker48b37032022-05-09 18:58:15 +0200147 [67] = "Apalis iMX8 QuadMax 8GB Wi-Fi / BT IT",
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
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200162const u32 toradex_ouis[] = {
163 [0] = 0x00142dUL,
164 [1] = 0x8c06cbUL,
165};
166
167static u32 get_serial_from_mac(struct toradex_eth_addr *eth_addr)
168{
169 int i;
170 u32 oui = ntohl(eth_addr->oui) >> 8;
171 u32 nic = ntohl(eth_addr->nic) >> 8;
172
173 for (i = 0; i < ARRAY_SIZE(toradex_ouis); i++) {
174 if (toradex_ouis[i] == oui)
175 break;
176 }
177
178 return (u32)((i << 24) + nic);
179}
180
181void get_mac_from_serial(u32 tdx_serial, struct toradex_eth_addr *eth_addr)
182{
183 u8 oui_index = tdx_serial >> 24;
184 u32 nic = tdx_serial & GENMASK(23, 0);
185 u32 oui;
186
187 if (oui_index >= ARRAY_SIZE(toradex_ouis)) {
188 puts("Can't find OUI for this serial#\n");
189 oui_index = 0;
190 }
191
192 oui = toradex_ouis[oui_index];
193
194 eth_addr->oui = htonl(oui << 8);
195 eth_addr->nic = htonl(nic << 8);
196}
197
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100198#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
199static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
200{
201 struct mmc *mmc;
202 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
203 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
204 uint part = CONFIG_TDX_CFG_BLOCK_PART;
205 uint blk_start;
206 int ret = 0;
207
208 /* Read production parameter config block from eMMC */
209 mmc = find_mmc_device(dev);
210 if (!mmc) {
211 puts("No MMC card found\n");
212 ret = -ENODEV;
213 goto out;
214 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200215 if (mmc_init(mmc)) {
216 puts("MMC init failed\n");
217 return -EINVAL;
218 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600219 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100220 if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
221 puts("MMC partition switch failed\n");
222 ret = -ENODEV;
223 goto out;
224 }
225 }
226 if (offset < 0)
227 offset += mmc->capacity;
228 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
229
230 if (!write) {
231 /* Careful reads a whole block of 512 bytes into config_block */
232 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
233 (unsigned char *)config_block) != 1) {
234 ret = -EIO;
235 goto out;
236 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100237 } else {
238 /* Just writing one 512 byte block */
239 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
240 (unsigned char *)config_block) != 1) {
241 ret = -EIO;
242 goto out;
243 }
244 }
245
246out:
247 /* Switch back to regular eMMC user partition */
248 blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
249
250 return ret;
251}
252#endif
253
254#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
255static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
256{
257 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200258 struct mtd_info *mtd = get_nand_dev_by_index(0);
259
260 if (!mtd)
261 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100262
263 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200264 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500265 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
266 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100267}
268
269static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
270{
271 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
272
273 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500274 return nand_write_skip_bad(get_nand_dev_by_index(0),
275 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100276 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
277 config_block, WITH_WR_VERIFY);
278}
279#endif
280
281#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
282static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
283{
284 /* Read production parameter config block from NOR flash */
285 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
286 TDX_CFG_BLOCK_MAX_SIZE);
287 return 0;
288}
289
290static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
291{
292 /* Write production parameter config block to NOR flash */
293 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
294 TDX_CFG_BLOCK_MAX_SIZE);
295}
296#endif
297
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300298#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
299static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
300{
301 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
302 TDX_CFG_BLOCK_MAX_SIZE);
303}
304
305static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
306{
307 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
308 TDX_CFG_BLOCK_MAX_SIZE);
309}
310#endif
311
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100312int read_tdx_cfg_block(void)
313{
314 int ret = 0;
315 u8 *config_block = NULL;
316 struct toradex_tag *tag;
317 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
318 int offset;
319
320 /* Allocate RAM area for config block */
321 config_block = memalign(ARCH_DMA_MINALIGN, size);
322 if (!config_block) {
323 printf("Not enough malloc space available!\n");
324 return -ENOMEM;
325 }
326
327 memset(config_block, 0, size);
328
329#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
330 ret = tdx_cfg_block_mmc_storage(config_block, 0);
331#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
332 ret = read_tdx_cfg_block_from_nand(config_block);
333#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
334 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300335#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
336 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100337#else
338 ret = -EINVAL;
339#endif
340 if (ret)
341 goto out;
342
343 /* Expect a valid tag first */
344 tag = (struct toradex_tag *)config_block;
345 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
346 valid_cfgblock = false;
347 ret = -EINVAL;
348 goto out;
349 }
350 valid_cfgblock = true;
351 offset = 4;
352
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300353 /*
354 * check if there is enough space for storing tag and value of the
355 * biggest element
356 */
357 while (offset + sizeof(struct toradex_tag) +
358 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100359 tag = (struct toradex_tag *)(config_block + offset);
360 offset += 4;
361 if (tag->id == TAG_INVALID)
362 break;
363
364 if (tag->flags == TAG_FLAG_VALID) {
365 switch (tag->id) {
366 case TAG_MAC:
367 memcpy(&tdx_eth_addr, config_block + offset,
368 6);
369
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200370 tdx_serial = get_serial_from_mac(&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100371 break;
372 case TAG_HW:
373 memcpy(&tdx_hw_tag, config_block + offset, 8);
374 break;
375 }
376 }
377
378 /* Get to next tag according to current tags length */
379 offset += tag->len * 4;
380 }
381
382 /* Cap product id to avoid issues with a yet unknown one */
Marcel Ziswiler8c9127c2019-03-25 17:18:29 +0100383 if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100384 sizeof(toradex_modules[0])))
385 tdx_hw_tag.prodid = 0;
386
387out:
388 free(config_block);
389 return ret;
390}
391
Philippe Schenker498f95a2022-06-13 19:35:23 +0200392static int parse_assembly_string(char *string_to_parse, u16 *assembly)
393{
394 if (string_to_parse[3] >= 'A' && string_to_parse[3] <= 'Z')
395 *assembly = string_to_parse[3] - 'A';
396 else if (string_to_parse[3] == '#')
397 *assembly = dectoul(&string_to_parse[4], NULL);
398 else
399 return -EINVAL;
400
401 return 0;
402}
403
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100404static int get_cfgblock_interactive(void)
405{
406 char message[CONFIG_SYS_CBSIZE];
407 char *soc;
408 char it = 'n';
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200409 char wb = 'n';
Philippe Schenker48b37032022-05-09 18:58:15 +0200410 char mem8g = 'n';
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100411 int len = 0;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200412 int ret = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100413
Stefan Agner68f58782019-04-09 17:24:08 +0200414 /* Unknown module by default */
415 tdx_hw_tag.prodid = 0;
416
Marcel Ziswiler384ca7f2022-04-13 11:33:32 +0200417 sprintf(message, "Is the module an IT version? [y/N] ");
Denys Drozdov8f6eaa92021-10-06 18:55:33 +0200418
419 len = cli_readline(message);
420 it = console_buffer[0];
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100421
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200422#if defined(CONFIG_TARGET_APALIS_IMX8) || \
423 defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200424 defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
425 defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
426 defined(CONFIG_TARGET_VERDIN_IMX8MP)
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200427 sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
428 len = cli_readline(message);
429 wb = console_buffer[0];
Philippe Schenker48b37032022-05-09 18:58:15 +0200430
431#if defined(CONFIG_TARGET_APALIS_IMX8)
432 if ((wb == 'y' || wb == 'Y') && (it == 'y' || it == 'Y')) {
433 sprintf(message, "Does your module have 8GB of RAM? [y/N] ");
434 len = cli_readline(message);
435 mem8g = console_buffer[0];
436 }
437#endif
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200438#endif
439
Simon Glass64b723f2017-08-03 12:22:12 -0600440 soc = env_get("soc");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100441 if (!strcmp("mx6", soc)) {
Stefan Agner68f58782019-04-09 17:24:08 +0200442#ifdef CONFIG_TARGET_APALIS_IMX6
443 if (it == 'y' || it == 'Y') {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100444 if (is_cpu_type(MXC_CPU_MX6Q))
445 tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
446 else
447 tdx_hw_tag.prodid = APALIS_IMX6D_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200448 } else {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100449 if (is_cpu_type(MXC_CPU_MX6Q))
450 tdx_hw_tag.prodid = APALIS_IMX6Q;
451 else
452 tdx_hw_tag.prodid = APALIS_IMX6D;
Stefan Agner68f58782019-04-09 17:24:08 +0200453 }
454#elif CONFIG_TARGET_COLIBRI_IMX6
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200455 if (it == 'y' || it == 'Y') {
Stefan Agner68f58782019-04-09 17:24:08 +0200456 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100457 tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
Stefan Agner68f58782019-04-09 17:24:08 +0200458 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100459 tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200460 } else {
Stefan Agner68f58782019-04-09 17:24:08 +0200461 if (is_cpu_type(MXC_CPU_MX6DL))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100462 tdx_hw_tag.prodid = COLIBRI_IMX6DL;
Stefan Agner68f58782019-04-09 17:24:08 +0200463 else if (is_cpu_type(MXC_CPU_MX6SOLO))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100464 tdx_hw_tag.prodid = COLIBRI_IMX6S;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200465 }
Stefan Agner68f58782019-04-09 17:24:08 +0200466#elif CONFIG_TARGET_COLIBRI_IMX6ULL
Stefan Agner68f58782019-04-09 17:24:08 +0200467 if (it == 'y' || it == 'Y') {
468 if (wb == 'y' || wb == 'Y')
469 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
470 else
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200471 if (gd->ram_size == 0x20000000)
472 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
473 else
474 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT_EMMC;
Stefan Agner68f58782019-04-09 17:24:08 +0200475 } else {
476 if (wb == 'y' || wb == 'Y')
477 tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
478 else
479 tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
480 }
481#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200482 } else if (!strcmp("imx7d", soc))
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200483 if (gd->ram_size == 0x20000000)
484 tdx_hw_tag.prodid = COLIBRI_IMX7D;
485 else
486 tdx_hw_tag.prodid = COLIBRI_IMX7D_EMMC;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200487 else if (!strcmp("imx7s", soc))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100488 tdx_hw_tag.prodid = COLIBRI_IMX7S;
Marcel Ziswiler00320612019-07-12 12:35:08 +0200489 else if (is_cpu_type(MXC_CPU_IMX8QM)) {
490 if (it == 'y' || it == 'Y') {
Philippe Schenker48b37032022-05-09 18:58:15 +0200491 if (wb == 'y' || wb == 'Y') {
492 if (mem8g == 'y' || mem8g == 'Y')
493 tdx_hw_tag.prodid = APALIS_IMX8QM_8GB_WIFI_BT_IT;
494 else
495 tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
496 }
Marcel Ziswiler00320612019-07-12 12:35:08 +0200497 else
498 tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
499 } else {
500 if (wb == 'y' || wb == 'Y')
501 tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
502 else
503 tdx_hw_tag.prodid = APALIS_IMX8QP;
504 }
505 } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
Denys Drozdov00b1e932022-04-13 11:33:25 +0200506#ifdef CONFIG_TARGET_COLIBRI_IMX8X
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200507 if (it == 'y' || it == 'Y') {
508 if (wb == 'y' || wb == 'Y')
509 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
510 else
511 tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
512 } else {
513 if (wb == 'y' || wb == 'Y')
514 tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
515 else
516 tdx_hw_tag.prodid = COLIBRI_IMX8DX;
517 }
Marcel Ziswiler75b93272020-01-28 14:42:23 +0100518#endif
Marcel Ziswiler3834f662020-10-28 11:58:15 +0200519 } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
520 if (wb == 'y' || wb == 'Y')
521 tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
522 else
523 tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200524 } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
Marcel Ziswiler3834f662020-10-28 11:58:15 +0200525 if (wb == 'y' || wb == 'Y')
526 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
527 else
528 tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200529 } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
530 tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200531 } else if (is_cpu_type(MXC_CPU_IMX8MPL)) {
532 tdx_hw_tag.prodid = VERDIN_IMX8MPQL_IT;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200533 } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
534 if (wb == 'y' || wb == 'Y')
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200535 if (gd->ram_size == 0x80000000)
536 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_2GB_WIFI_BT_IT;
537 else if (gd->ram_size == 0x200000000)
538 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_8GB_WIFI_BT;
539 else
540 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
Marcel Ziswiler4a9c7022020-10-28 11:58:08 +0200541 else
Marcel Ziswilerc066a442021-10-06 18:55:34 +0200542 if (it == 'y' || it == 'Y')
543 tdx_hw_tag.prodid = VERDIN_IMX8MPQ_IT;
544 else
545 tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
Marcel Ziswiler7e2954f2019-07-12 12:35:07 +0200546 } else if (!strcmp("tegra20", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100547 if (it == 'y' || it == 'Y')
548 if (gd->ram_size == 0x10000000)
549 tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
550 else
551 tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
552 else
553 if (gd->ram_size == 0x10000000)
554 tdx_hw_tag.prodid = COLIBRI_T20_256MB;
555 else
556 tdx_hw_tag.prodid = COLIBRI_T20_512MB;
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200557 }
Tom Rinieb6aadc2021-08-30 09:16:32 -0400558#if defined(CONFIG_TARGET_APALIS_T30) || defined(CONFIG_TARGET_COLIBRI_T30)
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200559 else if (!strcmp("tegra30", soc)) {
Tom Rinieb6aadc2021-08-30 09:16:32 -0400560#ifdef CONFIG_TARGET_APALIS_T30
561 if (it == 'y' || it == 'Y')
562 tdx_hw_tag.prodid = APALIS_T30_IT;
563 else
564 if (gd->ram_size == 0x40000000)
565 tdx_hw_tag.prodid = APALIS_T30_1GB;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100566 else
Tom Rinieb6aadc2021-08-30 09:16:32 -0400567 tdx_hw_tag.prodid = APALIS_T30_2GB;
568#else
569 if (it == 'y' || it == 'Y')
570 tdx_hw_tag.prodid = COLIBRI_T30_IT;
571 else
572 tdx_hw_tag.prodid = COLIBRI_T30;
573#endif
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200574 }
Tom Rinieb6aadc2021-08-30 09:16:32 -0400575#endif /* CONFIG_TARGET_APALIS_T30 || CONFIG_TARGET_COLIBRI_T30 */
Gerard Salvatella5ab3b1d2019-04-09 17:24:07 +0200576 else if (!strcmp("tegra124", soc)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100577 tdx_hw_tag.prodid = APALIS_TK1_2GB;
578 } else if (!strcmp("vf500", soc)) {
579 if (it == 'y' || it == 'Y')
580 tdx_hw_tag.prodid = COLIBRI_VF50_IT;
581 else
582 tdx_hw_tag.prodid = COLIBRI_VF50;
583 } else if (!strcmp("vf610", soc)) {
584 if (it == 'y' || it == 'Y')
585 tdx_hw_tag.prodid = COLIBRI_VF61_IT;
586 else
587 tdx_hw_tag.prodid = COLIBRI_VF61;
Stefan Agner68f58782019-04-09 17:24:08 +0200588 }
589
590 if (!tdx_hw_tag.prodid) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100591 printf("Module type not detectable due to unknown SoC\n");
592 return -1;
593 }
594
595 while (len < 4) {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200596 sprintf(message, "Enter the module version (e.g. V1.1B or V1.1#26): V");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100597 len = cli_readline(message);
598 }
599
600 tdx_hw_tag.ver_major = console_buffer[0] - '0';
601 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200602
603 ret = parse_assembly_string(console_buffer, &tdx_hw_tag.ver_assembly);
604 if (ret) {
605 printf("Parsing module version failed\n");
606 return ret;
607 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100608
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100609 while (len < 8) {
610 sprintf(message, "Enter module serial number: ");
611 len = cli_readline(message);
612 }
613
Simon Glassff9b9032021-07-24 09:03:30 -0600614 tdx_serial = dectoul(console_buffer, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100615
616 return 0;
617}
618
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300619static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
620 u32 *serial)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100621{
Denys Drozdov3a1c6572021-04-07 15:28:24 +0300622 char revision[3] = {barcode[6], barcode[7], '\0'};
623
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100624 if (strlen(barcode) < 16) {
625 printf("Argument too short, barcode is 16 chars long\n");
626 return -1;
627 }
628
629 /* Get hardware information from the first 8 digits */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300630 tag->ver_major = barcode[4] - '0';
631 tag->ver_minor = barcode[5] - '0';
Simon Glassff9b9032021-07-24 09:03:30 -0600632 tag->ver_assembly = dectoul(revision, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100633
634 barcode[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600635 tag->prodid = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100636
637 /* Parse second part of the barcode (serial number */
638 barcode += 8;
Simon Glassff9b9032021-07-24 09:03:30 -0600639 *serial = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100640
641 return 0;
642}
643
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300644static int write_tag(u8 *config_block, int *offset, int tag_id,
645 u8 *tag_data, size_t tag_data_size)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100646{
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100647 struct toradex_tag *tag;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300648
649 if (!offset || !config_block)
650 return -EINVAL;
651
652 tag = (struct toradex_tag *)(config_block + *offset);
653 tag->id = tag_id;
654 tag->flags = TAG_FLAG_VALID;
655 /* len is provided as number of 32bit values after the tag */
656 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
657 *offset += sizeof(struct toradex_tag);
658 if (tag_data && tag_data_size) {
659 memcpy(config_block + *offset, tag_data,
660 tag_data_size);
661 *offset += tag_data_size;
662 }
663
664 return 0;
665}
666
667#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
668int read_tdx_cfg_block_carrier(void)
669{
670 int ret = 0;
671 u8 *config_block = NULL;
672 struct toradex_tag *tag;
673 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
674 int offset;
675
676 /* Allocate RAM area for carrier config block */
677 config_block = memalign(ARCH_DMA_MINALIGN, size);
678 if (!config_block) {
679 printf("Not enough malloc space available!\n");
680 return -ENOMEM;
681 }
682
683 memset(config_block, 0, size);
684
685 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
686 size);
687 if (ret)
688 return ret;
689
690 /* Expect a valid tag first */
691 tag = (struct toradex_tag *)config_block;
692 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
693 valid_cfgblock_carrier = false;
694 ret = -EINVAL;
695 goto out;
696 }
697 valid_cfgblock_carrier = true;
698 offset = 4;
699
700 while (offset + sizeof(struct toradex_tag) +
701 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
702 tag = (struct toradex_tag *)(config_block + offset);
703 offset += 4;
704 if (tag->id == TAG_INVALID)
705 break;
706
707 if (tag->flags == TAG_FLAG_VALID) {
708 switch (tag->id) {
709 case TAG_CAR_SERIAL:
710 memcpy(&tdx_car_serial, config_block + offset,
711 sizeof(tdx_car_serial));
712 break;
713 case TAG_HW:
714 memcpy(&tdx_car_hw_tag, config_block +
715 offset, 8);
716 break;
717 }
718 }
719
720 /* Get to next tag according to current tags length */
721 offset += tag->len * 4;
722 }
723out:
724 free(config_block);
725 return ret;
726}
727
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300728int check_pid8_sanity(char *pid8)
729{
730 char s_carrierid_verdin_dev[5];
731 char s_carrierid_dahlia[5];
732
733 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
734 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
735
736 /* sane value check, first 4 chars which represent carrier id */
737 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
738 return 0;
739
740 if (!strncmp(pid8, s_carrierid_dahlia, 4))
741 return 0;
742
743 return -EINVAL;
744}
745
746int try_migrate_tdx_cfg_block_carrier(void)
747{
748 char pid8[8];
749 int offset = 0;
750 int ret = CMD_RET_SUCCESS;
751 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
752 u8 *config_block;
753
754 memset(pid8, 0x0, 8);
755 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
756 if (ret)
757 return ret;
758
759 if (check_pid8_sanity(pid8))
760 return -EINVAL;
761
762 /* Allocate RAM area for config block */
763 config_block = memalign(ARCH_DMA_MINALIGN, size);
764 if (!config_block) {
765 printf("Not enough malloc space available!\n");
766 return CMD_RET_FAILURE;
767 }
768
769 memset(config_block, 0xff, size);
770 /* we try parse PID8 concatenating zeroed serial number */
771 tdx_car_hw_tag.ver_major = pid8[4] - '0';
772 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
773 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
774
775 pid8[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600776 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300777
778 /* Valid Tag */
779 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
780
781 /* Product Tag */
782 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
783 sizeof(tdx_car_hw_tag));
784
785 /* Serial Tag */
786 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
787 sizeof(tdx_car_serial));
788
789 memset(config_block + offset, 0, 32 - offset);
790 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
791 size);
792 if (ret) {
793 printf("Failed to write Toradex Extra config block: %d\n",
794 ret);
795 ret = CMD_RET_FAILURE;
796 goto out;
797 }
798
799 printf("Successfully migrated to Toradex Config Block from PID8\n");
800
801out:
802 free(config_block);
803 return ret;
804}
805
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300806static int get_cfgblock_carrier_interactive(void)
807{
808 char message[CONFIG_SYS_CBSIZE];
809 int len;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200810 int ret = 0;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300811
812 printf("Supported carrier boards:\n");
813 printf("CARRIER BOARD NAME\t\t [ID]\n");
814 for (int i = 0; i < sizeof(toradex_carrier_boards) /
815 sizeof(toradex_carrier_boards[0]); i++)
816 if (toradex_carrier_boards[i])
817 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
818
819 sprintf(message, "Choose your carrier board (provide ID): ");
820 len = cli_readline(message);
Simon Glassff9b9032021-07-24 09:03:30 -0600821 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300822
823 do {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200824 sprintf(message, "Enter carrier board version (e.g. V1.1B or V1.1#26): V");
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300825 len = cli_readline(message);
826 } while (len < 4);
827
828 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
829 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200830
831 ret = parse_assembly_string(console_buffer, &tdx_car_hw_tag.ver_assembly);
832 if (ret) {
833 printf("Parsing module version failed\n");
834 return ret;
835 }
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300836
837 while (len < 8) {
838 sprintf(message, "Enter carrier board serial number: ");
839 len = cli_readline(message);
840 }
841
Simon Glassff9b9032021-07-24 09:03:30 -0600842 tdx_car_serial = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300843
844 return 0;
845}
846
847static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
848 char * const argv[])
849{
850 u8 *config_block;
851 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100852 int offset = 0;
853 int ret = CMD_RET_SUCCESS;
854 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100855 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100856
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300857 if (argc >= 3) {
858 if (argv[2][0] == '-' && argv[2][1] == 'y')
859 force_overwrite = 1;
860 }
861
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100862 /* Allocate RAM area for config block */
863 config_block = memalign(ARCH_DMA_MINALIGN, size);
864 if (!config_block) {
865 printf("Not enough malloc space available!\n");
866 return CMD_RET_FAILURE;
867 }
868
869 memset(config_block, 0xff, size);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300870 read_tdx_cfg_block_carrier();
871 if (valid_cfgblock_carrier && !force_overwrite) {
872 char message[CONFIG_SYS_CBSIZE];
873
874 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
875
876 if (!cli_readline(message))
877 goto out;
878
879 if (console_buffer[0] != 'y' &&
880 console_buffer[0] != 'Y')
881 goto out;
882 }
883
884 if (argc < 3 || (force_overwrite && argc < 4)) {
885 err = get_cfgblock_carrier_interactive();
886 } else {
887 if (force_overwrite)
888 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
889 &tdx_car_serial);
890 else
891 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
892 &tdx_car_serial);
893 }
894
895 if (err) {
896 ret = CMD_RET_FAILURE;
897 goto out;
898 }
899
900 /* Valid Tag */
901 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
902
903 /* Product Tag */
904 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
905 sizeof(tdx_car_hw_tag));
906
907 /* Serial Tag */
908 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
909 sizeof(tdx_car_serial));
910
911 memset(config_block + offset, 0, 32 - offset);
912 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
913 size);
914 if (err) {
915 printf("Failed to write Toradex Extra config block: %d\n",
916 ret);
917 ret = CMD_RET_FAILURE;
918 goto out;
919 }
920
921 printf("Toradex Extra config block successfully written\n");
922
923out:
924 free(config_block);
925 return ret;
926}
927
928#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
929
930static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
931 char * const argv[])
932{
933 u8 *config_block;
934 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
935 int offset = 0;
936 int ret = CMD_RET_SUCCESS;
937 int err;
938 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100939
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100940 if (argc >= 3) {
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300941#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
942 if (!strcmp(argv[2], "carrier"))
943 return do_cfgblock_carrier_create(cmdtp, flag,
944 --argc, ++argv);
945#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100946 if (argv[2][0] == '-' && argv[2][1] == 'y')
947 force_overwrite = 1;
948 }
949
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300950 /* Allocate RAM area for config block */
951 config_block = memalign(ARCH_DMA_MINALIGN, size);
952 if (!config_block) {
953 printf("Not enough malloc space available!\n");
954 return CMD_RET_FAILURE;
955 }
956
957 memset(config_block, 0xff, size);
958
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100959 read_tdx_cfg_block();
960 if (valid_cfgblock) {
961#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
962 /*
963 * On NAND devices, recreation is only allowed if the page is
964 * empty (config block invalid...)
965 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200966 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500967 CONFIG_TDX_CFG_BLOCK_OFFSET /
968 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100969 goto out;
970#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
971 /*
972 * On NOR devices, recreation is only allowed if the sector is
973 * empty and write protection is off (config block invalid...)
974 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200975 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 +0100976 CONFIG_TDX_CFG_BLOCK_OFFSET);
977 goto out;
978#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100979 if (!force_overwrite) {
980 char message[CONFIG_SYS_CBSIZE];
981
982 sprintf(message,
983 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100984
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100985 if (!cli_readline(message))
986 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100987
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100988 if (console_buffer[0] != 'y' &&
989 console_buffer[0] != 'Y')
990 goto out;
991 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100992#endif
993 }
994
995 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100996 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100997 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100998 } else {
999 if (force_overwrite)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001000 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
1001 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +01001002 else
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001003 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
1004 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +01001005 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001006 if (err) {
1007 ret = CMD_RET_FAILURE;
1008 goto out;
1009 }
1010
1011 /* Convert serial number to MAC address (the storage format) */
Philippe Schenkerd52a2572022-06-20 16:57:45 +02001012 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001013
1014 /* Valid Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001015 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001016
1017 /* Product Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001018 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
1019 sizeof(tdx_hw_tag));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001020
1021 /* MAC Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001022 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
1023 sizeof(tdx_eth_addr));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001024
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001025 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001026#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
1027 err = tdx_cfg_block_mmc_storage(config_block, 1);
1028#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
1029 err = write_tdx_cfg_block_to_nand(config_block);
1030#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
1031 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001032#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
1033 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001034#else
1035 err = -EINVAL;
1036#endif
1037 if (err) {
1038 printf("Failed to write Toradex config block: %d\n", ret);
1039 ret = CMD_RET_FAILURE;
1040 goto out;
1041 }
1042
1043 printf("Toradex config block successfully written\n");
1044
1045out:
1046 free(config_block);
1047 return ret;
1048}
1049
Simon Glassed38aef2020-05-10 11:40:03 -06001050static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
1051 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001052{
1053 int ret;
1054
1055 if (argc < 2)
1056 return CMD_RET_USAGE;
1057
1058 if (!strcmp(argv[1], "create")) {
1059 return do_cfgblock_create(cmdtp, flag, argc, argv);
1060 } else if (!strcmp(argv[1], "reload")) {
1061 ret = read_tdx_cfg_block();
1062 if (ret) {
1063 printf("Failed to reload Toradex config block: %d\n",
1064 ret);
1065 return CMD_RET_FAILURE;
1066 }
1067 return CMD_RET_SUCCESS;
1068 }
1069
1070 return CMD_RET_USAGE;
1071}
1072
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +03001073U_BOOT_CMD(
1074 cfgblock, 5, 0, do_cfgblock,
1075 "Toradex config block handling commands",
1076 "create [-y] [barcode] - (Re-)create Toradex config block\n"
1077 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
1078 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01001079);