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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <errno.h>
00022 #include <elf.h>
00023 #include <gpxe/image.h>
00024 #include <gpxe/elf.h>
00025 #include <gpxe/features.h>
00026 #include <gpxe/init.h>
00027 
00028 /**
00029  * @file
00030  *
00031  * ELF bootable image
00032  *
00033  */
00034 
00035 FEATURE ( FEATURE_IMAGE, "ELF", DHCP_EB_FEATURE_ELF, 1 );
00036 
00037 struct image_type elfboot_image_type __image_type ( PROBE_NORMAL );
00038 
00039 /**
00040  * Execute ELF image
00041  *
00042  * @v image             ELF image
00043  * @ret rc              Return status code
00044  */
00045 static int elfboot_exec ( struct image *image ) {
00046         physaddr_t entry = image->priv.phys;
00047 
00048         /* An ELF image has no callback interface, so we need to shut
00049          * down before invoking it.
00050          */
00051         shutdown ( SHUTDOWN_BOOT );
00052 
00053         /* Jump to OS with flat physical addressing */
00054         DBGC ( image, "ELF %p starting execution at %lx\n", image, entry );
00055         __asm__ __volatile__ ( PHYS_CODE ( "call *%%edi\n\t" )
00056                                : : "D" ( entry )
00057                                : "eax", "ebx", "ecx", "edx", "esi", "ebp",
00058                                  "memory" );
00059 
00060         DBGC ( image, "ELF %p returned\n", image );
00061 
00062         /* It isn't safe to continue after calling shutdown() */
00063         while ( 1 ) {}
00064 
00065         return -ECANCELED;  /* -EIMPOSSIBLE, anyone? */
00066 }
00067 
00068 /**
00069  * Load ELF image into memory
00070  *
00071  * @v image             ELF file
00072  * @ret rc              Return status code
00073  */
00074 static int elfboot_load ( struct image *image ) {
00075         Elf32_Ehdr ehdr;
00076         static const uint8_t e_ident[] = {
00077                 [EI_MAG0]       = ELFMAG0,
00078                 [EI_MAG1]       = ELFMAG1,
00079                 [EI_MAG2]       = ELFMAG2,
00080                 [EI_MAG3]       = ELFMAG3,
00081                 [EI_CLASS]      = ELFCLASS32,
00082                 [EI_DATA]       = ELFDATA2LSB,
00083                 [EI_VERSION]    = EV_CURRENT,
00084         };
00085         int rc;
00086 
00087         /* Read ELF header */
00088         copy_from_user ( &ehdr, image->data, 0, sizeof ( ehdr ) );
00089         if ( memcmp ( ehdr.e_ident, e_ident, sizeof ( e_ident ) ) != 0 ) {
00090                 DBG ( "Invalid ELF identifier\n" );
00091                 return -ENOEXEC;
00092         }
00093 
00094         /* This is an ELF image, valid or otherwise */
00095         if ( ! image->type )
00096                 image->type = &elfboot_image_type;
00097 
00098         /* Load the image using core ELF support */
00099         if ( ( rc = elf_load ( image ) ) != 0 ) {
00100                 DBGC ( image, "ELF %p could not load: %s\n",
00101                        image, strerror ( rc ) );
00102                 return rc;
00103         }
00104 
00105         return 0;
00106 }
00107 
00108 /** ELF image type */
00109 struct image_type elfboot_image_type __image_type ( PROBE_NORMAL ) = {
00110         .name = "ELF",
00111         .load = elfboot_load,
00112         .exec = elfboot_exec,
00113 };

Generated on Tue Apr 6 20:00:50 2010 for gPXE by  doxygen 1.5.7.1