]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[image] Provide image_set_data()
authorMichael Brown <mcb30@ipxe.org>
Wed, 20 Jan 2021 18:03:16 +0000 (18:03 +0000)
committerMichael Brown <mcb30@ipxe.org>
Fri, 22 Jan 2021 18:34:47 +0000 (18:34 +0000)
Extract part of the logic in initrd_init() to a standalone function
image_set_data().

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

index f96b23af4d7a26fd0eca5c296a4c7460ead1dd67..4de3bfafe261d1525d53044ec62bc21960207f71 100644 (file)
@@ -38,7 +38,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #include <ipxe/init.h>
 #include <ipxe/image.h>
 #include <ipxe/script.h>
-#include <ipxe/umalloc.h>
 #include <realmode.h>
 
 /** Command line physical address
@@ -202,23 +201,21 @@ static int initrd_init ( void ) {
                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;
        }
 
-       /* Allocate and copy initrd content */
-       image->data = umalloc ( initrd_len );
-       if ( ! image->data ) {
-               DBGC ( colour, "RUNTIME could not allocate %d bytes for "
-                      "initrd\n", initrd_len );
-               rc = -ENOMEM;
-               goto err_umalloc;
+       /* 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;
        }
-       image->len = initrd_len;
-       memcpy_user ( image->data, 0, phys_to_user ( initrd_phys ), 0,
-                     initrd_len );
 
        /* Mark initrd as consumed */
        initrd_phys = 0;
@@ -236,7 +233,7 @@ static int initrd_init ( void ) {
        return 0;
 
  err_register_image:
- err_umalloc:
+ err_set_data:
  err_set_name:
        image_put ( image );
  err_alloc_image:
index 078ce1bb9a96a7cab1bdd7f11969dc82d486a38f..54b9980250ba0867aa361176842b3e04739ae33d 100644 (file)
@@ -175,6 +175,30 @@ int image_set_cmdline ( struct image *image, const char *cmdline ) {
        return 0;
 }
 
+/**
+ * Set image data
+ *
+ * @v image            Image
+ * @v data             Image data
+ * @v len              Length of image data
+ * @ret rc             Return status code
+ */
+int image_set_data ( struct image *image, userptr_t data, size_t len ) {
+       userptr_t new;
+
+       /* (Re)allocate image data */
+       new = urealloc ( image->data, len );
+       if ( ! new )
+               return -ENOMEM;
+       image->data = new;
+
+       /* Copy in new image data */
+       memcpy_user ( image->data, 0, data, 0, len );
+       image->len = len;
+
+       return 0;
+}
+
 /**
  * Determine image type
  *
index 2e7eb4cee39698b1af44ef1773bf26948e9b6d33..4c38776072f1b7e47a17b3454198066714cc0d30 100644 (file)
@@ -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_data ( struct image *image, userptr_t data, size_t len );
 extern int register_image ( struct image *image );
 extern void unregister_image ( struct image *image );
 struct image * find_image ( const char *name );