Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrod Johnson committed Sep 20, 2011
2 parents 88258cb + 84f57df commit 66d0d22
Show file tree
Hide file tree
Showing 17 changed files with 681 additions and 69 deletions.
12 changes: 6 additions & 6 deletions src/config/general.h
Expand Up @@ -31,7 +31,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
* Timer configuration
*
*/
#define BANNER_TIMEOUT 20 /* Tenths of a second for which the shell
#define BANNER_TIMEOUT 0 /* Tenths of a second for which the shell
banner should appear */

/*
Expand All @@ -56,8 +56,8 @@ FILE_LICENCE ( GPL2_OR_LATER );

#define DOWNLOAD_PROTO_TFTP /* Trivial File Transfer Protocol */
#define DOWNLOAD_PROTO_HTTP /* Hypertext Transfer Protocol */
#undef DOWNLOAD_PROTO_HTTPS /* Secure Hypertext Transfer Protocol */
#define DOWNLOAD_PROTO_FTP /* File Transfer Protocol */
#define DOWNLOAD_PROTO_HTTPS /* Secure Hypertext Transfer Protocol */
#undef DOWNLOAD_PROTO_FTP /* File Transfer Protocol */
#undef DOWNLOAD_PROTO_TFTM /* Multicast Trivial File Transfer Protocol */
#undef DOWNLOAD_PROTO_SLAM /* Scalable Local Area Multicast */

Expand Down Expand Up @@ -99,9 +99,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
#undef IMAGE_MULTIBOOT /* MultiBoot image support */
#undef IMAGE_AOUT /* a.out image support */
#undef IMAGE_WINCE /* WinCE image support */
#define IMAGE_PXE /* PXE image support */
#define IMAGE_SCRIPT /* iPXE script image support */
#define IMAGE_BZIMAGE /* Linux bzImage image support */
//#define IMAGE_PXE /* PXE image support */
//#define IMAGE_SCRIPT /* iPXE script image support */
//#define IMAGE_BZIMAGE /* Linux bzImage image support */
#undef IMAGE_COMBOOT /* SYSLINUX COMBOOT image support */
//#define IMAGE_EFI /* EFI image support */

Expand Down
75 changes: 48 additions & 27 deletions src/crypto/asn1.c
Expand Up @@ -29,6 +29,20 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/

/* Disambiguate the various error causes */
#define EINVAL_ASN1_EMPTY \
__einfo_error ( EINFO_EINVAL_ASN1_EMPTY )
#define EINFO_EINVAL_ASN1_EMPTY \
__einfo_uniqify ( EINFO_EINVAL, 0x01, "Empty or underlength cursor" )
#define EINVAL_ASN1_LEN_LEN \
__einfo_error ( EINFO_EINVAL_ASN1_LEN_LEN )
#define EINFO_EINVAL_ASN1_LEN_LEN \
__einfo_uniqify ( EINFO_EINVAL, 0x02, "Length field overruns cursor" )
#define EINVAL_ASN1_LEN \
__einfo_error ( EINFO_EINVAL_ASN1_LEN )
#define EINFO_EINVAL_ASN1_LEN \
__einfo_uniqify ( EINFO_EINVAL, 0x03, "Field overruns cursor" )

