From 7f29f49f66bac7d1c4740495260c21ae9ee5d211 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Tue, 4 Jan 2022 12:58:43 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15316 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/sb/host/compilers/GCC/sbhost.mk | 1 + os/sb/host/sb.h | 1 + os/sb/host/sbelf.c | 110 +++++++++++++++++++++++++++++ os/sb/host/sbelf.h | 73 +++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 os/sb/host/sbelf.c create mode 100644 os/sb/host/sbelf.h diff --git a/os/sb/host/compilers/GCC/sbhost.mk b/os/sb/host/compilers/GCC/sbhost.mk index dbae38e1d..221b98bde 100644 --- a/os/sb/host/compilers/GCC/sbhost.mk +++ b/os/sb/host/compilers/GCC/sbhost.mk @@ -1,6 +1,7 @@ # List of the ChibiOS ARMv7-M sandbox host files. SBHOSTSRC = $(CHIBIOS)/os/sb/host/sbhost.c \ $(CHIBIOS)/os/sb/host/sbapi.c \ + $(CHIBIOS)/os/sb/host/sbelf.c \ $(CHIBIOS)/os/sb/host/sbposix.c SBHOSTASM = $(CHIBIOS)/os/sb/host/compilers/GCC/sbexc.S diff --git a/os/sb/host/sb.h b/os/sb/host/sb.h index bcc99c1b6..598c6b5e1 100644 --- a/os/sb/host/sb.h +++ b/os/sb/host/sb.h @@ -299,6 +299,7 @@ extern "C" { /*===========================================================================*/ #include "sbsysc.h" +#include "sbelf.h" #include "sbposix.h" #include "sbapi.h" #include "sbhost.h" diff --git a/os/sb/host/sbelf.c b/os/sb/host/sbelf.c new file mode 100644 index 000000000..d8018ae95 --- /dev/null +++ b/os/sb/host/sbelf.c @@ -0,0 +1,110 @@ +/* + ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014, + 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio. + + This file is part of ChibiOS. + + ChibiOS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation version 3 of the License. + + ChibiOS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file sb/host/sbelf.c + * @brief ARM SandBox ELF loader code. + * + * @addtogroup ARM_SANDBOX_ELF + * @{ + */ + +#include "ch.h" +#include "sb.h" + +#if (SB_CFG_ENABLE_VFS == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Module local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/** + * @brief Type of an ELF loader context. + */ +typedef struct elf_load_context { + vfs_file_node_c *fnp; + memory_area_t *map; + uint8_t *next; +} elf_load_context_t; + +/** + * @brief Type of an ELF32 header. + */ +typedef struct { + unsigned char e_ident[16]; + uint16_t e_type; + uint16_t e_machine; + uint32_t e_version; + uint32_t e_entry; + uint32_t e_phoff; + uint32_t e_shoff; + uint32_t e_flags; + uint16_t e_ehsize; + uint16_t e_phentsize; + uint16_t e_phnum; + uint16_t e_shentsize; + uint16_t e_shnum; + uint16_t e_shstrndx; +} elf32_header_t; + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +static void init_elf_context(elf_load_context_t *ctxp, + vfs_file_node_c *fnp, + memory_area_t *map) { + + ctxp->fnp = fnp; + ctxp->map = map; + ctxp->next = map->base; +} + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + +msg_t sbElfLoad(vfs_driver_c *drvp, const char *path, memory_area_t *map) { + vfs_file_node_c *fnp; + msg_t ret; + elf_load_context_t ctx; + + ret = vfsDrvOpenFile(drvp, path, VO_RDONLY, &fnp); + CH_RETURN_ON_ERROR(ret); + + init_elf_context(&ctx, fnp, map); + + return CH_RET_SUCCESS; +} + +#endif /* SB_CFG_ENABLE_VFS == TRUE */ + +/** @} */ diff --git a/os/sb/host/sbelf.h b/os/sb/host/sbelf.h new file mode 100644 index 000000000..79f96ba8a --- /dev/null +++ b/os/sb/host/sbelf.h @@ -0,0 +1,73 @@ +/* + ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014, + 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio. + + This file is part of ChibiOS. + + ChibiOS is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation version 3 of the License. + + ChibiOS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/** + * @file sb/host/sbloader.h + * @brief ARM SandBox ELF loader macros and structures. + * + * @addtogroup sbhost.h + * @{ + */ + +#ifndef SBELF_H +#define SBELF_H + +#if (SB_CFG_ENABLE_VFS == TRUE) || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Module constants. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module pre-compile time settings. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module data structures and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + msg_t sbElfLoad(vfs_driver_c *drvp, const char *path, memory_area_t *map); +#ifdef __cplusplus +} +#endif + +/*===========================================================================*/ +/* Module inline functions. */ +/*===========================================================================*/ + +#endif /* SB_CFG_ENABLE_VFS == TRUE */ + +#endif /* SBELF_H */ + +/** @} */