Skip to content

Commit

Permalink
[efi] Split device path functions out to efi_path.c
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Oct 16, 2020
1 parent bcf858c commit 2bf0fd3
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 54 deletions.
5 changes: 3 additions & 2 deletions src/drivers/usb/usbio.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <assert.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/Protocol/UsbIo.h>
#include <ipxe/usb.h>
Expand Down Expand Up @@ -206,7 +207,7 @@ static int usbio_open ( struct usbio_device *usbio, unsigned int interface ) {
path = usbio->path;
usbpath = usbio->usbpath;
usbpath->InterfaceNumber = interface;
end = efi_devpath_end ( path );
end = efi_path_end ( path );

/* Locate handle for this endpoint's interface */
if ( ( efirc = bs->LocateDevicePath ( &efi_usb_io_protocol_guid, &path,
Expand Down Expand Up @@ -1503,7 +1504,7 @@ static int usbio_path ( struct usbio_device *usbio ) {
path = u.interface;

/* Locate end of device path and sanity check */
len = efi_devpath_len ( path );
len = efi_path_len ( path );
if ( len < sizeof ( *usbpath ) ) {
DBGC ( usbio, "USBIO %s underlength device path\n",
efi_handle_name ( handle ) );
Expand Down
4 changes: 2 additions & 2 deletions src/image/efi_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/efi/efi_snp.h>
#include <ipxe/efi/efi_download.h>
#include <ipxe/efi/efi_file.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_wrap.h>
#include <ipxe/efi/efi_pxe.h>
Expand Down Expand Up @@ -75,7 +75,7 @@ efi_image_path ( struct image *image, EFI_DEVICE_PATH_PROTOCOL *parent ) {
size_t len;

/* Calculate device path lengths */
prefix_len = efi_devpath_len ( parent );
prefix_len = efi_path_len ( parent );
name_len = strlen ( image->name );
filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
Expand Down
19 changes: 19 additions & 0 deletions src/include/ipxe/efi/efi_path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _IPXE_EFI_PATH_H
#define _IPXE_EFI_PATH_H

/** @file
*
* EFI device paths
*
*/

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/DevicePath.h>

extern EFI_DEVICE_PATH_PROTOCOL *
efi_path_end ( EFI_DEVICE_PATH_PROTOCOL *path );
extern size_t efi_path_len ( EFI_DEVICE_PATH_PROTOCOL *path );

#endif /* _IPXE_EFI_PATH_H */
4 changes: 0 additions & 4 deletions src/include/ipxe/efi/efi_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/DevicePath.h>

struct device;

extern EFI_DEVICE_PATH_PROTOCOL *
efi_devpath_end ( EFI_DEVICE_PATH_PROTOCOL *path );
extern size_t efi_devpath_len ( EFI_DEVICE_PATH_PROTOCOL *path );
extern int efi_locate_device ( EFI_HANDLE device, EFI_GUID *protocol,
EFI_HANDLE *parent );
extern int efi_child_add ( EFI_HANDLE parent, EFI_HANDLE child );
Expand Down
8 changes: 4 additions & 4 deletions src/interface/efi/efi_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_snp.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_block.h>

/** ACPI table protocol protocol */
Expand Down Expand Up @@ -288,7 +288,7 @@ static int efi_block_hook ( unsigned int drive, struct uri **uris,
}

/* Calculate length of private data */
prefix_len = efi_devpath_len ( snpdev->path );
prefix_len = efi_path_len ( snpdev->path );
uri_len = format_uri ( uris[0], NULL, 0 );
vendor_len = ( sizeof ( *vendor ) +
( ( uri_len + 1 /* NUL */ ) * sizeof ( wchar_t ) ) );
Expand Down Expand Up @@ -551,7 +551,7 @@ static int efi_block_boot_image ( struct san_device *sandev, EFI_HANDLE handle,
}

/* Check if this device is a child of our block device */
prefix_len = efi_devpath_len ( block->path );
prefix_len = efi_path_len ( block->path );
if ( memcmp ( path.path, block->path, prefix_len ) != 0 ) {
/* Not a child device */
rc = -ENOTTY;
Expand All @@ -561,7 +561,7 @@ static int efi_block_boot_image ( struct san_device *sandev, EFI_HANDLE handle,
sandev->drive, efi_devpath_text ( path.path ) );

/* Construct device path for boot image */
end = efi_devpath_end ( path.path );
end = efi_path_end ( path.path );
prefix_len = ( ( ( void * ) end ) - ( ( void * ) path.path ) );
filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
( filename ?
Expand Down
4 changes: 2 additions & 2 deletions src/interface/efi/efi_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/base16.h>
#include <ipxe/vsprintf.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/Protocol/ComponentName.h>
#include <ipxe/efi/Protocol/ComponentName2.h>
#include <ipxe/efi/Protocol/DevicePathToText.h>
Expand Down Expand Up @@ -378,7 +378,7 @@ efi_devpath_text ( EFI_DEVICE_PATH_PROTOCOL *path ) {
/* If we have no DevicePathToText protocol then use a raw hex string */
if ( ! efidpt ) {
DBG ( "[No DevicePathToText]" );
len = efi_devpath_len ( path );
len = efi_path_len ( path );
base16_encode ( path, len, text, sizeof ( text ) );
return text;
}
Expand Down
4 changes: 2 additions & 2 deletions src/interface/efi/efi_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/efi/Protocol/ComponentName2.h>
#include <ipxe/efi/Protocol/DevicePath.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_driver.h>

/** @file
Expand Down Expand Up @@ -202,7 +202,7 @@ efi_driver_start ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
efi_handle_name ( device ), strerror ( rc ) );
goto err_open_path;
}
path_len = ( efi_devpath_len ( path.path ) + sizeof ( *path_end ) );
path_len = ( efi_path_len ( path.path ) + sizeof ( *path_end ) );

/* Allocate and initialise structure */
efidev = zalloc ( sizeof ( *efidev ) + path_len );
Expand Down
4 changes: 2 additions & 2 deletions src/interface/efi/efi_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/rotate.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/Protocol/LoadedImage.h>

/** Image handle passed to entry point */
Expand Down Expand Up @@ -252,7 +252,7 @@ EFI_STATUS efi_init ( EFI_HANDLE image_handle,
* path, since the device handle itself may become invalidated
* when we load our own drivers.
*/
device_path_len = ( efi_devpath_len ( device_path ) +
device_path_len = ( efi_path_len ( device_path ) +
sizeof ( EFI_DEVICE_PATH_PROTOCOL ) );
if ( ( efirc = bs->AllocatePool ( EfiBootServicesData, device_path_len,
&device_path_copy ) ) != 0 ) {
Expand Down
4 changes: 2 additions & 2 deletions src/interface/efi/efi_local.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/process.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/Protocol/SimpleFileSystem.h>
#include <ipxe/efi/Guid/FileInfo.h>
#include <ipxe/efi/Guid/FileSystemInfo.h>
Expand Down Expand Up @@ -425,7 +425,7 @@ static int efi_local_open_resolved ( struct efi_local *local,
static int efi_local_open_path ( struct efi_local *local, const char *path ) {
FILEPATH_DEVICE_PATH *fp = container_of ( efi_loaded_image->FilePath,
FILEPATH_DEVICE_PATH, Header);
size_t fp_len = ( fp ? efi_devpath_len ( &fp->Header ) : 0 );
size_t fp_len = ( fp ? efi_path_len ( &fp->Header ) : 0 );
char base[ fp_len / 2 /* Cannot exceed this length */ ];
size_t remaining = sizeof ( base );
size_t len;
Expand Down
57 changes: 57 additions & 0 deletions src/interface/efi/efi_path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2020 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_path.h>

/** @file
*
* EFI device paths
*
*/

/**
* Find end of device path
*
* @v path Path to device
* @ret path_end End of device path
*/
EFI_DEVICE_PATH_PROTOCOL * efi_path_end ( EFI_DEVICE_PATH_PROTOCOL *path ) {

while ( path->Type != END_DEVICE_PATH_TYPE ) {
path = ( ( ( void * ) path ) +
/* There's this amazing new-fangled thing known as
* a UINT16, but who wants to use one of those? */
( ( path->Length[1] << 8 ) | path->Length[0] ) );
}

return path;
}

/**
* Find length of device path (excluding terminator)
*
* @v path Path to device
* @ret path_len Length of device path
*/
size_t efi_path_len ( EFI_DEVICE_PATH_PROTOCOL *path ) {
EFI_DEVICE_PATH_PROTOCOL *end = efi_path_end ( path );

return ( ( ( void * ) end ) - ( ( void * ) path ) );
}
3 changes: 2 additions & 1 deletion src/interface/efi/efi_snp.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_watchdog.h>
#include <ipxe/efi/efi_snp.h>
Expand Down Expand Up @@ -1714,7 +1715,7 @@ static int efi_snp_probe ( struct net_device *netdev ) {
"%s", netdev->name );

/* Allocate the new device path */
path_prefix_len = efi_devpath_len ( efidev->path );
path_prefix_len = efi_path_len ( efidev->path );
snpdev->path = zalloc ( path_prefix_len + sizeof ( *macpath ) +
sizeof ( *vlanpath ) + sizeof ( *path_end ) );
if ( ! snpdev->path ) {
Expand Down
3 changes: 2 additions & 1 deletion src/interface/efi/efi_snp_hii.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/efi/efi_hii.h>
#include <ipxe/efi/efi_snp.h>
#include <ipxe/efi/efi_strings.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_utils.h>
#include <config/branding.h>

Expand Down Expand Up @@ -680,7 +681,7 @@ int efi_snp_hii_install ( struct efi_snp_device *snpdev ) {
}

/* Allocate the new device path */
path_prefix_len = efi_devpath_len ( snpdev->path );
path_prefix_len = efi_path_len ( snpdev->path );
snpdev->hii_child_path = zalloc ( path_prefix_len +
sizeof ( *vendor_path ) +
sizeof ( *path_end ) );
Expand Down
4 changes: 2 additions & 2 deletions src/interface/efi/efi_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <errno.h>
#include <assert.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/efi_path.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_usb.h>
#include <ipxe/usb.h>
Expand Down Expand Up @@ -1120,7 +1120,7 @@ static int efi_usb_install ( struct efi_usb_device *usbdev,

/* Calculate device path length */
path_count = ( usb_depth ( usbdev->usb ) + 1 );
path_prefix_len = efi_devpath_len ( efidev->path );
path_prefix_len = efi_path_len ( efidev->path );
path_len = ( path_prefix_len + ( path_count * sizeof ( *usbpath ) ) +
sizeof ( *path_end ) );

Expand Down
30 changes: 0 additions & 30 deletions src/interface/efi/efi_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/

/**
* Find end of device path
*
* @v path Path to device
* @ret path_end End of device path
*/
EFI_DEVICE_PATH_PROTOCOL * efi_devpath_end ( EFI_DEVICE_PATH_PROTOCOL *path ) {

while ( path->Type != END_DEVICE_PATH_TYPE ) {
path = ( ( ( void * ) path ) +
/* There's this amazing new-fangled thing known as
* a UINT16, but who wants to use one of those? */
( ( path->Length[1] << 8 ) | path->Length[0] ) );
}

return path;
}

/**
* Find length of device path (excluding terminator)
*
* @v path Path to device
* @ret path_len Length of device path
*/
size_t efi_devpath_len ( EFI_DEVICE_PATH_PROTOCOL *path ) {
EFI_DEVICE_PATH_PROTOCOL *end = efi_devpath_end ( path );

return ( ( ( void * ) end ) - ( ( void * ) path ) );
}

/**
* Locate parent device supporting a given protocol
*
Expand Down

0 comments on commit 2bf0fd3

Please sign in to comment.