From patchwork Mon Sep 23 22:30:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Maxiwell S. Garcia" X-Patchwork-Id: 1166224 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46cfDf6mCJz9sNF for ; Tue, 24 Sep 2019 08:31:14 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 46cfDf5WcYzDqPf for ; Tue, 24 Sep 2019 08:31:14 +1000 (AEST) X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=linux.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=maxiwell@linux.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 46cfD40Mq2zDqH4 for ; Tue, 24 Sep 2019 08:30:43 +1000 (AEST) Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x8NMQuRE045594 for ; Mon, 23 Sep 2019 18:30:39 -0400 Received: from ppma05wdc.us.ibm.com (1b.90.2fa9.ip4.static.sl-reverse.com [169.47.144.27]) by mx0a-001b2d01.pphosted.com with ESMTP id 2v76tmg514-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 23 Sep 2019 18:30:39 -0400 Received: from pps.filterd (ppma05wdc.us.ibm.com [127.0.0.1]) by ppma05wdc.us.ibm.com (8.16.0.27/8.16.0.27) with SMTP id x8NMUDY3020273 for ; Mon, 23 Sep 2019 22:30:38 GMT Received: from b03cxnp07029.gho.boulder.ibm.com (b03cxnp07029.gho.boulder.ibm.com [9.17.130.16]) by ppma05wdc.us.ibm.com with ESMTP id 2v5bg6y903-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 23 Sep 2019 22:30:38 +0000 Received: from b03ledav006.gho.boulder.ibm.com (b03ledav006.gho.boulder.ibm.com [9.17.130.237]) by b03cxnp07029.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x8NMUal841615776 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 23 Sep 2019 22:30:36 GMT Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C5E1EC605A; Mon, 23 Sep 2019 22:30:36 +0000 (GMT) Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 89566C6057; Mon, 23 Sep 2019 22:30:35 +0000 (GMT) Received: from maxibm.ibmuc.com (unknown [9.85.133.177]) by b03ledav006.gho.boulder.ibm.com (Postfix) with ESMTP; Mon, 23 Sep 2019 22:30:35 +0000 (GMT) From: "Maxiwell S. Garcia" To: petitboot@lists.ozlabs.org Subject: [PATCH v2 2/4] discover: Add helper functions to read ELF notes Date: Mon, 23 Sep 2019 19:30:22 -0300 Message-Id: <20190923223024.995-3-maxiwell@linux.ibm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190923223024.995-1-maxiwell@linux.ibm.com> References: <20190923223024.995-1-maxiwell@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-09-23_08:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1909230189 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" The libelf has low level functions to access the ELF structures. This commit adds two external higher level functions: elf_open_image(): - Get the ELF structure from a binary; elf_getnote_desc() - Get the ELF note 'descriptor' using both namespace and ELF type. The definitions used in the 'elf.h' was taken from linux source code: - arch/powerpc/include/asm/elfnote.h - arch/powerpc/kernel/note.S Signed-off-by: Maxiwell S. Garcia --- discover/Makefile.am | 4 ++- discover/elf.c | 86 ++++++++++++++++++++++++++++++++++++++++++++ discover/elf.h | 29 +++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 discover/elf.c create mode 100644 discover/elf.h diff --git a/discover/Makefile.am b/discover/Makefile.am index e2d0e93..2339203 100644 --- a/discover/Makefile.am +++ b/discover/Makefile.am @@ -80,7 +80,9 @@ discover_platform_ro_SOURCES = \ discover/ipmi.h \ discover/dt.c \ discover/dt.h \ - discover/hostboot.h + discover/hostboot.h \ + discover/elf.c \ + discover/elf.h if PLATFORM_ARM64 discover_platform_ro_SOURCES += discover/platform-arm64.c diff --git a/discover/elf.c b/discover/elf.c new file mode 100644 index 0000000..8c2711e --- /dev/null +++ b/discover/elf.c @@ -0,0 +1,86 @@ +#include +#include +#include + +#include +#include "elf.h" + +Elf *elf_open_image(const char *image) +{ + int fd; + Elf *elf = NULL; + int err; + + if (!image) { + pb_log_fn("kernel image path is null\n"); + return NULL; + } + + if ((elf_version(EV_CURRENT) == EV_NONE) || + ((fd = open(image, O_RDONLY, 0)) == -1) || + (!(elf = elf_begin(fd, ELF_C_READ, NULL)))) { + err = elf_errno(); + if (err) + pb_log_fn("failed to read %s elf: %s\n", + image, elf_errmsg(err)); + } + + return elf; +} + +static bool elf_getnote_offset(Elf_Data * const edata, + const char *namespace, + const uint32_t type, GElf_Nhdr *nhdr, + size_t *n_off, size_t *d_off) +{ + size_t off = 0; + size_t next; + + /* Iterate through notes */ + while ((next = gelf_getnote(edata, off, nhdr, n_off, d_off)) > 0) { + char *note_ns = (char *) edata->d_buf + (*n_off); + if ((strcmp(note_ns, namespace) == 0) && (nhdr->n_type == type)) + return true; + + off = next; + } + return false; +} + +void *elf_getnote_desc(Elf *elf, + const char *namespace, + uint32_t type) +{ + Elf_Scn *scn = NULL; + Elf_Data *edata = NULL; + GElf_Shdr shdr; + GElf_Nhdr nhdr; + + size_t n_off; + size_t d_off; + void *desc = NULL; + + if (!elf || !namespace) + return NULL; + + /* Iterate through sections */ + while ((scn = elf_nextscn(elf, scn))) { + gelf_getshdr(scn, &shdr); + + /* ELF might have more than one SHT_NOTE section but + only one has the 'namespace' note */ + if (shdr.sh_type == SHT_NOTE) { + edata = elf_getdata(scn, NULL); + if (elf_getnote_offset(edata, namespace, type, + &nhdr, &n_off, &d_off)) { + desc = calloc(nhdr.n_descsz, sizeof(char)); + memcpy(desc, edata->d_buf + d_off, + nhdr.n_descsz); + break; + } + } + } + + return desc; +} + diff --git a/discover/elf.h b/discover/elf.h new file mode 100644 index 0000000..742b791 --- /dev/null +++ b/discover/elf.h @@ -0,0 +1,29 @@ +#ifndef _PB_ELF_H +#define _PB_ELF_H + +#include +#include + +/* + * The PowerPC namespace in an ELF Note of the kernel binary is used to store + * capabilities and information which can be used by a bootloader or userland + * + * docs: Documentation/powerpc/elfnote.rst + */ +#define POWERPC_ELFNOTE_NAMESPACE "PowerPC" + +/* + * The capabilities supported/required by the kernel + * This type uses a bitmap as "desc" field. + */ +#define PPC_ELFNOTE_CAPABILITIES 0x1 + +/* bitmap fields: */ +#define PPCCAP_ULTRAVISOR_BIT 0x1 + +Elf *elf_open_image(const char *image); +void *elf_getnote_desc(Elf *elf, + const char *namespace, + uint32_t type); + +#endif /* _PB_ELF_H */