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