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.8KB

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