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 2.7KB

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