a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/*
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is free software; you can redistribute it and/or
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * modify it under the terms of the GNU General Public License as
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * published by the Free Software Foundation; either version 2 of the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * License, or any later version.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is distributed in the hope that it will be useful, but
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * WITHOUT ANY WARRANTY; without even the implied warranty of
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * General Public License for more details.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * You should have received a copy of the GNU General Public License
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * along with this program; if not, write to the Free Software
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncFILE_LICENCE ( GPL2_OR_LATER );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <errno.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <stdlib.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/efi/efi.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/image.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/init.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/features.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#include <ipxe/uri.h>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncFEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/** EFI loaded image protocol GUID */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstatic EFI_GUID efi_loaded_image_protocol_guid =
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_LOADED_IMAGE_PROTOCOL_GUID;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Create a Unicode command line for the image
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v image EFI image
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v devpath_out Device path to pass to image (output)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v cmdline_out Unicode command line (output)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v cmdline_len_out Length of command line in bytes (output)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret rc Return status code
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstatic int efi_image_make_cmdline ( struct image *image,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_DEVICE_PATH **devpath_out,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync VOID **cmdline_out,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync UINT32 *cmdline_len_out ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync char *uri;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync size_t uri_len;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync FILEPATH_DEVICE_PATH *devpath;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_DEVICE_PATH *endpath;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync size_t devpath_len;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync CHAR16 *cmdline;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync UINT32 cmdline_len;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync size_t args_len = 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync UINT32 i;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Get the URI string of the image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync uri_len = unparse_uri ( NULL, 0, image->uri, URI_ALL ) + 1;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Compute final command line length */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( image->cmdline ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync args_len = strlen ( image->cmdline ) + 1;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync cmdline_len = args_len + uri_len;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Allocate space for the uri, final command line and device path */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync cmdline = malloc ( cmdline_len * sizeof ( CHAR16 ) + uri_len
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync + SIZE_OF_FILEPATH_DEVICE_PATH
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync + uri_len * sizeof ( CHAR16 )
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync + sizeof ( EFI_DEVICE_PATH ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ! cmdline )
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return -ENOMEM;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync uri = (char *) ( cmdline + cmdline_len );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync devpath = (FILEPATH_DEVICE_PATH *) ( uri + uri_len );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync endpath = (EFI_DEVICE_PATH *) ( (char *) devpath
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync + SIZE_OF_FILEPATH_DEVICE_PATH
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync + uri_len * sizeof ( CHAR16 ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Build the iPXE device path */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync devpath->Header.Type = MEDIA_DEVICE_PATH;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync devpath->Header.SubType = MEDIA_FILEPATH_DP;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync devpath_len = SIZE_OF_FILEPATH_DEVICE_PATH
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync + uri_len * sizeof ( CHAR16 );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync devpath->Header.Length[0] = devpath_len & 0xFF;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync devpath->Header.Length[1] = devpath_len >> 8;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync endpath->Type = END_DEVICE_PATH_TYPE;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync endpath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync endpath->Length[0] = 4;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync endpath->Length[1] = 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync unparse_uri ( uri, uri_len, image->uri, URI_ALL );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Convert to Unicode */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync for ( i = 0 ; i < uri_len ; i++ ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync cmdline[i] = uri[i];
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync devpath->PathName[i] = uri[i];
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( image->cmdline ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync cmdline[uri_len - 1] = ' ';
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync for ( i = 0 ; i < args_len ; i++ ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync cmdline[i + uri_len] = image->cmdline[i];
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *devpath_out = &devpath->Header;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *cmdline_out = cmdline;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *cmdline_len_out = cmdline_len * sizeof ( CHAR16 );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Execute EFI image
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v image EFI image
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret rc Return status code
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstatic int efi_image_exec ( struct image *image ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync union {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_LOADED_IMAGE_PROTOCOL *image;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync void *interface;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync } loaded;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_HANDLE handle;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_HANDLE device_handle = NULL;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync UINTN exit_data_size;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync CHAR16 *exit_data;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_STATUS efirc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync int rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Attempt loading image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync user_to_virt ( image->data, 0 ),
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync image->len, &handle ) ) != 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Not an EFI image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( image, "EFIIMAGE %p could not load: %s\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync image, efi_strerror ( efirc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync rc = -ENOEXEC;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync goto err_load_image;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Get the loaded image protocol for the newly loaded image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync &loaded.interface, efi_image_handle,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( efirc ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Should never happen */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync rc = EFIRC_TO_RC ( efirc );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync goto err_open_protocol;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Pass an iPXE download protocol to the image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( rc = efi_download_install ( &device_handle ) ) != 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( image, "EFIIMAGE %p could not install iPXE download "
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync "protocol: %s\n", image, strerror ( rc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync goto err_download_install;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync loaded.image->DeviceHandle = device_handle;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync loaded.image->ParentHandle = efi_loaded_image;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( rc = efi_image_make_cmdline ( image, &loaded.image->FilePath,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync &loaded.image->LoadOptions,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync &loaded.image->LoadOptionsSize ) ) != 0 )
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync goto err_make_cmdline;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Start the image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync &exit_data ) ) != 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( image, "EFIIMAGE %p returned with status %s\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync image, efi_strerror ( efirc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync rc = EFIRC_TO_RC ( efirc );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync goto err_start_image;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Success */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync rc = 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync err_start_image:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync free ( loaded.image->LoadOptions );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync err_make_cmdline:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync efi_download_uninstall ( device_handle );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync err_download_install:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync err_open_protocol:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Unload the image. We can't leave it loaded, because we
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * have no "unload" operation.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync bs->UnloadImage ( handle );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync err_load_image:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return rc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/**
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Probe EFI image
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync *
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @v image EFI file
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @ret rc Return status code
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstatic int efi_image_probe ( struct image *image ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_HANDLE handle;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync EFI_STATUS efirc;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Attempt loading image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync user_to_virt ( image->data, 0 ),
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync image->len, &handle ) ) != 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Not an EFI image */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync DBGC ( image, "EFIIMAGE %p could not load: %s\n",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync image, efi_strerror ( efirc ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return -ENOEXEC;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync /* Unload the image. We can't leave it loaded, because we
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * have no "unload" operation.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync bs->UnloadImage ( handle );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync/** EFI image type */
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncstruct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync .name = "EFI",
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync .probe = efi_image_probe,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync .exec = efi_image_exec,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync};