You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /**
  20. * @file
  21. *
  22. * ELF image format
  23. *
  24. * A "pure" ELF image is not a bootable image. There are various
  25. * bootable formats based upon ELF (e.g. Multiboot), which share
  26. * common ELF-related functionality.
  27. */
  28. #include <errno.h>
  29. #include <elf.h>
  30. #include <ipxe/uaccess.h>
  31. #include <ipxe/segment.h>
  32. #include <ipxe/image.h>
  33. #include <ipxe/elf.h>
  34. typedef Elf32_Ehdr Elf_Ehdr;
  35. typedef Elf32_Phdr Elf_Phdr;
  36. typedef Elf32_Off Elf_Off;
  37. #define ELFCLASS ELFCLASS32
  38. /**
  39. * Load ELF segment into memory
  40. *
  41. * @v image ELF file
  42. * @v phdr ELF program header
  43. * @v ehdr ELF executable header
  44. * @ret entry Entry point, if found
  45. * @ret max Maximum used address
  46. * @ret rc Return status code
  47. */
  48. static int elf_load_segment ( struct image *image, Elf_Phdr *phdr,
  49. Elf_Ehdr *ehdr, physaddr_t *entry,
  50. physaddr_t *max ) {
  51. physaddr_t dest;
  52. physaddr_t end;
  53. userptr_t buffer;
  54. unsigned long e_offset;
  55. int rc;
  56. /* Do nothing for non-PT_LOAD segments */
  57. if ( phdr->p_type != PT_LOAD )
  58. return 0;
  59. /* Check segment lies within image */
  60. if ( ( phdr->p_offset + phdr->p_filesz ) > image->len ) {
  61. DBGC ( image, "ELF %p segment outside image\n", image );
  62. return -ENOEXEC;
  63. }
  64. /* Find start address: use physical address for preference,
  65. * fall back to virtual address if no physical address
  66. * supplied.
  67. */
  68. dest = phdr->p_paddr;
  69. if ( ! dest )
  70. dest = phdr->p_vaddr;
  71. if ( ! dest ) {
  72. DBGC ( image, "ELF %p segment loads to physical address 0\n",
  73. image );
  74. return -ENOEXEC;
  75. }
  76. buffer = phys_to_user ( dest );
  77. end = ( dest + phdr->p_memsz );
  78. DBGC ( image, "ELF %p loading segment [%x,%x) to [%x,%x,%x)\n", image,
  79. phdr->p_offset, ( phdr->p_offset + phdr->p_filesz ),
  80. phdr->p_paddr, ( phdr->p_paddr + phdr->p_filesz ),
  81. ( phdr->p_paddr + phdr->p_memsz ) );
  82. /* Verify and prepare segment */
  83. if ( ( rc = prep_segment ( buffer, phdr->p_filesz,
  84. phdr->p_memsz ) ) != 0 ) {
  85. DBGC ( image, "ELF %p could not prepare segment: %s\n",
  86. image, strerror ( rc ) );
  87. return rc;
  88. }
  89. /* Update maximum used address, if applicable */
  90. if ( end > *max )
  91. *max = end;
  92. /* Copy image to segment */
  93. memcpy_user ( buffer, 0, image->data, phdr->p_offset, phdr->p_filesz );
  94. /* Set execution address, if it lies within this segment */
  95. if ( ( e_offset = ( ehdr->e_entry - dest ) ) < phdr->p_filesz ) {
  96. *entry = ehdr->e_entry;
  97. DBGC ( image, "ELF %p found physical entry point at %lx\n",
  98. image, *entry );
  99. } else if ( ( e_offset = ( ehdr->e_entry - phdr->p_vaddr ) )
  100. < phdr->p_filesz ) {
  101. if ( ! *entry ) {
  102. *entry = ( dest + e_offset );
  103. DBGC ( image, "ELF %p found virtual entry point at %lx"
  104. " (virt %lx)\n", image, *entry,
  105. ( ( unsigned long ) ehdr->e_entry ) );
  106. }
  107. }
  108. return 0;
  109. }
  110. /**
  111. * Load ELF image into memory
  112. *
  113. * @v image ELF file
  114. * @ret entry Entry point
  115. * @ret max Maximum used address
  116. * @ret rc Return status code
  117. */
  118. int elf_load ( struct image *image, physaddr_t *entry, physaddr_t *max ) {
  119. static const uint8_t e_ident[] = {
  120. [EI_MAG0] = ELFMAG0,
  121. [EI_MAG1] = ELFMAG1,
  122. [EI_MAG2] = ELFMAG2,
  123. [EI_MAG3] = ELFMAG3,
  124. [EI_CLASS] = ELFCLASS,
  125. };
  126. Elf_Ehdr ehdr;
  127. Elf_Phdr phdr;
  128. Elf_Off phoff;
  129. unsigned int phnum;
  130. int rc;
  131. /* Read ELF header */
  132. copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
  133. if ( memcmp ( &ehdr.e_ident[EI_MAG0], e_ident,
  134. sizeof ( e_ident ) ) != 0 ) {
  135. DBGC ( image, "ELF %p has invalid signature\n", image );
  136. return -ENOEXEC;
  137. }
  138. /* Initialise maximum used address */
  139. *max = 0;
  140. /* Invalidate entry point */
  141. *entry = 0;
  142. /* Read ELF program headers */
  143. for ( phoff = ehdr.e_phoff , phnum = ehdr.e_phnum ; phnum ;
  144. phoff += ehdr.e_phentsize, phnum-- ) {
  145. if ( phoff > image->len ) {
  146. DBGC ( image, "ELF %p program header %d outside "
  147. "image\n", image, phnum );
  148. return -ENOEXEC;
  149. }
  150. copy_from_user ( &phdr, image->data, phoff, sizeof ( phdr ) );
  151. if ( ( rc = elf_load_segment ( image, &phdr, &ehdr,
  152. entry, max ) ) != 0 ) {
  153. return rc;
  154. }
  155. }
  156. /* Check for a valid execution address */
  157. if ( ! *entry ) {
  158. DBGC ( image, "ELF %p entry point %lx outside image\n",
  159. image, ( ( unsigned long ) ehdr.e_entry ) );
  160. return -ENOEXEC;
  161. }
  162. return 0;
  163. }