1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014 - 2022, Xilinx, Inc.
4  * (C) Copyright 2022 - 2023, Advanced Micro Devices, Inc.
5  *
6  * Michal Simek <michal.simek@amd.com>
7  */
8 
9 #include <common.h>
10 #include <efi.h>
11 #include <efi_loader.h>
12 #include <env.h>
13 #include <image.h>
14 #include <init.h>
15 #include <lmb.h>
16 #include <log.h>
17 #include <asm/global_data.h>
18 #include <asm/sections.h>
19 #include <dm/uclass.h>
20 #include <i2c.h>
21 #include <linux/sizes.h>
22 #include <malloc.h>
23 #include "board.h"
24 #include <dm.h>
25 #include <i2c_eeprom.h>
26 #include <net.h>
27 #include <generated/dt.h>
28 #include <slre.h>
29 #include <soc.h>
30 #include <linux/ctype.h>
31 #include <linux/kernel.h>
32 #include <uuid.h>
33 
34 #include "fru.h"
35 
36 #if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
37 struct efi_fw_image fw_images[] = {
38 #if defined(XILINX_BOOT_IMAGE_GUID)
39 	{
40 		.image_type_id = XILINX_BOOT_IMAGE_GUID,
41 		.fw_name = u"XILINX-BOOT",
42 		.image_index = 1,
43 	},
44 #endif
45 #if defined(XILINX_UBOOT_IMAGE_GUID)
46 	{
47 		.image_type_id = XILINX_UBOOT_IMAGE_GUID,
48 		.fw_name = u"XILINX-UBOOT",
49 		.image_index = 2,
50 	},
51 #endif
52 };
53 
54 struct efi_capsule_update_info update_info = {
55 	.images = fw_images,
56 };
57 
58 u8 num_image_type_guids = ARRAY_SIZE(fw_images);
59 #endif /* EFI_HAVE_CAPSULE_SUPPORT */
60 
61 #define EEPROM_HEADER_MAGIC		0xdaaddeed
62 #define EEPROM_HDR_MANUFACTURER_LEN	16
63 #define EEPROM_HDR_NAME_LEN		16
64 #define EEPROM_HDR_REV_LEN		8
65 #define EEPROM_HDR_SERIAL_LEN		20
66 #define EEPROM_HDR_NO_OF_MAC_ADDR	4
67 #define EEPROM_HDR_ETH_ALEN		ETH_ALEN
68 #define EEPROM_HDR_UUID_LEN		16
69 
70 struct xilinx_board_description {
71 	u32 header;
72 	char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1];
73 	char name[EEPROM_HDR_NAME_LEN + 1];
74 	char revision[EEPROM_HDR_REV_LEN + 1];
75 	char serial[EEPROM_HDR_SERIAL_LEN + 1];
76 	u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1];
77 	char uuid[EEPROM_HDR_UUID_LEN + 1];
78 };
79 
80 static int highest_id = -1;
81 static struct xilinx_board_description *board_info;
82 
83 #define XILINX_I2C_DETECTION_BITS	sizeof(struct fru_common_hdr)
84 
85 /* Variable which stores pointer to array which stores eeprom content */
86 struct xilinx_legacy_format {
87 	char board_sn[18]; /* 0x0 */
88 	char unused0[14]; /* 0x12 */
89 	char eth_mac[ETH_ALEN]; /* 0x20 */
90 	char unused1[170]; /* 0x26 */
91 	char board_name[11]; /* 0xd0 */
92 	char unused2[5]; /* 0xdc */
93 	char board_revision[3]; /* 0xe0 */
94 	char unused3[29]; /* 0xe3 */
95 };
96 
xilinx_eeprom_legacy_cleanup(char * eeprom,int size)97 static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size)
98 {
99 	int i;
100 	unsigned char byte;
101 
102 	for (i = 0; i < size; i++) {
103 		byte = eeprom[i];
104 
105 		/* Remove all non printable chars but ignore MAC address */
106 		if ((i < offsetof(struct xilinx_legacy_format, eth_mac) ||
107 		     i >= offsetof(struct xilinx_legacy_format, unused1)) &&
108 		     (byte < '!' || byte > '~')) {
109 			eeprom[i] = 0;
110 			continue;
111 		}
112 
113 		/* Convert strings to lower case */
114 		if (byte >= 'A' && byte <= 'Z')
115 			eeprom[i] = byte + 'a' - 'A';
116 	}
117 }
118 
xilinx_read_eeprom_legacy(struct udevice * dev,char * name,struct xilinx_board_description * desc)119 static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name,
120 				     struct xilinx_board_description *desc)
121 {
122 	int ret, size;
123 	struct xilinx_legacy_format *eeprom_content;
124 	bool eth_valid = false;
125 
126 	size = sizeof(*eeprom_content);
127 
128 	eeprom_content = calloc(1, size);
129 	if (!eeprom_content)
130 		return -ENOMEM;
131 
132 	debug("%s: I2C EEPROM read pass data at %p\n", __func__,
133 	      eeprom_content);
134 
135 	ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size);
136 	if (ret) {
137 		debug("%s: I2C EEPROM read failed\n", __func__);
138 		free(eeprom_content);
139 		return ret;
140 	}
141 
142 	xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size);
143 
144 	/* Terminating \0 chars are the part of desc fields already */
145 	strlcpy(desc->name, eeprom_content->board_name,
146 		sizeof(eeprom_content->board_name) + 1);
147 	strlcpy(desc->revision, eeprom_content->board_revision,
148 		sizeof(eeprom_content->board_revision) + 1);
149 	strlcpy(desc->serial, eeprom_content->board_sn,
150 		sizeof(eeprom_content->board_sn) + 1);
151 
152 	eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac);
153 	if (eth_valid)
154 		memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN);
155 
156 	printf("Xilinx I2C Legacy format at %s:\n", name);
157 	printf(" Board name:\t%s\n", desc->name);
158 	printf(" Board rev:\t%s\n", desc->revision);
159 	printf(" Board SN:\t%s\n", desc->serial);
160 
161 	if (eth_valid)
162 		printf(" Ethernet mac:\t%pM\n", desc->mac_addr);
163 
164 	desc->header = EEPROM_HEADER_MAGIC;
165 
166 	free(eeprom_content);
167 
168 	return ret;
169 }
170 
xilinx_detect_legacy(u8 * buffer)171 static bool xilinx_detect_legacy(u8 *buffer)
172 {
173 	int i;
174 	char c;
175 
176 	for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) {
177 		c = buffer[i];
178 
179 		if (c < '0' || c > '9')
180 			return false;
181 	}
182 
183 	return true;
184 }
185 
xilinx_read_eeprom_fru(struct udevice * dev,char * name,struct xilinx_board_description * desc)186 static int xilinx_read_eeprom_fru(struct udevice *dev, char *name,
187 				  struct xilinx_board_description *desc)
188 {
189 	int i, ret, eeprom_size;
190 	u8 *fru_content;
191 	u8 id = 0;
192 
193 	/* FIXME this is shortcut - if eeprom type is wrong it will fail */
194 	eeprom_size = i2c_eeprom_size(dev);
195 
196 	fru_content = calloc(1, eeprom_size);
197 	if (!fru_content)
198 		return -ENOMEM;
199 
200 	debug("%s: I2C EEPROM read pass data at %p\n", __func__,
201 	      fru_content);
202 
203 	ret = dm_i2c_read(dev, 0, (uchar *)fru_content,
204 			  eeprom_size);
205 	if (ret) {
206 		debug("%s: I2C EEPROM read failed\n", __func__);
207 		goto end;
208 	}
209 
210 	fru_capture((unsigned long)fru_content);
211 	if (gd->flags & GD_FLG_RELOC || (_DEBUG && IS_ENABLED(CONFIG_DTB_RESELECT))) {
212 		printf("Xilinx I2C FRU format at %s:\n", name);
213 		ret = fru_display(0);
214 		if (ret) {
215 			printf("FRU format decoding failed.\n");
216 			goto end;
217 		}
218 	}
219 
220 	if (desc->header == EEPROM_HEADER_MAGIC) {
221 		debug("Information already filled\n");
222 		ret = -EINVAL;
223 		goto end;
224 	}
225 
226 	/* It is clear that FRU was captured and structures were filled */
227 	strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name,
228 		sizeof(desc->manufacturer));
229 	strlcpy(desc->uuid, (char *)fru_data.brd.uuid,
230 		sizeof(desc->uuid));
231 	strlcpy(desc->name, (char *)fru_data.brd.product_name,
232 		sizeof(desc->name));
233 	for (i = 0; i < sizeof(desc->name); i++) {
234 		if (desc->name[i] == ' ')
235 			desc->name[i] = '\0';
236 	}
237 	strlcpy(desc->revision, (char *)fru_data.brd.rev,
238 		sizeof(desc->revision));
239 	for (i = 0; i < sizeof(desc->revision); i++) {
240 		if (desc->revision[i] == ' ')
241 			desc->revision[i] = '\0';
242 	}
243 	strlcpy(desc->serial, (char *)fru_data.brd.serial_number,
244 		sizeof(desc->serial));
245 
246 	while (id < EEPROM_HDR_NO_OF_MAC_ADDR) {
247 		if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id]))
248 			memcpy(&desc->mac_addr[id],
249 			       (char *)fru_data.mac.macid[id], ETH_ALEN);
250 		id++;
251 	}
252 
253 	desc->header = EEPROM_HEADER_MAGIC;
254 
255 end:
256 	free(fru_content);
257 	return ret;
258 }
259 
xilinx_detect_fru(u8 * buffer)260 static bool xilinx_detect_fru(u8 *buffer)
261 {
262 	u8 checksum = 0;
263 	int i;
264 
265 	checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr));
266 	if (checksum) {
267 		debug("%s Common header CRC FAIL\n", __func__);
268 		return false;
269 	}
270 
271 	bool all_zeros = true;
272 	/* Checksum over all zeros is also zero that's why detect this case */
273 	for (i = 0; i < sizeof(struct fru_common_hdr); i++) {
274 		if (buffer[i] != 0)
275 			all_zeros = false;
276 	}
277 
278 	if (all_zeros)
279 		return false;
280 
281 	debug("%s Common header CRC PASS\n", __func__);
282 	return true;
283 }
284 
xilinx_read_eeprom_single(char * name,struct xilinx_board_description * desc)285 static int xilinx_read_eeprom_single(char *name,
286 				     struct xilinx_board_description *desc)
287 {
288 	int ret;
289 	struct udevice *dev;
290 	ofnode eeprom;
291 	u8 buffer[XILINX_I2C_DETECTION_BITS];
292 
293 	eeprom = ofnode_get_aliases_node(name);
294 	if (!ofnode_valid(eeprom))
295 		return -ENODEV;
296 
297 	ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev);
298 	if (ret)
299 		return ret;
300 
301 	ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer));
302 	if (ret) {
303 		debug("%s: I2C EEPROM read failed\n", __func__);
304 		return ret;
305 	}
306 
307 	debug("%s: i2c memory detected: %s\n", __func__, name);
308 
309 	if (IS_ENABLED(CONFIG_CMD_FRU) && xilinx_detect_fru(buffer))
310 		return xilinx_read_eeprom_fru(dev, name, desc);
311 
312 	if (xilinx_detect_legacy(buffer))
313 		return xilinx_read_eeprom_legacy(dev, name, desc);
314 
315 	return -ENODEV;
316 }
317 
xilinx_read_eeprom(void)318 __maybe_unused int xilinx_read_eeprom(void)
319 {
320 	int id;
321 	char name_buf[8]; /* 8 bytes should be enough for nvmem+number */
322 	struct xilinx_board_description *desc;
323 
324 	highest_id = dev_read_alias_highest_id("nvmem");
325 	/* No nvmem aliases present */
326 	if (highest_id < 0)
327 		return -EINVAL;
328 
329 	board_info = calloc(1, sizeof(*desc) * (highest_id + 1));
330 	if (!board_info)
331 		return -ENOMEM;
332 
333 	debug("%s: Highest ID %d, board_info %p\n", __func__,
334 	      highest_id, board_info);
335 
336 	for (id = 0; id <= highest_id; id++) {
337 		snprintf(name_buf, sizeof(name_buf), "nvmem%d", id);
338 
339 		/* Alloc structure */
340 		desc = &board_info[id];
341 
342 		/* Ignoring return value for supporting multiple chips */
343 		xilinx_read_eeprom_single(name_buf, desc);
344 	}
345 
346 	/*
347 	 * Consider to clean board_info structure when board/cards are not
348 	 * detected.
349 	 */
350 
351 	return 0;
352 }
353 
354 #if defined(CONFIG_OF_BOARD)
board_fdt_blob_setup(int * err)355 void *board_fdt_blob_setup(int *err)
356 {
357 	void *fdt_blob;
358 
359 	*err = 0;
360 	if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
361 	    !IS_ENABLED(CONFIG_VERSAL_NO_DDR) &&
362 	    !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) {
363 		fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR;
364 
365 		if (fdt_magic(fdt_blob) == FDT_MAGIC)
366 			return fdt_blob;
367 
368 		debug("DTB is not passed via %p\n", fdt_blob);
369 	}
370 
371 	if (IS_ENABLED(CONFIG_SPL_BUILD)) {
372 		/*
373 		 * FDT is at end of BSS unless it is in a different memory
374 		 * region
375 		 */
376 		if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
377 			fdt_blob = (ulong *)&_image_binary_end;
378 		else
379 			fdt_blob = (ulong *)&__bss_end;
380 	} else {
381 		/* FDT is at end of image */
382 		fdt_blob = (ulong *)&_end;
383 	}
384 
385 	if (fdt_magic(fdt_blob) == FDT_MAGIC)
386 		return fdt_blob;
387 
388 	debug("DTB is also not passed via %p\n", fdt_blob);
389 
390 	*err = -EINVAL;
391 	return NULL;
392 }
393 #endif
394 
395 #if defined(CONFIG_BOARD_LATE_INIT)
env_set_by_index(const char * name,int index,char * data)396 static int env_set_by_index(const char *name, int index, char *data)
397 {
398 	char var[32];
399 
400 	if (!index)
401 		sprintf(var, "board_%s", name);
402 	else
403 		sprintf(var, "card%d_%s", index, name);
404 
405 	return env_set(var, data);
406 }
407 
board_late_init_xilinx(void)408 int board_late_init_xilinx(void)
409 {
410 	u32 ret = 0;
411 	int i, id, macid = 0;
412 	struct xilinx_board_description *desc;
413 	phys_size_t bootm_size = gd->ram_top - gd->ram_base;
414 
415 	if (!IS_ENABLED(CONFIG_MICROBLAZE)) {
416 		ulong scriptaddr;
417 
418 		scriptaddr = env_get_hex("scriptaddr", 0);
419 		ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr);
420 	}
421 
422 	if (IS_ENABLED(CONFIG_ARCH_ZYNQ) || IS_ENABLED(CONFIG_MICROBLAZE))
423 		bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M));
424 
425 	ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
426 
427 	ret |= env_set_addr("bootm_low", (void *)gd->ram_base);
428 	ret |= env_set_addr("bootm_size", (void *)bootm_size);
429 
430 	for (id = 0; id <= highest_id; id++) {
431 		desc = &board_info[id];
432 		if (desc && desc->header == EEPROM_HEADER_MAGIC) {
433 			if (desc->manufacturer[0])
434 				ret |= env_set_by_index("manufacturer", id,
435 							desc->manufacturer);
436 			if (desc->name[0])
437 				ret |= env_set_by_index("name", id,
438 							desc->name);
439 			if (desc->revision[0])
440 				ret |= env_set_by_index("rev", id,
441 							desc->revision);
442 			if (desc->serial[0])
443 				ret |= env_set_by_index("serial", id,
444 							desc->serial);
445 
446 			if (desc->uuid[0]) {
447 				unsigned char uuid[UUID_STR_LEN + 1];
448 				unsigned char *t = desc->uuid;
449 
450 				memset(uuid, 0, UUID_STR_LEN + 1);
451 
452 				sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
453 					t[0], t[1], t[2], t[3], t[4], t[5],
454 					t[6], t[7], t[8], t[9], t[10], t[11],
455 					t[12], t[13], t[14], t[15]);
456 				ret |= env_set_by_index("uuid", id, uuid);
457 			}
458 
459 			if (!CONFIG_IS_ENABLED(NET))
460 				continue;
461 
462 			for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) {
463 				if (is_valid_ethaddr((const u8 *)desc->mac_addr[i]))
464 					ret |= eth_env_set_enetaddr_by_index("eth",
465 							macid++, desc->mac_addr[i]);
466 			}
467 		}
468 	}
469 
470 	if (ret)
471 		printf("%s: Saving run time variables FAILED\n", __func__);
472 
473 	return 0;
474 }
475 #endif
476 
477 static char *board_name = DEVICE_TREE;
478 
board_fit_config_name_match(const char * name)479 int __maybe_unused board_fit_config_name_match(const char *name)
480 {
481 	debug("%s: Check %s, default %s\n", __func__, name, board_name);
482 
483 #if !defined(CONFIG_SPL_BUILD)
484 	if (IS_ENABLED(CONFIG_REGEX)) {
485 		struct slre slre;
486 		int ret;
487 
488 		ret = slre_compile(&slre, name);
489 		if (ret) {
490 			ret = slre_match(&slre, board_name, strlen(board_name),
491 					 NULL);
492 			debug("%s: name match ret = %d\n", __func__,  ret);
493 			return !ret;
494 		}
495 	}
496 #endif
497 
498 	if (!strcmp(name, board_name))
499 		return 0;
500 
501 	return -1;
502 }
503 
504 #if IS_ENABLED(CONFIG_DTB_RESELECT)
505 #define MAX_NAME_LENGTH	50
506 
board_name_decode(void)507 char * __maybe_unused __weak board_name_decode(void)
508 {
509 	char *board_local_name;
510 	struct xilinx_board_description *desc;
511 	int i, id;
512 
513 	board_local_name = calloc(1, MAX_NAME_LENGTH);
514 	if (!board_info)
515 		return NULL;
516 
517 	for (id = 0; id <= highest_id; id++) {
518 		desc = &board_info[id];
519 
520 		/* No board description */
521 		if (!desc)
522 			goto error;
523 
524 		/* Board is not detected */
525 		if (desc->header != EEPROM_HEADER_MAGIC)
526 			continue;
527 
528 		/* The first string should be soc name */
529 		if (!id)
530 			strcat(board_local_name, CONFIG_SYS_BOARD);
531 
532 		/*
533 		 * For two purpose here:
534 		 * soc_name- eg: zynqmp-
535 		 * and between base board and CC eg: ..revA-sck...
536 		 */
537 		strcat(board_local_name, "-");
538 
539 		if (desc->name[0]) {
540 			/* For DT composition name needs to be lowercase */
541 			for (i = 0; i < sizeof(desc->name); i++)
542 				desc->name[i] = tolower(desc->name[i]);
543 
544 			strcat(board_local_name, desc->name);
545 		}
546 		if (desc->revision[0]) {
547 			strcat(board_local_name, "-rev");
548 
549 			/* And revision needs to be uppercase */
550 			for (i = 0; i < sizeof(desc->revision); i++)
551 				desc->revision[i] = toupper(desc->revision[i]);
552 
553 			strcat(board_local_name, desc->revision);
554 		}
555 	}
556 
557 	/*
558 	 * Longer strings will end up with buffer overflow and potential
559 	 * attacks that's why check it
560 	 */
561 	if (strlen(board_local_name) >= MAX_NAME_LENGTH)
562 		panic("Board name can't be determined\n");
563 
564 	if (strlen(board_local_name))
565 		return board_local_name;
566 
567 error:
568 	free(board_local_name);
569 	return NULL;
570 }
571 
board_detection(void)572 bool __maybe_unused __weak board_detection(void)
573 {
574 	if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) {
575 		int ret;
576 
577 		ret = xilinx_read_eeprom();
578 		return !ret ? true : false;
579 	}
580 
581 	return false;
582 }
583 
soc_detection(void)584 bool __maybe_unused __weak soc_detection(void)
585 {
586 	return false;
587 }
588 
soc_name_decode(void)589 char * __maybe_unused __weak soc_name_decode(void)
590 {
591 	return NULL;
592 }
593 
embedded_dtb_select(void)594 int embedded_dtb_select(void)
595 {
596 	if (soc_detection()) {
597 		char *soc_local_name;
598 
599 		soc_local_name = soc_name_decode();
600 		if (soc_local_name) {
601 			board_name = soc_local_name;
602 			printf("Detected SOC name: %s\n", board_name);
603 
604 			/* Time to change DTB on fly */
605 			/* Both ways should work here */
606 			/* fdtdec_resetup(&rescan); */
607 			return fdtdec_setup();
608 		}
609 	}
610 
611 	if (board_detection()) {
612 		char *board_local_name;
613 
614 		board_local_name = board_name_decode();
615 		if (board_local_name) {
616 			board_name = board_local_name;
617 			printf("Detected name: %s\n", board_name);
618 
619 			/* Time to change DTB on fly */
620 			/* Both ways should work here */
621 			/* fdtdec_resetup(&rescan); */
622 			fdtdec_setup();
623 		}
624 	}
625 	return 0;
626 }
627 #endif
628 
629 #if defined(CONFIG_LMB)
board_get_usable_ram_top(phys_size_t total_size)630 phys_size_t board_get_usable_ram_top(phys_size_t total_size)
631 {
632 	phys_size_t size;
633 	phys_addr_t reg;
634 	struct lmb lmb;
635 
636 	if (!total_size)
637 		return gd->ram_top;
638 
639 	if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
640 		panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
641 
642 	/* found enough not-reserved memory to relocated U-Boot */
643 	lmb_init(&lmb);
644 	lmb_add(&lmb, gd->ram_base, gd->ram_size);
645 	boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
646 	size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
647 	reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
648 
649 	if (!reg)
650 		reg = gd->ram_top - size;
651 
652 	return reg + size;
653 }
654 #endif
655