iPXE
elfboot.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <errno.h>
00027 #include <elf.h>
00028 #include <ipxe/image.h>
00029 #include <ipxe/elf.h>
00030 #include <ipxe/features.h>
00031 #include <ipxe/init.h>
00032 
00033 /**
00034  * @file
00035  *
00036  * ELF bootable image
00037  *
00038  */
00039 
00040 FEATURE ( FEATURE_IMAGE, "ELF", DHCP_EB_FEATURE_ELF, 1 );
00041 
00042 /**
00043  * Execute ELF image
00044  *
00045  * @v image             ELF image
00046  * @ret rc              Return status code
00047  */
00048 static int elfboot_exec ( struct image *image ) {
00049         physaddr_t entry;
00050         physaddr_t max;
00051         int rc;
00052 
00053         /* Load the image using core ELF support */
00054         if ( ( rc = elf_load ( image, &entry, &max ) ) != 0 ) {
00055                 DBGC ( image, "ELF %p could not load: %s\n",
00056                        image, strerror ( rc ) );
00057                 return rc;
00058         }
00059 
00060         /* An ELF image has no callback interface, so we need to shut
00061          * down before invoking it.
00062          */
00063         shutdown_boot();
00064 
00065         /* Jump to OS with flat physical addressing */
00066         DBGC ( image, "ELF %p starting execution at %lx\n", image, entry );
00067         __asm__ __volatile__ ( PHYS_CODE ( "pushl %%ebp\n\t" /* gcc bug */
00068                                            "call *%%edi\n\t"
00069                                            "popl %%ebp\n\t" /* gcc bug */ )
00070                                : : "D" ( entry )
00071                                : "eax", "ebx", "ecx", "edx", "esi", "memory" );
00072 
00073         DBGC ( image, "ELF %p returned\n", image );
00074 
00075         /* It isn't safe to continue after calling shutdown() */
00076         while ( 1 ) {}
00077 
00078         return -ECANCELED;  /* -EIMPOSSIBLE, anyone? */
00079 }
00080 
00081 /**
00082  * Check that ELF segment uses flat physical addressing
00083  *
00084  * @v image             ELF file
00085  * @v phdr              ELF program header
00086  * @v dest              Destination address
00087  * @ret rc              Return status code
00088  */
00089 static int elfboot_check_segment ( struct image *image, Elf_Phdr *phdr,
00090                                    physaddr_t dest ) {
00091 
00092         /* Check that ELF segment uses flat physical addressing */
00093         if ( phdr->p_vaddr != dest ) {
00094                 DBGC ( image, "ELF %p uses virtual addressing (phys %x, "
00095                        "virt %x)\n", image, phdr->p_paddr, phdr->p_vaddr );
00096                 return -ENOEXEC;
00097         }
00098 
00099         return 0;
00100 }
00101 
00102 /**
00103  * Probe ELF image
00104  *
00105  * @v image             ELF file
00106  * @ret rc              Return status code
00107  */
00108 static int elfboot_probe ( struct image *image ) {
00109         Elf32_Ehdr ehdr;
00110         static const uint8_t e_ident[] = {
00111                 [EI_MAG0]       = ELFMAG0,
00112                 [EI_MAG1]       = ELFMAG1,
00113                 [EI_MAG2]       = ELFMAG2,
00114                 [EI_MAG3]       = ELFMAG3,
00115                 [EI_CLASS]      = ELFCLASS32,
00116                 [EI_DATA]       = ELFDATA2LSB,
00117                 [EI_VERSION]    = EV_CURRENT,
00118         };
00119         physaddr_t entry;
00120         physaddr_t max;
00121         int rc;
00122 
00123         /* Read ELF header */
00124         copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
00125         if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
00126                 DBGC ( image, "Invalid ELF identifier\n" );
00127                 return -ENOEXEC;
00128         }
00129 
00130         /* Check that this image uses flat physical addressing */
00131         if ( ( rc = elf_segments ( image, &ehdr, elfboot_check_segment,
00132                                    &entry, &max ) ) != 0 ) {
00133                 DBGC ( image, "Unloadable ELF image\n" );
00134                 return rc;
00135         }
00136 
00137         return 0;
00138 }
00139 
00140 /** ELF image type */
00141 struct image_type elfboot_image_type __image_type ( PROBE_NORMAL ) = {
00142         .name = "ELF",
00143         .probe = elfboot_probe,
00144         .exec = elfboot_exec,
00145 };