From de4f31cdcad7c4734c68da828351eeb7afd0360e Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 6 May 2021 13:09:30 +0100 Subject: [PATCH] [image] Provide image_set_len() utility function Signed-off-by: Michael Brown --- src/core/image.c | 31 ++++++++++++++++++++++++------- src/include/ipxe/image.h | 1 + 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/core/image.c b/src/core/image.c index 9fe77c54c..ce8cf868b 100644 --- a/src/core/image.c +++ b/src/core/image.c @@ -175,6 +175,26 @@ int image_set_cmdline ( struct image *image, const char *cmdline ) { return 0; } +/** + * Set image length + * + * @v image Image + * @v len Length of image data + * @ret rc Return status code + */ +int image_set_len ( struct image *image, size_t len ) { + userptr_t new; + + /* (Re)allocate image data */ + new = urealloc ( image->data, len ); + if ( ! new ) + return -ENOMEM; + image->data = new; + image->len = len; + + return 0; +} + /** * Set image data * @@ -184,17 +204,14 @@ int image_set_cmdline ( struct image *image, const char *cmdline ) { * @ret rc Return status code */ int image_set_data ( struct image *image, userptr_t data, size_t len ) { - userptr_t new; + int rc; - /* (Re)allocate image data */ - new = urealloc ( image->data, len ); - if ( ! new ) - return -ENOMEM; - image->data = new; + /* Set image length */ + if ( ( rc = image_set_len ( image, len ) ) != 0 ) + return rc; /* Copy in new image data */ memcpy_user ( image->data, 0, data, 0, len ); - image->len = len; return 0; } diff --git a/src/include/ipxe/image.h b/src/include/ipxe/image.h index 4fd270081..046edf9a5 100644 --- a/src/include/ipxe/image.h +++ b/src/include/ipxe/image.h @@ -175,6 +175,7 @@ extern struct image * alloc_image ( struct uri *uri ); extern int image_set_uri ( struct image *image, struct uri *uri ); extern int image_set_name ( struct image *image, const char *name ); extern int image_set_cmdline ( struct image *image, const char *cmdline ); +extern int image_set_len ( struct image *image, size_t len ); extern int image_set_data ( struct image *image, userptr_t data, size_t len ); extern int register_image ( struct image *image ); extern void unregister_image ( struct image *image );