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

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