blob: 12a558c7bba673252969f95e1d20c0b59028a78f [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) },
Emanuele Ghidoli898cdec2023-05-15 15:06:41 +0200142 [70] = { "Verdin iMX8M Plus Quad 8GB WB IT", TARGET_IS_ENABLED(VERDIN_IMX8MP) },
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100143};
144
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200145struct pid4list {
146 int pid4;
147 char * const name;
Igor Opaniuk414df352020-07-15 13:30:54 +0300148};
149
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200150const struct pid4list toradex_carrier_boards[] = {
151 /* the code assumes unknown at index 0 */
152 {0, "UNKNOWN CARRIER BOARD"},
153 {DAHLIA, "Dahlia"},
154 {VERDIN_DEVELOPMENT_BOARD, "Verdin Development Board"},
155 {YAVIA, "Yavia"},
156};
157
Max Krummenachercea09092023-07-18 11:07:34 +0200158const struct pid4list toradex_display_adapters[] = {
159 /* the code assumes unknown at index 0 */
160 {0, "UNKNOWN DISPLAY ADAPTER"},
161 {VERDIN_DSI_TO_HDMI_ADAPTER, "Verdin DSI to HDMI Adapter"},
162 {VERDIN_DSI_TO_LVDS_ADAPTER, "Verdin DSI to LVDS Adapter"},
Igor Opaniuk414df352020-07-15 13:30:54 +0300163};
164
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200165const u32 toradex_ouis[] = {
166 [0] = 0x00142dUL,
167 [1] = 0x8c06cbUL,
168};
169
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200170const char * const get_toradex_carrier_boards(int pid4)
171{
172 int i, index = 0;
173
174 for (i = 1; i < ARRAY_SIZE(toradex_carrier_boards); i++) {
175 if (pid4 == toradex_carrier_boards[i].pid4) {
176 index = i;
177 break;
178 }
179 }
180 return toradex_carrier_boards[index].name;
181}
182
Max Krummenachercea09092023-07-18 11:07:34 +0200183const char * const get_toradex_display_adapters(int pid4)
184{
185 int i, index = 0;
186
187 for (i = 1; i < ARRAY_SIZE(toradex_display_adapters); i++) {
188 if (pid4 == toradex_display_adapters[i].pid4) {
189 index = i;
190 break;
191 }
192 }
193 return toradex_display_adapters[index].name;
194}
195
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200196static u32 get_serial_from_mac(struct toradex_eth_addr *eth_addr)
197{
198 int i;
199 u32 oui = ntohl(eth_addr->oui) >> 8;
200 u32 nic = ntohl(eth_addr->nic) >> 8;
201
202 for (i = 0; i < ARRAY_SIZE(toradex_ouis); i++) {
203 if (toradex_ouis[i] == oui)
204 break;
205 }
206
207 return (u32)((i << 24) + nic);
208}
209
210void get_mac_from_serial(u32 tdx_serial, struct toradex_eth_addr *eth_addr)
211{
212 u8 oui_index = tdx_serial >> 24;
213 u32 nic = tdx_serial & GENMASK(23, 0);
214 u32 oui;
215
216 if (oui_index >= ARRAY_SIZE(toradex_ouis)) {
217 puts("Can't find OUI for this serial#\n");
218 oui_index = 0;
219 }
220
221 oui = toradex_ouis[oui_index];
222
223 eth_addr->oui = htonl(oui << 8);
224 eth_addr->nic = htonl(nic << 8);
225}
226
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100227#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
228static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
229{
230 struct mmc *mmc;
231 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
232 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
233 uint part = CONFIG_TDX_CFG_BLOCK_PART;
234 uint blk_start;
235 int ret = 0;
236
237 /* Read production parameter config block from eMMC */
238 mmc = find_mmc_device(dev);
239 if (!mmc) {
240 puts("No MMC card found\n");
241 ret = -ENODEV;
242 goto out;
243 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200244 if (mmc_init(mmc)) {
245 puts("MMC init failed\n");
246 return -EINVAL;
247 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600248 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Simon Glassdbfa32c2022-08-11 19:34:59 -0600249 if (blk_select_hwpart_devnum(UCLASS_MMC, dev, part)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100250 puts("MMC partition switch failed\n");
251 ret = -ENODEV;
252 goto out;
253 }
254 }
255 if (offset < 0)
256 offset += mmc->capacity;
257 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
258
259 if (!write) {
260 /* Careful reads a whole block of 512 bytes into config_block */
261 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
262 (unsigned char *)config_block) != 1) {
263 ret = -EIO;
264 goto out;
265 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100266 } else {
267 /* Just writing one 512 byte block */
268 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
269 (unsigned char *)config_block) != 1) {
270 ret = -EIO;
271 goto out;
272 }
273 }
274
275out:
276 /* Switch back to regular eMMC user partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600277 blk_select_hwpart_devnum(UCLASS_MMC, 0, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100278
279 return ret;
280}
281#endif
282
283#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
284static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
285{
286 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200287 struct mtd_info *mtd = get_nand_dev_by_index(0);
288
289 if (!mtd)
290 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100291
292 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200293 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500294 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
295 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100296}
297
298static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
299{
300 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
301
302 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500303 return nand_write_skip_bad(get_nand_dev_by_index(0),
304 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100305 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
306 config_block, WITH_WR_VERIFY);
307}
308#endif
309
310#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
311static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
312{
313 /* Read production parameter config block from NOR flash */
314 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
315 TDX_CFG_BLOCK_MAX_SIZE);
316 return 0;
317}
318
319static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
320{
321 /* Write production parameter config block to NOR flash */
322 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
323 TDX_CFG_BLOCK_MAX_SIZE);
324}
325#endif
326
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300327#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
328static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
329{
330 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
331 TDX_CFG_BLOCK_MAX_SIZE);
332}
333
334static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
335{
336 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
337 TDX_CFG_BLOCK_MAX_SIZE);
338}
339#endif
340
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100341int read_tdx_cfg_block(void)
342{
343 int ret = 0;
344 u8 *config_block = NULL;
345 struct toradex_tag *tag;
346 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
347 int offset;
348
349 /* Allocate RAM area for config block */
350 config_block = memalign(ARCH_DMA_MINALIGN, size);
351 if (!config_block) {
352 printf("Not enough malloc space available!\n");
353 return -ENOMEM;
354 }
355
356 memset(config_block, 0, size);
357
358#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
359 ret = tdx_cfg_block_mmc_storage(config_block, 0);
360#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
361 ret = read_tdx_cfg_block_from_nand(config_block);
362#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
363 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300364#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
365 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100366#else
367 ret = -EINVAL;
368#endif
369 if (ret)
370 goto out;
371
372 /* Expect a valid tag first */
373 tag = (struct toradex_tag *)config_block;
374 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
375 valid_cfgblock = false;
376 ret = -EINVAL;
377 goto out;
378 }
379 valid_cfgblock = true;
380 offset = 4;
381
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300382 /*
383 * check if there is enough space for storing tag and value of the
384 * biggest element
385 */
386 while (offset + sizeof(struct toradex_tag) +
387 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100388 tag = (struct toradex_tag *)(config_block + offset);
389 offset += 4;
390 if (tag->id == TAG_INVALID)
391 break;
392
393 if (tag->flags == TAG_FLAG_VALID) {
394 switch (tag->id) {
395 case TAG_MAC:
396 memcpy(&tdx_eth_addr, config_block + offset,
397 6);
398
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200399 tdx_serial = get_serial_from_mac(&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100400 break;
401 case TAG_HW:
402 memcpy(&tdx_hw_tag, config_block + offset, 8);
403 break;
404 }
405 }
406
407 /* Get to next tag according to current tags length */
408 offset += tag->len * 4;
409 }
410
411 /* Cap product id to avoid issues with a yet unknown one */
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200412 if (tdx_hw_tag.prodid >= ARRAY_SIZE(toradex_modules))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100413 tdx_hw_tag.prodid = 0;
414
415out:
416 free(config_block);
417 return ret;
418}
419
Philippe Schenker498f95a2022-06-13 19:35:23 +0200420static int parse_assembly_string(char *string_to_parse, u16 *assembly)
421{
422 if (string_to_parse[3] >= 'A' && string_to_parse[3] <= 'Z')
423 *assembly = string_to_parse[3] - 'A';
424 else if (string_to_parse[3] == '#')
425 *assembly = dectoul(&string_to_parse[4], NULL);
426 else
427 return -EINVAL;
428
429 return 0;
430}
431
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100432static int get_cfgblock_interactive(void)
433{
434 char message[CONFIG_SYS_CBSIZE];
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100435 int len = 0;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200436 int ret = 0;
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200437 unsigned int prodid;
438 int i;
Philippe Schenker48b37032022-05-09 18:58:15 +0200439
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200440 printf("Enabled modules:\n");
441 for (i = 0; i < ARRAY_SIZE(toradex_modules); i++) {
442 if (toradex_modules[i].is_enabled)
443 printf(" %04d %s\n", i, toradex_modules[i].name);
Philippe Schenker48b37032022-05-09 18:58:15 +0200444 }
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200445
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200446 sprintf(message, "Enter the module ID: ");
447 len = cli_readline(message);
Stefan Agner68f58782019-04-09 17:24:08 +0200448
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200449 prodid = dectoul(console_buffer, NULL);
450 if (prodid >= ARRAY_SIZE(toradex_modules) || !toradex_modules[prodid].is_enabled) {
451 printf("Parsing module id failed\n");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100452 return -1;
453 }
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200454 tdx_hw_tag.prodid = prodid;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100455
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200456 len = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100457 while (len < 4) {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200458 sprintf(message, "Enter the module version (e.g. V1.1B or V1.1#26): V");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100459 len = cli_readline(message);
460 }
461
462 tdx_hw_tag.ver_major = console_buffer[0] - '0';
463 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200464
465 ret = parse_assembly_string(console_buffer, &tdx_hw_tag.ver_assembly);
466 if (ret) {
467 printf("Parsing module version failed\n");
468 return ret;
469 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100470
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100471 while (len < 8) {
472 sprintf(message, "Enter module serial number: ");
473 len = cli_readline(message);
474 }
475
Simon Glassff9b9032021-07-24 09:03:30 -0600476 tdx_serial = dectoul(console_buffer, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100477
478 return 0;
479}
480
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300481static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
482 u32 *serial)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100483{
Denys Drozdov3a1c6572021-04-07 15:28:24 +0300484 char revision[3] = {barcode[6], barcode[7], '\0'};
485
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100486 if (strlen(barcode) < 16) {
487 printf("Argument too short, barcode is 16 chars long\n");
488 return -1;
489 }
490
491 /* Get hardware information from the first 8 digits */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300492 tag->ver_major = barcode[4] - '0';
493 tag->ver_minor = barcode[5] - '0';
Simon Glassff9b9032021-07-24 09:03:30 -0600494 tag->ver_assembly = dectoul(revision, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100495
496 barcode[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600497 tag->prodid = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100498
499 /* Parse second part of the barcode (serial number */
500 barcode += 8;
Simon Glassff9b9032021-07-24 09:03:30 -0600501 *serial = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100502
503 return 0;
504}
505
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300506static int write_tag(u8 *config_block, int *offset, int tag_id,
507 u8 *tag_data, size_t tag_data_size)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100508{
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100509 struct toradex_tag *tag;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300510
511 if (!offset || !config_block)
512 return -EINVAL;
513
514 tag = (struct toradex_tag *)(config_block + *offset);
515 tag->id = tag_id;
516 tag->flags = TAG_FLAG_VALID;
517 /* len is provided as number of 32bit values after the tag */
518 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
519 *offset += sizeof(struct toradex_tag);
520 if (tag_data && tag_data_size) {
521 memcpy(config_block + *offset, tag_data,
522 tag_data_size);
523 *offset += tag_data_size;
524 }
525
526 return 0;
527}
528
529#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
530int read_tdx_cfg_block_carrier(void)
531{
532 int ret = 0;
533 u8 *config_block = NULL;
534 struct toradex_tag *tag;
535 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
536 int offset;
537
538 /* Allocate RAM area for carrier config block */
539 config_block = memalign(ARCH_DMA_MINALIGN, size);
540 if (!config_block) {
541 printf("Not enough malloc space available!\n");
542 return -ENOMEM;
543 }
544
545 memset(config_block, 0, size);
546
547 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
548 size);
549 if (ret)
550 return ret;
551
552 /* Expect a valid tag first */
553 tag = (struct toradex_tag *)config_block;
554 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
555 valid_cfgblock_carrier = false;
556 ret = -EINVAL;
557 goto out;
558 }
559 valid_cfgblock_carrier = true;
560 offset = 4;
561
562 while (offset + sizeof(struct toradex_tag) +
563 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
564 tag = (struct toradex_tag *)(config_block + offset);
565 offset += 4;
566 if (tag->id == TAG_INVALID)
567 break;
568
569 if (tag->flags == TAG_FLAG_VALID) {
570 switch (tag->id) {
571 case TAG_CAR_SERIAL:
572 memcpy(&tdx_car_serial, config_block + offset,
573 sizeof(tdx_car_serial));
574 break;
575 case TAG_HW:
576 memcpy(&tdx_car_hw_tag, config_block +
577 offset, 8);
578 break;
579 }
580 }
581
582 /* Get to next tag according to current tags length */
583 offset += tag->len * 4;
584 }
585out:
586 free(config_block);
587 return ret;
588}
589
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300590int check_pid8_sanity(char *pid8)
591{
592 char s_carrierid_verdin_dev[5];
593 char s_carrierid_dahlia[5];
594
595 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
596 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
597
598 /* sane value check, first 4 chars which represent carrier id */
599 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
600 return 0;
601
602 if (!strncmp(pid8, s_carrierid_dahlia, 4))
603 return 0;
604
605 return -EINVAL;
606}
607
608int try_migrate_tdx_cfg_block_carrier(void)
609{
610 char pid8[8];
611 int offset = 0;
612 int ret = CMD_RET_SUCCESS;
613 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
614 u8 *config_block;
615
616 memset(pid8, 0x0, 8);
617 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
618 if (ret)
619 return ret;
620
621 if (check_pid8_sanity(pid8))
622 return -EINVAL;
623
624 /* Allocate RAM area for config block */
625 config_block = memalign(ARCH_DMA_MINALIGN, size);
626 if (!config_block) {
627 printf("Not enough malloc space available!\n");
628 return CMD_RET_FAILURE;
629 }
630
631 memset(config_block, 0xff, size);
632 /* we try parse PID8 concatenating zeroed serial number */
633 tdx_car_hw_tag.ver_major = pid8[4] - '0';
634 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
635 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
636
637 pid8[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600638 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300639
640 /* Valid Tag */
641 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
642
643 /* Product Tag */
644 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
645 sizeof(tdx_car_hw_tag));
646
647 /* Serial Tag */
648 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
649 sizeof(tdx_car_serial));
650
651 memset(config_block + offset, 0, 32 - offset);
652 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
653 size);
654 if (ret) {
655 printf("Failed to write Toradex Extra config block: %d\n",
656 ret);
657 ret = CMD_RET_FAILURE;
658 goto out;
659 }
660
661 printf("Successfully migrated to Toradex Config Block from PID8\n");
662
663out:
664 free(config_block);
665 return ret;
666}
667
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300668static int get_cfgblock_carrier_interactive(void)
669{
670 char message[CONFIG_SYS_CBSIZE];
671 int len;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200672 int ret = 0;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300673
674 printf("Supported carrier boards:\n");
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200675 printf("%30s\t[ID]\n", "CARRIER BOARD NAME");
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200676 for (int i = 0; i < ARRAY_SIZE(toradex_carrier_boards); i++)
Max Krummenacher2ded2d62023-07-18 11:07:33 +0200677 printf("%30s\t[%d]\n",
678 toradex_carrier_boards[i].name,
679 toradex_carrier_boards[i].pid4);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300680
681 sprintf(message, "Choose your carrier board (provide ID): ");
682 len = cli_readline(message);
Simon Glassff9b9032021-07-24 09:03:30 -0600683 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300684
685 do {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200686 sprintf(message, "Enter carrier board version (e.g. V1.1B or V1.1#26): V");
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300687 len = cli_readline(message);
688 } while (len < 4);
689
690 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
691 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200692
693 ret = parse_assembly_string(console_buffer, &tdx_car_hw_tag.ver_assembly);
694 if (ret) {
695 printf("Parsing module version failed\n");
696 return ret;
697 }
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300698
699 while (len < 8) {
700 sprintf(message, "Enter carrier board serial number: ");
701 len = cli_readline(message);
702 }
703
Simon Glassff9b9032021-07-24 09:03:30 -0600704 tdx_car_serial = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300705
706 return 0;
707}
708
709static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
710 char * const argv[])
711{
712 u8 *config_block;
713 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100714 int offset = 0;
715 int ret = CMD_RET_SUCCESS;
716 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100717 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100718
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300719 if (argc >= 3) {
720 if (argv[2][0] == '-' && argv[2][1] == 'y')
721 force_overwrite = 1;
722 }
723
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100724 /* Allocate RAM area for config block */
725 config_block = memalign(ARCH_DMA_MINALIGN, size);
726 if (!config_block) {
727 printf("Not enough malloc space available!\n");
728 return CMD_RET_FAILURE;
729 }
730
731 memset(config_block, 0xff, size);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300732 read_tdx_cfg_block_carrier();
733 if (valid_cfgblock_carrier && !force_overwrite) {
734 char message[CONFIG_SYS_CBSIZE];
735
736 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
737
738 if (!cli_readline(message))
739 goto out;
740
741 if (console_buffer[0] != 'y' &&
742 console_buffer[0] != 'Y')
743 goto out;
744 }
745
746 if (argc < 3 || (force_overwrite && argc < 4)) {
747 err = get_cfgblock_carrier_interactive();
748 } else {
749 if (force_overwrite)
750 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
751 &tdx_car_serial);
752 else
753 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
754 &tdx_car_serial);
755 }
756
757 if (err) {
758 ret = CMD_RET_FAILURE;
759 goto out;
760 }
761
762 /* Valid Tag */
763 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
764
765 /* Product Tag */
766 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
767 sizeof(tdx_car_hw_tag));
768
769 /* Serial Tag */
770 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
771 sizeof(tdx_car_serial));
772
773 memset(config_block + offset, 0, 32 - offset);
774 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
775 size);
776 if (err) {
777 printf("Failed to write Toradex Extra config block: %d\n",
778 ret);
779 ret = CMD_RET_FAILURE;
780 goto out;
781 }
782
783 printf("Toradex Extra config block successfully written\n");
784
785out:
786 free(config_block);
787 return ret;
788}
789
790#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
791
792static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
793 char * const argv[])
794{
795 u8 *config_block;
796 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
797 int offset = 0;
798 int ret = CMD_RET_SUCCESS;
799 int err;
800 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100801
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100802 if (argc >= 3) {
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300803#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
804 if (!strcmp(argv[2], "carrier"))
805 return do_cfgblock_carrier_create(cmdtp, flag,
806 --argc, ++argv);
807#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100808 if (argv[2][0] == '-' && argv[2][1] == 'y')
809 force_overwrite = 1;
810 }
811
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300812 /* Allocate RAM area for config block */
813 config_block = memalign(ARCH_DMA_MINALIGN, size);
814 if (!config_block) {
815 printf("Not enough malloc space available!\n");
816 return CMD_RET_FAILURE;
817 }
818
819 memset(config_block, 0xff, size);
820
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100821 read_tdx_cfg_block();
822 if (valid_cfgblock) {
823#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
824 /*
825 * On NAND devices, recreation is only allowed if the page is
826 * empty (config block invalid...)
827 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200828 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500829 CONFIG_TDX_CFG_BLOCK_OFFSET /
830 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100831 goto out;
832#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
833 /*
834 * On NOR devices, recreation is only allowed if the sector is
835 * empty and write protection is off (config block invalid...)
836 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200837 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 +0100838 CONFIG_TDX_CFG_BLOCK_OFFSET);
839 goto out;
840#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100841 if (!force_overwrite) {
842 char message[CONFIG_SYS_CBSIZE];
843
844 sprintf(message,
845 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100846
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100847 if (!cli_readline(message))
848 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100849
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100850 if (console_buffer[0] != 'y' &&
851 console_buffer[0] != 'Y')
852 goto out;
853 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100854#endif
855 }
856
857 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100858 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100859 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100860 } else {
861 if (force_overwrite)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300862 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
863 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100864 else
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300865 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
866 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100867 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100868 if (err) {
869 ret = CMD_RET_FAILURE;
870 goto out;
871 }
872
873 /* Convert serial number to MAC address (the storage format) */
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200874 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100875
876 /* Valid Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300877 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100878
879 /* Product Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300880 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
881 sizeof(tdx_hw_tag));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100882
883 /* MAC Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300884 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
885 sizeof(tdx_eth_addr));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100886
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100887 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100888#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
889 err = tdx_cfg_block_mmc_storage(config_block, 1);
890#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
891 err = write_tdx_cfg_block_to_nand(config_block);
892#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
893 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300894#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
895 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100896#else
897 err = -EINVAL;
898#endif
899 if (err) {
900 printf("Failed to write Toradex config block: %d\n", ret);
901 ret = CMD_RET_FAILURE;
902 goto out;
903 }
904
905 printf("Toradex config block successfully written\n");
906
907out:
908 free(config_block);
909 return ret;
910}
911
Simon Glassed38aef2020-05-10 11:40:03 -0600912static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
913 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100914{
915 int ret;
916
917 if (argc < 2)
918 return CMD_RET_USAGE;
919
920 if (!strcmp(argv[1], "create")) {
921 return do_cfgblock_create(cmdtp, flag, argc, argv);
922 } else if (!strcmp(argv[1], "reload")) {
923 ret = read_tdx_cfg_block();
924 if (ret) {
925 printf("Failed to reload Toradex config block: %d\n",
926 ret);
927 return CMD_RET_FAILURE;
928 }
929 return CMD_RET_SUCCESS;
930 }
931
932 return CMD_RET_USAGE;
933}
934
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300935U_BOOT_CMD(
936 cfgblock, 5, 0, do_cfgblock,
937 "Toradex config block handling commands",
938 "create [-y] [barcode] - (Re-)create Toradex config block\n"
939 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
940 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100941);