Subversion Repositories HelenOS

Rev

Rev 3004 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jiri Svoboda
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup loader
  30.  * @brief   Loads and runs programs from VFS.
  31.  * @{
  32.  */
  33. /**
  34.  * @file
  35.  * @brief   Loads and runs programs from VFS.
  36.  *
  37.  * The program loader is a special init binary. Its image is used
  38.  * to create a new task upon a @c task_spawn syscall. The syscall
  39.  * returns the id of a phone connected to the newly created task.
  40.  *
  41.  * The caller uses this phone to send the pathname and various other
  42.  * information to the loader. This is normally done by the C library
  43.  * and completely hidden from applications.
  44.  */
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <unistd.h>
  49. #include <fcntl.h>
  50. #include <sys/types.h>
  51. #include <ipc/ipc.h>
  52. #include <errno.h>
  53. #include <as.h>
  54.  
  55. #include <elf.h>
  56. #include <elf_load.h>
  57. #include <pcb.h>
  58.  
  59. /**
  60.  * Bias used for loading the dynamic linker. This will be soon replaced
  61.  * by automatic placement.
  62.  */
  63. #define RTLD_BIAS 0x80000
  64.  
  65. /** Pathname of the file that will be loaded */
  66. static char *pathname = NULL;
  67.  
  68. /** Receive a call setting pathname of the program to execute.
  69.  *
  70.  * @param rid
  71.  * @param request
  72.  */
  73. void iloader_set_pathname(ipc_callid_t rid, ipc_call_t *request)
  74. {
  75. //  ipc_callid_t callid;
  76.     size_t len;
  77.     char *name_buf;
  78.  
  79. /*  printf("iloader_set_pathname\n");
  80.     if (!ipc_data_write_receive(&callid, &len)) {
  81.         ipc_answer_0(callid, EINVAL);
  82.         ipc_answer_0(rid, EINVAL);
  83.         return;
  84.     }
  85. */
  86.     len = IPC_GET_ARG2(*request);
  87.     printf("alloc %d bytes\n", len+1);
  88.  
  89.     name_buf = malloc(len + 1);
  90.     if (!name_buf) {
  91. //      ipc_answer_0(callid, ENOMEM);
  92.         ipc_answer_0(rid, ENOMEM);
  93.         return;
  94.     }
  95.  
  96.     printf("write_finalize\n");
  97.     ipc_data_write_finalize(rid, name_buf, len);
  98. //  ipc_answer_0(rid, EOK);
  99.  
  100.     if (pathname != NULL) {
  101.         free(pathname);
  102.         pathname = NULL;
  103.     }
  104.  
  105.     pathname = name_buf;
  106. }
  107.  
  108. /** Load and run the previously selected program.
  109.  *
  110.  * @param rid
  111.  * @param request
  112.  * @return 0 on success, !0 on error.
  113.  */
  114. int iloader_run(ipc_callid_t rid, ipc_call_t *request)
  115. {
  116.     int rc;
  117.     pcb_t *pcb;
  118.  
  119.     elf_info_t prog_info;
  120.     elf_info_t interp_info;
  121.  
  122.     printf("Load program '%s'\n", pathname);
  123.  
  124.     rc = elf_load_file(pathname, 0, &prog_info);
  125.     if (rc < 0) {
  126.         printf("failed to load program\n");
  127.         return 1;
  128.     }
  129.  
  130.     printf("Create PCB\n");
  131.     if (elf_create_pcb(&prog_info) < 0) return 1;
  132.  
  133.     if (prog_info.interp == NULL) {
  134.         /* Statically linked program */
  135.         printf("Run statically linked program\n");
  136.         elf_run(&prog_info);
  137.         return 0;
  138.     }
  139.  
  140.     printf("Load dynamic linker '%s'\n", prog_info.interp);
  141.     rc = elf_load_file("/rtld.so", RTLD_BIAS, &interp_info);
  142.     if (rc < 0) {
  143.         printf("failed to load dynamic linker\n");
  144.         return 1;
  145.     }
  146.  
  147.     /*
  148.      * Provide dynamic linker with some useful data
  149.      */
  150.     pcb = (pcb_t *)PCB_ADDRESS;
  151.     pcb->rtld_dynamic = interp_info.dynamic;
  152.     pcb->rtld_bias = RTLD_BIAS;
  153.  
  154.     printf("run dynamic linker\n");
  155.     elf_run(&interp_info);
  156.  
  157.     return 0;
  158. }
  159.  
  160. /** Program loader main function.
  161.  *
  162.  * Receive and carry out commands (of which the last one should be
  163.  * to execute the loaded program).
  164.  */
  165. int main(int argc, char *argv[])
  166. {
  167.     ipc_callid_t callid;
  168.     ipc_call_t call;
  169.     int retval;
  170.     int len;
  171.  
  172.     while (1) {
  173.         callid = ipc_wait_for_call(&call);
  174.         printf("received call, method=%d\n", IPC_GET_METHOD(call));
  175.         switch (IPC_GET_METHOD(call)) {
  176.         case IPC_M_DATA_WRITE:
  177.             iloader_set_pathname(callid, &call);
  178.             iloader_run(callid, &call);
  179.             exit(0);
  180.             continue;
  181.         default:
  182.             retval = ENOENT;
  183.             break;
  184.         }
  185.         if ((callid & IPC_CALLID_NOTIFICATION) == 0) {
  186.             printf("responding EINVAL to method %d\n", IPC_GET_METHOD(call));
  187.             ipc_answer_0(callid, EINVAL);
  188.         }
  189.     }
  190.  
  191.     /* not reached */
  192.     return 0;
  193. }
  194.  
  195. /** @}
  196.  */
  197.