relocator.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/mm.h>
  19. #include <grub/misc.h>
  20. #include <grub/types.h>
  21. #include <grub/err.h>
  22. #include <grub/term.h>
  23. #include <grub/i386/relocator.h>
  24. #include <grub/relocator_private.h>
  25. #include <grub/i386/relocator_private.h>
  26. #include <grub/i386/pc/int.h>
  27. extern grub_uint8_t grub_relocator16_start;
  28. extern grub_uint8_t grub_relocator16_end;
  29. extern grub_uint16_t grub_relocator16_cs;
  30. extern grub_uint16_t grub_relocator16_ip;
  31. extern grub_uint16_t grub_relocator16_ds;
  32. extern grub_uint16_t grub_relocator16_es;
  33. extern grub_uint16_t grub_relocator16_fs;
  34. extern grub_uint16_t grub_relocator16_gs;
  35. extern grub_uint16_t grub_relocator16_ss;
  36. extern grub_uint16_t grub_relocator16_sp;
  37. extern grub_uint32_t grub_relocator16_edx;
  38. extern grub_uint32_t grub_relocator16_ebx;
  39. extern grub_uint32_t grub_relocator16_esi;
  40. extern grub_uint32_t grub_relocator16_ebp;
  41. extern grub_uint16_t grub_relocator16_keep_a20_enabled;
  42. extern grub_uint8_t grub_relocator32_start;
  43. extern grub_uint8_t grub_relocator32_end;
  44. extern grub_uint32_t grub_relocator32_eax;
  45. extern grub_uint32_t grub_relocator32_ebx;
  46. extern grub_uint32_t grub_relocator32_ecx;
  47. extern grub_uint32_t grub_relocator32_edx;
  48. extern grub_uint32_t grub_relocator32_eip;
  49. extern grub_uint32_t grub_relocator32_esp;
  50. extern grub_uint32_t grub_relocator32_ebp;
  51. extern grub_uint32_t grub_relocator32_esi;
  52. extern grub_uint32_t grub_relocator32_edi;
  53. extern grub_uint8_t grub_relocator64_start;
  54. extern grub_uint8_t grub_relocator64_end;
  55. extern grub_uint64_t grub_relocator64_rax;
  56. extern grub_uint64_t grub_relocator64_rbx;
  57. extern grub_uint64_t grub_relocator64_rcx;
  58. extern grub_uint64_t grub_relocator64_rdx;
  59. extern grub_uint64_t grub_relocator64_rip;
  60. extern grub_uint64_t grub_relocator64_rsp;
  61. extern grub_uint64_t grub_relocator64_rsi;
  62. extern grub_addr_t grub_relocator64_cr3;
  63. extern struct grub_i386_idt grub_relocator16_idt;
  64. #define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start)
  65. grub_err_t
  66. grub_relocator32_boot (struct grub_relocator *rel,
  67. struct grub_relocator32_state state,
  68. int avoid_efi_bootservices)
  69. {
  70. grub_err_t err;
  71. void *relst;
  72. grub_relocator_chunk_t ch;
  73. /* Specific memory range due to Global Descriptor Table for use by payload
  74. that we will store in returned chunk. The address range and preference
  75. are based on "THE LINUX/x86 BOOT PROTOCOL" specification. */
  76. err = grub_relocator_alloc_chunk_align_safe (rel, &ch, 0x1000, 0x9a000,
  77. RELOCATOR_SIZEOF (32), 16,
  78. GRUB_RELOCATOR_PREFERENCE_LOW,
  79. avoid_efi_bootservices);
  80. if (err)
  81. return err;
  82. grub_relocator32_eax = state.eax;
  83. grub_relocator32_ebx = state.ebx;
  84. grub_relocator32_ecx = state.ecx;
  85. grub_relocator32_edx = state.edx;
  86. grub_relocator32_eip = state.eip;
  87. grub_relocator32_esp = state.esp;
  88. grub_relocator32_ebp = state.ebp;
  89. grub_relocator32_esi = state.esi;
  90. grub_relocator32_edi = state.edi;
  91. grub_memmove (get_virtual_current_address (ch), &grub_relocator32_start,
  92. RELOCATOR_SIZEOF (32));
  93. err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
  94. &relst, NULL);
  95. if (err)
  96. return err;
  97. asm volatile ("cli");
  98. ((void (*) (void)) relst) ();
  99. /* Not reached. */
  100. return GRUB_ERR_NONE;
  101. }
  102. grub_err_t
  103. grub_relocator16_boot (struct grub_relocator *rel,
  104. struct grub_relocator16_state state)
  105. {
  106. grub_err_t err;
  107. void *relst;
  108. grub_relocator_chunk_t ch;
  109. /* Put it higher than the byte it checks for A20 check. */
  110. err = grub_relocator_alloc_chunk_align_safe (rel, &ch, 0x8010, 0xa0000,
  111. RELOCATOR_SIZEOF (16) +
  112. GRUB_RELOCATOR16_STACK_SIZE, 16,
  113. GRUB_RELOCATOR_PREFERENCE_NONE, 0);
  114. if (err)
  115. return err;
  116. grub_relocator16_cs = state.cs;
  117. grub_relocator16_ip = state.ip;
  118. grub_relocator16_ds = state.ds;
  119. grub_relocator16_es = state.es;
  120. grub_relocator16_fs = state.fs;
  121. grub_relocator16_gs = state.gs;
  122. grub_relocator16_ss = state.ss;
  123. grub_relocator16_sp = state.sp;
  124. grub_relocator16_ebp = state.ebp;
  125. grub_relocator16_ebx = state.ebx;
  126. grub_relocator16_edx = state.edx;
  127. grub_relocator16_esi = state.esi;
  128. #ifdef GRUB_MACHINE_PCBIOS
  129. grub_relocator16_idt = *grub_realidt;
  130. #else
  131. grub_relocator16_idt.base = 0;
  132. grub_relocator16_idt.limit = 0;
  133. #endif
  134. grub_relocator16_keep_a20_enabled = state.a20;
  135. grub_memmove (get_virtual_current_address (ch), &grub_relocator16_start,
  136. RELOCATOR_SIZEOF (16));
  137. err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
  138. &relst, NULL);
  139. if (err)
  140. return err;
  141. asm volatile ("cli");
  142. ((void (*) (void)) relst) ();
  143. /* Not reached. */
  144. return GRUB_ERR_NONE;
  145. }
  146. grub_err_t
  147. grub_relocator64_boot (struct grub_relocator *rel,
  148. struct grub_relocator64_state state,
  149. grub_addr_t min_addr, grub_addr_t max_addr)
  150. {
  151. grub_err_t err;
  152. void *relst;
  153. grub_relocator_chunk_t ch;
  154. err = grub_relocator_alloc_chunk_align_safe (rel, &ch, min_addr, max_addr,
  155. RELOCATOR_SIZEOF (64), 16,
  156. GRUB_RELOCATOR_PREFERENCE_NONE, 0);
  157. if (err)
  158. return err;
  159. grub_relocator64_rax = state.rax;
  160. grub_relocator64_rbx = state.rbx;
  161. grub_relocator64_rcx = state.rcx;
  162. grub_relocator64_rdx = state.rdx;
  163. grub_relocator64_rip = state.rip;
  164. grub_relocator64_rsp = state.rsp;
  165. grub_relocator64_rsi = state.rsi;
  166. grub_relocator64_cr3 = state.cr3;
  167. grub_memmove (get_virtual_current_address (ch), &grub_relocator64_start,
  168. RELOCATOR_SIZEOF (64));
  169. err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
  170. &relst, NULL);
  171. if (err)
  172. return err;
  173. asm volatile ("cli");
  174. ((void (*) (void)) relst) ();
  175. /* Not reached. */
  176. return GRUB_ERR_NONE;
  177. }