1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2015-2016 Freescale Semiconductor, Inc.
9 * Contains all the functions to handle parsing and loading of PE firmware
14 #include <dm/device-internal.h>
18 #include <linux/bitops.h>
19 #include <net/pfe_eth/pfe_eth.h>
20 #include <net/pfe_eth/pfe_firmware.h>
21 #include <spi_flash.h>
22 #ifdef CONFIG_CHAIN_OF_TRUST
23 #include <fsl_validate.h>
26 #define PFE_FIRMWARE_FIT_CNF_NAME "config@1"
28 static const void *pfe_fit_addr;
29 #ifdef CONFIG_CHAIN_OF_TRUST
30 static const void *pfe_esbc_hdr_addr;
34 * PFE elf firmware loader.
35 * Loads an elf firmware image into a list of PE's (specified using a bitmask)
37 * @param pe_mask Mask of PE id's to load firmware to
38 * @param pfe_firmware Pointer to the firmware image
40 * @return 0 on success, a negative value on error
42 static int pfe_load_elf(int pe_mask, uint8_t *pfe_firmware)
44 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *)pfe_firmware;
45 Elf32_Half sections = be16_to_cpu(elf_hdr->e_shnum);
46 Elf32_Shdr *shdr = (Elf32_Shdr *)(pfe_firmware +
47 be32_to_cpu(elf_hdr->e_shoff));
51 debug("%s: no of sections: %d\n", __func__, sections);
53 /* Some sanity checks */
54 if (strncmp((char *)&elf_hdr->e_ident[EI_MAG0], ELFMAG, SELFMAG)) {
55 printf("%s: incorrect elf magic number\n", __func__);
59 if (elf_hdr->e_ident[EI_CLASS] != ELFCLASS32) {
60 printf("%s: incorrect elf class(%x)\n", __func__,
61 elf_hdr->e_ident[EI_CLASS]);
65 if (elf_hdr->e_ident[EI_DATA] != ELFDATA2MSB) {
66 printf("%s: incorrect elf data(%x)\n", __func__,
67 elf_hdr->e_ident[EI_DATA]);
71 if (be16_to_cpu(elf_hdr->e_type) != ET_EXEC) {
72 printf("%s: incorrect elf file type(%x)\n", __func__,
73 be16_to_cpu(elf_hdr->e_type));
77 for (section = 0; section < sections; section++, shdr++) {
78 if (!(be32_to_cpu(shdr->sh_flags) & (SHF_WRITE | SHF_ALLOC |
81 for (id = 0; id < MAX_PE; id++)
82 if (pe_mask & BIT(id)) {
83 ret = pe_load_elf_section(id,
96 * Get PFE firmware from FIT image
98 * @param data pointer to PFE firmware
99 * @param size pointer to size of the firmware
100 * @param fw_name pfe firmware name, either class or tmu
102 * @return 0 on success, a negative value on error
104 static int pfe_get_fw(const void **data,
105 size_t *size, char *fw_name)
107 int conf_node_off, fw_node_off;
108 char *conf_node_name = NULL;
112 conf_node_name = PFE_FIRMWARE_FIT_CNF_NAME;
114 conf_node_off = fit_conf_get_node(pfe_fit_addr, conf_node_name);
115 if (conf_node_off < 0) {
116 printf("PFE Firmware: %s: no such config\n", conf_node_name);
120 fw_node_off = fit_conf_get_prop_node(pfe_fit_addr, conf_node_off,
122 if (fw_node_off < 0) {
123 printf("PFE Firmware: No '%s' in config\n",
128 if (!(fit_image_verify(pfe_fit_addr, fw_node_off))) {
129 printf("PFE Firmware: Bad firmware image (bad CRC)\n");
133 if (fit_image_get_data(pfe_fit_addr, fw_node_off, data, size)) {
134 printf("PFE Firmware: Can't get %s subimage data/size",
139 ret = fit_get_desc(pfe_fit_addr, fw_node_off, &desc);
141 printf("PFE Firmware: Can't get description\n");
143 printf("%s\n", desc);
149 * Check PFE FIT image
151 * @return 0 on success, a negative value on error
153 static int pfe_fit_check(void)
157 ret = fdt_check_header(pfe_fit_addr);
159 printf("PFE Firmware: Bad firmware image (not a FIT image)\n");
163 if (!fit_check_format(pfe_fit_addr)) {
164 printf("PFE Firmware: Bad firmware image (bad FIT header)\n");
172 int pfe_spi_flash_init(void)
174 struct spi_flash *pfe_flash;
177 void *addr = malloc(CONFIG_SYS_LS_PFE_FW_LENGTH);
182 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS,
184 CONFIG_ENV_SPI_MAX_HZ,
188 printf("SF: failed to probe spi\n");
190 device_remove(new, DM_REMOVE_NORMAL);
195 pfe_flash = dev_get_uclass_priv(new);
197 printf("SF: probe for pfe failed\n");
199 device_remove(new, DM_REMOVE_NORMAL);
203 ret = spi_flash_read(pfe_flash,
204 CONFIG_SYS_LS_PFE_FW_ADDR,
205 CONFIG_SYS_LS_PFE_FW_LENGTH,
208 printf("SF: read for pfe failed\n");
210 spi_flash_free(pfe_flash);
214 #ifdef CONFIG_CHAIN_OF_TRUST
215 void *hdr_addr = malloc(CONFIG_SYS_LS_PFE_ESBC_LENGTH);
219 spi_flash_free(pfe_flash);
223 ret = spi_flash_read(pfe_flash,
224 CONFIG_SYS_LS_PFE_ESBC_ADDR,
225 CONFIG_SYS_LS_PFE_ESBC_LENGTH,
228 printf("SF: failed to read pfe esbc header\n");
231 spi_flash_free(pfe_flash);
235 pfe_esbc_hdr_addr = hdr_addr;
238 spi_flash_free(pfe_flash);
244 * PFE firmware initialization.
245 * Loads different firmware files from FIT image.
246 * Initializes PE IMEM/DMEM and UTIL-PE DDR
247 * Initializes control path symbol addresses (by looking them up in the elf
249 * Takes PE's out of reset
251 * @return 0 on success, a negative value on error
253 int pfe_firmware_init(void)
255 #define PFE_KEY_HASH NULL
256 char *pfe_firmware_name;
257 const void *raw_image_addr;
258 size_t raw_image_size = 0;
260 #ifdef CONFIG_CHAIN_OF_TRUST
261 uintptr_t pfe_esbc_hdr = 0;
262 uintptr_t pfe_img_addr = 0;
267 ret = pfe_spi_flash_init();
271 ret = pfe_fit_check();
275 #ifdef CONFIG_CHAIN_OF_TRUST
276 pfe_esbc_hdr = (uintptr_t)pfe_esbc_hdr_addr;
277 pfe_img_addr = (uintptr_t)pfe_fit_addr;
278 if (fsl_check_boot_mode_secure() != 0) {
280 * In case of failure in validation, fsl_secboot_validate
281 * would not return back in case of Production environment
282 * with ITS=1. In Development environment (ITS=0 and
283 * SB_EN=1), the function may return back in case of
284 * non-fatal failures.
286 ret = fsl_secboot_validate(pfe_esbc_hdr,
290 printf("PFE firmware(s) validation failed\n");
292 printf("PFE firmware(s) validation Successful\n");
296 for (fw_count = 0; fw_count < 2; fw_count++) {
298 pfe_firmware_name = "class";
299 else if (fw_count == 1)
300 pfe_firmware_name = "tmu";
302 pfe_get_fw(&raw_image_addr, &raw_image_size, pfe_firmware_name);
303 pfe_firmware = malloc(raw_image_size);
306 memcpy((void *)pfe_firmware, (void *)raw_image_addr,
310 ret = pfe_load_elf(CLASS_MASK, pfe_firmware);
311 else if (fw_count == 1)
312 ret = pfe_load_elf(TMU_MASK, pfe_firmware);
315 printf("%s: %s firmware load failed\n", __func__,
319 debug("%s: %s firmware loaded\n", __func__, pfe_firmware_name);
325 gpi_enable(HGPI_BASE_ADDR);
332 * PFE firmware cleanup
335 void pfe_firmware_exit(void)
337 debug("%s\n", __func__);