multiboot_elfxx.c 11 KB

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