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
15 #include <net/pfe_eth/pfe_eth.h>
16 #include <net/pfe_eth/pfe_firmware.h>
17 #ifdef CONFIG_CHAIN_OF_TRUST
18 #include <fsl_validate.h>
21 #define PFE_FIRMWARE_FIT_CNF_NAME "config@1"
23 static const void *pfe_fit_addr = (void *)CONFIG_SYS_LS_PFE_FW_ADDR;
26 * PFE elf firmware loader.
27 * Loads an elf firmware image into a list of PE's (specified using a bitmask)
29 * @param pe_mask Mask of PE id's to load firmware to
30 * @param pfe_firmware Pointer to the firmware image
32 * @return 0 on success, a negative value on error
34 static int pfe_load_elf(int pe_mask, uint8_t *pfe_firmware)
36 Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *)pfe_firmware;
37 Elf32_Half sections = be16_to_cpu(elf_hdr->e_shnum);
38 Elf32_Shdr *shdr = (Elf32_Shdr *)(pfe_firmware +
39 be32_to_cpu(elf_hdr->e_shoff));
43 debug("%s: no of sections: %d\n", __func__, sections);
45 /* Some sanity checks */
46 if (strncmp((char *)&elf_hdr->e_ident[EI_MAG0], ELFMAG, SELFMAG)) {
47 printf("%s: incorrect elf magic number\n", __func__);
51 if (elf_hdr->e_ident[EI_CLASS] != ELFCLASS32) {
52 printf("%s: incorrect elf class(%x)\n", __func__,
53 elf_hdr->e_ident[EI_CLASS]);
57 if (elf_hdr->e_ident[EI_DATA] != ELFDATA2MSB) {
58 printf("%s: incorrect elf data(%x)\n", __func__,
59 elf_hdr->e_ident[EI_DATA]);
63 if (be16_to_cpu(elf_hdr->e_type) != ET_EXEC) {
64 printf("%s: incorrect elf file type(%x)\n", __func__,
65 be16_to_cpu(elf_hdr->e_type));
69 for (section = 0; section < sections; section++, shdr++) {
70 if (!(be32_to_cpu(shdr->sh_flags) & (SHF_WRITE | SHF_ALLOC |
73 for (id = 0; id < MAX_PE; id++)
74 if (pe_mask & BIT(id)) {
75 ret = pe_load_elf_section(id,
88 * Get PFE firmware from FIT image
90 * @param data pointer to PFE firmware
91 * @param size pointer to size of the firmware
92 * @param fw_name pfe firmware name, either class or tmu
94 * @return 0 on success, a negative value on error
96 static int pfe_get_fw(const void **data,
97 size_t *size, char *fw_name)
99 int conf_node_off, fw_node_off;
100 char *conf_node_name = NULL;
104 conf_node_name = PFE_FIRMWARE_FIT_CNF_NAME;
106 conf_node_off = fit_conf_get_node(pfe_fit_addr, conf_node_name);
107 if (conf_node_off < 0) {
108 printf("PFE Firmware: %s: no such config\n", conf_node_name);
112 fw_node_off = fit_conf_get_prop_node(pfe_fit_addr, conf_node_off,
114 if (fw_node_off < 0) {
115 printf("PFE Firmware: No '%s' in config\n",
120 if (!(fit_image_verify(pfe_fit_addr, fw_node_off))) {
121 printf("PFE Firmware: Bad firmware image (bad CRC)\n");
125 if (fit_image_get_data(pfe_fit_addr, fw_node_off, data, size)) {
126 printf("PFE Firmware: Can't get %s subimage data/size",
131 ret = fit_get_desc(pfe_fit_addr, fw_node_off, &desc);
133 printf("PFE Firmware: Can't get description\n");
135 printf("%s\n", desc);
141 * Check PFE FIT image
143 * @return 0 on success, a negative value on error
145 static int pfe_fit_check(void)
149 ret = fdt_check_header(pfe_fit_addr);
151 printf("PFE Firmware: Bad firmware image (not a FIT image)\n");
155 if (!fit_check_format(pfe_fit_addr)) {
156 printf("PFE Firmware: Bad firmware image (bad FIT header)\n");
165 * PFE firmware initialization.
166 * Loads different firmware files from FIT image.
167 * Initializes PE IMEM/DMEM and UTIL-PE DDR
168 * Initializes control path symbol addresses (by looking them up in the elf
170 * Takes PE's out of reset
172 * @return 0 on success, a negative value on error
174 int pfe_firmware_init(void)
176 #define PFE_KEY_HASH NULL
177 char *pfe_firmware_name;
178 const void *raw_image_addr;
179 size_t raw_image_size = 0;
181 #ifdef CONFIG_CHAIN_OF_TRUST
182 uintptr_t pfe_esbc_hdr = 0;
183 uintptr_t pfe_img_addr = 0;
188 ret = pfe_fit_check();
192 #ifdef CONFIG_CHAIN_OF_TRUST
193 pfe_esbc_hdr = CONFIG_SYS_LS_PFE_ESBC_ADDR;
194 pfe_img_addr = (uintptr_t)pfe_fit_addr;
195 if (fsl_check_boot_mode_secure() != 0) {
197 * In case of failure in validation, fsl_secboot_validate
198 * would not return back in case of Production environment
199 * with ITS=1. In Development environment (ITS=0 and
200 * SB_EN=1), the function may return back in case of
201 * non-fatal failures.
203 ret = fsl_secboot_validate(pfe_esbc_hdr,
207 printf("PFE firmware(s) validation failed\n");
209 printf("PFE firmware(s) validation Successful\n");
213 for (fw_count = 0; fw_count < 2; fw_count++) {
215 pfe_firmware_name = "class";
216 else if (fw_count == 1)
217 pfe_firmware_name = "tmu";
219 pfe_get_fw(&raw_image_addr, &raw_image_size, pfe_firmware_name);
220 pfe_firmware = malloc(raw_image_size);
223 memcpy((void *)pfe_firmware, (void *)raw_image_addr,
227 ret = pfe_load_elf(CLASS_MASK, pfe_firmware);
228 else if (fw_count == 1)
229 ret = pfe_load_elf(TMU_MASK, pfe_firmware);
232 printf("%s: %s firmware load failed\n", __func__,
236 debug("%s: %s firmware loaded\n", __func__, pfe_firmware_name);
242 gpi_enable(HGPI_BASE_ADDR);
249 * PFE firmware cleanup
252 void pfe_firmware_exit(void)
254 debug("%s\n", __func__);