]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
a2777ecb | 2 | /* |
08f80555 | 3 | * Copyright (c) 2016-2019 Toradex, Inc. |
a2777ecb MZ |
4 | */ |
5 | ||
6 | #include <common.h> | |
7 | #include "tdx-cfg-block.h" | |
8 | ||
9 | #if defined(CONFIG_TARGET_APALIS_IMX6) || defined(CONFIG_TARGET_COLIBRI_IMX6) | |
10 | #include <asm/arch/sys_proto.h> | |
11 | #else | |
12 | #define is_cpu_type(cpu) (0) | |
13 | #endif | |
14 | #if defined(CONFIG_CPU_PXA27X) | |
15 | #include <asm/arch-pxa/pxa.h> | |
16 | #else | |
17 | #define cpu_is_pxa27x(cpu) (0) | |
18 | #endif | |
19 | #include <cli.h> | |
20 | #include <console.h> | |
21 | #include <flash.h> | |
22 | #include <malloc.h> | |
23 | #include <mmc.h> | |
24 | #include <nand.h> | |
c62db35d | 25 | #include <asm/mach-types.h> |
a2777ecb MZ |
26 | |
27 | DECLARE_GLOBAL_DATA_PTR; | |
28 | ||
29 | #define TAG_VALID 0xcf01 | |
30 | #define TAG_MAC 0x0000 | |
31 | #define TAG_HW 0x0008 | |
32 | #define TAG_INVALID 0xffff | |
33 | ||
34 | #define TAG_FLAG_VALID 0x1 | |
35 | ||
36 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) | |
37 | #define TDX_CFG_BLOCK_MAX_SIZE 512 | |
38 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) | |
39 | #define TDX_CFG_BLOCK_MAX_SIZE 64 | |
40 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) | |
41 | #define TDX_CFG_BLOCK_MAX_SIZE 64 | |
42 | #else | |
43 | #error Toradex config block location not set | |
44 | #endif | |
45 | ||
46 | struct toradex_tag { | |
47 | u32 len:14; | |
48 | u32 flags:2; | |
49 | u32 id:16; | |
50 | }; | |
51 | ||
52 | bool valid_cfgblock; | |
53 | struct toradex_hw tdx_hw_tag; | |
54 | struct toradex_eth_addr tdx_eth_addr; | |
55 | u32 tdx_serial; | |
56 | ||
57 | const char * const toradex_modules[] = { | |
58 | [0] = "UNKNOWN MODULE", | |
59 | [1] = "Colibri PXA270 312MHz", | |
60 | [2] = "Colibri PXA270 520MHz", | |
61 | [3] = "Colibri PXA320 806MHz", | |
62 | [4] = "Colibri PXA300 208MHz", | |
63 | [5] = "Colibri PXA310 624MHz", | |
64 | [6] = "Colibri PXA320 806MHz IT", | |
65 | [7] = "Colibri PXA300 208MHz XT", | |
66 | [8] = "Colibri PXA270 312MHz", | |
67 | [9] = "Colibri PXA270 520MHz", | |
68 | [10] = "Colibri VF50 128MB", /* not currently on sale */ | |
69 | [11] = "Colibri VF61 256MB", | |
70 | [12] = "Colibri VF61 256MB IT", | |
71 | [13] = "Colibri VF50 128MB IT", | |
72 | [14] = "Colibri iMX6 Solo 256MB", | |
73 | [15] = "Colibri iMX6 DualLite 512MB", | |
74 | [16] = "Colibri iMX6 Solo 256MB IT", | |
75 | [17] = "Colibri iMX6 DualLite 512MB IT", | |
76 | [18] = "UNKNOWN MODULE", | |
77 | [19] = "UNKNOWN MODULE", | |
78 | [20] = "Colibri T20 256MB", | |
79 | [21] = "Colibri T20 512MB", | |
80 | [22] = "Colibri T20 512MB IT", | |
81 | [23] = "Colibri T30 1GB", | |
82 | [24] = "Colibri T20 256MB IT", | |
83 | [25] = "Apalis T30 2GB", | |
84 | [26] = "Apalis T30 1GB", | |
85 | [27] = "Apalis iMX6 Quad 1GB", | |
86 | [28] = "Apalis iMX6 Quad 2GB IT", | |
87 | [29] = "Apalis iMX6 Dual 512MB", | |
88 | [30] = "Colibri T30 1GB IT", | |
89 | [31] = "Apalis T30 1GB IT", | |
90 | [32] = "Colibri iMX7 Solo 256MB", | |
91 | [33] = "Colibri iMX7 Dual 512MB", | |
92 | [34] = "Apalis TK1 2GB", | |
93 | [35] = "Apalis iMX6 Dual 1GB IT", | |
d826b875 SA |
94 | [36] = "Colibri iMX6ULL 256MB", |
95 | [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / Bluetooth", | |
96 | [38] = "Colibri iMX8X", | |
97 | [39] = "Colibri iMX7 Dual 1GB (eMMC)", | |
98 | [40] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth IT", | |
99 | [41] = "Colibri iMX7 Dual 512MB EPDC", | |
100 | [42] = "Apalis TK1 4GB", | |
08f80555 GS |
101 | [43] = "Colibri T20 512MB IT SETEK", |
102 | [44] = "Colibri iMX6ULL 512MB IT", | |
103 | [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth", | |
a2777ecb MZ |
104 | }; |
105 | ||
106 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC | |
107 | static int tdx_cfg_block_mmc_storage(u8 *config_block, int write) | |
108 | { | |
109 | struct mmc *mmc; | |
110 | int dev = CONFIG_TDX_CFG_BLOCK_DEV; | |
111 | int offset = CONFIG_TDX_CFG_BLOCK_OFFSET; | |
112 | uint part = CONFIG_TDX_CFG_BLOCK_PART; | |
113 | uint blk_start; | |
114 | int ret = 0; | |
115 | ||
116 | /* Read production parameter config block from eMMC */ | |
117 | mmc = find_mmc_device(dev); | |
118 | if (!mmc) { | |
119 | puts("No MMC card found\n"); | |
120 | ret = -ENODEV; | |
121 | goto out; | |
122 | } | |
0e513e78 | 123 | if (part != mmc_get_blk_desc(mmc)->hwpart) { |
a2777ecb MZ |
124 | if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) { |
125 | puts("MMC partition switch failed\n"); | |
126 | ret = -ENODEV; | |
127 | goto out; | |
128 | } | |
129 | } | |
130 | if (offset < 0) | |
131 | offset += mmc->capacity; | |
132 | blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len; | |
133 | ||
134 | if (!write) { | |
135 | /* Careful reads a whole block of 512 bytes into config_block */ | |
136 | if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1, | |
137 | (unsigned char *)config_block) != 1) { | |
138 | ret = -EIO; | |
139 | goto out; | |
140 | } | |
a2777ecb MZ |
141 | } else { |
142 | /* Just writing one 512 byte block */ | |
143 | if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1, | |
144 | (unsigned char *)config_block) != 1) { | |
145 | ret = -EIO; | |
146 | goto out; | |
147 | } | |
148 | } | |
149 | ||
150 | out: | |
151 | /* Switch back to regular eMMC user partition */ | |
152 | blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0); | |
153 | ||
154 | return ret; | |
155 | } | |
156 | #endif | |
157 | ||
158 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND | |
159 | static int read_tdx_cfg_block_from_nand(unsigned char *config_block) | |
160 | { | |
161 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; | |
bc53fb19 SA |
162 | struct mtd_info *mtd = get_nand_dev_by_index(0); |
163 | ||
164 | if (!mtd) | |
165 | return -ENODEV; | |
a2777ecb MZ |
166 | |
167 | /* Read production parameter config block from NAND page */ | |
bc53fb19 | 168 | return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET, |
bf264cd0 GS |
169 | &size, NULL, TDX_CFG_BLOCK_MAX_SIZE, |
170 | config_block); | |
a2777ecb MZ |
171 | } |
172 | ||
173 | static int write_tdx_cfg_block_to_nand(unsigned char *config_block) | |
174 | { | |
175 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; | |
176 | ||
177 | /* Write production parameter config block to NAND page */ | |
bf264cd0 GS |
178 | return nand_write_skip_bad(get_nand_dev_by_index(0), |
179 | CONFIG_TDX_CFG_BLOCK_OFFSET, | |
a2777ecb MZ |
180 | &size, NULL, TDX_CFG_BLOCK_MAX_SIZE, |
181 | config_block, WITH_WR_VERIFY); | |
182 | } | |
183 | #endif | |
184 | ||
185 | #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR | |
186 | static int read_tdx_cfg_block_from_nor(unsigned char *config_block) | |
187 | { | |
188 | /* Read production parameter config block from NOR flash */ | |
189 | memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET, | |
190 | TDX_CFG_BLOCK_MAX_SIZE); | |
191 | return 0; | |
192 | } | |
193 | ||
194 | static int write_tdx_cfg_block_to_nor(unsigned char *config_block) | |
195 | { | |
196 | /* Write production parameter config block to NOR flash */ | |
197 | return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET, | |
198 | TDX_CFG_BLOCK_MAX_SIZE); | |
199 | } | |
200 | #endif | |
201 | ||
202 | int read_tdx_cfg_block(void) | |
203 | { | |
204 | int ret = 0; | |
205 | u8 *config_block = NULL; | |
206 | struct toradex_tag *tag; | |
207 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; | |
208 | int offset; | |
209 | ||
210 | /* Allocate RAM area for config block */ | |
211 | config_block = memalign(ARCH_DMA_MINALIGN, size); | |
212 | if (!config_block) { | |
213 | printf("Not enough malloc space available!\n"); | |
214 | return -ENOMEM; | |
215 | } | |
216 | ||
217 | memset(config_block, 0, size); | |
218 | ||
219 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) | |
220 | ret = tdx_cfg_block_mmc_storage(config_block, 0); | |
221 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) | |
222 | ret = read_tdx_cfg_block_from_nand(config_block); | |
223 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) | |
224 | ret = read_tdx_cfg_block_from_nor(config_block); | |
225 | #else | |
226 | ret = -EINVAL; | |
227 | #endif | |
228 | if (ret) | |
229 | goto out; | |
230 | ||
231 | /* Expect a valid tag first */ | |
232 | tag = (struct toradex_tag *)config_block; | |
233 | if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) { | |
234 | valid_cfgblock = false; | |
235 | ret = -EINVAL; | |
236 | goto out; | |
237 | } | |
238 | valid_cfgblock = true; | |
239 | offset = 4; | |
240 | ||
241 | while (offset < TDX_CFG_BLOCK_MAX_SIZE) { | |
242 | tag = (struct toradex_tag *)(config_block + offset); | |
243 | offset += 4; | |
244 | if (tag->id == TAG_INVALID) | |
245 | break; | |
246 | ||
247 | if (tag->flags == TAG_FLAG_VALID) { | |
248 | switch (tag->id) { | |
249 | case TAG_MAC: | |
250 | memcpy(&tdx_eth_addr, config_block + offset, | |
251 | 6); | |
252 | ||
253 | /* NIC part of MAC address is serial number */ | |
254 | tdx_serial = ntohl(tdx_eth_addr.nic) >> 8; | |
255 | break; | |
256 | case TAG_HW: | |
257 | memcpy(&tdx_hw_tag, config_block + offset, 8); | |
258 | break; | |
259 | } | |
260 | } | |
261 | ||
262 | /* Get to next tag according to current tags length */ | |
263 | offset += tag->len * 4; | |
264 | } | |
265 | ||
266 | /* Cap product id to avoid issues with a yet unknown one */ | |
ccdd3713 | 267 | if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) / |
a2777ecb MZ |
268 | sizeof(toradex_modules[0]))) |
269 | tdx_hw_tag.prodid = 0; | |
270 | ||
271 | out: | |
272 | free(config_block); | |
273 | return ret; | |
274 | } | |
275 | ||
276 | static int get_cfgblock_interactive(void) | |
277 | { | |
278 | char message[CONFIG_SYS_CBSIZE]; | |
279 | char *soc; | |
280 | char it = 'n'; | |
281 | int len; | |
282 | ||
89315f31 SA |
283 | /* Unknown module by default */ |
284 | tdx_hw_tag.prodid = 0; | |
285 | ||
a2777ecb MZ |
286 | if (cpu_is_pxa27x()) |
287 | sprintf(message, "Is the module the 312 MHz version? [y/N] "); | |
288 | else | |
289 | sprintf(message, "Is the module an IT version? [y/N] "); | |
290 | ||
291 | len = cli_readline(message); | |
292 | it = console_buffer[0]; | |
293 | ||
00caae6d | 294 | soc = env_get("soc"); |
a2777ecb | 295 | if (!strcmp("mx6", soc)) { |
89315f31 SA |
296 | #ifdef CONFIG_TARGET_APALIS_IMX6 |
297 | if (it == 'y' || it == 'Y') { | |
a2777ecb MZ |
298 | if (is_cpu_type(MXC_CPU_MX6Q)) |
299 | tdx_hw_tag.prodid = APALIS_IMX6Q_IT; | |
300 | else | |
301 | tdx_hw_tag.prodid = APALIS_IMX6D_IT; | |
89315f31 | 302 | } else { |
a2777ecb MZ |
303 | if (is_cpu_type(MXC_CPU_MX6Q)) |
304 | tdx_hw_tag.prodid = APALIS_IMX6Q; | |
305 | else | |
306 | tdx_hw_tag.prodid = APALIS_IMX6D; | |
89315f31 SA |
307 | } |
308 | #elif CONFIG_TARGET_COLIBRI_IMX6 | |
08f80555 | 309 | if (it == 'y' || it == 'Y') { |
89315f31 | 310 | if (is_cpu_type(MXC_CPU_MX6DL)) |
a2777ecb | 311 | tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT; |
89315f31 | 312 | else if (is_cpu_type(MXC_CPU_MX6SOLO)) |
a2777ecb | 313 | tdx_hw_tag.prodid = COLIBRI_IMX6S_IT; |
08f80555 | 314 | } else { |
89315f31 | 315 | if (is_cpu_type(MXC_CPU_MX6DL)) |
a2777ecb | 316 | tdx_hw_tag.prodid = COLIBRI_IMX6DL; |
89315f31 | 317 | else if (is_cpu_type(MXC_CPU_MX6SOLO)) |
a2777ecb | 318 | tdx_hw_tag.prodid = COLIBRI_IMX6S; |
08f80555 | 319 | } |
89315f31 SA |
320 | #elif CONFIG_TARGET_COLIBRI_IMX6ULL |
321 | char wb = 'n'; | |
322 | ||
323 | sprintf(message, "Does the module have Wi-Fi / Bluetooth? " \ | |
324 | "[y/N] "); | |
325 | len = cli_readline(message); | |
326 | wb = console_buffer[0]; | |
327 | if (it == 'y' || it == 'Y') { | |
328 | if (wb == 'y' || wb == 'Y') | |
329 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT; | |
330 | else | |
331 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT; | |
332 | } else { | |
333 | if (wb == 'y' || wb == 'Y') | |
334 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT; | |
335 | else | |
336 | tdx_hw_tag.prodid = COLIBRI_IMX6ULL; | |
337 | } | |
338 | #endif | |
08f80555 | 339 | } else if (!strcmp("imx7d", soc)) |
a2777ecb | 340 | tdx_hw_tag.prodid = COLIBRI_IMX7D; |
08f80555 | 341 | else if (!strcmp("imx7s", soc)) |
a2777ecb | 342 | tdx_hw_tag.prodid = COLIBRI_IMX7S; |
08f80555 | 343 | else if (!strcmp("tegra20", soc)) { |
a2777ecb MZ |
344 | if (it == 'y' || it == 'Y') |
345 | if (gd->ram_size == 0x10000000) | |
346 | tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT; | |
347 | else | |
348 | tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT; | |
349 | else | |
350 | if (gd->ram_size == 0x10000000) | |
351 | tdx_hw_tag.prodid = COLIBRI_T20_256MB; | |
352 | else | |
353 | tdx_hw_tag.prodid = COLIBRI_T20_512MB; | |
354 | } else if (cpu_is_pxa27x()) { | |
355 | if (it == 'y' || it == 'Y') | |
356 | tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ; | |
357 | else | |
358 | tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ; | |
08f80555 | 359 | } |
a2777ecb | 360 | #ifdef CONFIG_MACH_TYPE |
08f80555 | 361 | else if (!strcmp("tegra30", soc)) { |
a2777ecb MZ |
362 | if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) { |
363 | if (it == 'y' || it == 'Y') | |
364 | tdx_hw_tag.prodid = APALIS_T30_IT; | |
365 | else | |
366 | if (gd->ram_size == 0x40000000) | |
367 | tdx_hw_tag.prodid = APALIS_T30_1GB; | |
368 | else | |
369 | tdx_hw_tag.prodid = APALIS_T30_2GB; | |
370 | } else { | |
371 | if (it == 'y' || it == 'Y') | |
372 | tdx_hw_tag.prodid = COLIBRI_T30_IT; | |
373 | else | |
374 | tdx_hw_tag.prodid = COLIBRI_T30; | |
375 | } | |
08f80555 | 376 | } |
a2777ecb | 377 | #endif /* CONFIG_MACH_TYPE */ |
08f80555 | 378 | else if (!strcmp("tegra124", soc)) { |
a2777ecb MZ |
379 | tdx_hw_tag.prodid = APALIS_TK1_2GB; |
380 | } else if (!strcmp("vf500", soc)) { | |
381 | if (it == 'y' || it == 'Y') | |
382 | tdx_hw_tag.prodid = COLIBRI_VF50_IT; | |
383 | else | |
384 | tdx_hw_tag.prodid = COLIBRI_VF50; | |
385 | } else if (!strcmp("vf610", soc)) { | |
386 | if (it == 'y' || it == 'Y') | |
387 | tdx_hw_tag.prodid = COLIBRI_VF61_IT; | |
388 | else | |
389 | tdx_hw_tag.prodid = COLIBRI_VF61; | |
89315f31 SA |
390 | } |
391 | ||
392 | if (!tdx_hw_tag.prodid) { | |
a2777ecb MZ |
393 | printf("Module type not detectable due to unknown SoC\n"); |
394 | return -1; | |
395 | } | |
396 | ||
397 | while (len < 4) { | |
398 | sprintf(message, "Enter the module version (e.g. V1.1B): V"); | |
399 | len = cli_readline(message); | |
400 | } | |
401 | ||
402 | tdx_hw_tag.ver_major = console_buffer[0] - '0'; | |
403 | tdx_hw_tag.ver_minor = console_buffer[2] - '0'; | |
404 | tdx_hw_tag.ver_assembly = console_buffer[3] - 'A'; | |
405 | ||
08f80555 | 406 | if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1) |
a2777ecb MZ |
407 | tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ - |
408 | COLIBRI_PXA270_V1_312MHZ); | |
409 | ||
410 | while (len < 8) { | |
411 | sprintf(message, "Enter module serial number: "); | |
412 | len = cli_readline(message); | |
413 | } | |
414 | ||
415 | tdx_serial = simple_strtoul(console_buffer, NULL, 10); | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
420 | static int get_cfgblock_barcode(char *barcode) | |
421 | { | |
422 | if (strlen(barcode) < 16) { | |
423 | printf("Argument too short, barcode is 16 chars long\n"); | |
424 | return -1; | |
425 | } | |
426 | ||
427 | /* Get hardware information from the first 8 digits */ | |
428 | tdx_hw_tag.ver_major = barcode[4] - '0'; | |
429 | tdx_hw_tag.ver_minor = barcode[5] - '0'; | |
430 | tdx_hw_tag.ver_assembly = barcode[7] - '0'; | |
431 | ||
432 | barcode[4] = '\0'; | |
433 | tdx_hw_tag.prodid = simple_strtoul(barcode, NULL, 10); | |
434 | ||
435 | /* Parse second part of the barcode (serial number */ | |
436 | barcode += 8; | |
437 | tdx_serial = simple_strtoul(barcode, NULL, 10); | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
442 | static int do_cfgblock_create(cmd_tbl_t *cmdtp, int flag, int argc, | |
443 | char * const argv[]) | |
444 | { | |
445 | u8 *config_block; | |
446 | struct toradex_tag *tag; | |
447 | size_t size = TDX_CFG_BLOCK_MAX_SIZE; | |
448 | int offset = 0; | |
449 | int ret = CMD_RET_SUCCESS; | |
450 | int err; | |
587b13c6 | 451 | int force_overwrite = 0; |
a2777ecb MZ |
452 | |
453 | /* Allocate RAM area for config block */ | |
454 | config_block = memalign(ARCH_DMA_MINALIGN, size); | |
455 | if (!config_block) { | |
456 | printf("Not enough malloc space available!\n"); | |
457 | return CMD_RET_FAILURE; | |
458 | } | |
459 | ||
460 | memset(config_block, 0xff, size); | |
461 | ||
587b13c6 DS |
462 | if (argc >= 3) { |
463 | if (argv[2][0] == '-' && argv[2][1] == 'y') | |
464 | force_overwrite = 1; | |
465 | } | |
466 | ||
a2777ecb MZ |
467 | read_tdx_cfg_block(); |
468 | if (valid_cfgblock) { | |
469 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) | |
470 | /* | |
471 | * On NAND devices, recreation is only allowed if the page is | |
472 | * empty (config block invalid...) | |
473 | */ | |
08f80555 GS |
474 | printf("NAND erase block %d need to be erased before creating" \ |
475 | " a Toradex config block\n", | |
bf264cd0 GS |
476 | CONFIG_TDX_CFG_BLOCK_OFFSET / |
477 | get_nand_dev_by_index(0)->erasesize); | |
a2777ecb MZ |
478 | goto out; |
479 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) | |
480 | /* | |
481 | * On NOR devices, recreation is only allowed if the sector is | |
482 | * empty and write protection is off (config block invalid...) | |
483 | */ | |
08f80555 GS |
484 | printf("NOR sector at offset 0x%02x need to be erased and " \ |
485 | "unprotected before creating a Toradex config block\n", | |
a2777ecb MZ |
486 | CONFIG_TDX_CFG_BLOCK_OFFSET); |
487 | goto out; | |
488 | #else | |
587b13c6 DS |
489 | if (!force_overwrite) { |
490 | char message[CONFIG_SYS_CBSIZE]; | |
a2777ecb | 491 | |
587b13c6 DS |
492 | sprintf(message, |
493 | "A valid Toradex config block is present, still recreate? [y/N] "); | |
a2777ecb | 494 | |
587b13c6 DS |
495 | if (!cli_readline(message)) |
496 | goto out; | |
497 | ||
498 | if (console_buffer[0] != 'y' && | |
499 | console_buffer[0] != 'Y') | |
500 | goto out; | |
501 | } | |
a2777ecb MZ |
502 | #endif |
503 | } | |
504 | ||
505 | /* Parse new Toradex config block data... */ | |
587b13c6 | 506 | if (argc < 3 || (force_overwrite && argc < 4)) { |
a2777ecb | 507 | err = get_cfgblock_interactive(); |
587b13c6 DS |
508 | } else { |
509 | if (force_overwrite) | |
510 | err = get_cfgblock_barcode(argv[3]); | |
511 | else | |
512 | err = get_cfgblock_barcode(argv[2]); | |
513 | } | |
a2777ecb MZ |
514 | if (err) { |
515 | ret = CMD_RET_FAILURE; | |
516 | goto out; | |
517 | } | |
518 | ||
519 | /* Convert serial number to MAC address (the storage format) */ | |
520 | tdx_eth_addr.oui = htonl(0x00142dUL << 8); | |
521 | tdx_eth_addr.nic = htonl(tdx_serial << 8); | |
522 | ||
523 | /* Valid Tag */ | |
524 | tag = (struct toradex_tag *)config_block; | |
525 | tag->id = TAG_VALID; | |
526 | tag->flags = TAG_FLAG_VALID; | |
527 | tag->len = 0; | |
528 | offset += 4; | |
529 | ||
530 | /* Product Tag */ | |
531 | tag = (struct toradex_tag *)(config_block + offset); | |
532 | tag->id = TAG_HW; | |
533 | tag->flags = TAG_FLAG_VALID; | |
534 | tag->len = 2; | |
535 | offset += 4; | |
536 | ||
537 | memcpy(config_block + offset, &tdx_hw_tag, 8); | |
538 | offset += 8; | |
539 | ||
540 | /* MAC Tag */ | |
541 | tag = (struct toradex_tag *)(config_block + offset); | |
542 | tag->id = TAG_MAC; | |
543 | tag->flags = TAG_FLAG_VALID; | |
544 | tag->len = 2; | |
545 | offset += 4; | |
546 | ||
547 | memcpy(config_block + offset, &tdx_eth_addr, 6); | |
548 | offset += 6; | |
549 | memset(config_block + offset, 0, 32 - offset); | |
550 | ||
551 | #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC) | |
552 | err = tdx_cfg_block_mmc_storage(config_block, 1); | |
553 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND) | |
554 | err = write_tdx_cfg_block_to_nand(config_block); | |
555 | #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR) | |
556 | err = write_tdx_cfg_block_to_nor(config_block); | |
557 | #else | |
558 | err = -EINVAL; | |
559 | #endif | |
560 | if (err) { | |
561 | printf("Failed to write Toradex config block: %d\n", ret); | |
562 | ret = CMD_RET_FAILURE; | |
563 | goto out; | |
564 | } | |
565 | ||
566 | printf("Toradex config block successfully written\n"); | |
567 | ||
568 | out: | |
569 | free(config_block); | |
570 | return ret; | |
571 | } | |
572 | ||
573 | static int do_cfgblock(cmd_tbl_t *cmdtp, int flag, int argc, | |
574 | char * const argv[]) | |
575 | { | |
576 | int ret; | |
577 | ||
578 | if (argc < 2) | |
579 | return CMD_RET_USAGE; | |
580 | ||
581 | if (!strcmp(argv[1], "create")) { | |
582 | return do_cfgblock_create(cmdtp, flag, argc, argv); | |
583 | } else if (!strcmp(argv[1], "reload")) { | |
584 | ret = read_tdx_cfg_block(); | |
585 | if (ret) { | |
586 | printf("Failed to reload Toradex config block: %d\n", | |
587 | ret); | |
588 | return CMD_RET_FAILURE; | |
589 | } | |
590 | return CMD_RET_SUCCESS; | |
591 | } | |
592 | ||
593 | return CMD_RET_USAGE; | |
594 | } | |
595 | ||
596 | U_BOOT_CMD( | |
587b13c6 | 597 | cfgblock, 4, 0, do_cfgblock, |
a2777ecb | 598 | "Toradex config block handling commands", |
587b13c6 | 599 | "create [-y] [barcode] - (Re-)create Toradex config block\n" |
a2777ecb MZ |
600 | "cfgblock reload - Reload Toradex config block from flash" |
601 | ); |