2014-05-07 21:47:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/pcode.c
|
|
|
|
*
|
2018-09-15 18:49:41 +02:00
|
|
|
* Copyright (C) 2014-2018 Gregory Nutt. All rights reserved.
|
2014-05-07 21:47:52 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-05-08 19:08:01 +02:00
|
|
|
#include <sys/mount.h>
|
2014-05-07 21:47:52 +02:00
|
|
|
#include <stdint.h>
|
2014-05-08 19:08:01 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2014-05-07 21:47:52 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2014-05-07 21:47:52 +02:00
|
|
|
#include <nuttx/poff.h>
|
2018-09-15 18:49:41 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2016-07-20 22:02:18 +02:00
|
|
|
#include <nuttx/drivers/ramdisk.h>
|
2014-05-07 21:47:52 +02:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
|
|
|
#include <nuttx/binfmt/pcode.h>
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#include "pexec.h"
|
|
|
|
#include "pedefs.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_BINFMT_PCODE
|
2014-05-07 21:47:52 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2018-08-24 14:10:50 +02:00
|
|
|
|
2014-05-08 19:08:01 +02:00
|
|
|
/* Check configuration. This is not all of the configuration settings that
|
|
|
|
* are required -- only the more obvious.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS < 1
|
2014-05-09 00:58:10 +02:00
|
|
|
# error You must provide file descriptors via CONFIG_NFILE_DESCRIPTORS in your configuration file
|
2014-05-08 19:08:01 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_BINFMT_DISABLE
|
2014-05-09 00:58:10 +02:00
|
|
|
# error The binary loader is disabled (CONFIG_BINFMT_DISABLE)!
|
2014-05-08 19:08:01 +02:00
|
|
|
#endif
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
#ifndef CONFIG_SCHED_ONEXIT
|
|
|
|
# error CONFIG_SCHED_ONEXIT is required
|
2014-05-08 19:08:01 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#ifndef CONFIG_BINFMT_PCODE_VARSTACKSIZE
|
|
|
|
# define CONFIG_BINFMT_PCODE_VARSTACKSIZE 1024
|
2014-05-09 16:52:11 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#ifndef CONFIG_BINFMT_PCODE_STRSTACKSIZE
|
|
|
|
# define CONFIG_BINFMT_PCODE_STRSTACKSIZE 128
|
2014-05-09 16:52:11 +02:00
|
|
|
#endif
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#ifdef CONFIG_BINFMT_PCODE_TEST_FS
|
2014-05-08 19:08:01 +02:00
|
|
|
# ifndef CONFIG_FS_ROMFS
|
2014-05-09 00:58:10 +02:00
|
|
|
# error You must select CONFIG_FS_ROMFS in your configuration file
|
2014-05-08 19:08:01 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef CONFIG_DISABLE_MOUNTPOINT
|
2014-05-09 00:58:10 +02:00
|
|
|
# error You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT in your configuration file
|
2014-05-08 19:08:01 +02:00
|
|
|
# endif
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
# ifndef CONFIG_BINFMT_PCODE_TEST_DEVMINOR
|
|
|
|
# define CONFIG_BINFMT_PCODE_TEST_DEVMINOR 0
|
2014-05-08 19:08:01 +02:00
|
|
|
# endif
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
# ifndef CONFIG_BINFMT_PCODE_TEST_DEVPATH
|
|
|
|
# define CONFIG_BINFMT_PCODE_TEST_DEVPATH "/dev/ram0"
|
2014-05-08 19:08:01 +02:00
|
|
|
# endif
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
# ifndef CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT
|
|
|
|
# define CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT "/bin"
|
2014-05-08 19:08:01 +02:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Describe the ROMFS file system */
|
|
|
|
|
|
|
|
#define SECTORSIZE 512
|
|
|
|
#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
|
2014-05-07 21:47:52 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
struct binfmt_handoff_s
|
|
|
|
{
|
|
|
|
sem_t exclsem; /* Supports mutually exclusive access */
|
|
|
|
FAR struct binary_s *binp; /* Binary format being handed off */
|
|
|
|
FAR char *fullpath; /* Full path to the P-Code file */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
static int pcode_run(FAR char *exepath, size_t varsize, size_t strsize);
|
|
|
|
#ifdef CONFIG_BINFMT_PCODE_TEST_FS
|
|
|
|
static int pcode_mount_testfs(void);
|
|
|
|
#endif
|
|
|
|
#if !defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL)
|
|
|
|
static void pcode_onexit(int exitcode, FAR void *arg);
|
|
|
|
#endif
|
|
|
|
#if !defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL)
|
|
|
|
static int pcode_proxy(int argc, char **argv);
|
|
|
|
#endif
|
2014-05-09 00:58:10 +02:00
|
|
|
static int pcode_load(FAR struct binary_s *binp);
|
|
|
|
static int pcode_unload(FAR struct binary_s *binp);
|
2014-05-07 21:47:52 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct binfmt_s g_pcode_binfmt =
|
|
|
|
{
|
2014-05-09 00:58:10 +02:00
|
|
|
NULL, /* next */
|
|
|
|
pcode_load, /* load */
|
|
|
|
pcode_unload, /* unload */
|
2014-05-07 21:47:52 +02:00
|
|
|
};
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
struct binfmt_handoff_s g_pcode_handoff;
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#ifdef CONFIG_BINFMT_PCODE_TEST_FS
|
2014-05-08 19:08:01 +02:00
|
|
|
# include "romfs.h"
|
|
|
|
#endif
|
|
|
|
|
2014-05-07 21:47:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcode_run
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute/interpret a P-Code file. This function does not return until
|
|
|
|
* the P-code program terminates or until a fatal error occurs.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* exepath - The full path to the P-Code binary.
|
|
|
|
* varsize - Size of the P-Code variable stack
|
|
|
|
* strsize - the size of the P-Code string stack.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK if the P-Code program successfully terminated; A negated errno value
|
|
|
|
* is returned on the event of any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcode_run(FAR char *exepath, size_t varsize, size_t strsize)
|
|
|
|
{
|
|
|
|
FAR struct pexec_s *st;
|
|
|
|
int errcode;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Load the POFF file into memory */
|
|
|
|
|
|
|
|
st = pload(exepath, varsize, varsize);
|
|
|
|
if (!st)
|
|
|
|
{
|
|
|
|
berr("ERROR: Could not load %s\n", exepath);
|
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
binfo("Loaded %s\n", exepath);
|
|
|
|
|
|
|
|
/* Execute the P-Code program until a stopping condition occurs */
|
|
|
|
|
2018-08-24 13:51:44 +02:00
|
|
|
for (; ; )
|
2016-06-30 19:25:59 +02:00
|
|
|
{
|
|
|
|
/* Execute the instruction; Check for exceptional conditions */
|
|
|
|
|
|
|
|
errcode = pexec(st);
|
|
|
|
if (errcode != eNOERROR)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errcode != eEXIT)
|
|
|
|
{
|
|
|
|
/* REVISIT: Select a more appropriated return errocode */
|
|
|
|
|
|
|
|
berr("ERROR: Runtime error 0x%02x -- Execution Stopped\n", errcode);
|
|
|
|
ret = -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up resources used by the interpreter */
|
|
|
|
|
|
|
|
binfo("Execution terminated\n");
|
|
|
|
pexec_release(st);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:08:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcode_mount_testfs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If so configured, then mount the P-Code test file system
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#ifdef CONFIG_BINFMT_PCODE_TEST_FS
|
2014-05-08 19:08:01 +02:00
|
|
|
static int pcode_mount_testfs(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create a ROM disk for the ROMFS filesystem */
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
binfo("Registering romdisk at /dev/ram%d\n", CONFIG_BINFMT_PCODE_TEST_DEVMINOR);
|
|
|
|
ret = romdisk_register(CONFIG_BINFMT_PCODE_TEST_DEVMINOR, (FAR uint8_t *)romfs_img,
|
2014-05-08 19:08:01 +02:00
|
|
|
NSECTORS(ROMFS_IMG_LEN), SECTORSIZE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: romdisk_register failed: %d\n", ret);
|
2014-05-08 19:08:01 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mount the test file system */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Mounting ROMFS filesystem at target=%s with source=%s\n",
|
2016-06-30 19:25:59 +02:00
|
|
|
CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT,
|
|
|
|
CONFIG_BINFMT_PCODE_TEST_DEVPATH);
|
2014-05-08 19:08:01 +02:00
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
ret = mount(CONFIG_BINFMT_PCODE_TEST_DEVPATH,
|
|
|
|
CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT,
|
2014-05-08 19:08:01 +02:00
|
|
|
"romfs", MS_RDONLY, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-08-29 01:00:43 +02:00
|
|
|
int errval = get_errno();
|
2014-05-08 19:08:01 +02:00
|
|
|
DEBUGASSERT(errval > 0);
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: mount(%s,%s,romfs) failed: %d\n",
|
2016-06-30 19:25:59 +02:00
|
|
|
CONFIG_BINFMT_PCODE_TEST_DEVPATH,
|
|
|
|
CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT, errval);
|
|
|
|
|
2014-05-08 19:08:01 +02:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the system support the PATH variable? Has the PATH variable
|
|
|
|
* already been set? If YES and NO, then set the PATH variable to
|
|
|
|
* the ROMFS mountpoint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_BINFMT_EXEPATH) && !defined(CONFIG_PATH_INITIAL)
|
2016-06-30 19:25:59 +02:00
|
|
|
(void)setenv("PATH", CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT, 1);
|
2014-05-08 19:08:01 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define pcode_mount_testfs() (OK)
|
|
|
|
#endif
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcode_onexit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the proxy program that runs and starts the P-Code interpreter.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-29 22:47:22 +02:00
|
|
|
#if !defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL)
|
2014-05-09 00:58:10 +02:00
|
|
|
static void pcode_onexit(int exitcode, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct binary_s *binp = (FAR struct binary_s *)arg;
|
|
|
|
DEBUGASSERT(binp);
|
|
|
|
|
|
|
|
/* And unload the module */
|
|
|
|
|
2017-10-02 23:30:55 +02:00
|
|
|
(void)unload_module(binp);
|
2014-05-09 00:58:10 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-05-07 21:47:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcode_proxy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the proxy program that runs and starts the P-Code interpreter.
|
|
|
|
*
|
2014-08-29 22:47:22 +02:00
|
|
|
* REVISIT: There are issues here when CONFIG_BUILD_PROTECTED or
|
|
|
|
* CONFIG_BUILD_KERNEL are selected. Also this implementation is too highly
|
|
|
|
* couple to logic in the apps/ directory.
|
2014-05-09 00:58:10 +02:00
|
|
|
*
|
2014-05-07 21:47:52 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-29 22:47:22 +02:00
|
|
|
#if !defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL)
|
2014-05-07 21:47:52 +02:00
|
|
|
static int pcode_proxy(int argc, char **argv)
|
|
|
|
{
|
2014-05-09 00:58:10 +02:00
|
|
|
FAR struct binary_s *binp;
|
|
|
|
FAR char *fullpath;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the struct binary_s instance from the handoff structure */
|
|
|
|
|
|
|
|
binp = g_pcode_handoff.binp;
|
|
|
|
g_pcode_handoff.binp = NULL;
|
|
|
|
fullpath = g_pcode_handoff.fullpath;
|
|
|
|
g_pcode_handoff.fullpath = NULL;
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_pcode_handoff.exclsem);
|
2014-05-09 00:58:10 +02:00
|
|
|
DEBUGASSERT(binp && fullpath);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Executing %s\n", fullpath);
|
2014-05-09 00:58:10 +02:00
|
|
|
|
|
|
|
/* Set-up the on-exit handler that will unload the module on exit */
|
|
|
|
|
|
|
|
ret = on_exit(pcode_onexit, binp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: on_exit failed: %d\n", get_errno());
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(fullpath);
|
2014-05-09 00:58:10 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the P-code file and execute it */
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
ret = pcode_run(fullpath, CONFIG_BINFMT_PCODE_VARSTACKSIZE,
|
|
|
|
CONFIG_BINFMT_PCODE_STRSTACKSIZE);
|
2014-05-09 16:52:11 +02:00
|
|
|
|
|
|
|
/* We no longer need the fullpath */
|
2014-05-09 00:58:10 +02:00
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(fullpath);
|
2014-05-09 00:58:10 +02:00
|
|
|
|
2014-05-09 16:52:11 +02:00
|
|
|
/* Check the result of the interpretation */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: Execution failed\n");
|
2014-05-09 16:52:11 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-05-07 21:47:52 +02:00
|
|
|
|
2014-05-09 16:52:11 +02:00
|
|
|
return EXIT_SUCCESS;
|
2014-05-07 21:47:52 +02:00
|
|
|
}
|
|
|
|
#else
|
2014-08-29 22:47:22 +02:00
|
|
|
# error Missing logic for the case of CONFIG_BUILD_PROTECTED/KERNEL
|
2014-05-07 21:47:52 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-05-09 00:58:10 +02:00
|
|
|
* Name: pcode_load
|
2014-05-07 21:47:52 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-05-09 00:58:10 +02:00
|
|
|
* Verify that the file is a pcode binary.
|
2014-05-07 21:47:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
static int pcode_load(struct binary_s *binp)
|
2014-05-07 21:47:52 +02:00
|
|
|
{
|
|
|
|
FAR struct poff_fileheader_s hdr;
|
|
|
|
FAR uint8_t *ptr;
|
|
|
|
size_t remaining;
|
|
|
|
ssize_t nread;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Loading file: %s\n", binp->filename);
|
2014-05-07 21:47:52 +02:00
|
|
|
|
|
|
|
/* Open the binary file for reading (only) */
|
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
fd = nx_open(binp->filename, O_RDONLY);
|
2014-05-07 21:47:52 +02:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2018-09-15 18:49:41 +02:00
|
|
|
berr("ERROR: Failed to open binary %s: %d\n", binp->filename, fd);
|
|
|
|
return fd;
|
2014-05-07 21:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the POFF file header */
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
for (remaining = sizeof(struct poff_fileheader_s),
|
|
|
|
ptr = (FAR uint8_t *)&hdr; remaining > 0; )
|
2014-05-07 21:47:52 +02:00
|
|
|
{
|
|
|
|
/* Read the next GULP */
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
nread = nx_read(fd, ptr, remaining);
|
2014-05-07 21:47:52 +02:00
|
|
|
if (nread < 0)
|
|
|
|
{
|
2017-10-11 20:13:41 +02:00
|
|
|
/* If the failure is EINTR, then this is not an error; the
|
|
|
|
* nx_read() was simply interrupted by a signal.
|
2014-05-07 21:47:52 +02:00
|
|
|
*/
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
if (nread != -EINTR)
|
2014-05-07 21:47:52 +02:00
|
|
|
{
|
2017-10-11 20:13:41 +02:00
|
|
|
berr("ERROR: read failed: %d\n", (int)nread);
|
|
|
|
ret = nread;
|
2014-05-07 21:47:52 +02:00
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Interrupted by a signal\n");
|
2014-05-07 21:47:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set up for the next gulp */
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
DEBUGASSERT(nread > 0 && nread <= remaining);
|
2014-05-07 21:47:52 +02:00
|
|
|
remaining -= nread;
|
|
|
|
ptr += nread;
|
2014-05-09 00:58:10 +02:00
|
|
|
}
|
2014-05-07 21:47:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#ifdef CONFIG_BINFMT_PCODE_DUMPBUFFER
|
2014-05-07 21:47:52 +02:00
|
|
|
lib_dumpbuffer("POFF File Header", &hdr, sizeof(poff_fileheader_s));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Verify that the file is a P-Code executable */
|
|
|
|
|
|
|
|
if (memcmp(&hdr.fh_ident, FHI_POFF_MAG, 4) != 0 || hdr.fh_type != FHT_EXEC)
|
|
|
|
{
|
2016-06-16 20:33:32 +02:00
|
|
|
_err("ERROR: File is not a P-code executable: %d\n");
|
2014-05-07 21:47:52 +02:00
|
|
|
ret = -ENOEXEC;
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the load information.
|
2014-08-29 22:47:22 +02:00
|
|
|
* REVISIT: There are issues here when CONFIG_BUILD_PROTECTED or
|
|
|
|
* CONFIG_BUILD_KERNEL are selected.
|
2014-05-07 21:47:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
binp->entrypt = pcode_proxy;
|
2016-06-30 19:25:59 +02:00
|
|
|
binp->stacksize = CONFIG_BINFMT_PCODE_STACKSIZE;
|
|
|
|
binp->priority = CONFIG_BINFMT_PCODE_PRIORITY;
|
2014-05-07 21:47:52 +02:00
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
/* Get exclusive access to the p-code handoff structure */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&g_pcode_handoff.exclsem);
|
|
|
|
DEBUGASSERT(ret == OK || ret == -EINTR);
|
2014-05-09 00:58:10 +02:00
|
|
|
}
|
2017-10-04 23:22:27 +02:00
|
|
|
while (ret == -EINTR);
|
2014-05-09 00:58:10 +02:00
|
|
|
|
|
|
|
/* Save the data that we need to handoff to the child thread */
|
|
|
|
|
|
|
|
DEBUGASSERT(g_pcode_handoff.binp == NULL &&
|
|
|
|
g_pcode_handoff.fullpath == NULL);
|
|
|
|
|
|
|
|
/* Duplicate the full path to the binary */
|
|
|
|
|
|
|
|
g_pcode_handoff.fullpath = strdup(binp->filename);
|
|
|
|
if (!g_pcode_handoff.fullpath)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: Failed to duplicate the full path: %d\n",
|
2014-05-09 00:58:10 +02:00
|
|
|
binp->filename);
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_pcode_handoff.exclsem);
|
2014-05-09 00:58:10 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_pcode_handoff.binp = binp;
|
|
|
|
|
|
|
|
/* Successfully identified (but not really loaded) a p-code binary */
|
2014-05-07 21:47:52 +02:00
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_fd:
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcode_unload
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when the pcode binary is unloaded. This is necessary primarily
|
|
|
|
* to handler error conditions where unload_module is called after
|
|
|
|
* pcode_load without having executed the P-Code module.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcode_unload(struct binary_s *binp)
|
|
|
|
{
|
|
|
|
/* Increment the semaphore count back to one if appropriate */
|
|
|
|
|
|
|
|
if (g_pcode_handoff.binp)
|
|
|
|
{
|
|
|
|
g_pcode_handoff.binp = NULL;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_pcode_handoff.exclsem);
|
2014-05-09 00:58:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2014-05-07 21:47:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcode_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2018-08-24 14:10:50 +02:00
|
|
|
* In order to use the P-code binary format, this function must be called
|
|
|
|
* during system initialization to register the P-Code binary format.
|
2014-05-07 21:47:52 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* This is a NuttX internal function so it follows the convention that
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int pcode_initialize(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-05-09 00:58:10 +02:00
|
|
|
/* Initialize globals */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&g_pcode_handoff.exclsem, 0, 1);
|
2014-05-09 00:58:10 +02:00
|
|
|
|
2014-05-08 19:08:01 +02:00
|
|
|
/* Mount the test file system */
|
|
|
|
|
|
|
|
ret = pcode_mount_testfs();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: Failed to mount test file system: %d\n", ret);
|
2014-05-08 19:08:01 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-07 21:47:52 +02:00
|
|
|
/* Register ourselves as a binfmt loader */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Registering P-Code Loader\n");
|
2014-05-07 21:47:52 +02:00
|
|
|
|
|
|
|
ret = register_binfmt(&g_pcode_binfmt);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to register binfmt: %d\n", ret);
|
2014-05-07 21:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcode_uninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unregister the pcode binary loader
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void pcode_uninitialize(void)
|
|
|
|
{
|
2014-05-08 19:08:01 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Unregister the binary format */
|
|
|
|
|
|
|
|
ret = unregister_binfmt(&g_pcode_binfmt);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-02-01 15:33:04 +01:00
|
|
|
berr("ERROR: unregister_binfmt() failed: %d\n", ret);
|
2014-05-08 19:08:01 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#ifdef CONFIG_BINFMT_PCODE_TEST_FS
|
|
|
|
ret = umount(CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT);
|
2014-05-08 19:08:01 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-08-29 01:00:43 +02:00
|
|
|
int errval = get_errno();
|
2014-05-08 19:08:01 +02:00
|
|
|
DEBUGASSERT(errval > 0);
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
berr("ERROR: umount(%s) failed: %d\n",
|
|
|
|
CONFIG_BINFMT_PCODE_TEST_MOUNTPOINT, errval);
|
2014-05-08 19:08:01 +02:00
|
|
|
UNUSED(errval);
|
|
|
|
}
|
|
|
|
#endif
|
2014-05-09 00:58:10 +02:00
|
|
|
|
|
|
|
/* Uninitialize globals */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&g_pcode_handoff.exclsem);
|
2014-05-07 21:47:52 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 19:25:59 +02:00
|
|
|
#endif /* CONFIG_BINFMT_PCODE */
|