module.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * module.c
  3. *
  4. * Copyright (C) 2018 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <module.h>
  20. #include <exec/elf.h>
  21. #include <memory.h>
  22. extern dword_t load_module_elf(const void *address, module_t *module);
  23. typedef dword_t (*driver_load_proc_t)(const char *parameters);
  24. char driver_name[] = "kernel";
  25. static DECLARE_LIST(modules);
  26. module_t *get_module_from_address(void *address)
  27. {
  28. uintptr_t addr = (uintptr_t)address;
  29. list_entry_t *i, *j;
  30. for (i = modules.next; i != &modules; i = i->next)
  31. {
  32. module_t *module = CONTAINER_OF(i, module_t, link);
  33. for (j = module->segments.next; j != &module->segments; j = j->next)
  34. {
  35. module_segment_t *segment = CONTAINER_OF(j, module_segment_t, link);
  36. if (addr >= (uintptr_t)segment->address && addr < ((uintptr_t)segment->address + segment->size)) return module;
  37. }
  38. }
  39. return NULL;
  40. }
  41. dword_t module_load_from_physical(module_t *module, const char *parameters)
  42. {
  43. module->name = NULL;
  44. module->load_proc = NULL;
  45. module->unload_proc = NULL;
  46. list_init(&module->segments);
  47. void *address = NULL;
  48. dword_t ret = map_memory((void*)module->physical_address, &address, module->size, MEMORY_BLOCK_ACCESSIBLE);
  49. if (ret != ERR_SUCCESS) return ret;
  50. ret = load_module_elf(address, module);
  51. if (ret != ERR_SUCCESS) goto cleanup;
  52. if (!module->name)
  53. {
  54. // TODO
  55. return ERR_INVALID;
  56. }
  57. ret = module->load_proc(parameters);
  58. if (ret != ERR_SUCCESS)
  59. {
  60. // TODO
  61. return ret;
  62. }
  63. list_append(&modules, &module->link);
  64. cleanup:
  65. unmap_memory(address);
  66. return ret;
  67. }
  68. dword_t module_unload(module_t *module)
  69. {
  70. return ERR_NOSYSCALL; // TODO
  71. }