multiboot_elfxx.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,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. #if defined(MULTIBOOT_LOAD_ELF32)
  19. # define XX 32
  20. # define E_MACHINE MULTIBOOT_ELF32_MACHINE
  21. # define ELFCLASSXX ELFCLASS32
  22. # define Elf_Ehdr Elf32_Ehdr
  23. # define Elf_Phdr Elf32_Phdr
  24. # define Elf_Shdr Elf32_Shdr
  25. #elif defined(MULTIBOOT_LOAD_ELF64)
  26. # define XX 64
  27. # define E_MACHINE MULTIBOOT_ELF64_MACHINE
  28. # define ELFCLASSXX ELFCLASS64
  29. # define Elf_Ehdr Elf64_Ehdr
  30. # define Elf_Phdr Elf64_Phdr
  31. # define Elf_Shdr Elf64_Shdr
  32. #else
  33. #error "I'm confused"
  34. #endif
  35. #include <grub/i386/relocator.h>
  36. #define CONCAT(a,b) CONCAT_(a, b)
  37. #define CONCAT_(a,b) a ## b
  38. #pragma GCC diagnostic ignored "-Wcast-align"
  39. /* Check if BUFFER contains ELF32 (or ELF64). */
  40. static int
  41. CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
  42. {
  43. Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  44. return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
  45. }
  46. static grub_err_t
  47. CONCAT(grub_multiboot_load_elf, XX) (mbi_load_data_t *mld)
  48. {
  49. Elf_Ehdr *ehdr = (Elf_Ehdr *) mld->buffer;
  50. char *phdr_base;
  51. grub_err_t err;
  52. grub_relocator_chunk_t ch;
  53. grub_uint32_t load_offset = 0, load_size;
  54. int i;
  55. void *source = NULL;
  56. if (ehdr->e_ident[EI_MAG0] != ELFMAG0
  57. || ehdr->e_ident[EI_MAG1] != ELFMAG1
  58. || ehdr->e_ident[EI_MAG2] != ELFMAG2
  59. || ehdr->e_ident[EI_MAG3] != ELFMAG3
  60. || ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
  61. return grub_error(GRUB_ERR_UNKNOWN_OS, N_("invalid arch-independent ELF magic"));
  62. if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX || ehdr->e_machine != E_MACHINE
  63. || ehdr->e_version != EV_CURRENT)
  64. return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
  65. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
  66. return grub_error (GRUB_ERR_UNKNOWN_OS, N_("this ELF file is not of the right type"));
  67. /* FIXME: Should we support program headers at strange locations? */
  68. if (ehdr->e_phoff + (grub_uint32_t) ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
  69. return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
  70. phdr_base = (char *) mld->buffer + ehdr->e_phoff;
  71. #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
  72. mld->link_base_addr = ~0;
  73. /* Calculate lowest and highest load address. */
  74. for (i = 0; i < ehdr->e_phnum; i++)
  75. if (phdr(i)->p_type == PT_LOAD)
  76. {
  77. mld->link_base_addr = grub_min (mld->link_base_addr, phdr(i)->p_paddr);
  78. highest_load = grub_max (highest_load, phdr(i)->p_paddr + phdr(i)->p_memsz);
  79. }
  80. #ifdef MULTIBOOT_LOAD_ELF64
  81. if (highest_load >= 0x100000000)
  82. return grub_error (GRUB_ERR_BAD_OS, "segment crosses 4 GiB border");
  83. #endif
  84. if (mld->relocatable)
  85. {
  86. load_size = highest_load - mld->link_base_addr;
  87. grub_dprintf ("multiboot_loader", "align=0x%lx, preference=0x%x, "
  88. "load_size=0x%x, avoid_efi_boot_services=%d\n",
  89. (long) mld->align, mld->preference, load_size,
  90. mld->avoid_efi_boot_services);
  91. if (load_size > mld->max_addr || mld->min_addr > mld->max_addr - load_size)
  92. return grub_error (GRUB_ERR_BAD_OS, "invalid min/max address and/or load size");
  93. err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch,
  94. mld->min_addr, mld->max_addr - load_size,
  95. load_size, mld->align ? mld->align : 1,
  96. mld->preference, mld->avoid_efi_boot_services);
  97. if (err)
  98. {
  99. grub_dprintf ("multiboot_loader", "Cannot allocate memory for OS image\n");
  100. return err;
  101. }
  102. mld->load_base_addr = get_physical_target_address (ch);
  103. source = get_virtual_current_address (ch);
  104. }
  105. else
  106. mld->load_base_addr = mld->link_base_addr;
  107. grub_dprintf ("multiboot_loader", "relocatable=%d, link_base_addr=0x%x, "
  108. "load_base_addr=0x%x\n", mld->relocatable,
  109. mld->link_base_addr, mld->load_base_addr);
  110. /* Load every loadable segment in memory. */
  111. for (i = 0; i < ehdr->e_phnum; i++)
  112. {
  113. if (phdr(i)->p_type == PT_LOAD)
  114. {
  115. grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
  116. i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
  117. if (mld->relocatable)
  118. {
  119. load_offset = phdr(i)->p_paddr - mld->link_base_addr;
  120. grub_dprintf ("multiboot_loader", "segment %d: load_offset=0x%x\n", i, load_offset);
  121. }
  122. else
  123. {
  124. err = grub_relocator_alloc_chunk_addr (GRUB_MULTIBOOT (relocator), &ch,
  125. phdr(i)->p_paddr, phdr(i)->p_memsz);
  126. if (err)
  127. {
  128. grub_dprintf ("multiboot_loader", "Cannot allocate memory for OS image\n");
  129. return err;
  130. }
  131. source = get_virtual_current_address (ch);
  132. }
  133. if (phdr(i)->p_filesz != 0)
  134. {
  135. if (grub_file_seek (mld->file, (grub_off_t) phdr(i)->p_offset)
  136. == (grub_off_t) -1)
  137. return grub_errno;
  138. if (grub_file_read (mld->file, (grub_uint8_t *) source + load_offset, phdr(i)->p_filesz)
  139. != (grub_ssize_t) phdr(i)->p_filesz)
  140. {
  141. if (!grub_errno)
  142. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  143. mld->filename);
  144. return grub_errno;
  145. }
  146. }
  147. if (phdr(i)->p_filesz < phdr(i)->p_memsz)
  148. grub_memset ((grub_uint8_t *) source + load_offset + phdr(i)->p_filesz, 0,
  149. phdr(i)->p_memsz - phdr(i)->p_filesz);
  150. }
  151. }
  152. for (i = 0; i < ehdr->e_phnum; i++)
  153. if (phdr(i)->p_vaddr <= ehdr->e_entry
  154. && phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
  155. {
  156. GRUB_MULTIBOOT (payload_eip) = (ehdr->e_entry - phdr(i)->p_vaddr)
  157. + phdr(i)->p_paddr;
  158. #ifdef MULTIBOOT_LOAD_ELF64
  159. # ifdef __mips
  160. /* We still in 32-bit mode. */
  161. if ((ehdr->e_entry - phdr(i)->p_vaddr)
  162. + phdr(i)->p_paddr < 0xffffffff80000000ULL)
  163. return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
  164. # else
  165. /* We still in 32-bit mode. */
  166. if ((ehdr->e_entry - phdr(i)->p_vaddr)
  167. + phdr(i)->p_paddr > 0xffffffff)
  168. return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
  169. # endif
  170. #endif
  171. break;
  172. }
  173. if (i == ehdr->e_phnum)
  174. return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
  175. #if defined (__i386__) || defined (__x86_64__)
  176. #elif defined (__mips)
  177. GRUB_MULTIBOOT (payload_eip) |= 0x80000000;
  178. #else
  179. #error Please complete this
  180. #endif
  181. if (ehdr->e_shnum)
  182. {
  183. grub_uint8_t *shdr, *shdrptr;
  184. shdr = grub_malloc ((grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize);
  185. if (!shdr)
  186. return grub_errno;
  187. if (grub_file_seek (mld->file, ehdr->e_shoff) == (grub_off_t) -1)
  188. {
  189. grub_free (shdr);
  190. return grub_errno;
  191. }
  192. if (grub_file_read (mld->file, shdr, (grub_uint32_t) ehdr->e_shnum * ehdr->e_shentsize)
  193. != (grub_ssize_t) ehdr->e_shnum * ehdr->e_shentsize)
  194. {
  195. if (!grub_errno)
  196. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  197. mld->filename);
  198. return grub_errno;
  199. }
  200. for (shdrptr = shdr, i = 0; i < ehdr->e_shnum;
  201. shdrptr += ehdr->e_shentsize, i++)
  202. {
  203. Elf_Shdr *sh = (Elf_Shdr *) shdrptr;
  204. void *src;
  205. grub_addr_t target;
  206. if (mld->mbi_ver >= 2 && (sh->sh_type == SHT_REL || sh->sh_type == SHT_RELA))
  207. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "ELF files with relocs are not supported yet");
  208. /* This section is a loaded section,
  209. so we don't care. */
  210. if (sh->sh_addr != 0)
  211. continue;
  212. /* This section is empty, so we don't care. */
  213. if (sh->sh_size == 0)
  214. continue;
  215. err = grub_relocator_alloc_chunk_align (GRUB_MULTIBOOT (relocator), &ch, 0,
  216. (0xffffffff - sh->sh_size) + 1,
  217. sh->sh_size, sh->sh_addralign,
  218. GRUB_RELOCATOR_PREFERENCE_NONE,
  219. mld->avoid_efi_boot_services);
  220. if (err)
  221. {
  222. grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
  223. return err;
  224. }
  225. src = get_virtual_current_address (ch);
  226. target = get_physical_target_address (ch);
  227. if (grub_file_seek (mld->file, sh->sh_offset) == (grub_off_t) -1)
  228. return grub_errno;
  229. if (grub_file_read (mld->file, src, sh->sh_size)
  230. != (grub_ssize_t) sh->sh_size)
  231. {
  232. if (!grub_errno)
  233. grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
  234. mld->filename);
  235. return grub_errno;
  236. }
  237. sh->sh_addr = target;
  238. }
  239. GRUB_MULTIBOOT (add_elfsyms) (ehdr->e_shnum, ehdr->e_shentsize,
  240. ehdr->e_shstrndx, shdr);
  241. }
  242. #undef phdr
  243. return grub_errno;
  244. }
  245. #undef XX
  246. #undef E_MACHINE
  247. #undef ELFCLASSXX
  248. #undef Elf_Ehdr
  249. #undef Elf_Phdr
  250. #undef Elf_Shdr