/*
* Copyright (c) 2008 Jiri Svoboda
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/** @addtogroup loader
* @brief Loads and runs programs from VFS.
* @{
*/
/**
* @file
* @brief Loads and runs programs from VFS.
*
* The program loader is a special init binary. Its image is used
* to create a new task upon a @c task_spawn syscall. The syscall
* returns the id of a phone connected to the newly created task.
*
* The caller uses this phone to send the pathname and various other
* information to the loader. This is normally done by the C library
* and completely hidden from applications.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <ipc/ipc.h>
#include <ipc/loader.h>
#include <loader/pcb.h>
#include <errno.h>
#include <async.h>
#include <as.h>
#include <elf.h>
#include <elf_load.h>
#include <arch/pcb.h>
/**
* Bias used for loading the dynamic linker. This will be soon replaced
* by automatic placement.
*/
#define RTLD_BIAS 0x80000
/** Pathname of the file that will be loaded */
static char *pathname = NULL;
/** Receive a call setting pathname of the program to execute.
*
* @param rid
* @param request
*/
static void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
{
ipc_callid_t callid;
size_t len;
char *name_buf;
if (!ipc_data_write_receive(&callid, &len)) {
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
return;
}
if (!name_buf) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_0(rid, ENOMEM);
return;
}
ipc_data_write_finalize(callid, name_buf, len);
ipc_answer_0(rid, EOK);
if (pathname != NULL) {
pathname = NULL;
}
name_buf[len] = '\0';
pathname = name_buf;
}
/** Load and run the previously selected program.
*
* @param rid
* @param request
* @return 0 on success, !0 on error.
*/
static int iloader_run(ipc_callid_t rid, ipc_call_t *request)
{
int rc;
pcb_t *pcb;
elf_info_t prog_info;
elf_info_t interp_info;
// printf("Load program '%s'\n", pathname);
rc = elf_load_file(pathname, 0, &prog_info);
if (rc < 0) {
printf("failed to load program\n");
ipc_answer_0(rid, EINVAL);
return 1;
}
// printf("Create PCB\n");
if (elf_create_pcb(&prog_info) < 0) {
ipc_answer_0(rid, ENOMEM);
return 1;
}
if (prog_info.interp == NULL) {
/* Statically linked program */
// printf("Run statically linked program\n");
// printf("entry point: 0x%llx\n", prog_info.entry);
ipc_answer_0(rid, EOK);
close_console();
elf_run(&prog_info, __pcb_get());
return 0;
}
printf("Load dynamic linker '%s'\n", prog_info.
interp);
rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
if (rc < 0) {
printf("failed to load dynamic linker\n");
ipc_answer_0(rid, EINVAL);
return 1;
}
/*
* Provide dynamic linker with some useful data
*/
pcb = (pcb_t *)PCB_ADDRESS;
pcb->rtld_dynamic = interp_info.dynamic;
pcb->rtld_bias = RTLD_BIAS;
printf("run dynamic linker\n");
printf("entry point: 0x%llx\n", interp_info.
entry);
close_console();
ipc_answer_0(rid, EOK);
elf_run(&interp_info, __pcb_get());
/* Not reached(?) */
return 0;
}
/** Handle loader connection.
*
* Receive and carry out commands (of which the last one should be
* to execute the loaded program).
*/
static void loader_connection(ipc_callid_t iid, ipc_call_t *icall)
{
ipc_callid_t callid;
ipc_call_t call;
int retval;
/* Ignore parameters, the connection is already open */
(void)iid; (void)icall;
while (1) {
callid = async_get_call(&call);
// printf("received call from phone %d, method=%d\n",
// call.in_phone_hash, IPC_GET_METHOD(call));
switch (IPC_GET_METHOD(call)) {
case LOADER_SET_PATHNAME:
iloader_set_pathname(callid, &call);
continue;
case LOADER_RUN:
iloader_run(callid, &call);
continue;
default:
retval = ENOENT;
break;
}
if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
printf("responding EINVAL to method %d\n", IPC_GET_METHOD
(call
));
ipc_answer_0(callid, EINVAL);
}
}
}
/** Program loader main function.
*/
int main(int argc, char *argv[])
{
ipc_callid_t callid;
ipc_call_t call;
ipcarg_t phone_hash;
/* The first call only communicates the incoming phone hash */
callid = ipc_wait_for_call(&call);
if (IPC_GET_METHOD(call) != LOADER_HELLO) {
if (IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP)
ipc_answer_0(callid, EINVAL);
return 1;
}
ipc_answer_0(callid, EOK);
phone_hash = call.in_phone_hash;
/*
* FIXME: up until now no async calls can be used!!!
* (Which means e.g. printf() cannot be used)
*/
async_new_connection(phone_hash, 0, NULL, loader_connection);
async_manager();
/* not reached */
return 0;
}
/** @}
*/