efi_image.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 <gpxe/efi/efi.h>
00023 #include <gpxe/image.h>
00024 #include <gpxe/features.h>
00025 
00026 FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
00027 
00028 struct image_type efi_image_type __image_type ( PROBE_NORMAL );
00029 
00030 /**
00031  * Execute EFI image
00032  *
00033  * @v image             EFI image
00034  * @ret rc              Return status code
00035  */
00036 static int efi_image_exec ( struct image *image ) {
00037         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00038         EFI_HANDLE handle;
00039         UINTN exit_data_size;
00040         CHAR16 *exit_data;
00041         EFI_STATUS efirc;
00042 
00043         /* Attempt loading image */
00044         if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
00045                                        user_to_virt ( image->data, 0 ),
00046                                        image->len, &handle ) ) != 0 ) {
00047                 /* Not an EFI image */
00048                 DBGC ( image, "EFIIMAGE %p could not load: %s\n",
00049                        image, efi_strerror ( efirc ) );
00050                 return -ENOEXEC;
00051         }
00052 
00053         /* Start the image */
00054         if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
00055                                         &exit_data ) ) != 0 ) {
00056                 DBGC ( image, "EFIIMAGE %p returned with status %s\n",
00057                        image, efi_strerror ( efirc ) );
00058                 goto done;
00059         }
00060 
00061  done:
00062         /* Unload the image.  We can't leave it loaded, because we
00063          * have no "unload" operation.
00064          */
00065         bs->UnloadImage ( handle );
00066 
00067         return EFIRC_TO_RC ( efirc );
00068 }
00069 
00070 /**
00071  * Load EFI image into memory
00072  *
00073  * @v image             EFI file
00074  * @ret rc              Return status code
00075  */
00076 static int efi_image_load ( struct image *image ) {
00077         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00078         EFI_HANDLE handle;
00079         EFI_STATUS efirc;
00080 
00081         /* Attempt loading image */
00082         if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
00083                                        user_to_virt ( image->data, 0 ),
00084                                        image->len, &handle ) ) != 0 ) {
00085                 /* Not an EFI image */
00086                 DBGC ( image, "EFIIMAGE %p could not load: %s\n",
00087                        image, efi_strerror ( efirc ) );
00088                 return -ENOEXEC;
00089         }
00090 
00091         /* This is an EFI image */
00092         if ( ! image->type )
00093                 image->type = &efi_image_type;
00094 
00095         /* Unload the image.  We can't leave it loaded, because we
00096          * have no "unload" operation.
00097          */
00098         bs->UnloadImage ( handle );
00099 
00100         return 0;
00101 }
00102 
00103 /** EFI image type */
00104 struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
00105         .name = "EFI",
00106         .load = efi_image_load,
00107         .exec = efi_image_exec,
00108 };

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1