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.

elfboot.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <errno.h>
  25. #include <elf.h>
  26. #include <ipxe/image.h>
  27. #include <ipxe/elf.h>
  28. #include <ipxe/features.h>
  29. #include <ipxe/init.h>
  30. /**
  31. * @file
  32. *
  33. * ELF bootable image
  34. *
  35. */
  36. FEATURE ( FEATURE_IMAGE, "ELF", DHCP_EB_FEATURE_ELF, 1 );
  37. /**
  38. * Execute ELF image
  39. *
  40. * @v image ELF image
  41. * @ret rc Return status code
  42. */
  43. static int elfboot_exec ( struct image *image ) {
  44. physaddr_t entry;
  45. physaddr_t max;
  46. int rc;
  47. /* Load the image using core ELF support */
  48. if ( ( rc = elf_load ( image, &entry, &max ) ) != 0 ) {
  49. DBGC ( image, "ELF %p could not load: %s\n",
  50. image, strerror ( rc ) );
  51. return rc;
  52. }
  53. /* An ELF image has no callback interface, so we need to shut
  54. * down before invoking it.
  55. */
  56. shutdown_boot();
  57. /* Jump to OS with flat physical addressing */
  58. DBGC ( image, "ELF %p starting execution at %lx\n", image, entry );
  59. __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
  60. "call *%%edi\n\t"
  61. "popl %%ebp\n\t" /* gcc bug */ )
  62. : : "D" ( entry )
  63. : "eax", "ebx", "ecx", "edx", "esi", "memory" );
  64. DBGC ( image, "ELF %p returned\n", image );
  65. /* It isn't safe to continue after calling shutdown() */
  66. while ( 1 ) {}
  67. return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
  68. }
  69. /**
  70. * Probe ELF image
  71. *
  72. * @v image ELF file
  73. * @ret rc Return status code
  74. */
  75. static int elfboot_probe ( struct image *image ) {
  76. Elf32_Ehdr ehdr;
  77. static const uint8_t e_ident[] = {
  78. [EI_MAG0] = ELFMAG0,
  79. [EI_MAG1] = ELFMAG1,
  80. [EI_MAG2] = ELFMAG2,
  81. [EI_MAG3] = ELFMAG3,
  82. [EI_CLASS] = ELFCLASS32,
  83. [EI_DATA] = ELFDATA2LSB,
  84. [EI_VERSION] = EV_CURRENT,
  85. };
  86. /* Read ELF header */
  87. copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
  88. if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
  89. DBG ( "Invalid ELF identifier\n" );
  90. return -ENOEXEC;
  91. }
  92. return 0;
  93. }
  94. /** ELF image type */
  95. struct image_type elfboot_image_type __image_type ( PROBE_NORMAL ) = {
  96. .name = "ELF",
  97. .probe = elfboot_probe,
  98. .exec = elfboot_exec,
  99. };