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

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