Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp2764067ybf; Mon, 2 Mar 2020 15:13:13 -0800 (PST) X-Google-Smtp-Source: ADFU+vuxr2hi9rCLrFFgdZUmyFvhlBKu64eyiwLpRRSsUFfDk3yteugwkB+N43YxS5MGi4hFXwVT X-Received: by 2002:a05:6830:c9:: with SMTP id x9mr1270256oto.8.1583190793439; Mon, 02 Mar 2020 15:13:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583190793; cv=none; d=google.com; s=arc-20160816; b=Cf05c5dC7Wgp8hzOXyHp+LdNigjVhb/jPnunliMZISE3dCEtazWGmAgBguyVSdky/O VJYdlxa/XbUWNFr+occ21cdQHuUz7HmEMWn8Nz99NSwJDPD4i7RjDYelZUq6a5otoHm3 jtG6g1lhn0SN6ah5vePTPJhfQWYUKLVNkXwRp2fUceASV9DV5oS07ckvvh9iuM2nk0RA 0uGpebBBZwl7/35ELDYN+gj334pVRxhYx0+Q0ZQUde0vDnxDkYwGqzTVzICRXLGzEjjC GsWwjLsH9A4j0lATl7jjdyGMuKlMzi5y1WM12ol/dGHGFKLP+awY5jBlLvnxDCqOVCiL OHYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=cZHkLPyQi4lEOu+PeEyqnnKuw6NNgD2Yqbp/RubP2bc=; b=iI3JNdlL/ebGbW4d5lFJLj66s3Etg+fH/DqgfH0DNOqS7LTn94cRHvIN8w0SsUl13u AXRIBR00dEEiFKWsXEIKGrC3iVXBivX/TO68An2cb/ygXnzWv51zkvo7rqpk8ariw2co nnisxF5VxiIIWgwzJYhsdlMGA9qOJVXjPeLbHlPDOWOpXTA/BrXpHgncJYXikpfk4i6b mvgtvodn4YRFZwjn+r2prz4Um8z0WIn4jkj8Qd+PmkCWzrY1KT1GYeXKPSHF61PtAr29 dKZK27+lVwmX3NjDg8ccUOjTOVeXoanNo4J5gKqAwPGtKBAfvYoha6sJ9T6OZwzkAI6k jMQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ofnLSi+j; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n70si5855103oig.95.2020.03.02.15.12.51; Mon, 02 Mar 2020 15:13:13 -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=@linaro.org header.s=google header.b=ofnLSi+j; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726793AbgCBXMl (ORCPT + 99 others); Mon, 2 Mar 2020 18:12:41 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:37299 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726752AbgCBXMl (ORCPT ); Mon, 2 Mar 2020 18:12:41 -0500 Received: by mail-pf1-f196.google.com with SMTP id p14so458949pfn.4 for ; Mon, 02 Mar 2020 15:12:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=cZHkLPyQi4lEOu+PeEyqnnKuw6NNgD2Yqbp/RubP2bc=; b=ofnLSi+jS3OXUcK8vE0gMbM1QBDxh7b5jS1IxWi3HkGpKKOEcm3WJd9aEk0Us7IUKO 8nmFWDJfiuwlFyqsXCcVKRAwIBQaYQl/evwVVX80Y3CPQhjcxITZ1o5Jk7P5xhZf2YmV JUbFU5wRnrXfDlxWlBhwcEx9xdQfFjkkoY8zYrA9cLB/YTtxDa+Ix3UgLS37laXFK+lF YO5R32/SpDIyenCGvur0FFCxabBbXTYfaf1ITOLkeTmU1Kzvh/HtnQ2WmKgFvoaxK9pj quRJ7rwJz3FmBNE/PilR6rrZsmbZ6ywl2wXJqkGSS2Gt0C8UBc0UKbqYmILkeUOjgzI9 blIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=cZHkLPyQi4lEOu+PeEyqnnKuw6NNgD2Yqbp/RubP2bc=; b=RAUAr4PMcselqcylCFB0cgJcxe4DS8FdnnrQpgnrKnvP8Hry2p3LSLYmB2A9OXs/x7 RsfiiugUCtFq3B64MnLRX+SU6gF/8ahnv9NrJTTBAIUFgcUCNCNqB299AuAaQhjfNv3P VwEcTex5sFC7RwHWqobyuZ+JdIGtW2/NGfHwSXzqmB+PRWDrUTs9BYqeeOCWbeqXVfQ3 88FjtKDn8fNnrmrkozD7PDympkQoNaRFPiVTZY3zl1Mmb6HrM5tJZUdER/7ilZW+z1EB /kgqeEYP/q18uYJynAUjNGzLnjEiki0IUtuIn59Xl6JYArVd70zfSkvPxF/0uE4hfQlC /btQ== X-Gm-Message-State: ANhLgQ1JkU1u2QLuiub4n6h23ATmMCp/eNzJgcGiByjGui+eUsCA9V0e JfPB3xw25kLE172tRniVGsctww== X-Received: by 2002:a63:f403:: with SMTP id g3mr1256700pgi.62.1583190759696; Mon, 02 Mar 2020 15:12:39 -0800 (PST) Received: from yoga (pat_11.qualcomm.com. [192.35.156.11]) by smtp.gmail.com with ESMTPSA id x65sm9095993pfd.34.2020.03.02.15.12.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Mar 2020 15:12:39 -0800 (PST) Date: Mon, 2 Mar 2020 15:12:36 -0800 From: Bjorn Andersson To: Clement Leger Cc: Ohad Ben-Cohen , Jonathan Corbet , Shawn Guo , Sascha Hauer , linux-remoteproc@vger.kernel.org, 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 Subject: Re: [PATCH v5 4/8] remoteproc: Add elf helpers to access elf64 and elf32 fields Message-ID: <20200302231236.GD262924@yoga> References: <20200210162209.23149-1-cleger@kalray.eu> <20200302093902.27849-1-cleger@kalray.eu> <20200302093902.27849-5-cleger@kalray.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200302093902.27849-5-cleger@kalray.eu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon 02 Mar 01:38 PST 2020, Clement Leger wrote: > 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 Reviewed-by: Bjorn Andersson > --- > drivers/remoteproc/remoteproc_elf_helpers.h | 96 +++++++++++++++++++++++++++++ > 1 file changed, 96 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..4b6be7b6bf4d > --- /dev/null > +++ b/drivers/remoteproc/remoteproc_elf_helpers.h > @@ -0,0 +1,96 @@ > +/* 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 >