From b99477b3fa6d4063314a313f62b7ae784bcbe710 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 20 Jan 2021 18:08:04 +0000 Subject: [image] Add the "imgmem" command Provide the "imgmem" command to create an image from an existing block of memory, for debugging purposes only. Signed-off-by: Michael Brown --- src/include/usr/imgmgmt.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/include/usr') diff --git a/src/include/usr/imgmgmt.h b/src/include/usr/imgmgmt.h index 806df0bfb..c59cf1a0b 100644 --- a/src/include/usr/imgmgmt.h +++ b/src/include/usr/imgmgmt.h @@ -18,5 +18,7 @@ extern int imgdownload_string ( const char *uri_string, unsigned long timeout, extern int imgacquire ( const char *name, unsigned long timeout, struct image **image ); extern void imgstat ( struct image *image ); +extern int imgmem ( userptr_t data, size_t len, const char *name, + struct image **image ); #endif /* _USR_IMGMGMT_H */ -- cgit v1.2.3-55-g7522 From 989a7a8032db02eb0524bd78a674d3b087dea3a6 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 25 Jan 2021 16:18:28 +0000 Subject: [image] Provide image_memory() Consolidate the remaining logic common to initrd_init() and imgmem() into a shared image_memory() function. Signed-off-by: Michael Brown --- src/arch/x86/core/runtime.c | 44 +++++----------------------------------- src/core/image.c | 44 ++++++++++++++++++++++++++++++++++++++++ src/hci/commands/image_mem_cmd.c | 4 +--- src/include/ipxe/image.h | 2 ++ src/include/usr/imgmgmt.h | 3 +-- src/usr/imgmgmt.c | 41 ++++++++----------------------------- 6 files changed, 62 insertions(+), 76 deletions(-) (limited to 'src/include/usr') diff --git a/src/arch/x86/core/runtime.c b/src/arch/x86/core/runtime.c index 4de3bfafe..02072b5bf 100644 --- a/src/arch/x86/core/runtime.c +++ b/src/arch/x86/core/runtime.c @@ -179,7 +179,6 @@ static int cmdline_init ( void ) { */ static int initrd_init ( void ) { struct image *image; - int rc; /* Do nothing if no initrd was specified */ if ( ! initrd_phys ) { @@ -193,51 +192,18 @@ static int initrd_init ( void ) { DBGC ( colour, "RUNTIME found initrd at [%x,%x)\n", initrd_phys, ( initrd_phys + initrd_len ) ); - /* Allocate image */ - image = alloc_image ( NULL ); + /* Create initrd image */ + image = image_memory ( "", phys_to_user ( initrd_phys ), + initrd_len ); if ( ! image ) { - DBGC ( colour, "RUNTIME could not allocate image for " - "initrd\n" ); - rc = -ENOMEM; - goto err_alloc_image; - } - - /* Set image name */ - if ( ( rc = image_set_name ( image, "" ) ) != 0 ) { - DBGC ( colour, "RUNTIME could not set image name: %s\n", - strerror ( rc ) ); - goto err_set_name; - } - - /* Set image content */ - if ( ( rc = image_set_data ( image, phys_to_user ( initrd_phys ), - initrd_len ) ) != 0 ) { - DBGC ( colour, "RUNTIME could not set image data: %s\n", - strerror ( rc ) ); - goto err_set_data; + DBGC ( colour, "RUNTIME could not create initrd image\n" ); + return -ENOMEM; } /* Mark initrd as consumed */ initrd_phys = 0; - /* Register image */ - if ( ( rc = register_image ( image ) ) != 0 ) { - DBGC ( colour, "RUNTIME could not register initrd: %s\n", - strerror ( rc ) ); - goto err_register_image; - } - - /* Drop our reference to the image */ - image_put ( image ); - return 0; - - err_register_image: - err_set_data: - err_set_name: - image_put ( image ); - err_alloc_image: - return rc; } /** diff --git a/src/core/image.c b/src/core/image.c index 54b998025..9fe77c54c 100644 --- a/src/core/image.c +++ b/src/core/image.c @@ -505,3 +505,47 @@ int image_set_trust ( int require_trusted, int permanent ) { return 0; } + +/** + * Create registered image from block of memory + * + * @v name Name + * @v data Image data + * @v len Length + * @ret image Image, or NULL on error + */ +struct image * image_memory ( const char *name, userptr_t data, size_t len ) { + struct image *image; + int rc; + + /* Allocate image */ + image = alloc_image ( NULL ); + if ( ! image ) { + rc = -ENOMEM; + goto err_alloc_image; + } + + /* Set name */ + if ( ( rc = image_set_name ( image, name ) ) != 0 ) + goto err_set_name; + + /* Set data */ + if ( ( rc = image_set_data ( image, data, len ) ) != 0 ) + goto err_set_data; + + /* Register image */ + if ( ( rc = register_image ( image ) ) != 0 ) + goto err_register; + + /* Drop local reference to image */ + image_put ( image ); + + return image; + + err_register: + err_set_data: + err_set_name: + image_put ( image ); + err_alloc_image: + return NULL; +} diff --git a/src/hci/commands/image_mem_cmd.c b/src/hci/commands/image_mem_cmd.c index 61d50534d..c8bfab1ad 100644 --- a/src/hci/commands/image_mem_cmd.c +++ b/src/hci/commands/image_mem_cmd.c @@ -60,7 +60,6 @@ static struct command_descriptor imgmem_cmd = */ static int imgmem_exec ( int argc, char **argv ) { struct imgmem_options opts; - struct image *image; unsigned int data; unsigned int len; int rc; @@ -82,8 +81,7 @@ static int imgmem_exec ( int argc, char **argv ) { return rc; /* Create image */ - if ( ( rc = imgmem ( phys_to_user ( data ), len, opts.name, - &image ) ) != 0 ) + if ( ( rc = imgmem ( opts.name, phys_to_user ( data ), len ) ) != 0 ) return rc; return 0; diff --git a/src/include/ipxe/image.h b/src/include/ipxe/image.h index 4c3877607..4fd270081 100644 --- a/src/include/ipxe/image.h +++ b/src/include/ipxe/image.h @@ -184,6 +184,8 @@ extern int image_replace ( struct image *replacement ); extern int image_select ( struct image *image ); extern struct image * image_find_selected ( void ); extern int image_set_trust ( int require_trusted, int permanent ); +extern struct image * image_memory ( const char *name, userptr_t data, + size_t len ); extern int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ); extern int image_asn1 ( struct image *image, size_t offset, struct asn1_cursor **cursor ); diff --git a/src/include/usr/imgmgmt.h b/src/include/usr/imgmgmt.h index c59cf1a0b..14fb7cbc6 100644 --- a/src/include/usr/imgmgmt.h +++ b/src/include/usr/imgmgmt.h @@ -18,7 +18,6 @@ extern int imgdownload_string ( const char *uri_string, unsigned long timeout, extern int imgacquire ( const char *name, unsigned long timeout, struct image **image ); extern void imgstat ( struct image *image ); -extern int imgmem ( userptr_t data, size_t len, const char *name, - struct image **image ); +extern int imgmem ( const char *name, userptr_t data, size_t len ); #endif /* _USR_IMGMGMT_H */ diff --git a/src/usr/imgmgmt.c b/src/usr/imgmgmt.c index bf4c745ea..f8d149153 100644 --- a/src/usr/imgmgmt.c +++ b/src/usr/imgmgmt.c @@ -173,43 +173,20 @@ void imgstat ( struct image *image ) { /** * Create image from block of memory * + * @v name Name * @v data Image data * @v len Length - * @v name Name - * @v image Image to fill in * @ret rc Return status code */ -int imgmem ( userptr_t data, size_t len, const char *name, - struct image **image ) { - int rc; - - /* Allocate image */ - *image = alloc_image ( NULL ); - if ( ! *image ) { - rc = -ENOMEM; - goto err_alloc_image; - } - - /* Set name */ - if ( ( rc = image_set_name ( *image, name ) ) != 0 ) - goto err_set_name; +int imgmem ( const char *name, userptr_t data, size_t len ) { + struct image *image; - /* Set data */ - if ( ( rc = image_set_data ( *image, data, len ) ) != 0 ) { - printf ( "Could not set image data: %s\n", strerror ( rc ) ); - goto err_set_data; - } - - /* Register image */ - if ( ( rc = register_image ( *image ) ) != 0 ) { - printf ( "Could not register image: %s\n", strerror ( rc ) ); - goto err_register_image; + /* Create image */ + image = image_memory ( name, data, len ); + if ( ! image ) { + printf ( "Could not create image\n" ); + return -ENOMEM; } - err_register_image: - err_set_data: - err_set_name: - image_put ( *image ); - err_alloc_image: - return rc; + return 0; } -- cgit v1.2.3-55-g7522 From 42db0bd0417721a026313fc151f36a8ef0bff4e4 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 26 Jan 2021 15:44:59 +0000 Subject: [cmdline] Expose "iflinkwait" as a command Signed-off-by: Michael Brown --- src/hci/commands/ifmgmt_cmd.c | 56 +++++++++++++++++++++++++++++++++++++++++++ src/include/usr/ifmgmt.h | 3 ++- src/usr/ifmgmt.c | 11 +++++---- src/usr/lotest.c | 4 ++-- 4 files changed, 67 insertions(+), 7 deletions(-) (limited to 'src/include/usr') diff --git a/src/hci/commands/ifmgmt_cmd.c b/src/hci/commands/ifmgmt_cmd.c index 2e976d3fb..591cb3da8 100644 --- a/src/hci/commands/ifmgmt_cmd.c +++ b/src/hci/commands/ifmgmt_cmd.c @@ -250,6 +250,58 @@ int ifconf_exec ( int argc, char **argv ) { return ifcommon_exec ( argc, argv, &ifconf_cmd ); } +/** "iflinkwait" option list */ +struct iflinkwait_options { + /** Link timeout */ + unsigned long timeout; +}; + +/** "iflinkwait" option list */ +static struct option_descriptor iflinkwait_opts[] = { + OPTION_DESC ( "timeout", 't', required_argument, + struct iflinkwait_options, timeout, parse_timeout ), +}; + +/** + * "iflinkwait" payload + * + * @v netdev Network device + * @v opts Command options + * @ret rc Return status code + */ +static int iflinkwait_payload ( struct net_device *netdev, + struct iflinkwait_options *opts ) { + int rc; + + /* Wait for link-up */ + if ( ( rc = iflinkwait ( netdev, opts->timeout, 1 ) ) != 0 ) { + + /* Close device on failure, to avoid memory exhaustion */ + netdev_close ( netdev ); + + return rc; + } + + return 0; +} + +/** "iflinkwait" command descriptor */ +static struct ifcommon_command_descriptor iflinkwait_cmd = + IFCOMMON_COMMAND_DESC ( struct iflinkwait_options, iflinkwait_opts, + 0, MAX_ARGUMENTS, "[...]", + iflinkwait_payload, 1 ); + +/** + * The "iflinkwait" command + * + * @v argc Argument count + * @v argv Argument list + * @ret rc Return status code + */ +static int iflinkwait_exec ( int argc, char **argv ) { + return ifcommon_exec ( argc, argv, &iflinkwait_cmd ); +} + /** Interface management commands */ struct command ifmgmt_commands[] __command = { { @@ -268,4 +320,8 @@ struct command ifmgmt_commands[] __command = { .name = "ifconf", .exec = ifconf_exec, }, + { + .name = "iflinkwait", + .exec = iflinkwait_exec, + }, }; diff --git a/src/include/usr/ifmgmt.h b/src/include/usr/ifmgmt.h index 52f88f957..8d8a6bb56 100644 --- a/src/include/usr/ifmgmt.h +++ b/src/include/usr/ifmgmt.h @@ -18,6 +18,7 @@ extern int ifconf ( struct net_device *netdev, unsigned long timeout ); extern void ifclose ( struct net_device *netdev ); extern void ifstat ( struct net_device *netdev ); -extern int iflinkwait ( struct net_device *netdev, unsigned long timeout ); +extern int iflinkwait ( struct net_device *netdev, unsigned long timeout, + int verbose ); #endif /* _USR_IFMGMT_H */ diff --git a/src/usr/ifmgmt.c b/src/usr/ifmgmt.c index f1172bafb..2150bfff7 100644 --- a/src/usr/ifmgmt.c +++ b/src/usr/ifmgmt.c @@ -212,17 +212,20 @@ static int iflinkwait_progress ( struct ifpoller *ifpoller ) { * * @v netdev Network device * @v timeout Timeout period, in ticks + * @v verbose Always display progress message + * @ret rc Return status code */ -int iflinkwait ( struct net_device *netdev, unsigned long timeout ) { +int iflinkwait ( struct net_device *netdev, unsigned long timeout, + int verbose ) { int rc; /* Ensure device is open */ if ( ( rc = ifopen ( netdev ) ) != 0 ) return rc; - /* Return immediately if link is already up */ + /* Return immediately if link is already up, unless being verbose */ netdev_poll ( netdev ); - if ( netdev_link_ok ( netdev ) ) + if ( netdev_link_ok ( netdev ) && ( ! verbose ) ) return 0; /* Wait for link-up */ @@ -273,7 +276,7 @@ int ifconf ( struct net_device *netdev, int rc; /* Ensure device is open and link is up */ - if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT ) ) != 0 ) + if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT, 0 ) ) != 0 ) return rc; /* Start configuration */ diff --git a/src/usr/lotest.c b/src/usr/lotest.c index 6b75b5048..5b88ef27e 100644 --- a/src/usr/lotest.c +++ b/src/usr/lotest.c @@ -208,9 +208,9 @@ int loopback_test ( struct net_device *sender, struct net_device *receiver, return rc; /* Wait for link-up */ - if ( ( rc = iflinkwait ( sender, 0 ) ) != 0 ) + if ( ( rc = iflinkwait ( sender, 0, 0 ) ) != 0 ) return rc; - if ( ( rc = iflinkwait ( receiver, 0 ) ) != 0 ) + if ( ( rc = iflinkwait ( receiver, 0, 0 ) ) != 0 ) return rc; /* Allocate data buffer */ -- cgit v1.2.3-55-g7522