The Pedigree Project  0.1
modload/main.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "elf.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #define MODULE_FMT "root»/system/modules/%s.o"
26 
27 extern void pedigree_module_load(char *file);
28 extern int pedigree_module_is_loaded(char *name);
29 int load_module(char *name);
30 
31 int load_module(char *name)
32 {
33  // Make the file name
34  char *file = (char *) malloc(
35  strlen(name) + strlen(MODULE_FMT) -
36  1); // -1 because we have -2 (%s) and +1 (\0)
37  sprintf(file, MODULE_FMT, name);
38  // Try to open the file
39  FILE *fp = fopen(file, "r");
40  if (fp == NULL)
41  {
42  printf("Module file %s not found!\n", file);
43  return -1;
44  }
45  fseek(fp, 0, SEEK_END);
46  size_t len = ftell(fp);
47  fseek(fp, 0, SEEK_SET);
48  // Read the contents of the file
49  uint8_t *buffer = (uint8_t *) malloc(len);
50  fread(buffer, len, 1, fp);
51  fclose(fp);
52  // Create the elf structure
53  Elf_t *elf = elf_create(buffer, len);
54  if (!elf)
55  {
56  free(buffer);
57  free(file);
58  return -1;
59  }
60 
61  // Grab the name and dependencies symbols
62  ElfSymbol_t *name_sym = elf_get_symbol(elf, "g_pModuleName"),
63  *deps_sym = elf_get_symbol(elf, "g_pDepends");
64  if (!name_sym)
65  {
66  printf("g_pModuleName symbol not found!\n");
67  return -1;
68  }
69  if (!deps_sym)
70  {
71  printf("g_pDepends symbols not found!\n");
72  return -1;
73  }
74 
75  char *modname = (char *) elf_relptr(elf, name_sym, *SYM_PTR(elf, name_sym));
76  if (pedigree_module_is_loaded(modname))
77  {
78  printf("Module %s is already loaded!\n", modname);
79  return -1;
80  }
81 
82  printf("Module %s being loaded...\n", modname);
83  uint32_t *deps = SYM_PTR(elf, deps_sym);
84  for (size_t i = 0; deps[i]; i++)
85  {
86  char *depname = (char *) elf_relptr(elf, deps_sym, deps[i]);
87  if (pedigree_module_is_loaded(depname))
88  continue;
89  if (!load_module(depname) && pedigree_module_is_loaded(depname))
90  continue;
91  printf("Dependency %s of %s couldn't been loaded!\n", depname, modname);
92  return -1;
93  }
94 
95  // Finally load the module
96  pedigree_module_load(file);
97  if (pedigree_module_is_loaded(modname))
98  return 0;
99  printf("Module %s couldn't been loaded\n", modname);
100  return -1;
101 }
102 
103 int main(int argc, char **argv)
104 {
105  if (argc < 2)
106  {
107  printf("Usage: %s <module name>\n", argv[0]);
108  return 0;
109  }
110  else
111  return load_module(argv[1]);
112 }
Definition: elf.h:90