multiboot_elfxx.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #elif defined(MULTIBOOT_LOAD_ELF64)
  25. # define XX 64
  26. # define E_MACHINE MULTIBOOT_ELF64_MACHINE
  27. # define ELFCLASSXX ELFCLASS64
  28. # define Elf_Ehdr Elf64_Ehdr
  29. # define Elf_Phdr Elf64_Phdr
  30. #else
  31. #error "I'm confused"
  32. #endif
  33. #include <grub/i386/relocator.h>
  34. #define CONCAT(a,b) CONCAT_(a, b)
  35. #define CONCAT_(a,b) a ## b
  36. /* Check if BUFFER contains ELF32 (or ELF64). */
  37. static int
  38. CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
  39. {
  40. Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  41. return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
  42. }
  43. static grub_err_t
  44. CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)
  45. {
  46. Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
  47. char *phdr_base;
  48. int lowest_segment = -1, highest_segment = -1;
  49. int i;
  50. grub_size_t code_size;
  51. if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX)
  52. return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF class");
  53. if (ehdr->e_ident[EI_MAG0] != ELFMAG0
  54. || ehdr->e_ident[EI_MAG1] != ELFMAG1
  55. || ehdr->e_ident[EI_MAG2] != ELFMAG2
  56. || ehdr->e_ident[EI_MAG3] != ELFMAG3
  57. || ehdr->e_version != EV_CURRENT
  58. || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
  59. || ehdr->e_machine != E_MACHINE)
  60. return grub_error(GRUB_ERR_UNKNOWN_OS, "no valid ELF header found");
  61. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
  62. return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
  63. /* FIXME: Should we support program headers at strange locations? */
  64. if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
  65. return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
  66. #ifdef MULTIBOOT_LOAD_ELF64
  67. /* We still in 32-bit mode. */
  68. if (ehdr->e_entry > 0xffffffff)
  69. return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
  70. #endif
  71. phdr_base = (char *) buffer + ehdr->e_phoff;
  72. #define phdr(i) ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
  73. for (i = 0; i < ehdr->e_phnum; i++)
  74. if (phdr(i)->p_type == PT_LOAD && phdr(i)->p_filesz != 0)
  75. {
  76. /* Beware that segment 0 isn't necessarily loadable */
  77. if (lowest_segment == -1
  78. || phdr(i)->p_paddr < phdr(lowest_segment)->p_paddr)
  79. lowest_segment = i;
  80. if (highest_segment == -1
  81. || phdr(i)->p_paddr > phdr(highest_segment)->p_paddr)
  82. highest_segment = i;
  83. }
  84. if (lowest_segment == -1)
  85. return grub_error (GRUB_ERR_BAD_OS, "ELF contains no loadable segments");
  86. code_size = (phdr(highest_segment)->p_paddr + phdr(highest_segment)->p_memsz) - phdr(lowest_segment)->p_paddr;
  87. grub_multiboot_payload_dest = phdr(lowest_segment)->p_paddr;
  88. grub_multiboot_pure_size += code_size;
  89. grub_multiboot_alloc_mbi = grub_multiboot_get_mbi_size () + 65536;
  90. grub_multiboot_payload_orig
  91. = grub_relocator32_alloc (grub_multiboot_pure_size + grub_multiboot_alloc_mbi);
  92. if (!grub_multiboot_payload_orig)
  93. return grub_errno;
  94. /* Load every loadable segment in memory. */
  95. for (i = 0; i < ehdr->e_phnum; i++)
  96. {
  97. if (phdr(i)->p_type == PT_LOAD && phdr(i)->p_filesz != 0)
  98. {
  99. char *load_this_module_at = (char *) (grub_multiboot_payload_orig + (long) (phdr(i)->p_paddr - phdr(lowest_segment)->p_paddr));
  100. grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
  101. i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
  102. if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
  103. == (grub_off_t) -1)
  104. return grub_error (GRUB_ERR_BAD_OS,
  105. "invalid offset in program header");
  106. if (grub_file_read (file, load_this_module_at, phdr(i)->p_filesz)
  107. != (grub_ssize_t) phdr(i)->p_filesz)
  108. return grub_error (GRUB_ERR_BAD_OS,
  109. "couldn't read segment from file");
  110. if (phdr(i)->p_filesz < phdr(i)->p_memsz)
  111. grub_memset (load_this_module_at + phdr(i)->p_filesz, 0,
  112. phdr(i)->p_memsz - phdr(i)->p_filesz);
  113. }
  114. }
  115. for (i = 0; i < ehdr->e_phnum; i++)
  116. if (phdr(i)->p_vaddr <= ehdr->e_entry
  117. && phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
  118. {
  119. grub_multiboot_payload_eip = grub_multiboot_payload_dest
  120. + (ehdr->e_entry - phdr(i)->p_vaddr) + (phdr(i)->p_paddr - phdr(lowest_segment)->p_paddr);
  121. break;
  122. }
  123. if (i == ehdr->e_phnum)
  124. return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
  125. #undef phdr
  126. return grub_errno;
  127. }
  128. #undef XX
  129. #undef E_MACHINE
  130. #undef ELFCLASSXX
  131. #undef Elf_Ehdr
  132. #undef Elf_Phdr