blob: 3f2f6b943918e5f1e09e21e797593f0371d344a1 [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 Ziswiler7a28dfc2016-11-16 17:49:22 +010014#include <cli.h>
15#include <console.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060016#include <env.h>
Tom Rini063c9382022-07-23 13:05:03 -040017#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010018#include <flash.h>
Tom Rini063c9382022-07-23 13:05:03 -040019#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010020#include <malloc.h>
21#include <mmc.h>
22#include <nand.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060023#include <asm/mach-types.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010024
25DECLARE_GLOBAL_DATA_PTR;
26
27#define TAG_VALID 0xcf01
28#define TAG_MAC 0x0000
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030029#define TAG_CAR_SERIAL 0x0021
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010030#define TAG_HW 0x0008
31#define TAG_INVALID 0xffff
32
33#define TAG_FLAG_VALID 0x1
34
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030035#define TDX_EEPROM_ID_MODULE 0
36#define TDX_EEPROM_ID_CARRIER 1
37
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010038#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
39#define TDX_CFG_BLOCK_MAX_SIZE 512
40#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
41#define TDX_CFG_BLOCK_MAX_SIZE 64
42#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
43#define TDX_CFG_BLOCK_MAX_SIZE 64
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030044#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
45#define TDX_CFG_BLOCK_MAX_SIZE 64
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010046#else
47#error Toradex config block location not set
48#endif
49
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030050#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
51#define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
52#endif
53
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010054struct toradex_tag {
55 u32 len:14;
56 u32 flags:2;
57 u32 id:16;
58};
59
60bool valid_cfgblock;
61struct toradex_hw tdx_hw_tag;
62struct toradex_eth_addr tdx_eth_addr;
63u32 tdx_serial;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +030064#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
65u32 tdx_car_serial;
66bool valid_cfgblock_carrier;
67struct toradex_hw tdx_car_hw_tag;
68#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010069
Francesco Dolcinifed3bc72022-07-21 15:17:34 +020070#define TARGET_IS_ENABLED(x) IS_ENABLED(CONFIG_TARGET_ ## x)
71
72const struct toradex_som toradex_modules[] = {
Francesco Dolcini5afd8582022-07-21 15:17:37 +020073 [0] = { "UNKNOWN MODULE", 0 },
74 [1] = { "Colibri PXA270 312MHz", 0 },
75 [2] = { "Colibri PXA270 520MHz", 0 },
76 [3] = { "Colibri PXA320 806MHz", 0 },
77 [4] = { "Colibri PXA300 208MHz", 0 },
78 [5] = { "Colibri PXA310 624MHz", 0 },
79 [6] = { "Colibri PXA320IT 806MHz", 0 },
80 [7] = { "Colibri PXA300 208MHz XT", 0 },
81 [8] = { "Colibri PXA270 312MHz", 0 },
82 [9] = { "Colibri PXA270 520MHz", 0 },
83 [10] = { "Colibri VF50 128MB", TARGET_IS_ENABLED(COLIBRI_VF) },
84 [11] = { "Colibri VF61 256MB", TARGET_IS_ENABLED(COLIBRI_VF) },
85 [12] = { "Colibri VF61 256MB IT", TARGET_IS_ENABLED(COLIBRI_VF) },
86 [13] = { "Colibri VF50 128MB IT", TARGET_IS_ENABLED(COLIBRI_VF) },
87 [14] = { "Colibri iMX6S 256MB", TARGET_IS_ENABLED(COLIBRI_IMX6) },
88 [15] = { "Colibri iMX6DL 512MB", TARGET_IS_ENABLED(COLIBRI_IMX6) },
89 [16] = { "Colibri iMX6S 256MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6) },
90 [17] = { "Colibri iMX6DL 512MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6) },
91 [18] = { "UNKNOWN MODULE", 0 },
92 [19] = { "UNKNOWN MODULE", 0 },
93 [20] = { "Colibri T20 256MB", TARGET_IS_ENABLED(COLIBRI_T20) },
94 [21] = { "Colibri T20 512MB", TARGET_IS_ENABLED(COLIBRI_T20) },
95 [22] = { "Colibri T20 512MB IT", TARGET_IS_ENABLED(COLIBRI_T20) },
96 [23] = { "Colibri T30 1GB", TARGET_IS_ENABLED(COLIBRI_T30) },
97 [24] = { "Colibri T20 256MB IT", TARGET_IS_ENABLED(COLIBRI_T20) },
98 [25] = { "Apalis T30 2GB", TARGET_IS_ENABLED(APALIS_T30) },
99 [26] = { "Apalis T30 1GB", TARGET_IS_ENABLED(APALIS_T30) },
100 [27] = { "Apalis iMX6Q 1GB", TARGET_IS_ENABLED(APALIS_IMX6) },
101 [28] = { "Apalis iMX6Q 2GB IT", TARGET_IS_ENABLED(APALIS_IMX6) },
102 [29] = { "Apalis iMX6D 512MB", TARGET_IS_ENABLED(APALIS_IMX6) },
103 [30] = { "Colibri T30 1GB IT", TARGET_IS_ENABLED(COLIBRI_T30) },
104 [31] = { "Apalis T30 1GB IT", TARGET_IS_ENABLED(APALIS_T30) },
105 [32] = { "Colibri iMX7S 256MB", TARGET_IS_ENABLED(COLIBRI_IMX7) },
106 [33] = { "Colibri iMX7D 512MB", TARGET_IS_ENABLED(COLIBRI_IMX7) },
107 [34] = { "Apalis TK1 2GB", TARGET_IS_ENABLED(APALIS_TK1) },
108 [35] = { "Apalis iMX6D 1GB IT", TARGET_IS_ENABLED(APALIS_IMX6) },
109 [36] = { "Colibri iMX6ULL 256MB", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
110 [37] = { "Apalis iMX8QM 4GB WB IT", TARGET_IS_ENABLED(APALIS_IMX8) },
111 [38] = { "Colibri iMX8QXP 2GB WB IT", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
112 [39] = { "Colibri iMX7D 1GB", TARGET_IS_ENABLED(COLIBRI_IMX7) },
113 [40] = { "Colibri iMX6ULL 512MB WB IT", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
114 [41] = { "Colibri iMX7D 512MB EPDC", TARGET_IS_ENABLED(COLIBRI_IMX7) },
115 [42] = { "Apalis TK1 4GB", TARGET_IS_ENABLED(APALIS_TK1) },
116 [43] = { "Colibri T20 512MB IT SETEK", TARGET_IS_ENABLED(COLIBRI_T20) },
117 [44] = { "Colibri iMX6ULL 512MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
118 [45] = { "Colibri iMX6ULL 512MB WB", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
119 [46] = { "Apalis iMX8QXP 2GB WB IT", 0 },
120 [47] = { "Apalis iMX8QM 4GB IT", TARGET_IS_ENABLED(APALIS_IMX8) },
121 [48] = { "Apalis iMX8QP 2GB WB", TARGET_IS_ENABLED(APALIS_IMX8) },
122 [49] = { "Apalis iMX8QP 2GB", TARGET_IS_ENABLED(APALIS_IMX8) },
123 [50] = { "Colibri iMX8QXP 2GB IT", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
124 [51] = { "Colibri iMX8DX 1GB WB", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
125 [52] = { "Colibri iMX8DX 1GB", TARGET_IS_ENABLED(COLIBRI_IMX8X) },
126 [53] = { "Apalis iMX8QXP 2GB ECC IT", 0 },
127 [54] = { "Apalis iMX8DXP 1GB", TARGET_IS_ENABLED(APALIS_IMX8) },
128 [55] = { "Verdin iMX8M Mini Quad 2GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
129 [56] = { "Verdin iMX8M Nano Quad 1GB WB", 0 },
130 [57] = { "Verdin iMX8M Mini DualLite 1GB", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
131 [58] = { "Verdin iMX8M Plus Quad 4GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
132 [59] = { "Verdin iMX8M Mini Quad 2GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
133 [60] = { "Verdin iMX8M Mini DualLite 1GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
134 [61] = { "Verdin iMX8M Plus Quad 2GB", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
135 [62] = { "Colibri iMX6ULL 1GB IT", TARGET_IS_ENABLED(COLIBRI_IMX6ULL) },
136 [63] = { "Verdin iMX8M Plus Quad 4GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
137 [64] = { "Verdin iMX8M Plus Quad 2GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
138 [65] = { "Verdin iMX8M Plus QuadLite 1GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
139 [66] = { "Verdin iMX8M Plus Quad 8GB WB", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
140 [67] = { "Apalis iMX8QM 8GB WB IT", TARGET_IS_ENABLED(APALIS_IMX8) },
141 [68] = { "Verdin iMX8M Mini Quad 2GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
Marcel Ziswiler2bb00b82023-08-04 12:08:05 +0200142 [69] = { "Verdin AM62 Quad 1GB WB IT", TARGET_IS_ENABLED(VERDIN_AM62_A53) },
Emanuele Ghidoli898cdec2023-05-15 15:06:41 +0200143 [70] = { "Verdin iMX8M Plus Quad 8GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
Marcel Ziswiler2bb00b82023-08-04 12:08:05 +0200144 [71] = { "Verdin AM62 Solo 512MB", TARGET_IS_ENABLED(VERDIN_AM62_A53) },
145 [72] = { "Verdin AM62 Solo 512MB WB IT", TARGET_IS_ENABLED(VERDIN_AM62_A53) },
146 [73] = { "Verdin AM62 Dual 1GB ET", TARGET_IS_ENABLED(VERDIN_AM62_A53) },
147 [74] = { "Verdin AM62 Dual 1GB IT", TARGET_IS_ENABLED(VERDIN_AM62_A53) },
148 [75] = { "Verdin AM62 Dual 1GB WB IT", TARGET_IS_ENABLED(VERDIN_AM62_A53) },
149 [76] = { "Verdin AM62 Quad 2GB WB IT", TARGET_IS_ENABLED(VERDIN_AM62_A53) },
Joao Paulo Goncalvesbdd3ce42024-01-22 17:09:30 -0300150 [77] = { "Colibri iMX6S 256MB", TARGET_IS_ENABLED(COLIBRI_IMX6) },
151 [78] = { "Colibri iMX6S 256MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6) },
152 [79] = { "Colibri iMX6DL 512MB", TARGET_IS_ENABLED(COLIBRI_IMX6) },
153 [80] = { "Colibri iMX6DL 512MB IT", TARGET_IS_ENABLED(COLIBRI_IMX6) },
154 [81] = { "Colibri iMX7D 512MB", TARGET_IS_ENABLED(COLIBRI_IMX7) },
155 [82] = { "Apalis iMX6D 512MB", TARGET_IS_ENABLED(APALIS_IMX6) },
156 [83] = { "Apalis iMX6Q 1GB", TARGET_IS_ENABLED(APALIS_IMX6) },
157 [84] = { "Apalis iMX6D 1GB IT", TARGET_IS_ENABLED(APALIS_IMX6) },
158 [85] = { "Apalis iMX6Q 2GB IT", TARGET_IS_ENABLED(APALIS_IMX6) },
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100159};
160
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200161struct pid4list {
162 int pid4;
163 char * const name;
Igor Opaniuk414df352020-07-15 13:30:54 +0300164};
165
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200166const struct pid4list toradex_carrier_boards[] = {
167 /* the code assumes unknown at index 0 */
168 {0, "UNKNOWN CARRIER BOARD"},
169 {DAHLIA, "Dahlia"},
170 {VERDIN_DEVELOPMENT_BOARD, "Verdin Development Board"},
171 {YAVIA, "Yavia"},
172};
173
Max Krummenachercea09092023-07-18 11:07:34 +0200174const struct pid4list toradex_display_adapters[] = {
175 /* the code assumes unknown at index 0 */
176 {0, "UNKNOWN DISPLAY ADAPTER"},
177 {VERDIN_DSI_TO_HDMI_ADAPTER, "Verdin DSI to HDMI Adapter"},
178 {VERDIN_DSI_TO_LVDS_ADAPTER, "Verdin DSI to LVDS Adapter"},
Igor Opaniuk414df352020-07-15 13:30:54 +0300179};
180
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200181const u32 toradex_ouis[] = {
182 [0] = 0x00142dUL,
183 [1] = 0x8c06cbUL,
184};
185
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200186const char * const get_toradex_carrier_boards(int pid4)
187{
188 int i, index = 0;
189
190 for (i = 1; i < ARRAY_SIZE(toradex_carrier_boards); i++) {
191 if (pid4 == toradex_carrier_boards[i].pid4) {
192 index = i;
193 break;
194 }
195 }
196 return toradex_carrier_boards[index].name;
197}
198
Max Krummenachercea09092023-07-18 11:07:34 +0200199const char * const get_toradex_display_adapters(int pid4)
200{
201 int i, index = 0;
202
203 for (i = 1; i < ARRAY_SIZE(toradex_display_adapters); i++) {
204 if (pid4 == toradex_display_adapters[i].pid4) {
205 index = i;
206 break;
207 }
208 }
209 return toradex_display_adapters[index].name;
210}
211
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200212static u32 get_serial_from_mac(struct toradex_eth_addr *eth_addr)
213{
214 int i;
215 u32 oui = ntohl(eth_addr->oui) >> 8;
216 u32 nic = ntohl(eth_addr->nic) >> 8;
217
218 for (i = 0; i < ARRAY_SIZE(toradex_ouis); i++) {
219 if (toradex_ouis[i] == oui)
220 break;
221 }
222
223 return (u32)((i << 24) + nic);
224}
225
226void get_mac_from_serial(u32 tdx_serial, struct toradex_eth_addr *eth_addr)
227{
228 u8 oui_index = tdx_serial >> 24;
229 u32 nic = tdx_serial & GENMASK(23, 0);
230 u32 oui;
231
232 if (oui_index >= ARRAY_SIZE(toradex_ouis)) {
233 puts("Can't find OUI for this serial#\n");
234 oui_index = 0;
235 }
236
237 oui = toradex_ouis[oui_index];
238
239 eth_addr->oui = htonl(oui << 8);
240 eth_addr->nic = htonl(nic << 8);
241}
242
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100243#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
244static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
245{
246 struct mmc *mmc;
247 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
248 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
249 uint part = CONFIG_TDX_CFG_BLOCK_PART;
250 uint blk_start;
251 int ret = 0;
252
253 /* Read production parameter config block from eMMC */
254 mmc = find_mmc_device(dev);
255 if (!mmc) {
256 puts("No MMC card found\n");
257 ret = -ENODEV;
258 goto out;
259 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200260 if (mmc_init(mmc)) {
261 puts("MMC init failed\n");
262 return -EINVAL;
263 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600264 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Simon Glassdbfa32c2022-08-11 19:34:59 -0600265 if (blk_select_hwpart_devnum(UCLASS_MMC, dev, part)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100266 puts("MMC partition switch failed\n");
267 ret = -ENODEV;
268 goto out;
269 }
270 }
271 if (offset < 0)
272 offset += mmc->capacity;
273 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
274
275 if (!write) {
276 /* Careful reads a whole block of 512 bytes into config_block */
277 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
278 (unsigned char *)config_block) != 1) {
279 ret = -EIO;
280 goto out;
281 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100282 } else {
283 /* Just writing one 512 byte block */
284 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
285 (unsigned char *)config_block) != 1) {
286 ret = -EIO;
287 goto out;
288 }
289 }
290
291out:
292 /* Switch back to regular eMMC user partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600293 blk_select_hwpart_devnum(UCLASS_MMC, 0, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100294
295 return ret;
296}
297#endif
298
299#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
300static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
301{
302 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200303 struct mtd_info *mtd = get_nand_dev_by_index(0);
304
305 if (!mtd)
306 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100307
308 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200309 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500310 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
311 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100312}
313
314static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
315{
316 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
317
318 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500319 return nand_write_skip_bad(get_nand_dev_by_index(0),
320 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100321 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
322 config_block, WITH_WR_VERIFY);
323}
324#endif
325
326#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
327static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
328{
329 /* Read production parameter config block from NOR flash */
330 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
331 TDX_CFG_BLOCK_MAX_SIZE);
332 return 0;
333}
334
335static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
336{
337 /* Write production parameter config block to NOR flash */
338 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
339 TDX_CFG_BLOCK_MAX_SIZE);
340}
341#endif
342
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300343#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
344static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
345{
346 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
347 TDX_CFG_BLOCK_MAX_SIZE);
348}
349
350static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
351{
352 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
353 TDX_CFG_BLOCK_MAX_SIZE);
354}
355#endif
356
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100357int read_tdx_cfg_block(void)
358{
359 int ret = 0;
360 u8 *config_block = NULL;
361 struct toradex_tag *tag;
362 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
363 int offset;
364
365 /* Allocate RAM area for config block */
366 config_block = memalign(ARCH_DMA_MINALIGN, size);
367 if (!config_block) {
368 printf("Not enough malloc space available!\n");
369 return -ENOMEM;
370 }
371
372 memset(config_block, 0, size);
373
374#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
375 ret = tdx_cfg_block_mmc_storage(config_block, 0);
376#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
377 ret = read_tdx_cfg_block_from_nand(config_block);
378#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
379 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300380#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
381 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100382#else
383 ret = -EINVAL;
384#endif
385 if (ret)
386 goto out;
387
388 /* Expect a valid tag first */
389 tag = (struct toradex_tag *)config_block;
390 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
391 valid_cfgblock = false;
392 ret = -EINVAL;
393 goto out;
394 }
395 valid_cfgblock = true;
396 offset = 4;
397
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300398 /*
399 * check if there is enough space for storing tag and value of the
400 * biggest element
401 */
402 while (offset + sizeof(struct toradex_tag) +
403 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100404 tag = (struct toradex_tag *)(config_block + offset);
405 offset += 4;
406 if (tag->id == TAG_INVALID)
407 break;
408
409 if (tag->flags == TAG_FLAG_VALID) {
410 switch (tag->id) {
411 case TAG_MAC:
412 memcpy(&tdx_eth_addr, config_block + offset,
413 6);
414
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200415 tdx_serial = get_serial_from_mac(&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100416 break;
417 case TAG_HW:
418 memcpy(&tdx_hw_tag, config_block + offset, 8);
419 break;
420 }
421 }
422
423 /* Get to next tag according to current tags length */
424 offset += tag->len * 4;
425 }
426
427 /* Cap product id to avoid issues with a yet unknown one */
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200428 if (tdx_hw_tag.prodid >= ARRAY_SIZE(toradex_modules))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100429 tdx_hw_tag.prodid = 0;
430
431out:
432 free(config_block);
433 return ret;
434}
435
Philippe Schenker498f95a2022-06-13 19:35:23 +0200436static int parse_assembly_string(char *string_to_parse, u16 *assembly)
437{
438 if (string_to_parse[3] >= 'A' && string_to_parse[3] <= 'Z')
439 *assembly = string_to_parse[3] - 'A';
440 else if (string_to_parse[3] == '#')
441 *assembly = dectoul(&string_to_parse[4], NULL);
442 else
443 return -EINVAL;
444
445 return 0;
446}
447
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100448static int get_cfgblock_interactive(void)
449{
450 char message[CONFIG_SYS_CBSIZE];
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100451 int len = 0;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200452 int ret = 0;
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200453 unsigned int prodid;
454 int i;
Philippe Schenker48b37032022-05-09 18:58:15 +0200455
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200456 printf("Enabled modules:\n");
457 for (i = 0; i < ARRAY_SIZE(toradex_modules); i++) {
458 if (toradex_modules[i].is_enabled)
459 printf(" %04d %s\n", i, toradex_modules[i].name);
Philippe Schenker48b37032022-05-09 18:58:15 +0200460 }
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200461
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200462 sprintf(message, "Enter the module ID: ");
463 len = cli_readline(message);
Stefan Agner68f58782019-04-09 17:24:08 +0200464
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200465 prodid = dectoul(console_buffer, NULL);
466 if (prodid >= ARRAY_SIZE(toradex_modules) || !toradex_modules[prodid].is_enabled) {
467 printf("Parsing module id failed\n");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100468 return -1;
469 }
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200470 tdx_hw_tag.prodid = prodid;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100471
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200472 len = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100473 while (len < 4) {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200474 sprintf(message, "Enter the module version (e.g. V1.1B or V1.1#26): V");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100475 len = cli_readline(message);
476 }
477
478 tdx_hw_tag.ver_major = console_buffer[0] - '0';
479 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200480
481 ret = parse_assembly_string(console_buffer, &tdx_hw_tag.ver_assembly);
482 if (ret) {
483 printf("Parsing module version failed\n");
484 return ret;
485 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100486
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100487 while (len < 8) {
488 sprintf(message, "Enter module serial number: ");
489 len = cli_readline(message);
490 }
491
Simon Glassff9b9032021-07-24 09:03:30 -0600492 tdx_serial = dectoul(console_buffer, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100493
494 return 0;
495}
496
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300497static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
498 u32 *serial)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100499{
Denys Drozdov3a1c6572021-04-07 15:28:24 +0300500 char revision[3] = {barcode[6], barcode[7], '\0'};
501
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100502 if (strlen(barcode) < 16) {
503 printf("Argument too short, barcode is 16 chars long\n");
504 return -1;
505 }
506
507 /* Get hardware information from the first 8 digits */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300508 tag->ver_major = barcode[4] - '0';
509 tag->ver_minor = barcode[5] - '0';
Simon Glassff9b9032021-07-24 09:03:30 -0600510 tag->ver_assembly = dectoul(revision, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100511
512 barcode[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600513 tag->prodid = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100514
515 /* Parse second part of the barcode (serial number */
516 barcode += 8;
Simon Glassff9b9032021-07-24 09:03:30 -0600517 *serial = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100518
519 return 0;
520}
521
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300522static int write_tag(u8 *config_block, int *offset, int tag_id,
523 u8 *tag_data, size_t tag_data_size)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100524{
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100525 struct toradex_tag *tag;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300526
527 if (!offset || !config_block)
528 return -EINVAL;
529
530 tag = (struct toradex_tag *)(config_block + *offset);
531 tag->id = tag_id;
532 tag->flags = TAG_FLAG_VALID;
533 /* len is provided as number of 32bit values after the tag */
534 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
535 *offset += sizeof(struct toradex_tag);
536 if (tag_data && tag_data_size) {
537 memcpy(config_block + *offset, tag_data,
538 tag_data_size);
539 *offset += tag_data_size;
540 }
541
542 return 0;
543}
544
545#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
546int read_tdx_cfg_block_carrier(void)
547{
548 int ret = 0;
549 u8 *config_block = NULL;
550 struct toradex_tag *tag;
551 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
552 int offset;
553
554 /* Allocate RAM area for carrier config block */
555 config_block = memalign(ARCH_DMA_MINALIGN, size);
556 if (!config_block) {
557 printf("Not enough malloc space available!\n");
558 return -ENOMEM;
559 }
560
561 memset(config_block, 0, size);
562
563 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
564 size);
565 if (ret)
566 return ret;
567
568 /* Expect a valid tag first */
569 tag = (struct toradex_tag *)config_block;
570 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
571 valid_cfgblock_carrier = false;
572 ret = -EINVAL;
573 goto out;
574 }
575 valid_cfgblock_carrier = true;
576 offset = 4;
577
578 while (offset + sizeof(struct toradex_tag) +
579 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
580 tag = (struct toradex_tag *)(config_block + offset);
581 offset += 4;
582 if (tag->id == TAG_INVALID)
583 break;
584
585 if (tag->flags == TAG_FLAG_VALID) {
586 switch (tag->id) {
587 case TAG_CAR_SERIAL:
588 memcpy(&tdx_car_serial, config_block + offset,
589 sizeof(tdx_car_serial));
590 break;
591 case TAG_HW:
592 memcpy(&tdx_car_hw_tag, config_block +
593 offset, 8);
594 break;
595 }
596 }
597
598 /* Get to next tag according to current tags length */
599 offset += tag->len * 4;
600 }
601out:
602 free(config_block);
603 return ret;
604}
605
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300606int check_pid8_sanity(char *pid8)
607{
608 char s_carrierid_verdin_dev[5];
609 char s_carrierid_dahlia[5];
610
611 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
612 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
613
614 /* sane value check, first 4 chars which represent carrier id */
615 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
616 return 0;
617
618 if (!strncmp(pid8, s_carrierid_dahlia, 4))
619 return 0;
620
621 return -EINVAL;
622}
623
624int try_migrate_tdx_cfg_block_carrier(void)
625{
626 char pid8[8];
627 int offset = 0;
628 int ret = CMD_RET_SUCCESS;
629 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
630 u8 *config_block;
631
632 memset(pid8, 0x0, 8);
633 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
634 if (ret)
635 return ret;
636
637 if (check_pid8_sanity(pid8))
638 return -EINVAL;
639
640 /* Allocate RAM area for config block */
641 config_block = memalign(ARCH_DMA_MINALIGN, size);
642 if (!config_block) {
643 printf("Not enough malloc space available!\n");
644 return CMD_RET_FAILURE;
645 }
646
647 memset(config_block, 0xff, size);
648 /* we try parse PID8 concatenating zeroed serial number */
649 tdx_car_hw_tag.ver_major = pid8[4] - '0';
650 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
651 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
652
653 pid8[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600654 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300655
656 /* Valid Tag */
657 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
658
659 /* Product Tag */
660 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
661 sizeof(tdx_car_hw_tag));
662
663 /* Serial Tag */
664 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
665 sizeof(tdx_car_serial));
666
667 memset(config_block + offset, 0, 32 - offset);
668 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
669 size);
670 if (ret) {
671 printf("Failed to write Toradex Extra config block: %d\n",
672 ret);
673 ret = CMD_RET_FAILURE;
674 goto out;
675 }
676
677 printf("Successfully migrated to Toradex Config Block from PID8\n");
678
679out:
680 free(config_block);
681 return ret;
682}
683
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300684static int get_cfgblock_carrier_interactive(void)
685{
686 char message[CONFIG_SYS_CBSIZE];
687 int len;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200688 int ret = 0;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300689
690 printf("Supported carrier boards:\n");
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200691 printf("%30s\t[ID]\n", "CARRIER BOARD NAME");
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200692 for (int i = 0; i < ARRAY_SIZE(toradex_carrier_boards); i++)
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200693 printf("%30s\t[%d]\n",
694 toradex_carrier_boards[i].name,
695 toradex_carrier_boards[i].pid4);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300696
697 sprintf(message, "Choose your carrier board (provide ID): ");
698 len = cli_readline(message);
Simon Glassff9b9032021-07-24 09:03:30 -0600699 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300700
701 do {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200702 sprintf(message, "Enter carrier board version (e.g. V1.1B or V1.1#26): V");
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300703 len = cli_readline(message);
704 } while (len < 4);
705
706 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
707 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200708
709 ret = parse_assembly_string(console_buffer, &tdx_car_hw_tag.ver_assembly);
710 if (ret) {
711 printf("Parsing module version failed\n");
712 return ret;
713 }
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300714
715 while (len < 8) {
716 sprintf(message, "Enter carrier board serial number: ");
717 len = cli_readline(message);
718 }
719
Simon Glassff9b9032021-07-24 09:03:30 -0600720 tdx_car_serial = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300721
722 return 0;
723}
724
725static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
726 char * const argv[])
727{
728 u8 *config_block;
729 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100730 int offset = 0;
731 int ret = CMD_RET_SUCCESS;
732 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100733 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100734
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300735 if (argc >= 3) {
736 if (argv[2][0] == '-' && argv[2][1] == 'y')
737 force_overwrite = 1;
738 }
739
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100740 /* Allocate RAM area for config block */
741 config_block = memalign(ARCH_DMA_MINALIGN, size);
742 if (!config_block) {
743 printf("Not enough malloc space available!\n");
744 return CMD_RET_FAILURE;
745 }
746
747 memset(config_block, 0xff, size);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300748 read_tdx_cfg_block_carrier();
749 if (valid_cfgblock_carrier && !force_overwrite) {
750 char message[CONFIG_SYS_CBSIZE];
751
752 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
753
754 if (!cli_readline(message))
755 goto out;
756
757 if (console_buffer[0] != 'y' &&
758 console_buffer[0] != 'Y')
759 goto out;
760 }
761
762 if (argc < 3 || (force_overwrite && argc < 4)) {
763 err = get_cfgblock_carrier_interactive();
764 } else {
765 if (force_overwrite)
766 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
767 &tdx_car_serial);
768 else
769 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
770 &tdx_car_serial);
771 }
772
773 if (err) {
774 ret = CMD_RET_FAILURE;
775 goto out;
776 }
777
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 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
791 size);
792 if (err) {
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("Toradex Extra config block successfully written\n");
800
801out:
802 free(config_block);
803 return ret;
804}
805
806#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
807
808static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
809 char * const argv[])
810{
811 u8 *config_block;
812 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
813 int offset = 0;
814 int ret = CMD_RET_SUCCESS;
815 int err;
816 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100817
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100818 if (argc >= 3) {
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300819#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
820 if (!strcmp(argv[2], "carrier"))
821 return do_cfgblock_carrier_create(cmdtp, flag,
822 --argc, ++argv);
823#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100824 if (argv[2][0] == '-' && argv[2][1] == 'y')
825 force_overwrite = 1;
826 }
827
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300828 /* Allocate RAM area for config block */
829 config_block = memalign(ARCH_DMA_MINALIGN, size);
830 if (!config_block) {
831 printf("Not enough malloc space available!\n");
832 return CMD_RET_FAILURE;
833 }
834
835 memset(config_block, 0xff, size);
836
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100837 read_tdx_cfg_block();
838 if (valid_cfgblock) {
839#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
840 /*
841 * On NAND devices, recreation is only allowed if the page is
842 * empty (config block invalid...)
843 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200844 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500845 CONFIG_TDX_CFG_BLOCK_OFFSET /
846 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100847 goto out;
848#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
849 /*
850 * On NOR devices, recreation is only allowed if the sector is
851 * empty and write protection is off (config block invalid...)
852 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200853 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 +0100854 CONFIG_TDX_CFG_BLOCK_OFFSET);
855 goto out;
856#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100857 if (!force_overwrite) {
858 char message[CONFIG_SYS_CBSIZE];
859
860 sprintf(message,
861 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100862
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100863 if (!cli_readline(message))
864 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100865
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100866 if (console_buffer[0] != 'y' &&
867 console_buffer[0] != 'Y')
868 goto out;
869 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100870#endif
871 }
872
873 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100874 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100875 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100876 } else {
877 if (force_overwrite)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300878 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
879 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100880 else
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300881 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
882 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100883 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100884 if (err) {
885 ret = CMD_RET_FAILURE;
886 goto out;
887 }
888
889 /* Convert serial number to MAC address (the storage format) */
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200890 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100891
892 /* Valid Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300893 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100894
895 /* Product Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300896 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
897 sizeof(tdx_hw_tag));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100898
899 /* MAC Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300900 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
901 sizeof(tdx_eth_addr));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100902
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100903 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100904#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
905 err = tdx_cfg_block_mmc_storage(config_block, 1);
906#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
907 err = write_tdx_cfg_block_to_nand(config_block);
908#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
909 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300910#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
911 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100912#else
913 err = -EINVAL;
914#endif
915 if (err) {
916 printf("Failed to write Toradex config block: %d\n", ret);
917 ret = CMD_RET_FAILURE;
918 goto out;
919 }
920
921 printf("Toradex config block successfully written\n");
922
923out:
924 free(config_block);
925 return ret;
926}
927
Simon Glassed38aef2020-05-10 11:40:03 -0600928static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
929 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100930{
931 int ret;
932
933 if (argc < 2)
934 return CMD_RET_USAGE;
935
936 if (!strcmp(argv[1], "create")) {
937 return do_cfgblock_create(cmdtp, flag, argc, argv);
938 } else if (!strcmp(argv[1], "reload")) {
939 ret = read_tdx_cfg_block();
940 if (ret) {
941 printf("Failed to reload Toradex config block: %d\n",
942 ret);
943 return CMD_RET_FAILURE;
944 }
945 return CMD_RET_SUCCESS;
946 }
947
948 return CMD_RET_USAGE;
949}
950
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300951U_BOOT_CMD(
952 cfgblock, 5, 0, do_cfgblock,
953 "Toradex config block handling commands",
954 "create [-y] [barcode] - (Re-)create Toradex config block\n"
955 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
956 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100957);