2009-06-17 18:28:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/nxflat.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2009-06-17 18:28:50 +02:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2009-12-15 18:06:59 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <nxflat.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
|
|
|
#include <nuttx/binfmt/nxflat.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#ifdef CONFIG_NXFLAT
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-25 19:44:35 +02:00
|
|
|
/* CONFIG_DEBUG, CONFIG_DEBUG_VERBOSE, and CONFIG_DEBUG_BINFMT have to be
|
2009-06-25 23:59:30 +02:00
|
|
|
* defined or CONFIG_NXFLAT_DUMPBUFFER does nothing.
|
2009-06-25 19:44:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DEBUG_VERBOSE) || !defined (CONFIG_DEBUG_BINFMT)
|
2009-06-25 23:59:30 +02:00
|
|
|
# undef CONFIG_NXFLAT_DUMPBUFFER
|
2009-06-25 19:44:35 +02:00
|
|
|
#endif
|
|
|
|
|
2009-06-25 23:59:30 +02:00
|
|
|
#ifdef CONFIG_NXFLAT_DUMPBUFFER
|
2009-06-25 19:44:35 +02:00
|
|
|
# define nxflat_dumpbuffer(m,b,n) bvdbgdumpbuffer(m,b,n)
|
|
|
|
#else
|
|
|
|
# define nxflat_dumpbuffer(m,b,n)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(a,b) (a < b ? a : b)
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nxflat_loadbinary(struct binary_s *binp);
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT)
|
|
|
|
static void nxflat_dumploadinfo(struct nxflat_loadinfo_s *loadinfo);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2009-06-17 20:45:48 +02:00
|
|
|
* Private Data
|
2009-06-17 18:28:50 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct binfmt_s g_nxflatbinfmt =
|
|
|
|
{
|
|
|
|
NULL, /* next */
|
|
|
|
nxflat_loadbinary, /* load */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_dumploadinfo
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_BINFMT)
|
|
|
|
static void nxflat_dumploadinfo(struct nxflat_loadinfo_s *loadinfo)
|
|
|
|
{
|
2009-06-17 20:45:48 +02:00
|
|
|
unsigned long dsize = loadinfo->datasize + loadinfo->bsssize;
|
2009-06-17 18:28:50 +02:00
|
|
|
|
|
|
|
bdbg("LOAD_INFO:\n");
|
|
|
|
bdbg(" ISPACE:\n");
|
|
|
|
bdbg(" ispace: %08lx\n", loadinfo->ispace);
|
2009-06-17 20:45:48 +02:00
|
|
|
bdbg(" entryoffs: %08lx\n", loadinfo->entryoffs);
|
|
|
|
bdbg(" isize: %08lx\n", loadinfo->isize);
|
2009-06-17 18:28:50 +02:00
|
|
|
|
|
|
|
bdbg(" DSPACE:\n");
|
|
|
|
bdbg(" dspace: %08lx\n", loadinfo->dspace);
|
2009-06-25 19:44:35 +02:00
|
|
|
if (loadinfo->dspace != NULL)
|
2009-06-25 16:38:42 +02:00
|
|
|
{
|
|
|
|
bdbg(" crefs: %d\n", loadinfo->dspace->crefs);
|
|
|
|
bdbg(" region: %08lx\n", loadinfo->dspace->region);
|
|
|
|
}
|
2009-06-17 20:45:48 +02:00
|
|
|
bdbg(" datasize: %08lx\n", loadinfo->datasize);
|
|
|
|
bdbg(" bsssize: %08lx\n", loadinfo->bsssize);
|
|
|
|
bdbg(" (pad): %08lx\n", loadinfo->dsize - dsize);
|
|
|
|
bdbg(" stacksize: %08lx\n", loadinfo->stacksize);
|
|
|
|
bdbg(" dsize: %08lx\n", loadinfo->dsize);
|
2009-06-17 18:28:50 +02:00
|
|
|
|
|
|
|
bdbg(" RELOCS:\n");
|
2009-06-17 20:45:48 +02:00
|
|
|
bdbg(" relocstart: %08lx\n", loadinfo->relocstart);
|
2009-06-25 16:38:42 +02:00
|
|
|
bdbg(" reloccount: %d\n", loadinfo->reloccount);
|
2009-06-17 18:28:50 +02:00
|
|
|
|
|
|
|
bdbg(" HANDLES:\n");
|
|
|
|
bdbg(" filfd: %d\n", loadinfo->filfd);
|
|
|
|
}
|
2009-06-21 21:36:07 +02:00
|
|
|
#else
|
2009-06-17 18:28:50 +02:00
|
|
|
# define nxflat_dumploadinfo(i)
|
2009-06-21 21:36:07 +02:00
|
|
|
#endif
|
2009-06-17 18:28:50 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_loadbinary
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verify that the file is an NXFLAT binary and, if so, load the NXFLAT
|
|
|
|
* binary into memory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nxflat_loadbinary(struct binary_s *binp)
|
|
|
|
{
|
|
|
|
struct nxflat_loadinfo_s loadinfo; /* Contains globals for libnxflat */
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
bvdbg("Loading file: %s\n", binp->filename);
|
|
|
|
|
|
|
|
/* Initialize the xflat library to load the program binary. */
|
|
|
|
|
2009-06-25 23:59:30 +02:00
|
|
|
ret = nxflat_init(binp->filename, &loadinfo);
|
2009-06-17 18:28:50 +02:00
|
|
|
nxflat_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
bdbg("Failed to initialize for load of NXFLT program: %d\n", ret);
|
2009-06-26 00:25:30 +02:00
|
|
|
goto errout;
|
2009-06-17 18:28:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the program binary */
|
|
|
|
|
|
|
|
ret = nxflat_load(&loadinfo);
|
|
|
|
nxflat_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
bdbg("Failed to load NXFLT program binary: %d\n", ret);
|
2009-06-26 00:25:30 +02:00
|
|
|
goto errout_with_init;
|
2009-06-17 18:28:50 +02:00
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:39 +02:00
|
|
|
/* Bind the program to the exported symbol table */
|
|
|
|
|
|
|
|
ret = nxflat_bind(&loadinfo, binp->exports, binp->nexports);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
bdbg("Failed to bind symbols program binary: %d\n", ret);
|
2009-06-26 00:25:30 +02:00
|
|
|
goto errout_with_load;
|
2009-06-21 15:41:39 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* Return the load information */
|
|
|
|
|
2009-06-17 20:45:48 +02:00
|
|
|
binp->entrypt = (main_t)(loadinfo.ispace + loadinfo.entryoffs);
|
|
|
|
binp->ispace = (void*)loadinfo.ispace;
|
|
|
|
binp->dspace = (void*)loadinfo.dspace;
|
|
|
|
binp->isize = loadinfo.isize;
|
|
|
|
binp->stacksize = loadinfo.stacksize;
|
2009-06-17 18:28:50 +02:00
|
|
|
|
2009-12-15 18:06:59 +01:00
|
|
|
nxflat_dumpbuffer("Entry code", (FAR const uint8_t*)binp->entrypt,
|
2009-06-25 23:59:30 +02:00
|
|
|
MIN(binp->isize - loadinfo.entryoffs,512));
|
2009-06-26 00:25:30 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
nxflat_uninit(&loadinfo);
|
|
|
|
return OK;
|
2009-06-26 00:25:30 +02:00
|
|
|
|
|
|
|
errout_with_load:
|
|
|
|
nxflat_unload(&loadinfo);
|
|
|
|
errout_with_init:
|
|
|
|
nxflat_uninit(&loadinfo);
|
|
|
|
errout:
|
|
|
|
return ret;
|
2009-06-17 18:28:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Public Functions
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Name: nxflat_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* NXFLAT support is built unconditionally. However, it order to
|
|
|
|
* use this binary format, this function must be called during system
|
|
|
|
* format in order to register the NXFLAT binary format.
|
|
|
|
*
|
2009-06-17 22:25:27 +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.
|
|
|
|
*
|
2009-06-17 18:28:50 +02:00
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
int nxflat_initialize(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Register ourselves as a binfmt loader */
|
|
|
|
|
|
|
|
bvdbg("Registering NXFLAT\n");
|
|
|
|
ret = register_binfmt(&g_nxflatbinfmt);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
bdbg("Failed to register binfmt: %d\n", ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_uninitialize
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unregister the NXFLAT binary loader
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2009-06-17 18:28:50 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void nxflat_uninitialize(void)
|
|
|
|
{
|
|
|
|
unregister_binfmt(&g_nxflatbinfmt);
|
|
|
|
}
|
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#endif /* CONFIG_NXFLAT */
|
|
|
|
|