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 <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "elf.h"
25
26#define MODULE_FMT "/usr/lib/modules/%s.o"
27
28extern void pedigree_module_load(char* file);
29extern int pedigree_module_is_loaded(char* name);
30int load_module(char* name);
31
32int load_module(char* name) {
33 // Make the file name
34 char* file = (char*)malloc(strlen(name) + strlen(MODULE_FMT) -
35 1); // -1 because we have -2 (%s) and +1 (\0)
36 sprintf(file, MODULE_FMT, name);
37 // Try to open the file
38 FILE* fp = fopen(file, "r");
39 if (fp == NULL) {
40 printf("Module file %s not found!\n", file);
41 return -1;
42 }
43 fseek(fp, 0, SEEK_END);
44 size_t len = ftell(fp);
45 fseek(fp, 0, SEEK_SET);
46 // Read the contents of the file
47 uint8_t* buffer = (uint8_t*)malloc(len);
48 fread(buffer, len, 1, fp);
49 fclose(fp);
50 // Create the elf structure
51 Elf_t* elf = elf_create(buffer, len);
52 if (!elf) {
53 free(buffer);
54 free(file);
55 return -1;
56 }
57
58 // Grab the name and dependencies symbols
59 ElfSymbol_t *name_sym = elf_get_symbol(elf, "g_pModuleName"),
60 *deps_sym = elf_get_symbol(elf, "g_pDepends");
61 if (!name_sym) {
62 printf("g_pModuleName symbol not found!\n");
63 return -1;
64 }
65 if (!deps_sym) {
66 printf("g_pDepends symbols not found!\n");
67 return -1;
68 }
69
70 char* modname = (char*)elf_relptr(elf, name_sym, *SYM_PTR(elf, name_sym));
71 if (pedigree_module_is_loaded(modname)) {
72 printf("Module %s is already loaded!\n", modname);
73 return -1;
74 }
75
76 printf("Module %s being loaded...\n", modname);
77 uint32_t* deps = SYM_PTR(elf, deps_sym);
78 for (size_t i = 0; deps[i]; i++) {
79 char* depname = (char*)elf_relptr(elf, deps_sym, deps[i]);
80 if (pedigree_module_is_loaded(depname))
81 continue;
82 if (!load_module(depname) && pedigree_module_is_loaded(depname))
83 continue;
84 printf("Dependency %s of %s couldn't been loaded!\n", depname, modname);
85 return -1;
86 }
87
88 // Finally load the module
89 pedigree_module_load(file);
90 if (pedigree_module_is_loaded(modname))
91 return 0;
92 printf("Module %s couldn't been loaded\n", modname);
93 return -1;
94}
95
96int main(int argc, char** argv) {
97 if (argc < 2) {
98 printf("Usage: %s <module name>\n", argv[0]);
99 return 0;
100 } else
101 return load_module(argv[1]);
102}
Definition elf.h:86