blob: a6e3c6afae8091011ac1e1b18c53afe9446caa07 [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
Tom Rinidec7ea02024-05-20 13:35:03 -06006#include <config.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) },
Joao Paulo Goncalves9dcd9a92024-01-31 14:32:04 -0300159 [86] = { "Verdin iMX8M Mini DualLite 2GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
Joao Paulo Goncalves92b38e72024-03-08 11:18:01 -0300160 [87] = { "Verdin iMX8M Mini Quad 2GB IT", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
Emanuele Ghidoli16d61622024-05-28 11:59:39 +0200161 [88] = { "Aquila AM69 Octa 32GB WB IT", TARGET_IS_ENABLED(AQUILA_AM69_A72) },
Emanuele Ghidoli4e9f01d2024-05-28 11:59:40 +0200162 [89] = { "Verdin iMX95 Hexa 16GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX95) },
Emanuele Ghidolid7c9b8a2024-05-28 11:59:41 +0200163 [90] = { "Verdin iMX8M Mini Quad 4GB WB ET", TARGET_IS_ENABLED(VERDIN_IMX8MM) },
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100164};
165
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200166struct pid4list {
167 int pid4;
168 char * const name;
Igor Opaniuk414df352020-07-15 13:30:54 +0300169};
170
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200171const struct pid4list toradex_carrier_boards[] = {
172 /* the code assumes unknown at index 0 */
173 {0, "UNKNOWN CARRIER BOARD"},
174 {DAHLIA, "Dahlia"},
175 {VERDIN_DEVELOPMENT_BOARD, "Verdin Development Board"},
176 {YAVIA, "Yavia"},
177};
178
Max Krummenachercea09092023-07-18 11:07:34 +0200179const struct pid4list toradex_display_adapters[] = {
180 /* the code assumes unknown at index 0 */
181 {0, "UNKNOWN DISPLAY ADAPTER"},
182 {VERDIN_DSI_TO_HDMI_ADAPTER, "Verdin DSI to HDMI Adapter"},
183 {VERDIN_DSI_TO_LVDS_ADAPTER, "Verdin DSI to LVDS Adapter"},
Igor Opaniuk414df352020-07-15 13:30:54 +0300184};
185
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200186const u32 toradex_ouis[] = {
187 [0] = 0x00142dUL,
188 [1] = 0x8c06cbUL,
189};
190
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200191const char * const get_toradex_carrier_boards(int pid4)
192{
193 int i, index = 0;
194
195 for (i = 1; i < ARRAY_SIZE(toradex_carrier_boards); i++) {
196 if (pid4 == toradex_carrier_boards[i].pid4) {
197 index = i;
198 break;
199 }
200 }
201 return toradex_carrier_boards[index].name;
202}
203
Max Krummenachercea09092023-07-18 11:07:34 +0200204const char * const get_toradex_display_adapters(int pid4)
205{
206 int i, index = 0;
207
208 for (i = 1; i < ARRAY_SIZE(toradex_display_adapters); i++) {
209 if (pid4 == toradex_display_adapters[i].pid4) {
210 index = i;
211 break;
212 }
213 }
214 return toradex_display_adapters[index].name;
215}
216
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200217static u32 get_serial_from_mac(struct toradex_eth_addr *eth_addr)
218{
219 int i;
220 u32 oui = ntohl(eth_addr->oui) >> 8;
221 u32 nic = ntohl(eth_addr->nic) >> 8;
222
223 for (i = 0; i < ARRAY_SIZE(toradex_ouis); i++) {
224 if (toradex_ouis[i] == oui)
225 break;
226 }
227
228 return (u32)((i << 24) + nic);
229}
230
231void get_mac_from_serial(u32 tdx_serial, struct toradex_eth_addr *eth_addr)
232{
233 u8 oui_index = tdx_serial >> 24;
234 u32 nic = tdx_serial & GENMASK(23, 0);
235 u32 oui;
236
237 if (oui_index >= ARRAY_SIZE(toradex_ouis)) {
238 puts("Can't find OUI for this serial#\n");
239 oui_index = 0;
240 }
241
242 oui = toradex_ouis[oui_index];
243
244 eth_addr->oui = htonl(oui << 8);
245 eth_addr->nic = htonl(nic << 8);
246}
247
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100248#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
249static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
250{
251 struct mmc *mmc;
252 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
253 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
254 uint part = CONFIG_TDX_CFG_BLOCK_PART;
255 uint blk_start;
256 int ret = 0;
257
258 /* Read production parameter config block from eMMC */
259 mmc = find_mmc_device(dev);
260 if (!mmc) {
261 puts("No MMC card found\n");
262 ret = -ENODEV;
263 goto out;
264 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200265 if (mmc_init(mmc)) {
266 puts("MMC init failed\n");
267 return -EINVAL;
268 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600269 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Simon Glassdbfa32c2022-08-11 19:34:59 -0600270 if (blk_select_hwpart_devnum(UCLASS_MMC, dev, part)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100271 puts("MMC partition switch failed\n");
272 ret = -ENODEV;
273 goto out;
274 }
275 }
276 if (offset < 0)
277 offset += mmc->capacity;
278 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
279
280 if (!write) {
281 /* Careful reads a whole block of 512 bytes into config_block */
282 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
283 (unsigned char *)config_block) != 1) {
284 ret = -EIO;
285 goto out;
286 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100287 } else {
288 /* Just writing one 512 byte block */
289 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
290 (unsigned char *)config_block) != 1) {
291 ret = -EIO;
292 goto out;
293 }
294 }
295
296out:
297 /* Switch back to regular eMMC user partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600298 blk_select_hwpart_devnum(UCLASS_MMC, 0, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100299
300 return ret;
301}
302#endif
303
304#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
305static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
306{
307 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200308 struct mtd_info *mtd = get_nand_dev_by_index(0);
309
310 if (!mtd)
311 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100312
313 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200314 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500315 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
316 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100317}
318
319static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
320{
321 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
322
323 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500324 return nand_write_skip_bad(get_nand_dev_by_index(0),
325 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100326 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
327 config_block, WITH_WR_VERIFY);
328}
329#endif
330
331#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
332static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
333{
334 /* Read production parameter config block from NOR flash */
335 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
336 TDX_CFG_BLOCK_MAX_SIZE);
337 return 0;
338}
339
340static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
341{
342 /* Write production parameter config block to NOR flash */
343 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
344 TDX_CFG_BLOCK_MAX_SIZE);
345}
346#endif
347
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300348#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
349static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
350{
351 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
352 TDX_CFG_BLOCK_MAX_SIZE);
353}
354
355static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
356{
357 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
358 TDX_CFG_BLOCK_MAX_SIZE);
359}
360#endif
361
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100362int read_tdx_cfg_block(void)
363{
364 int ret = 0;
365 u8 *config_block = NULL;
366 struct toradex_tag *tag;
367 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
368 int offset;
369
370 /* Allocate RAM area for config block */
371 config_block = memalign(ARCH_DMA_MINALIGN, size);
372 if (!config_block) {
373 printf("Not enough malloc space available!\n");
374 return -ENOMEM;
375 }
376
377 memset(config_block, 0, size);
378
379#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
380 ret = tdx_cfg_block_mmc_storage(config_block, 0);
381#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
382 ret = read_tdx_cfg_block_from_nand(config_block);
383#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
384 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300385#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
386 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100387#else
388 ret = -EINVAL;
389#endif
390 if (ret)
391 goto out;
392
393 /* Expect a valid tag first */
394 tag = (struct toradex_tag *)config_block;
395 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
396 valid_cfgblock = false;
397 ret = -EINVAL;
398 goto out;
399 }
400 valid_cfgblock = true;
401 offset = 4;
402
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300403 /*
404 * check if there is enough space for storing tag and value of the
405 * biggest element
406 */
407 while (offset + sizeof(struct toradex_tag) +
408 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100409 tag = (struct toradex_tag *)(config_block + offset);
410 offset += 4;
411 if (tag->id == TAG_INVALID)
412 break;
413
414 if (tag->flags == TAG_FLAG_VALID) {
415 switch (tag->id) {
416 case TAG_MAC:
417 memcpy(&tdx_eth_addr, config_block + offset,
418 6);
419
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200420 tdx_serial = get_serial_from_mac(&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100421 break;
422 case TAG_HW:
423 memcpy(&tdx_hw_tag, config_block + offset, 8);
424 break;
425 }
426 }
427
428 /* Get to next tag according to current tags length */
429 offset += tag->len * 4;
430 }
431
432 /* Cap product id to avoid issues with a yet unknown one */
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200433 if (tdx_hw_tag.prodid >= ARRAY_SIZE(toradex_modules))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100434 tdx_hw_tag.prodid = 0;
435
436out:
437 free(config_block);
438 return ret;
439}
440
Philippe Schenker498f95a2022-06-13 19:35:23 +0200441static int parse_assembly_string(char *string_to_parse, u16 *assembly)
442{
443 if (string_to_parse[3] >= 'A' && string_to_parse[3] <= 'Z')
444 *assembly = string_to_parse[3] - 'A';
445 else if (string_to_parse[3] == '#')
446 *assembly = dectoul(&string_to_parse[4], NULL);
447 else
448 return -EINVAL;
449
450 return 0;
451}
452
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100453static int get_cfgblock_interactive(void)
454{
455 char message[CONFIG_SYS_CBSIZE];
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100456 int len = 0;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200457 int ret = 0;
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200458 unsigned int prodid;
459 int i;
Philippe Schenker48b37032022-05-09 18:58:15 +0200460
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200461 printf("Enabled modules:\n");
462 for (i = 0; i < ARRAY_SIZE(toradex_modules); i++) {
463 if (toradex_modules[i].is_enabled)
464 printf(" %04d %s\n", i, toradex_modules[i].name);
Philippe Schenker48b37032022-05-09 18:58:15 +0200465 }
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200466
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200467 sprintf(message, "Enter the module ID: ");
468 len = cli_readline(message);
Stefan Agner68f58782019-04-09 17:24:08 +0200469
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200470 prodid = dectoul(console_buffer, NULL);
471 if (prodid >= ARRAY_SIZE(toradex_modules) || !toradex_modules[prodid].is_enabled) {
472 printf("Parsing module id failed\n");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100473 return -1;
474 }
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200475 tdx_hw_tag.prodid = prodid;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100476
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200477 len = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100478 while (len < 4) {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200479 sprintf(message, "Enter the module version (e.g. V1.1B or V1.1#26): V");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100480 len = cli_readline(message);
481 }
482
483 tdx_hw_tag.ver_major = console_buffer[0] - '0';
484 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200485
486 ret = parse_assembly_string(console_buffer, &tdx_hw_tag.ver_assembly);
487 if (ret) {
488 printf("Parsing module version failed\n");
489 return ret;
490 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100491
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100492 while (len < 8) {
493 sprintf(message, "Enter module serial number: ");
494 len = cli_readline(message);
495 }
496
Simon Glassff9b9032021-07-24 09:03:30 -0600497 tdx_serial = dectoul(console_buffer, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100498
499 return 0;
500}
501
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300502static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
503 u32 *serial)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100504{
Denys Drozdov3a1c6572021-04-07 15:28:24 +0300505 char revision[3] = {barcode[6], barcode[7], '\0'};
506
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100507 if (strlen(barcode) < 16) {
508 printf("Argument too short, barcode is 16 chars long\n");
509 return -1;
510 }
511
512 /* Get hardware information from the first 8 digits */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300513 tag->ver_major = barcode[4] - '0';
514 tag->ver_minor = barcode[5] - '0';
Simon Glassff9b9032021-07-24 09:03:30 -0600515 tag->ver_assembly = dectoul(revision, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100516
517 barcode[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600518 tag->prodid = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100519
520 /* Parse second part of the barcode (serial number */
521 barcode += 8;
Simon Glassff9b9032021-07-24 09:03:30 -0600522 *serial = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100523
524 return 0;
525}
526
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300527static int write_tag(u8 *config_block, int *offset, int tag_id,
528 u8 *tag_data, size_t tag_data_size)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100529{
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100530 struct toradex_tag *tag;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300531
532 if (!offset || !config_block)
533 return -EINVAL;
534
535 tag = (struct toradex_tag *)(config_block + *offset);
536 tag->id = tag_id;
537 tag->flags = TAG_FLAG_VALID;
538 /* len is provided as number of 32bit values after the tag */
539 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
540 *offset += sizeof(struct toradex_tag);
541 if (tag_data && tag_data_size) {
542 memcpy(config_block + *offset, tag_data,
543 tag_data_size);
544 *offset += tag_data_size;
545 }
546
547 return 0;
548}
549
550#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
551int read_tdx_cfg_block_carrier(void)
552{
553 int ret = 0;
554 u8 *config_block = NULL;
555 struct toradex_tag *tag;
556 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
557 int offset;
558
559 /* Allocate RAM area for carrier config block */
560 config_block = memalign(ARCH_DMA_MINALIGN, size);
561 if (!config_block) {
562 printf("Not enough malloc space available!\n");
563 return -ENOMEM;
564 }
565
566 memset(config_block, 0, size);
567
568 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
569 size);
570 if (ret)
571 return ret;
572
573 /* Expect a valid tag first */
574 tag = (struct toradex_tag *)config_block;
575 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
576 valid_cfgblock_carrier = false;
577 ret = -EINVAL;
578 goto out;
579 }
580 valid_cfgblock_carrier = true;
581 offset = 4;
582
583 while (offset + sizeof(struct toradex_tag) +
584 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
585 tag = (struct toradex_tag *)(config_block + offset);
586 offset += 4;
587 if (tag->id == TAG_INVALID)
588 break;
589
590 if (tag->flags == TAG_FLAG_VALID) {
591 switch (tag->id) {
592 case TAG_CAR_SERIAL:
593 memcpy(&tdx_car_serial, config_block + offset,
594 sizeof(tdx_car_serial));
595 break;
596 case TAG_HW:
597 memcpy(&tdx_car_hw_tag, config_block +
598 offset, 8);
599 break;
600 }
601 }
602
603 /* Get to next tag according to current tags length */
604 offset += tag->len * 4;
605 }
606out:
607 free(config_block);
608 return ret;
609}
610
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300611int check_pid8_sanity(char *pid8)
612{
613 char s_carrierid_verdin_dev[5];
614 char s_carrierid_dahlia[5];
615
616 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
617 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
618
619 /* sane value check, first 4 chars which represent carrier id */
620 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
621 return 0;
622
623 if (!strncmp(pid8, s_carrierid_dahlia, 4))
624 return 0;
625
626 return -EINVAL;
627}
628
629int try_migrate_tdx_cfg_block_carrier(void)
630{
631 char pid8[8];
632 int offset = 0;
633 int ret = CMD_RET_SUCCESS;
634 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
635 u8 *config_block;
636
637 memset(pid8, 0x0, 8);
638 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
639 if (ret)
640 return ret;
641
642 if (check_pid8_sanity(pid8))
643 return -EINVAL;
644
645 /* Allocate RAM area for config block */
646 config_block = memalign(ARCH_DMA_MINALIGN, size);
647 if (!config_block) {
648 printf("Not enough malloc space available!\n");
649 return CMD_RET_FAILURE;
650 }
651
652 memset(config_block, 0xff, size);
653 /* we try parse PID8 concatenating zeroed serial number */
654 tdx_car_hw_tag.ver_major = pid8[4] - '0';
655 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
656 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
657
658 pid8[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600659 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300660
661 /* Valid Tag */
662 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
663
664 /* Product Tag */
665 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
666 sizeof(tdx_car_hw_tag));
667
668 /* Serial Tag */
669 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
670 sizeof(tdx_car_serial));
671
672 memset(config_block + offset, 0, 32 - offset);
673 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
674 size);
675 if (ret) {
676 printf("Failed to write Toradex Extra config block: %d\n",
677 ret);
678 ret = CMD_RET_FAILURE;
679 goto out;
680 }
681
682 printf("Successfully migrated to Toradex Config Block from PID8\n");
683
684out:
685 free(config_block);
686 return ret;
687}
688
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300689static int get_cfgblock_carrier_interactive(void)
690{
691 char message[CONFIG_SYS_CBSIZE];
692 int len;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200693 int ret = 0;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300694
695 printf("Supported carrier boards:\n");
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200696 printf("%30s\t[ID]\n", "CARRIER BOARD NAME");
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200697 for (int i = 0; i < ARRAY_SIZE(toradex_carrier_boards); i++)
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200698 printf("%30s\t[%d]\n",
699 toradex_carrier_boards[i].name,
700 toradex_carrier_boards[i].pid4);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300701
702 sprintf(message, "Choose your carrier board (provide ID): ");
703 len = cli_readline(message);
Simon Glassff9b9032021-07-24 09:03:30 -0600704 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300705
706 do {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200707 sprintf(message, "Enter carrier board version (e.g. V1.1B or V1.1#26): V");
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300708 len = cli_readline(message);
709 } while (len < 4);
710
711 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
712 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200713
714 ret = parse_assembly_string(console_buffer, &tdx_car_hw_tag.ver_assembly);
715 if (ret) {
716 printf("Parsing module version failed\n");
717 return ret;
718 }
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300719
720 while (len < 8) {
721 sprintf(message, "Enter carrier board serial number: ");
722 len = cli_readline(message);
723 }
724
Simon Glassff9b9032021-07-24 09:03:30 -0600725 tdx_car_serial = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300726
727 return 0;
728}
729
730static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
731 char * const argv[])
732{
733 u8 *config_block;
734 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100735 int offset = 0;
736 int ret = CMD_RET_SUCCESS;
737 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100738 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100739
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300740 if (argc >= 3) {
741 if (argv[2][0] == '-' && argv[2][1] == 'y')
742 force_overwrite = 1;
743 }
744
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100745 /* Allocate RAM area for config block */
746 config_block = memalign(ARCH_DMA_MINALIGN, size);
747 if (!config_block) {
748 printf("Not enough malloc space available!\n");
749 return CMD_RET_FAILURE;
750 }
751
752 memset(config_block, 0xff, size);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300753 read_tdx_cfg_block_carrier();
754 if (valid_cfgblock_carrier && !force_overwrite) {
755 char message[CONFIG_SYS_CBSIZE];
756
757 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
758
759 if (!cli_readline(message))
760 goto out;
761
762 if (console_buffer[0] != 'y' &&
763 console_buffer[0] != 'Y')
764 goto out;
765 }
766
767 if (argc < 3 || (force_overwrite && argc < 4)) {
768 err = get_cfgblock_carrier_interactive();
769 } else {
770 if (force_overwrite)
771 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
772 &tdx_car_serial);
773 else
774 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
775 &tdx_car_serial);
776 }
777
778 if (err) {
779 ret = CMD_RET_FAILURE;
780 goto out;
781 }
782
783 /* Valid Tag */
784 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
785
786 /* Product Tag */
787 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
788 sizeof(tdx_car_hw_tag));
789
790 /* Serial Tag */
791 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
792 sizeof(tdx_car_serial));
793
794 memset(config_block + offset, 0, 32 - offset);
795 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
796 size);
797 if (err) {
798 printf("Failed to write Toradex Extra config block: %d\n",
799 ret);
800 ret = CMD_RET_FAILURE;
801 goto out;
802 }
803
804 printf("Toradex Extra config block successfully written\n");
805
806out:
807 free(config_block);
808 return ret;
809}
810
811#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
812
813static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
814 char * const argv[])
815{
816 u8 *config_block;
817 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
818 int offset = 0;
819 int ret = CMD_RET_SUCCESS;
820 int err;
821 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100822
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100823 if (argc >= 3) {
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300824#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
825 if (!strcmp(argv[2], "carrier"))
826 return do_cfgblock_carrier_create(cmdtp, flag,
827 --argc, ++argv);
828#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100829 if (argv[2][0] == '-' && argv[2][1] == 'y')
830 force_overwrite = 1;
831 }
832
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300833 /* Allocate RAM area for config block */
834 config_block = memalign(ARCH_DMA_MINALIGN, size);
835 if (!config_block) {
836 printf("Not enough malloc space available!\n");
837 return CMD_RET_FAILURE;
838 }
839
840 memset(config_block, 0xff, size);
841
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100842 read_tdx_cfg_block();
843 if (valid_cfgblock) {
844#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
845 /*
846 * On NAND devices, recreation is only allowed if the page is
847 * empty (config block invalid...)
848 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200849 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500850 CONFIG_TDX_CFG_BLOCK_OFFSET /
851 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100852 goto out;
853#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
854 /*
855 * On NOR devices, recreation is only allowed if the sector is
856 * empty and write protection is off (config block invalid...)
857 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200858 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 +0100859 CONFIG_TDX_CFG_BLOCK_OFFSET);
860 goto out;
861#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100862 if (!force_overwrite) {
863 char message[CONFIG_SYS_CBSIZE];
864
865 sprintf(message,
866 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100867
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100868 if (!cli_readline(message))
869 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100870
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100871 if (console_buffer[0] != 'y' &&
872 console_buffer[0] != 'Y')
873 goto out;
874 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100875#endif
876 }
877
878 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100879 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100880 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100881 } else {
882 if (force_overwrite)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300883 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
884 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100885 else
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300886 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
887 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100888 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100889 if (err) {
890 ret = CMD_RET_FAILURE;
891 goto out;
892 }
893
894 /* Convert serial number to MAC address (the storage format) */
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200895 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100896
897 /* Valid Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300898 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100899
900 /* Product Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300901 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
902 sizeof(tdx_hw_tag));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100903
904 /* MAC Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300905 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
906 sizeof(tdx_eth_addr));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100907
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100908 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100909#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
910 err = tdx_cfg_block_mmc_storage(config_block, 1);
911#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
912 err = write_tdx_cfg_block_to_nand(config_block);
913#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
914 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300915#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
916 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100917#else
918 err = -EINVAL;
919#endif
920 if (err) {
921 printf("Failed to write Toradex config block: %d\n", ret);
922 ret = CMD_RET_FAILURE;
923 goto out;
924 }
925
926 printf("Toradex config block successfully written\n");
927
928out:
929 free(config_block);
930 return ret;
931}
932
Simon Glassed38aef2020-05-10 11:40:03 -0600933static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
934 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100935{
936 int ret;
937
938 if (argc < 2)
939 return CMD_RET_USAGE;
940
941 if (!strcmp(argv[1], "create")) {
942 return do_cfgblock_create(cmdtp, flag, argc, argv);
943 } else if (!strcmp(argv[1], "reload")) {
944 ret = read_tdx_cfg_block();
945 if (ret) {
946 printf("Failed to reload Toradex config block: %d\n",
947 ret);
948 return CMD_RET_FAILURE;
949 }
950 return CMD_RET_SUCCESS;
951 }
952
953 return CMD_RET_USAGE;
954}
955
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300956U_BOOT_CMD(
957 cfgblock, 5, 0, do_cfgblock,
958 "Toradex config block handling commands",
959 "create [-y] [barcode] - (Re-)create Toradex config block\n"
960 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
961 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100962);