Subversion Repositories HelenOS

Rev

Rev 2929 | Go to most recent revision | Blame | Compare with Previous | 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.  */
  36.  
  37. #include <stdio.h>
  38. #include <unistd.h>
  39. #include <fcntl.h>
  40. #include <sys/types.h>
  41.  
  42. #include "elf.h"
  43.  
  44. unsigned int elf_load(int fd, elf_header_t *header);
  45.  
  46. typedef void (*entry_point_t)(void);
  47.  
  48. int elf_load_file(char *file_name, elf_header_t *header)
  49. {
  50.     int fd;
  51.     int rc;
  52.  
  53.     printf("open and read '%s'...\n", file_name);
  54.  
  55.     fd = open(file_name, 0);
  56.     if (fd < 0) {
  57.         printf("failed opening file\n");
  58.         return -1;
  59.     }
  60.  
  61.     rc = elf_load(fd, header);
  62.     printf("elf_load() -> %d\n", rc);
  63.  
  64.     close(fd);
  65.  
  66.     return rc;
  67. }
  68.  
  69. void elf_run(elf_header_t *header)
  70. {
  71.     entry_point_t entry_point;
  72.  
  73.     entry_point = (entry_point_t)header->e_entry;
  74.     (*entry_point)();
  75.  
  76.     /* not reached */
  77. }
  78.  
  79. int main(int argc, char *argv[])
  80. {
  81.     elf_header_t header;
  82.     char *file_name;
  83.     int rc;
  84.  
  85.     printf("This is loader\n");
  86.     getchar();
  87.  
  88.     file_name = "/tetris";
  89.     rc = elf_load_file(file_name, &header);
  90.     printf("open and read '%s'...\n", file_name);
  91.  
  92.     if (rc < 0) {
  93.         printf("failed to load program\n");
  94.         return 1;
  95.     }
  96.  
  97.     printf("run program\n");
  98.     elf_run(&header);
  99.  
  100.     /* not reached */
  101.     return 0;
  102. }
  103.  
  104. /** @}
  105.  */
  106.