]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[image] Provide image_memory()
authorMichael Brown <mcb30@ipxe.org>
Mon, 25 Jan 2021 16:18:28 +0000 (16:18 +0000)
committerMichael Brown <mcb30@ipxe.org>
Mon, 25 Jan 2021 17:03:56 +0000 (17:03 +0000)
Consolidate the remaining logic common to initrd_init() and imgmem()
into a shared image_memory() function.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/arch/x86/core/runtime.c
src/core/image.c
src/hci/commands/image_mem_cmd.c
src/include/ipxe/image.h
src/include/usr/imgmgmt.h
src/usr/imgmgmt.c

index 4de3bfafe261d1525d53044ec62bc21960207f71..02072b5bf1dde22f9c0b62c7d194476c2556a1a7 100644 (file)
@@ -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 ( "<INITRD>", 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, "<INITRD>" ) ) != 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;
 }
 
 /**
index 54b9980250ba0867aa361176842b3e04739ae33d..9fe77c54c2cef9f2bf919c0e9c7cd76e94c08710 100644 (file)
@@ -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;
+}
index 61d50534d03fe753f31e3b91cb22b072129b14a3..c8bfab1ad67e3600f608ad0b5e609a5d44f32505 100644 (file)
@@ -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;
index 4c38776072f1b7e47a17b3454198066714cc0d30..4fd2700817b3a4edb314558bd9d362c5e837e3e1 100644 (file)
@@ -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 );
index c59cf1a0b778ec55f42cae109b9ebedd72201f6e..14fb7cbc6147e32a5d33cb6a4cc1bd5ef998e05d 100644 (file)
@@ -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 */
index bf4c745eac7de65caefb8838963e788f004f15b4..f8d149153adee531c9b6206e32114212c2cce9b0 100644 (file)
@@ -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;
 }