loadcore.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* Load runtime image of EFIemu. Functions specific to 32/64-bit mode */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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 General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/err.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/efiemu/efiemu.h>
  23. #include <grub/cpu/efiemu.h>
  24. #include <grub/elf.h>
  25. #include <grub/i18n.h>
  26. /* ELF symbols and their values */
  27. static struct grub_efiemu_elf_sym *grub_efiemu_elfsyms = 0;
  28. static int grub_efiemu_nelfsyms = 0;
  29. /* Return the address of a section whose index is N. */
  30. static grub_err_t
  31. grub_efiemu_get_section_addr (grub_efiemu_segment_t segs, unsigned n,
  32. int *handle, grub_off_t *off)
  33. {
  34. grub_efiemu_segment_t seg;
  35. for (seg = segs; seg; seg = seg->next)
  36. if (seg->section == n)
  37. {
  38. *handle = seg->handle;
  39. *off = seg->off;
  40. return GRUB_ERR_NONE;
  41. }
  42. return grub_error (GRUB_ERR_BAD_OS, "section %d not found", n);
  43. }
  44. grub_err_t
  45. SUFFIX (grub_efiemu_loadcore_unload) (void)
  46. {
  47. grub_free (grub_efiemu_elfsyms);
  48. grub_efiemu_elfsyms = 0;
  49. return GRUB_ERR_NONE;
  50. }
  51. /* Check if EHDR is a valid ELF header. */
  52. int
  53. SUFFIX (grub_efiemu_check_header) (void *ehdr, grub_size_t size)
  54. {
  55. Elf_Ehdr *e = ehdr;
  56. /* Check the header size. */
  57. if (size < sizeof (Elf_Ehdr))
  58. return 0;
  59. /* Check the magic numbers. */
  60. if (!SUFFIX (grub_arch_efiemu_check_header) (ehdr)
  61. || e->e_ident[EI_MAG0] != ELFMAG0
  62. || e->e_ident[EI_MAG1] != ELFMAG1
  63. || e->e_ident[EI_MAG2] != ELFMAG2
  64. || e->e_ident[EI_MAG3] != ELFMAG3
  65. || e->e_ident[EI_VERSION] != EV_CURRENT
  66. || e->e_version != EV_CURRENT)
  67. return 0;
  68. return 1;
  69. }
  70. /* Load all segments from memory specified by E. */
  71. static grub_err_t
  72. grub_efiemu_load_segments (grub_efiemu_segment_t segs, const Elf_Ehdr *e)
  73. {
  74. Elf_Shdr *s;
  75. grub_efiemu_segment_t cur;
  76. grub_dprintf ("efiemu", "loading segments\n");
  77. for (cur=segs; cur; cur = cur->next)
  78. {
  79. s = (Elf_Shdr *)cur->srcptr;
  80. if ((s->sh_flags & SHF_ALLOC) && s->sh_size)
  81. {
  82. void *addr;
  83. addr = (grub_uint8_t *) grub_efiemu_mm_obtain_request (cur->handle)
  84. + cur->off;
  85. switch (s->sh_type)
  86. {
  87. case SHT_PROGBITS:
  88. grub_memcpy (addr, (char *) e + s->sh_offset, s->sh_size);
  89. break;
  90. case SHT_NOBITS:
  91. grub_memset (addr, 0, s->sh_size);
  92. break;
  93. }
  94. }
  95. }
  96. return GRUB_ERR_NONE;
  97. }
  98. /* Get a string at offset OFFSET from strtab */
  99. static char *
  100. grub_efiemu_get_string (unsigned offset, const Elf_Ehdr *e)
  101. {
  102. unsigned i;
  103. Elf_Shdr *s;
  104. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  105. i < e->e_shnum;
  106. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  107. if (s->sh_type == SHT_STRTAB && offset < s->sh_size)
  108. return (char *) e + s->sh_offset + offset;
  109. return 0;
  110. }
  111. /* Request memory for segments and fill segments info */
  112. static grub_err_t
  113. grub_efiemu_init_segments (grub_efiemu_segment_t *segs, const Elf_Ehdr *e)
  114. {
  115. unsigned i;
  116. Elf_Shdr *s;
  117. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  118. i < e->e_shnum;
  119. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  120. {
  121. if (s->sh_flags & SHF_ALLOC)
  122. {
  123. grub_efiemu_segment_t seg;
  124. seg = (grub_efiemu_segment_t) grub_malloc (sizeof (*seg));
  125. if (! seg)
  126. return grub_errno;
  127. if (s->sh_size)
  128. {
  129. seg->handle
  130. = grub_efiemu_request_memalign
  131. (s->sh_addralign, s->sh_size,
  132. s->sh_flags & SHF_EXECINSTR ? GRUB_EFI_RUNTIME_SERVICES_CODE
  133. : GRUB_EFI_RUNTIME_SERVICES_DATA);
  134. if (seg->handle < 0)
  135. {
  136. grub_free (seg);
  137. return grub_errno;
  138. }
  139. seg->off = 0;
  140. }
  141. /*
  142. .text-physical doesn't need to be relocated when switching to
  143. virtual mode
  144. */
  145. if (!grub_strcmp (grub_efiemu_get_string (s->sh_name, e),
  146. ".text-physical"))
  147. seg->ptv_rel_needed = 0;
  148. else
  149. seg->ptv_rel_needed = 1;
  150. seg->size = s->sh_size;
  151. seg->section = i;
  152. seg->next = *segs;
  153. seg->srcptr = s;
  154. *segs = seg;
  155. }
  156. }
  157. return GRUB_ERR_NONE;
  158. }
  159. /* Count symbols and relocators and allocate/request memory for them */
  160. static grub_err_t
  161. grub_efiemu_count_symbols (const Elf_Ehdr *e)
  162. {
  163. unsigned i;
  164. Elf_Shdr *s;
  165. int num = 0;
  166. /* Symbols */
  167. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  168. i < e->e_shnum;
  169. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  170. if (s->sh_type == SHT_SYMTAB)
  171. break;
  172. if (i == e->e_shnum)
  173. return grub_error (GRUB_ERR_BAD_OS, N_("no symbol table"));
  174. grub_efiemu_nelfsyms = (unsigned) s->sh_size / (unsigned) s->sh_entsize;
  175. grub_efiemu_elfsyms = (struct grub_efiemu_elf_sym *)
  176. grub_malloc (sizeof (struct grub_efiemu_elf_sym) * grub_efiemu_nelfsyms);
  177. /* Relocators */
  178. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  179. i < e->e_shnum;
  180. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  181. if (s->sh_type == SHT_REL || s->sh_type == SHT_RELA)
  182. num += ((unsigned) s->sh_size) / ((unsigned) s->sh_entsize);
  183. grub_efiemu_request_symbols (num);
  184. return GRUB_ERR_NONE;
  185. }
  186. /* Fill grub_efiemu_elfsyms with symbol values */
  187. static grub_err_t
  188. grub_efiemu_resolve_symbols (grub_efiemu_segment_t segs, Elf_Ehdr *e)
  189. {
  190. unsigned i;
  191. Elf_Shdr *s;
  192. Elf_Sym *sym;
  193. const char *str;
  194. Elf_Word size, entsize;
  195. grub_dprintf ("efiemu", "resolving symbols\n");
  196. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  197. i < e->e_shnum;
  198. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  199. if (s->sh_type == SHT_SYMTAB)
  200. break;
  201. if (i == e->e_shnum)
  202. return grub_error (GRUB_ERR_BAD_OS, N_("no symbol table"));
  203. sym = (Elf_Sym *) ((char *) e + s->sh_offset);
  204. size = s->sh_size;
  205. entsize = s->sh_entsize;
  206. s = (Elf_Shdr *) ((char *) e + e->e_shoff + e->e_shentsize * s->sh_link);
  207. str = (char *) e + s->sh_offset;
  208. for (i = 0;
  209. i < size / entsize;
  210. i++, sym = (Elf_Sym *) ((char *) sym + entsize))
  211. {
  212. unsigned char type = ELF_ST_TYPE (sym->st_info);
  213. unsigned char bind = ELF_ST_BIND (sym->st_info);
  214. int handle;
  215. grub_off_t off;
  216. grub_err_t err;
  217. const char *name = str + sym->st_name;
  218. grub_efiemu_elfsyms[i].section = sym->st_shndx;
  219. switch (type)
  220. {
  221. case STT_NOTYPE:
  222. /* Resolve a global symbol. */
  223. if (sym->st_name != 0 && sym->st_shndx == 0)
  224. {
  225. err = grub_efiemu_resolve_symbol (name, &handle, &off);
  226. if (err)
  227. return err;
  228. grub_efiemu_elfsyms[i].handle = handle;
  229. grub_efiemu_elfsyms[i].off = off;
  230. }
  231. else
  232. sym->st_value = 0;
  233. break;
  234. case STT_OBJECT:
  235. err = grub_efiemu_get_section_addr (segs, sym->st_shndx,
  236. &handle, &off);
  237. if (err)
  238. return err;
  239. off += sym->st_value;
  240. if (bind != STB_LOCAL)
  241. {
  242. err = grub_efiemu_register_symbol (name, handle, off);
  243. if (err)
  244. return err;
  245. }
  246. grub_efiemu_elfsyms[i].handle = handle;
  247. grub_efiemu_elfsyms[i].off = off;
  248. break;
  249. case STT_FUNC:
  250. err = grub_efiemu_get_section_addr (segs, sym->st_shndx,
  251. &handle, &off);
  252. if (err)
  253. return err;
  254. off += sym->st_value;
  255. if (bind != STB_LOCAL)
  256. {
  257. err = grub_efiemu_register_symbol (name, handle, off);
  258. if (err)
  259. return err;
  260. }
  261. grub_efiemu_elfsyms[i].handle = handle;
  262. grub_efiemu_elfsyms[i].off = off;
  263. break;
  264. case STT_SECTION:
  265. err = grub_efiemu_get_section_addr (segs, sym->st_shndx,
  266. &handle, &off);
  267. if (err)
  268. {
  269. grub_efiemu_elfsyms[i].handle = 0;
  270. grub_efiemu_elfsyms[i].off = 0;
  271. grub_errno = GRUB_ERR_NONE;
  272. break;
  273. }
  274. grub_efiemu_elfsyms[i].handle = handle;
  275. grub_efiemu_elfsyms[i].off = off;
  276. break;
  277. case STT_FILE:
  278. grub_efiemu_elfsyms[i].handle = 0;
  279. grub_efiemu_elfsyms[i].off = 0;
  280. break;
  281. default:
  282. return grub_error (GRUB_ERR_BAD_MODULE,
  283. "unknown symbol type `%d'", (int) type);
  284. }
  285. }
  286. return GRUB_ERR_NONE;
  287. }
  288. /* Load runtime to the memory and request memory for definitive location*/
  289. grub_err_t
  290. SUFFIX (grub_efiemu_loadcore_init) (void *core, const char *filename,
  291. grub_size_t core_size,
  292. grub_efiemu_segment_t *segments)
  293. {
  294. Elf_Ehdr *e = (Elf_Ehdr *) core;
  295. grub_err_t err;
  296. if (e->e_type != ET_REL)
  297. return grub_error (GRUB_ERR_BAD_MODULE, N_("this ELF file is not of the right type"));
  298. /* Make sure that every section is within the core. */
  299. if ((grub_size_t) core_size < e->e_shoff + (grub_uint32_t) e->e_shentsize * e->e_shnum)
  300. return grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  301. filename);
  302. err = grub_efiemu_init_segments (segments, core);
  303. if (err)
  304. return err;
  305. err = grub_efiemu_count_symbols (core);
  306. if (err)
  307. return err;
  308. grub_efiemu_request_symbols (1);
  309. return GRUB_ERR_NONE;
  310. }
  311. /* Load runtime definitively */
  312. grub_err_t
  313. SUFFIX (grub_efiemu_loadcore_load) (void *core,
  314. grub_size_t core_size
  315. __attribute__ ((unused)),
  316. grub_efiemu_segment_t segments)
  317. {
  318. grub_err_t err;
  319. err = grub_efiemu_load_segments (segments, core);
  320. if (err)
  321. return err;
  322. err = grub_efiemu_resolve_symbols (segments, core);
  323. if (err)
  324. return err;
  325. err = SUFFIX (grub_arch_efiemu_relocate_symbols) (segments,
  326. grub_efiemu_elfsyms,
  327. core);
  328. if (err)
  329. return err;
  330. return GRUB_ERR_NONE;
  331. }