2015-12-10 16:53:31 +01:00
|
|
|
/****************************************************************************
|
2015-12-12 17:51:54 +01:00
|
|
|
* sched/module/mod_insmod.c
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <elf32.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2015-12-13 00:42:25 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-12-12 14:09:17 +01:00
|
|
|
#include <nuttx/module.h>
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2015-12-12 14:09:17 +01:00
|
|
|
#include "module/module.h"
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MODULE
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
/* CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT have to be defined or
|
|
|
|
* CONFIG_MODULE_DUMPBUFFER does nothing.
|
2015-12-10 16:53:31 +01:00
|
|
|
*/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
|
2015-12-10 16:53:31 +01:00
|
|
|
# undef CONFIG_MODULE_DUMPBUFFER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_MODULE_DUMPBUFFER
|
2016-06-11 19:59:51 +02:00
|
|
|
# define mod_dumpbuffer(m,b,n) sinfodumpbuffer(m,b,n)
|
2015-12-10 16:53:31 +01:00
|
|
|
#else
|
|
|
|
# define mod_dumpbuffer(m,b,n)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(a,b) (a < b ? a : b)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mod_dumploadinfo
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
#if defined(CONFIG_DEBUG_INFO) && defined(CONFIG_DEBUG_BINFMT)
|
2015-12-12 14:09:17 +01:00
|
|
|
static void mod_dumploadinfo(FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("LOAD_INFO:\n");
|
|
|
|
sinfo(" textalloc: %08lx\n", (long)loadinfo->textalloc);
|
|
|
|
sinfo(" datastart: %08lx\n", (long)loadinfo->datastart);
|
|
|
|
sinfo(" textsize: %ld\n", (long)loadinfo->textsize);
|
|
|
|
sinfo(" datasize: %ld\n", (long)loadinfo->datasize);
|
|
|
|
sinfo(" filelen: %ld\n", (long)loadinfo->filelen);
|
|
|
|
sinfo(" filfd: %d\n", loadinfo->filfd);
|
|
|
|
sinfo(" symtabidx: %d\n", loadinfo->symtabidx);
|
|
|
|
sinfo(" strtabidx: %d\n", loadinfo->strtabidx);
|
|
|
|
|
|
|
|
sinfo("ELF Header:\n");
|
|
|
|
sinfo(" e_ident: %02x %02x %02x %02x\n",
|
2015-12-10 16:53:31 +01:00
|
|
|
loadinfo->ehdr.e_ident[0], loadinfo->ehdr.e_ident[1],
|
|
|
|
loadinfo->ehdr.e_ident[2], loadinfo->ehdr.e_ident[3]);
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo(" e_type: %04x\n", loadinfo->ehdr.e_type);
|
|
|
|
sinfo(" e_machine: %04x\n", loadinfo->ehdr.e_machine);
|
|
|
|
sinfo(" e_version: %08x\n", loadinfo->ehdr.e_version);
|
|
|
|
sinfo(" e_entry: %08lx\n", (long)loadinfo->ehdr.e_entry);
|
|
|
|
sinfo(" e_phoff: %d\n", loadinfo->ehdr.e_phoff);
|
|
|
|
sinfo(" e_shoff: %d\n", loadinfo->ehdr.e_shoff);
|
|
|
|
sinfo(" e_flags: %08x\n" , loadinfo->ehdr.e_flags);
|
|
|
|
sinfo(" e_ehsize: %d\n", loadinfo->ehdr.e_ehsize);
|
|
|
|
sinfo(" e_phentsize: %d\n", loadinfo->ehdr.e_phentsize);
|
|
|
|
sinfo(" e_phnum: %d\n", loadinfo->ehdr.e_phnum);
|
|
|
|
sinfo(" e_shentsize: %d\n", loadinfo->ehdr.e_shentsize);
|
|
|
|
sinfo(" e_shnum: %d\n", loadinfo->ehdr.e_shnum);
|
|
|
|
sinfo(" e_shstrndx: %d\n", loadinfo->ehdr.e_shstrndx);
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
if (loadinfo->shdr && loadinfo->ehdr.e_shnum > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
|
|
|
FAR Elf32_Shdr *shdr = &loadinfo->shdr[i];
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Sections %d:\n", i);
|
|
|
|
sinfo(" sh_name: %08x\n", shdr->sh_name);
|
|
|
|
sinfo(" sh_type: %08x\n", shdr->sh_type);
|
|
|
|
sinfo(" sh_flags: %08x\n", shdr->sh_flags);
|
|
|
|
sinfo(" sh_addr: %08x\n", shdr->sh_addr);
|
|
|
|
sinfo(" sh_offset: %d\n", shdr->sh_offset);
|
|
|
|
sinfo(" sh_size: %d\n", shdr->sh_size);
|
|
|
|
sinfo(" sh_link: %d\n", shdr->sh_link);
|
|
|
|
sinfo(" sh_info: %d\n", shdr->sh_info);
|
|
|
|
sinfo(" sh_addralign: %d\n", shdr->sh_addralign);
|
|
|
|
sinfo(" sh_entsize: %d\n", shdr->sh_entsize);
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define mod_dumploadinfo(i)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-12-11 14:27:45 +01:00
|
|
|
* Name: mod_dumpinitializer
|
2015-12-10 16:53:31 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MODULE_DUMPBUFFER
|
2015-12-11 14:27:45 +01:00
|
|
|
static void mod_dumpinitializer(mod_initializer_t initializer,
|
|
|
|
FAR struct mod_loadinfo_s *loadinfo)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2015-12-11 14:27:45 +01:00
|
|
|
mod_dumpbuffer("Initializer code", (FAR const uint8_t *)initializer,
|
2015-12-10 16:53:31 +01:00
|
|
|
MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
|
|
|
|
}
|
|
|
|
#else
|
2015-12-11 14:27:45 +01:00
|
|
|
# define mod_dumpinitializer(b,l)
|
2015-12-10 16:53:31 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-12-11 17:55:21 +01:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: insmod
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-12-11 17:55:21 +01:00
|
|
|
* Verify that the file is an ELF module binary and, if so, load the
|
|
|
|
* module into kernel memory and initialize it for use.
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2015-12-13 15:10:01 +01:00
|
|
|
* NOTE: mod_setsymtab had to have been called in board-specific OS logic
|
|
|
|
* prior to calling this function from application logic (perhaps via
|
|
|
|
* boardctl(BOARDIOC_OS_SYMTAB). Otherwise, insmod will be unable to
|
|
|
|
* resolve symbols in the OS module.
|
|
|
|
*
|
2015-12-13 00:42:25 +01:00
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* filename - Full path to the module binary to be loaded
|
|
|
|
* modulename - The name that can be used to refer to the module after
|
|
|
|
* it has been loaded.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success. On any failure, -1 (ERROR) is returned the
|
|
|
|
* errno value is set appropriately.
|
|
|
|
*
|
2015-12-10 16:53:31 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-12-13 15:10:01 +01:00
|
|
|
int insmod(FAR const char *filename, FAR const char *modulename)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2015-12-12 14:09:17 +01:00
|
|
|
struct mod_loadinfo_s loadinfo;
|
2015-12-13 00:42:25 +01:00
|
|
|
FAR struct module_s *modp;
|
2015-12-12 17:51:54 +01:00
|
|
|
mod_initializer_t initializer;
|
2015-12-11 17:55:21 +01:00
|
|
|
int ret;
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
DEBUGASSERT(filename != NULL && modulename != NULL);
|
2016-06-11 19:59:51 +02:00
|
|
|
sinfo("Loading file: %s\n", filename);
|
2015-12-13 00:42:25 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the module registry */
|
|
|
|
|
|
|
|
mod_registry_lock();
|
|
|
|
|
|
|
|
/* Check if this module is already installed */
|
|
|
|
|
|
|
|
if (mod_registry_find(modulename) != NULL)
|
|
|
|
{
|
|
|
|
mod_registry_unlock();
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
/* Initialize the ELF library to load the program binary. */
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
ret = mod_initialize(filename, &loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
mod_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("ERROR: Failed to initialize to load module: %d\n", ret);
|
2015-12-13 00:42:25 +01:00
|
|
|
goto errout_with_lock;
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
/* Allocate a module registry entry to hold the module data */
|
|
|
|
|
|
|
|
modp = (FAR struct module_s *)kmm_zalloc(sizeof(struct module_s));
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Failed to initialize for load of ELF program: %d\n", ret);
|
2015-12-13 00:42:25 +01:00
|
|
|
goto errout_with_loadinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the module name in the registry entry */
|
|
|
|
|
|
|
|
strncpy(modp->modulename, modulename, MODULENAME_MAX);
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/* Load the program binary */
|
|
|
|
|
2015-12-12 14:09:17 +01:00
|
|
|
ret = mod_load(&loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
mod_dumploadinfo(&loadinfo);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Failed to load ELF program binary: %d\n", ret);
|
2015-12-13 00:42:25 +01:00
|
|
|
goto errout_with_registry_entry;
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 15:10:01 +01:00
|
|
|
/* Bind the program to the kernel symbol table */
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2015-12-13 15:10:01 +01:00
|
|
|
ret = mod_bind(&loadinfo);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Failed to bind symbols program binary: %d\n", ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
goto errout_with_load;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the load information */
|
|
|
|
|
2015-12-11 17:55:21 +01:00
|
|
|
modp->alloc = (FAR void *)loadinfo.textalloc;
|
2015-12-13 01:56:32 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
|
|
|
|
modp->textsize = loadinfo.textsize;
|
|
|
|
modp->datasize = loadinfo.datasize;
|
|
|
|
#endif
|
2015-12-10 16:53:31 +01:00
|
|
|
|
2015-12-11 14:27:45 +01:00
|
|
|
/* Get the module initializer entry point */
|
|
|
|
|
2015-12-12 17:51:54 +01:00
|
|
|
initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
|
2015-12-13 01:56:32 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
|
|
|
|
modp->initializer = initializer;
|
|
|
|
#endif
|
2015-12-12 17:51:54 +01:00
|
|
|
mod_dumpinitializer(initializer, &loadinfo);
|
2015-12-11 14:27:45 +01:00
|
|
|
|
2015-12-12 17:51:54 +01:00
|
|
|
/* Call the module initializer */
|
2015-12-11 14:27:45 +01:00
|
|
|
|
2015-12-12 17:51:54 +01:00
|
|
|
ret = initializer(&modp->uninitializer, &modp->arg);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Failed to initialize the module: %d\n", ret);
|
2015-12-12 17:51:54 +01:00
|
|
|
goto errout_with_load;
|
2015-12-11 14:27:45 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
/* Add the new module entry to the registry */
|
|
|
|
|
|
|
|
mod_registry_add(modp);
|
|
|
|
|
2015-12-12 14:09:17 +01:00
|
|
|
mod_uninitialize(&loadinfo);
|
2015-12-13 00:42:25 +01:00
|
|
|
mod_registry_unlock();
|
2015-12-10 16:53:31 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_load:
|
2015-12-12 14:09:17 +01:00
|
|
|
mod_unload(&loadinfo);
|
2015-12-13 00:42:25 +01:00
|
|
|
errout_with_registry_entry:
|
|
|
|
kmm_free(modp);
|
|
|
|
errout_with_loadinfo:
|
2015-12-12 14:09:17 +01:00
|
|
|
mod_uninitialize(&loadinfo);
|
2015-12-13 00:42:25 +01:00
|
|
|
errout_with_lock:
|
|
|
|
mod_registry_unlock();
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
2015-12-10 16:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_MODULE */
|