/**
* Start parsing ASN.1 object
*
Expand All @@ -40,32 +54,23 @@ FILE_LICENCE ( GPL2_OR_LATER );
* object body (i.e. the first byte following the length byte(s)), and
* the length of the object body (i.e. the number of bytes until the
* following object tag, if any) is returned.
*
* If any error occurs (i.e. if the object is not of the expected
* type, or if we overflow beyond the end of the ASN.1 object), then
* the cursor will be invalidated and a negative value will be
* returned.
*/
static int asn1_start ( struct asn1_cursor *cursor,
unsigned int type ) {
static int asn1_start ( struct asn1_cursor *cursor, unsigned int type ) {
unsigned int len_len;
unsigned int len;
int rc;

/* Sanity check */
if ( cursor->len < 2 /* Tag byte and first length byte */ ) {
if ( cursor->len )
DBGC ( cursor, "ASN1 %p too short\n", cursor );
rc = -EINVAL;
goto notfound;
return -EINVAL_ASN1_EMPTY;
}

/* Check the tag byte */
if ( *( ( uint8_t * ) cursor->data ) != type ) {
DBGC ( cursor, "ASN1 %p type mismatch (expected %d, got %d)\n",
cursor, type, *( ( uint8_t * ) cursor->data ) );
rc = -ENXIO;
goto notfound;
return -ENXIO;
}
cursor->data++;
cursor->len--;
Expand All @@ -82,8 +87,7 @@ static int asn1_start ( struct asn1_cursor *cursor,
if ( cursor->len < len_len ) {
DBGC ( cursor, "ASN1 %p bad length field length %d (max "
"%zd)\n", cursor, len_len, cursor->len );
rc = -EINVAL;
goto notfound;
return -EINVAL_ASN1_LEN_LEN;
}

/* Extract the length and sanity check */
Expand All @@ -96,16 +100,10 @@ static int asn1_start ( struct asn1_cursor *cursor,
if ( cursor->len < len ) {
DBGC ( cursor, "ASN1 %p bad length %d (max %zd)\n",
cursor, len, cursor->len );
rc = -EINVAL;
goto notfound;
return -EINVAL_ASN1_LEN;
}

return len;

notfound:
cursor->data = NULL;
cursor->len = 0;
return rc;
}

/**
Expand All @@ -123,8 +121,10 @@ int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) {
int len;

len = asn1_start ( cursor, type );
if ( len < 0 )
if ( len < 0 ) {
asn1_invalidate_cursor ( cursor );
return len;
}

cursor->len = len;
DBGC ( cursor, "ASN1 %p entered object type %02x (len %x)\n",
Expand All @@ -134,17 +134,17 @@ int asn1_enter ( struct asn1_cursor *cursor, unsigned int type ) {
}

/**
* Skip ASN.1 object
* Skip ASN.1 object if present
*
* @v cursor ASN.1 object cursor
* @v type Expected type
* @ret rc Return status code
*
* The object cursor will be updated to point to the next ASN.1
* object. If any error occurs, the object cursor will be
* invalidated.
* object. If any error occurs, the object cursor will not be
* modified.
*/
int asn1_skip ( struct asn1_cursor *cursor, unsigned int type ) {
int asn1_skip_if_exists ( struct asn1_cursor *cursor, unsigned int type ) {
int len;

len = asn1_start ( cursor, type );
Expand All @@ -158,9 +158,30 @@ int asn1_skip ( struct asn1_cursor *cursor, unsigned int type ) {

if ( ! cursor->len ) {
DBGC ( cursor, "ASN1 %p reached end of object\n", cursor );
cursor->data = NULL;
return -ENOENT;
}

return 0;
}

/**
* Skip ASN.1 object
*
* @v cursor ASN.1 object cursor
* @v type Expected type
* @ret rc Return status code
*
* The object cursor will be updated to point to the next ASN.1
* object. If any error occurs, the object cursor will be
* invalidated.
*/
int asn1_skip ( struct asn1_cursor *cursor, unsigned int type ) {
int rc;

if ( ( rc = asn1_skip_if_exists ( cursor, type ) ) < 0 ) {
asn1_invalidate_cursor ( cursor );
return rc;
}

return 0;
}
2 changes: 1 addition & 1 deletion src/crypto/x509.c
Expand Up @@ -55,7 +55,7 @@ static int x509_public_key ( const struct asn1_cursor *certificate,
memcpy ( &cursor, certificate, sizeof ( cursor ) );
rc = ( asn1_enter ( &cursor, ASN1_SEQUENCE ), /* Certificate */
asn1_enter ( &cursor, ASN1_SEQUENCE ), /* tbsCertificate */
asn1_skip ( &cursor, ASN1_EXPLICIT_TAG ), /* version */
asn1_skip_if_exists ( &cursor, ASN1_EXPLICIT_TAG ), /* version */
asn1_skip ( &cursor, ASN1_INTEGER ), /* serialNumber */
asn1_skip ( &cursor, ASN1_SEQUENCE ), /* signature */
asn1_skip ( &cursor, ASN1_SEQUENCE ), /* issuer */
Expand Down
107 changes: 107 additions & 0 deletions src/image/efi_image.c
Expand Up @@ -19,13 +19,97 @@
FILE_LICENCE ( GPL2_OR_LATER );

#include <errno.h>
#include <stdlib.h>
#include <ipxe/efi/efi.h>
#include <ipxe/image.h>
#include <ipxe/init.h>
#include <ipxe/features.h>
#include <ipxe/uri.h>

FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );

/** EFI loaded image protocol GUID */
static EFI_GUID efi_loaded_image_protocol_guid
= EFI_LOADED_IMAGE_PROTOCOL_GUID;


/**
* Create a Unicode command line for the image
*
* @v image EFI image
* @v devpath_out Device path to pass to image (output)
* @v cmdline_out Unicode command line (output)
* @v cmdline_len_out Length of command line in bytes (output)
* @ret rc Return status code
*/
static int efi_image_make_cmdline ( struct image *image,
EFI_DEVICE_PATH **devpath_out,
VOID **cmdline_out,
UINT32 *cmdline_len_out ) {
char *uri;
size_t uri_len;
FILEPATH_DEVICE_PATH *devpath;
EFI_DEVICE_PATH *endpath;
size_t devpath_len;
CHAR16 *cmdline = NULL;
UINT32 cmdline_len;
size_t args_len = 0;
UINT32 i;

/* Get the URI string of the image */
uri_len = unparse_uri ( NULL, 0, image->uri, URI_ALL ) + 1;

/* Compute final command line length */
if ( image->cmdline != NULL ) {
args_len = strlen ( image->cmdline ) + 1;
}
cmdline_len = args_len + uri_len;

/* Allocate space for the uri, final command line and device path */
cmdline = malloc ( cmdline_len * sizeof ( CHAR16 ) + uri_len
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ uri_len * sizeof ( CHAR16 )
+ sizeof ( EFI_DEVICE_PATH ) );
if ( cmdline == NULL ) {
return -ENOMEM;
}
uri = (char *) ( cmdline + cmdline_len );
devpath = (FILEPATH_DEVICE_PATH *) ( uri + uri_len );
endpath = (EFI_DEVICE_PATH *) ( (char *) devpath
+ SIZE_OF_FILEPATH_DEVICE_PATH
+ uri_len * sizeof ( CHAR16 ) );

/* Build the gPXE device path */
devpath->Header.Type = MEDIA_DEVICE_PATH;
devpath->Header.SubType = MEDIA_FILEPATH_DP;
devpath_len = SIZE_OF_FILEPATH_DEVICE_PATH
+ uri_len * sizeof ( CHAR16 );
devpath->Header.Length[0] = devpath_len & 0xFF;
devpath->Header.Length[1] = devpath_len >> 8;
endpath->Type = END_DEVICE_PATH_TYPE;
endpath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
endpath->Length[0] = 4;
endpath->Length[1] = 0;
unparse_uri ( uri, uri_len, image->uri, URI_ALL );

/* Convert to Unicode */
for ( i = 0; i < uri_len; i++ ) {
cmdline[i] = uri[i];
devpath->PathName[i] = uri[i];
}
if ( image->cmdline ) {
cmdline[uri_len - 1] = ' ';
}
for ( i = 0; i < args_len; i++ ) {
cmdline[i + uri_len] = image->cmdline[i];
}

*devpath_out = &devpath->Header;
*cmdline_out = cmdline;
*cmdline_len_out = cmdline_len * sizeof ( CHAR16 );
return 0;
}

/**
* Execute EFI image
*
Expand All @@ -34,7 +118,10 @@ FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
*/
static int efi_image_exec ( struct image *image ) {
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
EFI_LOADED_IMAGE_PROTOCOL *loaded_image = NULL;
void *loaded_image_void;
EFI_HANDLE handle;
EFI_HANDLE device_handle = NULL;
UINTN exit_data_size;
CHAR16 *exit_data;
EFI_STATUS efirc;
Expand All @@ -50,13 +137,33 @@ static int efi_image_exec ( struct image *image ) {
return -ENOEXEC;
}

/* Get the loaded image protocol for the newly loaded image */
efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid,
&loaded_image_void, efi_image_handle, NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL );
if ( efirc ) {
/* Should never happen */
rc = EFIRC_TO_RC ( efirc );
}
loaded_image = loaded_image_void;

/* Pass an IPXE download protocol to the image */
rc = efi_download_install ( &device_handle );
loaded_image->DeviceHandle = device_handle;
loaded_image->ParentHandle = efi_loaded_image;
rc = efi_image_make_cmdline ( image, &loaded_image->FilePath,&loaded_image->LoadOptions,&loaded_image->LoadOptionsSize );


/* Start the image */
if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
&exit_data ) ) != 0 ) {
DBGC ( image, "EFIIMAGE %p returned with status %s\n",
image, efi_strerror ( efirc ) );
}
rc = EFIRC_TO_RC ( efirc );
if ( device_handle ) {
efi_download_uninstall ( device_handle );
}

/* Unload the image. We can't leave it loaded, because we
* have no "unload" operation.
Expand Down
8 changes: 7 additions & 1 deletion src/image/script.c
Expand Up @@ -221,11 +221,17 @@ static const char *goto_label;
* @ret rc Return status code
*/
static int goto_find_label ( const char *line ) {
size_t len = strlen ( goto_label );

if ( line[0] != ':' )
return -ENOENT;
if ( strcmp ( goto_label, &line[1] ) != 0 )

if ( strncmp ( goto_label, &line[1], len ) != 0 )
return -ENOENT;

if ( line[ 1 + len ] && ! isspace ( line[ 1 + len ] ) )
return -ENOENT;

return 0;
}

Expand Down
12 changes: 12 additions & 0 deletions src/include/ipxe/asn1.h
Expand Up @@ -28,7 +28,19 @@ struct asn1_cursor {
size_t len;
};

/**
* Invalidate ASN.1 object cursor
*
* @v cursor ASN.1 object cursor
*/
static inline __attribute__ (( always_inline )) void
asn1_invalidate_cursor ( struct asn1_cursor *cursor ) {
cursor->len = 0;
}

extern int asn1_enter ( struct asn1_cursor *cursor, unsigned int type );
extern int asn1_skip_if_exists ( struct asn1_cursor *cursor,
unsigned int type );
extern int asn1_skip ( struct asn1_cursor *cursor, unsigned int type );

#endif /* _IPXE_ASN1_H */
9 changes: 6 additions & 3 deletions src/include/ipxe/dhcp.h
Expand Up @@ -660,15 +660,18 @@ struct dhcphdr {
/** Setting block name used for BootServerDHCP responses */
#define PXEBS_SETTINGS_NAME "pxebs"

extern uint32_t dhcp_last_xid;
extern unsigned int dhcp_chaddr ( struct net_device *netdev, void *chaddr,
uint16_t *flags );
extern int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
struct net_device *netdev, uint8_t msgtype,
const void *options, size_t options_len,
void *data, size_t max_len );
uint32_t xid, const void *options,
size_t options_len, void *data,
size_t max_len );
extern int dhcp_create_request ( struct dhcp_packet *dhcppkt,
struct net_device *netdev,
unsigned int msgtype, struct in_addr ciaddr,
unsigned int msgtype, uint32_t xid,
struct in_addr ciaddr,
void *data, size_t max_len );
extern int start_dhcp ( struct interface *job, struct net_device *netdev );
extern int start_pxebs ( struct interface *job, struct net_device *netdev,
Expand Down

0 comments on commit 66d0d22

Please sign in to comment.