elfboot.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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();
  44. /* Jump to OS with flat physical addressing */
  45. __asm__ __volatile__ ( PHYS_CODE ( "call *%%edi\n\t" )
  46. : : "D" ( entry )
  47. : "eax", "ebx", "ecx", "edx", "esi", "ebp",
  48. "memory" );
  49. DBGC ( image, "ELF %p returned\n", image );
  50. /* It isn't safe to continue after calling shutdown() */
  51. while ( 1 ) {}
  52. return -ECANCELED; /* -EIMPOSSIBLE, anyone? */
  53. }
  54. /**
  55. * Load ELF image into memory
  56. *
  57. * @v image ELF file
  58. * @ret rc Return status code
  59. */
  60. static int elfboot_load ( struct image *image ) {
  61. Elf32_Ehdr ehdr;
  62. static const uint8_t e_ident[] = {
  63. [EI_MAG0] = ELFMAG0,
  64. [EI_MAG1] = ELFMAG1,
  65. [EI_MAG2] = ELFMAG2,
  66. [EI_MAG3] = ELFMAG3,
  67. [EI_CLASS] = ELFCLASS32,
  68. [EI_DATA] = ELFDATA2LSB,
  69. [EI_VERSION] = EV_CURRENT,
  70. };
  71. int rc;
  72. /* Read ELF header */
  73. copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
  74. if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
  75. DBG ( "Invalid ELF identifier\n" );
  76. return -ENOEXEC;
  77. }
  78. /* This is an ELF image, valid or otherwise */
  79. if ( ! image->type )
  80. image->type = &elfboot_image_type;
  81. /* Load the image using core ELF support */
  82. if ( ( rc = elf_load ( image ) ) != 0 ) {
  83. DBGC ( image, "ELF %p could not load: %s\n",
  84. image, strerror ( rc ) );
  85. return rc;
  86. }
  87. return 0;
  88. }
  89. /** ELF image type */
  90. struct image_type elfboot_image_type __image_type ( PROBE_NORMAL ) = {
  91. .name = "ELF",
  92. .load = elfboot_load,
  93. .exec = elfboot_exec,
  94. };