blob: e513f4a29196a5177d16d6b7385f61cebb36dc58 [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
Igor Opaniuk414df352020-07-15 13:30:54 +0300145const char * const toradex_carrier_boards[] = {
146 [0] = "UNKNOWN CARRIER BOARD",
147 [155] = "Dahlia",
148 [156] = "Verdin Development Board",
149};
150
151const char * const toradex_display_adapters[] = {
152 [0] = "UNKNOWN DISPLAY ADAPTER",
153 [157] = "Verdin DSI to HDMI Adapter",
154 [159] = "Verdin DSI to LVDS Adapter",
155};
156
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200157const u32 toradex_ouis[] = {
158 [0] = 0x00142dUL,
159 [1] = 0x8c06cbUL,
160};
161
162static u32 get_serial_from_mac(struct toradex_eth_addr *eth_addr)
163{
164 int i;
165 u32 oui = ntohl(eth_addr->oui) >> 8;
166 u32 nic = ntohl(eth_addr->nic) >> 8;
167
168 for (i = 0; i < ARRAY_SIZE(toradex_ouis); i++) {
169 if (toradex_ouis[i] == oui)
170 break;
171 }
172
173 return (u32)((i << 24) + nic);
174}
175
176void get_mac_from_serial(u32 tdx_serial, struct toradex_eth_addr *eth_addr)
177{
178 u8 oui_index = tdx_serial >> 24;
179 u32 nic = tdx_serial & GENMASK(23, 0);
180 u32 oui;
181
182 if (oui_index >= ARRAY_SIZE(toradex_ouis)) {
183 puts("Can't find OUI for this serial#\n");
184 oui_index = 0;
185 }
186
187 oui = toradex_ouis[oui_index];
188
189 eth_addr->oui = htonl(oui << 8);
190 eth_addr->nic = htonl(nic << 8);
191}
192
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100193#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
194static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
195{
196 struct mmc *mmc;
197 int dev = CONFIG_TDX_CFG_BLOCK_DEV;
198 int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
199 uint part = CONFIG_TDX_CFG_BLOCK_PART;
200 uint blk_start;
201 int ret = 0;
202
203 /* Read production parameter config block from eMMC */
204 mmc = find_mmc_device(dev);
205 if (!mmc) {
206 puts("No MMC card found\n");
207 ret = -ENODEV;
208 goto out;
209 }
Stefan Agnerdd202342019-07-12 12:35:05 +0200210 if (mmc_init(mmc)) {
211 puts("MMC init failed\n");
212 return -EINVAL;
213 }
Simon Glass8c4c5c82017-04-23 20:02:11 -0600214 if (part != mmc_get_blk_desc(mmc)->hwpart) {
Simon Glassdbfa32c2022-08-11 19:34:59 -0600215 if (blk_select_hwpart_devnum(UCLASS_MMC, dev, part)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100216 puts("MMC partition switch failed\n");
217 ret = -ENODEV;
218 goto out;
219 }
220 }
221 if (offset < 0)
222 offset += mmc->capacity;
223 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
224
225 if (!write) {
226 /* Careful reads a whole block of 512 bytes into config_block */
227 if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
228 (unsigned char *)config_block) != 1) {
229 ret = -EIO;
230 goto out;
231 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100232 } else {
233 /* Just writing one 512 byte block */
234 if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
235 (unsigned char *)config_block) != 1) {
236 ret = -EIO;
237 goto out;
238 }
239 }
240
241out:
242 /* Switch back to regular eMMC user partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600243 blk_select_hwpart_devnum(UCLASS_MMC, 0, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100244
245 return ret;
246}
247#endif
248
249#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
250static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
251{
252 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
Stefan Agner8843b6d2018-08-06 09:19:18 +0200253 struct mtd_info *mtd = get_nand_dev_by_index(0);
254
255 if (!mtd)
256 return -ENODEV;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100257
258 /* Read production parameter config block from NAND page */
Stefan Agner8843b6d2018-08-06 09:19:18 +0200259 return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
Grygorii Strashkobb314622017-06-26 19:13:06 -0500260 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
261 config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100262}
263
264static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
265{
266 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
267
268 /* Write production parameter config block to NAND page */
Grygorii Strashkobb314622017-06-26 19:13:06 -0500269 return nand_write_skip_bad(get_nand_dev_by_index(0),
270 CONFIG_TDX_CFG_BLOCK_OFFSET,
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100271 &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
272 config_block, WITH_WR_VERIFY);
273}
274#endif
275
276#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
277static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
278{
279 /* Read production parameter config block from NOR flash */
280 memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
281 TDX_CFG_BLOCK_MAX_SIZE);
282 return 0;
283}
284
285static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
286{
287 /* Write production parameter config block to NOR flash */
288 return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
289 TDX_CFG_BLOCK_MAX_SIZE);
290}
291#endif
292
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300293#ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
294static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
295{
296 return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
297 TDX_CFG_BLOCK_MAX_SIZE);
298}
299
300static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
301{
302 return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
303 TDX_CFG_BLOCK_MAX_SIZE);
304}
305#endif
306
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100307int read_tdx_cfg_block(void)
308{
309 int ret = 0;
310 u8 *config_block = NULL;
311 struct toradex_tag *tag;
312 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
313 int offset;
314
315 /* Allocate RAM area for config block */
316 config_block = memalign(ARCH_DMA_MINALIGN, size);
317 if (!config_block) {
318 printf("Not enough malloc space available!\n");
319 return -ENOMEM;
320 }
321
322 memset(config_block, 0, size);
323
324#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
325 ret = tdx_cfg_block_mmc_storage(config_block, 0);
326#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
327 ret = read_tdx_cfg_block_from_nand(config_block);
328#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
329 ret = read_tdx_cfg_block_from_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300330#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
331 ret = read_tdx_cfg_block_from_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100332#else
333 ret = -EINVAL;
334#endif
335 if (ret)
336 goto out;
337
338 /* Expect a valid tag first */
339 tag = (struct toradex_tag *)config_block;
340 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
341 valid_cfgblock = false;
342 ret = -EINVAL;
343 goto out;
344 }
345 valid_cfgblock = true;
346 offset = 4;
347
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300348 /*
349 * check if there is enough space for storing tag and value of the
350 * biggest element
351 */
352 while (offset + sizeof(struct toradex_tag) +
353 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100354 tag = (struct toradex_tag *)(config_block + offset);
355 offset += 4;
356 if (tag->id == TAG_INVALID)
357 break;
358
359 if (tag->flags == TAG_FLAG_VALID) {
360 switch (tag->id) {
361 case TAG_MAC:
362 memcpy(&tdx_eth_addr, config_block + offset,
363 6);
364
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200365 tdx_serial = get_serial_from_mac(&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100366 break;
367 case TAG_HW:
368 memcpy(&tdx_hw_tag, config_block + offset, 8);
369 break;
370 }
371 }
372
373 /* Get to next tag according to current tags length */
374 offset += tag->len * 4;
375 }
376
377 /* Cap product id to avoid issues with a yet unknown one */
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200378 if (tdx_hw_tag.prodid >= ARRAY_SIZE(toradex_modules))
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100379 tdx_hw_tag.prodid = 0;
380
381out:
382 free(config_block);
383 return ret;
384}
385
Philippe Schenker498f95a2022-06-13 19:35:23 +0200386static int parse_assembly_string(char *string_to_parse, u16 *assembly)
387{
388 if (string_to_parse[3] >= 'A' && string_to_parse[3] <= 'Z')
389 *assembly = string_to_parse[3] - 'A';
390 else if (string_to_parse[3] == '#')
391 *assembly = dectoul(&string_to_parse[4], NULL);
392 else
393 return -EINVAL;
394
395 return 0;
396}
397
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100398static int get_cfgblock_interactive(void)
399{
400 char message[CONFIG_SYS_CBSIZE];
Marcel Ziswilereca26ba2020-01-28 14:42:24 +0100401 int len = 0;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200402 int ret = 0;
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200403 unsigned int prodid;
404 int i;
Philippe Schenker48b37032022-05-09 18:58:15 +0200405
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200406 printf("Enabled modules:\n");
407 for (i = 0; i < ARRAY_SIZE(toradex_modules); i++) {
408 if (toradex_modules[i].is_enabled)
409 printf(" %04d %s\n", i, toradex_modules[i].name);
Philippe Schenker48b37032022-05-09 18:58:15 +0200410 }
Marcel Ziswiler35e3c6e2019-07-12 12:35:06 +0200411
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200412 sprintf(message, "Enter the module ID: ");
413 len = cli_readline(message);
Stefan Agner68f58782019-04-09 17:24:08 +0200414
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200415 prodid = dectoul(console_buffer, NULL);
416 if (prodid >= ARRAY_SIZE(toradex_modules) || !toradex_modules[prodid].is_enabled) {
417 printf("Parsing module id failed\n");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100418 return -1;
419 }
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200420 tdx_hw_tag.prodid = prodid;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100421
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200422 len = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100423 while (len < 4) {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200424 sprintf(message, "Enter the module version (e.g. V1.1B or V1.1#26): V");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100425 len = cli_readline(message);
426 }
427
428 tdx_hw_tag.ver_major = console_buffer[0] - '0';
429 tdx_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200430
431 ret = parse_assembly_string(console_buffer, &tdx_hw_tag.ver_assembly);
432 if (ret) {
433 printf("Parsing module version failed\n");
434 return ret;
435 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100436
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100437 while (len < 8) {
438 sprintf(message, "Enter module serial number: ");
439 len = cli_readline(message);
440 }
441
Simon Glassff9b9032021-07-24 09:03:30 -0600442 tdx_serial = dectoul(console_buffer, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100443
444 return 0;
445}
446
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300447static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
448 u32 *serial)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100449{
Denys Drozdov3a1c6572021-04-07 15:28:24 +0300450 char revision[3] = {barcode[6], barcode[7], '\0'};
451
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100452 if (strlen(barcode) < 16) {
453 printf("Argument too short, barcode is 16 chars long\n");
454 return -1;
455 }
456
457 /* Get hardware information from the first 8 digits */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300458 tag->ver_major = barcode[4] - '0';
459 tag->ver_minor = barcode[5] - '0';
Simon Glassff9b9032021-07-24 09:03:30 -0600460 tag->ver_assembly = dectoul(revision, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100461
462 barcode[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600463 tag->prodid = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100464
465 /* Parse second part of the barcode (serial number */
466 barcode += 8;
Simon Glassff9b9032021-07-24 09:03:30 -0600467 *serial = dectoul(barcode, NULL);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100468
469 return 0;
470}
471
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300472static int write_tag(u8 *config_block, int *offset, int tag_id,
473 u8 *tag_data, size_t tag_data_size)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100474{
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100475 struct toradex_tag *tag;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300476
477 if (!offset || !config_block)
478 return -EINVAL;
479
480 tag = (struct toradex_tag *)(config_block + *offset);
481 tag->id = tag_id;
482 tag->flags = TAG_FLAG_VALID;
483 /* len is provided as number of 32bit values after the tag */
484 tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
485 *offset += sizeof(struct toradex_tag);
486 if (tag_data && tag_data_size) {
487 memcpy(config_block + *offset, tag_data,
488 tag_data_size);
489 *offset += tag_data_size;
490 }
491
492 return 0;
493}
494
495#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
496int read_tdx_cfg_block_carrier(void)
497{
498 int ret = 0;
499 u8 *config_block = NULL;
500 struct toradex_tag *tag;
501 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
502 int offset;
503
504 /* Allocate RAM area for carrier config block */
505 config_block = memalign(ARCH_DMA_MINALIGN, size);
506 if (!config_block) {
507 printf("Not enough malloc space available!\n");
508 return -ENOMEM;
509 }
510
511 memset(config_block, 0, size);
512
513 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
514 size);
515 if (ret)
516 return ret;
517
518 /* Expect a valid tag first */
519 tag = (struct toradex_tag *)config_block;
520 if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
521 valid_cfgblock_carrier = false;
522 ret = -EINVAL;
523 goto out;
524 }
525 valid_cfgblock_carrier = true;
526 offset = 4;
527
528 while (offset + sizeof(struct toradex_tag) +
529 sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
530 tag = (struct toradex_tag *)(config_block + offset);
531 offset += 4;
532 if (tag->id == TAG_INVALID)
533 break;
534
535 if (tag->flags == TAG_FLAG_VALID) {
536 switch (tag->id) {
537 case TAG_CAR_SERIAL:
538 memcpy(&tdx_car_serial, config_block + offset,
539 sizeof(tdx_car_serial));
540 break;
541 case TAG_HW:
542 memcpy(&tdx_car_hw_tag, config_block +
543 offset, 8);
544 break;
545 }
546 }
547
548 /* Get to next tag according to current tags length */
549 offset += tag->len * 4;
550 }
551out:
552 free(config_block);
553 return ret;
554}
555
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300556int check_pid8_sanity(char *pid8)
557{
558 char s_carrierid_verdin_dev[5];
559 char s_carrierid_dahlia[5];
560
561 sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
562 sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
563
564 /* sane value check, first 4 chars which represent carrier id */
565 if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
566 return 0;
567
568 if (!strncmp(pid8, s_carrierid_dahlia, 4))
569 return 0;
570
571 return -EINVAL;
572}
573
574int try_migrate_tdx_cfg_block_carrier(void)
575{
576 char pid8[8];
577 int offset = 0;
578 int ret = CMD_RET_SUCCESS;
579 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
580 u8 *config_block;
581
582 memset(pid8, 0x0, 8);
583 ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
584 if (ret)
585 return ret;
586
587 if (check_pid8_sanity(pid8))
588 return -EINVAL;
589
590 /* Allocate RAM area for config block */
591 config_block = memalign(ARCH_DMA_MINALIGN, size);
592 if (!config_block) {
593 printf("Not enough malloc space available!\n");
594 return CMD_RET_FAILURE;
595 }
596
597 memset(config_block, 0xff, size);
598 /* we try parse PID8 concatenating zeroed serial number */
599 tdx_car_hw_tag.ver_major = pid8[4] - '0';
600 tdx_car_hw_tag.ver_minor = pid8[5] - '0';
601 tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
602
603 pid8[4] = '\0';
Simon Glassff9b9032021-07-24 09:03:30 -0600604 tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
Igor Opaniuke9ad67a2020-07-15 13:30:56 +0300605
606 /* Valid Tag */
607 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
608
609 /* Product Tag */
610 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
611 sizeof(tdx_car_hw_tag));
612
613 /* Serial Tag */
614 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
615 sizeof(tdx_car_serial));
616
617 memset(config_block + offset, 0, 32 - offset);
618 ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
619 size);
620 if (ret) {
621 printf("Failed to write Toradex Extra config block: %d\n",
622 ret);
623 ret = CMD_RET_FAILURE;
624 goto out;
625 }
626
627 printf("Successfully migrated to Toradex Config Block from PID8\n");
628
629out:
630 free(config_block);
631 return ret;
632}
633
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300634static int get_cfgblock_carrier_interactive(void)
635{
636 char message[CONFIG_SYS_CBSIZE];
637 int len;
Philippe Schenker498f95a2022-06-13 19:35:23 +0200638 int ret = 0;
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300639
640 printf("Supported carrier boards:\n");
641 printf("CARRIER BOARD NAME\t\t [ID]\n");
Francesco Dolcini2dc68142022-07-21 15:17:33 +0200642 for (int i = 0; i < ARRAY_SIZE(toradex_carrier_boards); i++)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300643 if (toradex_carrier_boards[i])
644 printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
645
646 sprintf(message, "Choose your carrier board (provide ID): ");
647 len = cli_readline(message);
Simon Glassff9b9032021-07-24 09:03:30 -0600648 tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300649
650 do {
Philippe Schenker498f95a2022-06-13 19:35:23 +0200651 sprintf(message, "Enter carrier board version (e.g. V1.1B or V1.1#26): V");
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300652 len = cli_readline(message);
653 } while (len < 4);
654
655 tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
656 tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
Philippe Schenker498f95a2022-06-13 19:35:23 +0200657
658 ret = parse_assembly_string(console_buffer, &tdx_car_hw_tag.ver_assembly);
659 if (ret) {
660 printf("Parsing module version failed\n");
661 return ret;
662 }
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300663
664 while (len < 8) {
665 sprintf(message, "Enter carrier board serial number: ");
666 len = cli_readline(message);
667 }
668
Simon Glassff9b9032021-07-24 09:03:30 -0600669 tdx_car_serial = dectoul(console_buffer, NULL);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300670
671 return 0;
672}
673
674static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
675 char * const argv[])
676{
677 u8 *config_block;
678 size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100679 int offset = 0;
680 int ret = CMD_RET_SUCCESS;
681 int err;
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100682 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100683
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300684 if (argc >= 3) {
685 if (argv[2][0] == '-' && argv[2][1] == 'y')
686 force_overwrite = 1;
687 }
688
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100689 /* Allocate RAM area for config block */
690 config_block = memalign(ARCH_DMA_MINALIGN, size);
691 if (!config_block) {
692 printf("Not enough malloc space available!\n");
693 return CMD_RET_FAILURE;
694 }
695
696 memset(config_block, 0xff, size);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300697 read_tdx_cfg_block_carrier();
698 if (valid_cfgblock_carrier && !force_overwrite) {
699 char message[CONFIG_SYS_CBSIZE];
700
701 sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
702
703 if (!cli_readline(message))
704 goto out;
705
706 if (console_buffer[0] != 'y' &&
707 console_buffer[0] != 'Y')
708 goto out;
709 }
710
711 if (argc < 3 || (force_overwrite && argc < 4)) {
712 err = get_cfgblock_carrier_interactive();
713 } else {
714 if (force_overwrite)
715 err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
716 &tdx_car_serial);
717 else
718 err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
719 &tdx_car_serial);
720 }
721
722 if (err) {
723 ret = CMD_RET_FAILURE;
724 goto out;
725 }
726
727 /* Valid Tag */
728 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
729
730 /* Product Tag */
731 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
732 sizeof(tdx_car_hw_tag));
733
734 /* Serial Tag */
735 write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
736 sizeof(tdx_car_serial));
737
738 memset(config_block + offset, 0, 32 - offset);
739 err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
740 size);
741 if (err) {
742 printf("Failed to write Toradex Extra config block: %d\n",
743 ret);
744 ret = CMD_RET_FAILURE;
745 goto out;
746 }
747
748 printf("Toradex Extra config block successfully written\n");
749
750out:
751 free(config_block);
752 return ret;
753}
754
755#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
756
757static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
758 char * const argv[])
759{
760 u8 *config_block;
761 size_t size = TDX_CFG_BLOCK_MAX_SIZE;
762 int offset = 0;
763 int ret = CMD_RET_SUCCESS;
764 int err;
765 int force_overwrite = 0;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100766
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100767 if (argc >= 3) {
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300768#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
769 if (!strcmp(argv[2], "carrier"))
770 return do_cfgblock_carrier_create(cmdtp, flag,
771 --argc, ++argv);
772#endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100773 if (argv[2][0] == '-' && argv[2][1] == 'y')
774 force_overwrite = 1;
775 }
776
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300777 /* Allocate RAM area for config block */
778 config_block = memalign(ARCH_DMA_MINALIGN, size);
779 if (!config_block) {
780 printf("Not enough malloc space available!\n");
781 return CMD_RET_FAILURE;
782 }
783
784 memset(config_block, 0xff, size);
785
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100786 read_tdx_cfg_block();
787 if (valid_cfgblock) {
788#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
789 /*
790 * On NAND devices, recreation is only allowed if the page is
791 * empty (config block invalid...)
792 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200793 printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
Grygorii Strashkobb314622017-06-26 19:13:06 -0500794 CONFIG_TDX_CFG_BLOCK_OFFSET /
795 get_nand_dev_by_index(0)->erasesize);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100796 goto out;
797#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
798 /*
799 * On NOR devices, recreation is only allowed if the sector is
800 * empty and write protection is off (config block invalid...)
801 */
Marcel Ziswiler6cf2f782019-07-12 12:35:09 +0200802 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 +0100803 CONFIG_TDX_CFG_BLOCK_OFFSET);
804 goto out;
805#else
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100806 if (!force_overwrite) {
807 char message[CONFIG_SYS_CBSIZE];
808
809 sprintf(message,
810 "A valid Toradex config block is present, still recreate? [y/N] ");
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100811
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100812 if (!cli_readline(message))
813 goto out;
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100814
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100815 if (console_buffer[0] != 'y' &&
816 console_buffer[0] != 'Y')
817 goto out;
818 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100819#endif
820 }
821
822 /* Parse new Toradex config block data... */
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100823 if (argc < 3 || (force_overwrite && argc < 4)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100824 err = get_cfgblock_interactive();
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100825 } else {
826 if (force_overwrite)
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300827 err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
828 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100829 else
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300830 err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
831 &tdx_serial);
Dominik Sliwaf1e10592019-03-25 17:18:27 +0100832 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100833 if (err) {
834 ret = CMD_RET_FAILURE;
835 goto out;
836 }
837
838 /* Convert serial number to MAC address (the storage format) */
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200839 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100840
841 /* Valid Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300842 write_tag(config_block, &offset, TAG_VALID, NULL, 0);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100843
844 /* Product Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300845 write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
846 sizeof(tdx_hw_tag));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100847
848 /* MAC Tag */
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300849 write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
850 sizeof(tdx_eth_addr));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100851
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100852 memset(config_block + offset, 0, 32 - offset);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100853#if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
854 err = tdx_cfg_block_mmc_storage(config_block, 1);
855#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
856 err = write_tdx_cfg_block_to_nand(config_block);
857#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
858 err = write_tdx_cfg_block_to_nor(config_block);
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300859#elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
860 err = write_tdx_cfg_block_to_eeprom(config_block);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100861#else
862 err = -EINVAL;
863#endif
864 if (err) {
865 printf("Failed to write Toradex config block: %d\n", ret);
866 ret = CMD_RET_FAILURE;
867 goto out;
868 }
869
870 printf("Toradex config block successfully written\n");
871
872out:
873 free(config_block);
874 return ret;
875}
876
Simon Glassed38aef2020-05-10 11:40:03 -0600877static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
878 char *const argv[])
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100879{
880 int ret;
881
882 if (argc < 2)
883 return CMD_RET_USAGE;
884
885 if (!strcmp(argv[1], "create")) {
886 return do_cfgblock_create(cmdtp, flag, argc, argv);
887 } else if (!strcmp(argv[1], "reload")) {
888 ret = read_tdx_cfg_block();
889 if (ret) {
890 printf("Failed to reload Toradex config block: %d\n",
891 ret);
892 return CMD_RET_FAILURE;
893 }
894 return CMD_RET_SUCCESS;
895 }
896
897 return CMD_RET_USAGE;
898}
899
Igor Opaniuk67c9bfc2020-07-15 13:30:55 +0300900U_BOOT_CMD(
901 cfgblock, 5, 0, do_cfgblock,
902 "Toradex config block handling commands",
903 "create [-y] [barcode] - (Re-)create Toradex config block\n"
904 "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
905 "cfgblock reload - Reload Toradex config block from flash"
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100906);