Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3898086ybv; Mon, 10 Feb 2020 08:24:50 -0800 (PST) X-Google-Smtp-Source: APXvYqxQz28VlEu2q/WxY4nw7gTRE/fT9rc+IYarK+VxMmIOc8SILlwBG5KHkADey4+uwyGcdZr9 X-Received: by 2002:aca:d610:: with SMTP id n16mr1339452oig.108.1581351890154; Mon, 10 Feb 2020 08:24:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581351890; cv=none; d=google.com; s=arc-20160816; b=T+oG0WrzwzJcr+oD2CBBT8ap9N0luezIv/IJYC4i/3CbVLDqntK4BYBfhM2/xLnafM jdlUFxnMnSCAlGim2euNLQHUPnE5+/BixiUo3C3kj/z6QuDatCFxKv16D1dVKJNLxGJm /47IRIY6NEBOP4XK2/lDB8VrTfT6n47xczjpQfSHzunkG6KtRtP1lp1WkugDYeXDfqMn /MO+/o5/ztSDaZXTICN28dxg0gfNFa7bvd1XQbV7YHB8ZdHnfgMItZ6VBTtjtWhyYZEa XUN//NaUKD14rGplGg0tz3cdOm0tF0gK/m4bCn4xfauCSf3kPaxD419ulDJp0X2GRI8y Q0kA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=lbGrzt+EeUkdMfPQfhAHlwY5IQfWOsG9uidshk6KgKw=; b=DPj3gE75VQy34chn1I7aRsZcfbR9R3Rx2pJ3XcIV0MiCZQsXNw6wlivb89cwNo3C+q rKVAsRAOM8XCvJcJXB9DaXmj14MHs6nea1RQQaP5fOF21L35w556qj8DU0csR7xbxtmm v0DYjC+TRLU76CzAb4PsNdi/ev6tU5SzW/aHMTsLxLySBDC6XVo+faTxGT6zUeuFlr/F lC20GdtecAk0n9THddhAWY65TnUeIJRZia6a8E/UrXqJ+Ian7lrMLc5xZ/EJgL82B5b1 vMHbZ96Mji0kcOUc+ZkczWqSic5I3eRo6f74gCdIi12KdcF62WyHeZIuOFZj/J7Icu9y +IqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kalray.eu header.s=32AE1B44-9502-11E5-BA35-3734643DEF29 header.b=HVcomNbs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=kalray.eu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t11si394577oih.187.2020.02.10.08.24.38; Mon, 10 Feb 2020 08:24:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kalray.eu header.s=32AE1B44-9502-11E5-BA35-3734643DEF29 header.b=HVcomNbs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=kalray.eu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727961AbgBJQYB (ORCPT + 99 others); Mon, 10 Feb 2020 11:24:01 -0500 Received: from zimbra2.kalray.eu ([92.103.151.219]:58774 "EHLO zimbra2.kalray.eu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727579AbgBJQYA (ORCPT ); Mon, 10 Feb 2020 11:24:00 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra2.kalray.eu (Postfix) with ESMTP id 36E2127E079C; Mon, 10 Feb 2020 17:23:58 +0100 (CET) Received: from zimbra2.kalray.eu ([127.0.0.1]) by localhost (zimbra2.kalray.eu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id tmX7p0hWeVXR; Mon, 10 Feb 2020 17:23:57 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by zimbra2.kalray.eu (Postfix) with ESMTP id C7C0627E081F; Mon, 10 Feb 2020 17:23:57 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.10.3 zimbra2.kalray.eu C7C0627E081F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kalray.eu; s=32AE1B44-9502-11E5-BA35-3734643DEF29; t=1581351837; bh=lbGrzt+EeUkdMfPQfhAHlwY5IQfWOsG9uidshk6KgKw=; h=From:To:Date:Message-Id; b=HVcomNbs+t3vyBltSMKBnSeAfWkcbn809nStrCoRo1zwD7AxO5LP0L0ZuvUAH4ckL GNXCOiAGhaRvmcuCrO7REaBqtHf3b2obA8OYdZAiSEXNw+Uf/DzfUHZ2nTtcgbiCtg K8rlWDge9d16banmYXi9qd1Ds196myNy92nYJ2Y0= X-Virus-Scanned: amavisd-new at zimbra2.kalray.eu Received: from zimbra2.kalray.eu ([127.0.0.1]) by localhost (zimbra2.kalray.eu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 4SYyeIE-rC0J; Mon, 10 Feb 2020 17:23:57 +0100 (CET) Received: from triton.lin.mbt.kalray.eu (unknown [192.168.37.25]) by zimbra2.kalray.eu (Postfix) with ESMTPSA id A6DCE27E079C; Mon, 10 Feb 2020 17:23:57 +0100 (CET) From: Clement Leger To: Ohad Ben-Cohen , Bjorn Andersson , Jonathan Corbet , Shawn Guo , Sascha Hauer , linux-remoteproc@vger.kernel.org Cc: Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Andy Gross , Patrice Chotard , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-arm-msm@vger.kernel.org, Arnaud Pouliquen , Loic PALLARDY , s-anna , Mathieu Poirier , Clement Leger Subject: [PATCH v4 3/5] remoteproc: Add elf helpers to access elf64 and elf32 fields Date: Mon, 10 Feb 2020 17:22:07 +0100 Message-Id: <20200210162209.23149-4-cleger@kalray.eu> X-Mailer: git-send-email 2.15.0.276.g89ea799 In-Reply-To: <20200210162209.23149-1-cleger@kalray.eu> References: <527785289.2852303.1581062223707.JavaMail.zimbra@kalray.eu> <20200210162209.23149-1-cleger@kalray.eu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org elf32 and elf64 mainly differ by their types. In order to avoid copy/pasting the whole loader code, generate static inline functions which will access values according to the elf class. It allows to keep a common loader basis. In order to accommodate both elf types sizes, the maximum size for a elf header member is chosen using the maximum value of the field for both elf class. Signed-off-by: Clement Leger --- drivers/remoteproc/remoteproc_elf_helpers.h | 95 +++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 drivers/remoteproc/remoteproc_elf_helpers.h diff --git a/drivers/remoteproc/remoteproc_elf_helpers.h b/drivers/remoteproc/remoteproc_elf_helpers.h new file mode 100644 index 000000000000..a29c17aaedb7 --- /dev/null +++ b/drivers/remoteproc/remoteproc_elf_helpers.h @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Remote processor elf helpers defines + * + * Copyright (C) 2020 Kalray, Inc. + */ + +#ifndef REMOTEPROC_ELF_LOADER_H +#define REMOTEPROC_ELF_LOADER_H + +#include +#include + +/** + * fw_elf_get_class - Get elf class + * @fw: the ELF firmware image + * + * Note that we use and elf32_hdr to access the class since the start of the + * struct is the same for both elf class + * + * Return: elf class of the firmware + */ +static inline u8 fw_elf_get_class(const struct firmware *fw) +{ + struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data; + + return ehdr->e_ident[EI_CLASS]; +} + +static inline void elf_hdr_init_ident(struct elf32_hdr *hdr, u8 class) +{ + memcpy(hdr->e_ident, ELFMAG, SELFMAG); + hdr->e_ident[EI_CLASS] = class; + hdr->e_ident[EI_DATA] = ELFDATA2LSB; + hdr->e_ident[EI_VERSION] = EV_CURRENT; + hdr->e_ident[EI_OSABI] = ELFOSABI_NONE; +} + +/* Generate getter and setter for a specific elf struct/field */ +#define ELF_GEN_FIELD_GET_SET(__s, __field, __type) \ +static inline __type elf_##__s##_get_##__field(u8 class, const void *arg) \ +{ \ + if (class == ELFCLASS32) \ + return (__type) ((const struct elf32_##__s *) arg)->__field; \ + else \ + return (__type) ((const struct elf64_##__s *) arg)->__field; \ +} \ +static inline void elf_##__s##_set_##__field(u8 class, void *arg, __type value) \ +{ \ + if (class == ELFCLASS32) \ + ((struct elf32_##__s *) arg)->__field = (__type) value; \ + else \ + ((struct elf64_##__s *) arg)->__field = (__type) value; \ +} + +ELF_GEN_FIELD_GET_SET(hdr, e_entry, u64) +ELF_GEN_FIELD_GET_SET(hdr, e_phnum, u16) +ELF_GEN_FIELD_GET_SET(hdr, e_shnum, u16) +ELF_GEN_FIELD_GET_SET(hdr, e_phoff, u64) +ELF_GEN_FIELD_GET_SET(hdr, e_shoff, u64) +ELF_GEN_FIELD_GET_SET(hdr, e_shstrndx, u16) +ELF_GEN_FIELD_GET_SET(hdr, e_machine, u16) +ELF_GEN_FIELD_GET_SET(hdr, e_type, u16) +ELF_GEN_FIELD_GET_SET(hdr, e_version, u32) +ELF_GEN_FIELD_GET_SET(hdr, e_ehsize, u32) +ELF_GEN_FIELD_GET_SET(hdr, e_phentsize, u16) + +ELF_GEN_FIELD_GET_SET(phdr, p_paddr, u64) +ELF_GEN_FIELD_GET_SET(phdr, p_vaddr, u64) +ELF_GEN_FIELD_GET_SET(phdr, p_filesz, u64) +ELF_GEN_FIELD_GET_SET(phdr, p_memsz, u64) +ELF_GEN_FIELD_GET_SET(phdr, p_type, u32) +ELF_GEN_FIELD_GET_SET(phdr, p_offset, u64) +ELF_GEN_FIELD_GET_SET(phdr, p_flags, u32) +ELF_GEN_FIELD_GET_SET(phdr, p_align, u64) + +ELF_GEN_FIELD_GET_SET(shdr, sh_size, u64) +ELF_GEN_FIELD_GET_SET(shdr, sh_offset, u64) +ELF_GEN_FIELD_GET_SET(shdr, sh_name, u32) +ELF_GEN_FIELD_GET_SET(shdr, sh_addr, u64) + +#define ELF_STRUCT_SIZE(__s) \ +static inline unsigned long elf_size_of_##__s(u8 class) \ +{ \ + if (class == ELFCLASS32)\ + return sizeof(struct elf32_##__s); \ + else \ + return sizeof(struct elf64_##__s); \ +} + +ELF_STRUCT_SIZE(shdr) +ELF_STRUCT_SIZE(phdr) +ELF_STRUCT_SIZE(hdr) + +#endif /* REMOTEPROC_ELF_LOADER_H */ -- 2.15.0.276.g89ea799