loadcore_common.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Load runtime image of EFIemu. Functions common 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/file.h>
  20. #include <grub/err.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/efiemu/efiemu.h>
  24. #include <grub/cpu/efiemu.h>
  25. /* Are we in 32 or 64-bit mode?*/
  26. static grub_efiemu_mode_t grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
  27. /* Runtime ELF file */
  28. static grub_ssize_t efiemu_core_size;
  29. static void *efiemu_core = 0;
  30. /* Linked list of segments */
  31. static grub_efiemu_segment_t efiemu_segments = 0;
  32. /* equivalent to sizeof (grub_efi_uintn_t) but taking the mode into account*/
  33. int
  34. grub_efiemu_sizeof_uintn_t (void)
  35. {
  36. if (grub_efiemu_mode == GRUB_EFIEMU32)
  37. return 4;
  38. if (grub_efiemu_mode == GRUB_EFIEMU64)
  39. return 8;
  40. return 0;
  41. }
  42. /* Check the header and set mode */
  43. static grub_err_t
  44. grub_efiemu_check_header (void *ehdr, grub_size_t size,
  45. grub_efiemu_mode_t *mode)
  46. {
  47. /* Check the magic numbers. */
  48. if ((*mode == GRUB_EFIEMU_NOTLOADED || *mode == GRUB_EFIEMU32)
  49. && grub_efiemu_check_header32 (ehdr,size))
  50. {
  51. *mode = GRUB_EFIEMU32;
  52. return GRUB_ERR_NONE;
  53. }
  54. if ((*mode == GRUB_EFIEMU_NOTLOADED || *mode == GRUB_EFIEMU64)
  55. && grub_efiemu_check_header64 (ehdr,size))
  56. {
  57. *mode = GRUB_EFIEMU64;
  58. return GRUB_ERR_NONE;
  59. }
  60. return grub_error (GRUB_ERR_BAD_OS, "invalid ELF magic");
  61. }
  62. /* Unload segments */
  63. static int
  64. grub_efiemu_unload_segs (grub_efiemu_segment_t seg)
  65. {
  66. grub_efiemu_segment_t segn;
  67. for (; seg; seg = segn)
  68. {
  69. segn = seg->next;
  70. grub_efiemu_mm_return_request (seg->handle);
  71. grub_free (seg);
  72. }
  73. return 1;
  74. }
  75. grub_err_t
  76. grub_efiemu_loadcore_unload(void)
  77. {
  78. switch (grub_efiemu_mode)
  79. {
  80. case GRUB_EFIEMU32:
  81. grub_efiemu_loadcore_unload32 ();
  82. break;
  83. case GRUB_EFIEMU64:
  84. grub_efiemu_loadcore_unload64 ();
  85. break;
  86. default:
  87. break;
  88. }
  89. grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
  90. grub_free (efiemu_core);
  91. efiemu_core = 0;
  92. grub_efiemu_unload_segs (efiemu_segments);
  93. efiemu_segments = 0;
  94. grub_efiemu_free_syms ();
  95. return GRUB_ERR_NONE;
  96. }
  97. /* Load runtime file and do some initial preparations */
  98. grub_err_t
  99. grub_efiemu_loadcore_init (grub_file_t file,
  100. const char *filename)
  101. {
  102. grub_err_t err;
  103. efiemu_core_size = grub_file_size (file);
  104. efiemu_core = 0;
  105. efiemu_core = grub_malloc (efiemu_core_size);
  106. if (! efiemu_core)
  107. return grub_errno;
  108. if (grub_file_read (file, efiemu_core, efiemu_core_size)
  109. != (int) efiemu_core_size)
  110. {
  111. grub_free (efiemu_core);
  112. efiemu_core = 0;
  113. return grub_errno;
  114. }
  115. if (grub_efiemu_check_header (efiemu_core, efiemu_core_size,
  116. &grub_efiemu_mode))
  117. {
  118. grub_free (efiemu_core);
  119. efiemu_core = 0;
  120. return GRUB_ERR_BAD_MODULE;
  121. }
  122. switch (grub_efiemu_mode)
  123. {
  124. case GRUB_EFIEMU32:
  125. err = grub_efiemu_loadcore_init32 (efiemu_core, filename,
  126. efiemu_core_size,
  127. &efiemu_segments);
  128. if (err)
  129. {
  130. grub_free (efiemu_core);
  131. efiemu_core = 0;
  132. grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
  133. return err;
  134. }
  135. break;
  136. case GRUB_EFIEMU64:
  137. err = grub_efiemu_loadcore_init64 (efiemu_core, filename,
  138. efiemu_core_size,
  139. &efiemu_segments);
  140. if (err)
  141. {
  142. grub_free (efiemu_core);
  143. efiemu_core = 0;
  144. grub_efiemu_mode = GRUB_EFIEMU_NOTLOADED;
  145. return err;
  146. }
  147. break;
  148. default:
  149. return grub_error (GRUB_ERR_BUG, "unknown EFI runtime");
  150. }
  151. return GRUB_ERR_NONE;
  152. }
  153. grub_err_t
  154. grub_efiemu_loadcore_load (void)
  155. {
  156. grub_err_t err;
  157. switch (grub_efiemu_mode)
  158. {
  159. case GRUB_EFIEMU32:
  160. err = grub_efiemu_loadcore_load32 (efiemu_core, efiemu_core_size,
  161. efiemu_segments);
  162. if (err)
  163. grub_efiemu_loadcore_unload ();
  164. return err;
  165. case GRUB_EFIEMU64:
  166. err = grub_efiemu_loadcore_load64 (efiemu_core, efiemu_core_size,
  167. efiemu_segments);
  168. if (err)
  169. grub_efiemu_loadcore_unload ();
  170. return err;
  171. default:
  172. return grub_error (GRUB_ERR_BUG, "unknown EFI runtime");
  173. }
  174. }