^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Remote Processor Framework Elf loader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2011 Texas Instruments, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2011 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Ohad Ben-Cohen <ohad@wizery.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Brian Swetland <swetland@google.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Mark Grosen <mgrosen@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Fernando Guzman Lugo <fernando.lugo@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Suman Anna <s-anna@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Robert Tivy <rtivy@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Armando Uribe De Leon <x0095078@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Sjur Brændeland <sjur.brandeland@stericsson.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define pr_fmt(fmt) "%s: " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/remoteproc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "remoteproc_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "remoteproc_elf_helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * rproc_elf_sanity_check() - Sanity Check for ELF32/ELF64 firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @rproc: the remote processor handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @fw: the ELF firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Make sure this fw image is sane (ie a correct ELF32/ELF64 file).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) const char *name = rproc->firmware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct device *dev = &rproc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Elf files are beginning with the same structure. Thus, to simplify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * header parsing, we can use the elf32_hdr one for both elf64 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * elf32.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct elf32_hdr *ehdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) u32 elf_shdr_get_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u64 phoff, shoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) char class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) u16 phnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!fw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dev_err(dev, "failed to load %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (fw->size < sizeof(struct elf32_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dev_err(dev, "Image is too small\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ehdr = (struct elf32_hdr *)fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) dev_err(dev, "Image is corrupted (bad magic)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) class = ehdr->e_ident[EI_CLASS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (class != ELFCLASS32 && class != ELFCLASS64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dev_err(dev, "Unsupported class: %d\n", class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dev_err(dev, "elf64 header is too small\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* We assume the firmware has the same endianness as the host */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) # ifdef __LITTLE_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) # else /* BIG ENDIAN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) dev_err(dev, "Unsupported firmware endianness\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) phoff = elf_hdr_get_e_phoff(class, fw->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) shoff = elf_hdr_get_e_shoff(class, fw->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) phnum = elf_hdr_get_e_phnum(class, fw->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) elf_shdr_get_size = elf_size_of_shdr(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (fw->size < shoff + elf_shdr_get_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) dev_err(dev, "Image is too small\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (phnum == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) dev_err(dev, "No loadable segments\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (phoff > fw->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) dev_err(dev, "Firmware size is too small\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dev_dbg(dev, "Firmware is an elf%d file\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) class == ELFCLASS32 ? 32 : 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) EXPORT_SYMBOL(rproc_elf_sanity_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * rproc_elf_get_boot_addr() - Get rproc's boot address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @rproc: the remote processor handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @fw: the ELF firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * This function returns the entry point address of the ELF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Note that the boot address is not a configurable property of all remote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * processors. Some will always boot at a specific hard-coded address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return elf_hdr_get_e_entry(fw_elf_get_class(fw), fw->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) EXPORT_SYMBOL(rproc_elf_get_boot_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * rproc_elf_load_segments() - load firmware segments to memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @rproc: remote processor which will be booted using these fw segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @fw: the ELF firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * This function loads the firmware segments to memory, where the remote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * processor expects them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Some remote processors will expect their code and data to be placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * in specific device addresses, and can't have them dynamically assigned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * We currently support only those kind of remote processors, and expect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * the program header's paddr member to contain those addresses. We then go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * through the physically contiguous "carveout" memory regions which we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * allocated (and mapped) earlier on behalf of the remote processor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * and "translate" device address to kernel addresses, so we can copy the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * segments where they are expected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Currently we only support remote processors that required carveout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * allocations and got them mapped onto their iommus. Some processors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * might be different: they might not have iommus, and would prefer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * directly allocate memory for every segment/resource. This is not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * supported, though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct device *dev = &rproc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) const void *ehdr, *phdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u16 phnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) const u8 *elf_data = fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u8 class = fw_elf_get_class(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u32 elf_phdr_get_size = elf_size_of_phdr(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ehdr = elf_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) phnum = elf_hdr_get_e_phnum(class, ehdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* go through the available ELF segments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) u64 da = elf_phdr_get_p_paddr(class, phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) u64 memsz = elf_phdr_get_p_memsz(class, phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) u64 filesz = elf_phdr_get_p_filesz(class, phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) u64 offset = elf_phdr_get_p_offset(class, phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) u32 type = elf_phdr_get_p_type(class, phdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) bool is_iomem = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (type != PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) type, da, memsz, filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (filesz > memsz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) filesz, memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (offset + filesz > fw->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) offset + filesz, fw->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!rproc_u64_fit_in_size_t(memsz)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) dev_err(dev, "size (%llx) does not fit in size_t type\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) ret = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* grab the kernel address for this device address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ptr = rproc_da_to_va(rproc, da, memsz, &is_iomem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* put the segment where the remote processor expects it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (filesz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (is_iomem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) memcpy_toio((void __iomem *)ptr, elf_data + offset, filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) memcpy(ptr, elf_data + offset, filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Zero out remaining memory for this segment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * This isn't strictly required since dma_alloc_coherent already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * did this for us. albeit harmless, we may consider removing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (memsz > filesz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (is_iomem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) memset_io((void __iomem *)(ptr + filesz), 0, memsz - filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) memset(ptr + filesz, 0, memsz - filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) EXPORT_SYMBOL(rproc_elf_load_segments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static const void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) find_table(struct device *dev, const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) const void *shdr, *name_table_shdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) const char *name_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct resource_table *table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) const u8 *elf_data = (void *)fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) u8 class = fw_elf_get_class(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) size_t fw_size = fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) const void *ehdr = elf_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) u16 shnum = elf_hdr_get_e_shnum(class, ehdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) u32 elf_shdr_get_size = elf_size_of_shdr(class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) u16 shstrndx = elf_hdr_get_e_shstrndx(class, ehdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* look for the resource table and handle it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* First, get the section header according to the elf class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) shdr = elf_data + elf_hdr_get_e_shoff(class, ehdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* Compute name table section header entry in shdr array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) name_table_shdr = shdr + (shstrndx * elf_shdr_get_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* Finally, compute the name table section address in elf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) name_table = elf_data + elf_shdr_get_sh_offset(class, name_table_shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) for (i = 0; i < shnum; i++, shdr += elf_shdr_get_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) u64 size = elf_shdr_get_sh_size(class, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) u64 offset = elf_shdr_get_sh_offset(class, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) u32 name = elf_shdr_get_sh_name(class, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (strcmp(name_table + name, ".resource_table"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) table = (struct resource_table *)(elf_data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* make sure we have the entire table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (offset + size > fw_size || offset + size < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) dev_err(dev, "resource table truncated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /* make sure table has at least the header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (sizeof(struct resource_table) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) dev_err(dev, "header-less resource table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /* we don't support any version beyond the first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (table->ver != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dev_err(dev, "unsupported fw ver: %d\n", table->ver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* make sure reserved bytes are zeroes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (table->reserved[0] || table->reserved[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dev_err(dev, "non zero reserved bytes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /* make sure the offsets array isn't truncated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (struct_size(table, offset, table->num) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) dev_err(dev, "resource table incomplete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return shdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * rproc_elf_load_rsc_table() - load the resource table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * @rproc: the rproc handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * @fw: the ELF firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * This function finds the resource table inside the remote processor's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * firmware, load it into the @cached_table and update @table_ptr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * Return: 0 on success, negative errno on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) const void *shdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct device *dev = &rproc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct resource_table *table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) const u8 *elf_data = fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) size_t tablesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) u8 class = fw_elf_get_class(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) u64 sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) shdr = find_table(dev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (!shdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) sh_offset = elf_shdr_get_sh_offset(class, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) table = (struct resource_table *)(elf_data + sh_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) tablesz = elf_shdr_get_sh_size(class, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * Create a copy of the resource table. When a virtio device starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * and calls vring_new_virtqueue() the address of the allocated vring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * will be stored in the cached_table. Before the device is started,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * cached_table will be copied into device memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (!rproc->cached_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) rproc->table_ptr = rproc->cached_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) rproc->table_sz = tablesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) EXPORT_SYMBOL(rproc_elf_load_rsc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * rproc_elf_find_loaded_rsc_table() - find the loaded resource table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * @rproc: the rproc handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * @fw: the ELF firmware image
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * This function finds the location of the loaded resource table. Don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * call this function if the table wasn't loaded yet - it's a bug if you do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * Returns the pointer to the resource table if it is found or NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * If the table wasn't loaded yet the result is unspecified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) const struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) const void *shdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) u64 sh_addr, sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) u8 class = fw_elf_get_class(fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct device *dev = &rproc->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) shdr = find_table(&rproc->dev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (!shdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) sh_addr = elf_shdr_get_sh_addr(class, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) sh_size = elf_shdr_get_sh_size(class, shdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (!rproc_u64_fit_in_size_t(sh_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) dev_err(dev, "size (%llx) does not fit in size_t type\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return rproc_da_to_va(rproc, sh_addr, sh_size, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);