]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[cpio] Split out bzImage initrd CPIO header construction
authorMichael Brown <mcb30@ipxe.org>
Mon, 17 May 2021 13:57:48 +0000 (14:57 +0100)
committerMichael Brown <mcb30@ipxe.org>
Fri, 21 May 2021 14:19:38 +0000 (15:19 +0100)
iPXE will construct CPIO headers for images that have a non-empty
command line, thereby allowing raw images (without CPIO headers) to be
injected into a dynamically constructed initrd.  This feature is
currently implemented within the BIOS-only bzImage format support.

Split out the CPIO header construction logic to allow for reuse in
other contexts such as in a UEFI build.

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

index 51498bf956a1bea7f3f02c90eb0a47723b6038ef..a782127a1c40098dc716fb3adc5771ffc2c540cd 100644 (file)
@@ -326,32 +326,6 @@ static void bzimage_set_cmdline ( struct image *image,
        DBGC ( image, "bzImage %p command line \"%s\"\n", image, cmdline );
 }
 
-/**
- * Parse standalone image command line for cpio parameters
- *
- * @v image            bzImage file
- * @v cpio             CPIO header
- * @v cmdline          Command line
- */
-static void bzimage_parse_cpio_cmdline ( struct image *image,
-                                        struct cpio_header *cpio,
-                                        const char *cmdline ) {
-       char *arg;
-       char *end;
-       unsigned int mode;
-
-       /* Look for "mode=" */
-       if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
-               arg += 5;
-               mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
-               if ( *end && ( *end != ' ' ) ) {
-                       DBGC ( image, "bzImage %p strange \"mode=\""
-                              "terminator '%c'\n", image, *end );
-               }
-               cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
-       }
-}
-
 /**
  * Align initrd length
  *
@@ -374,11 +348,9 @@ static inline size_t bzimage_align ( size_t len ) {
 static size_t bzimage_load_initrd ( struct image *image,
                                    struct image *initrd,
                                    userptr_t address ) {
-       char *filename = initrd->cmdline;
-       char *cmdline;
+       const char *filename = cpio_name ( initrd );
        struct cpio_header cpio;
        size_t offset;
-       size_t name_len;
        size_t pad_len;
 
        /* Do not include kernel image itself as an initrd */
@@ -386,25 +358,7 @@ static size_t bzimage_load_initrd ( struct image *image,
                return 0;
 
        /* Create cpio header for non-prebuilt images */
-       if ( filename && filename[0] ) {
-               cmdline = strchr ( filename, ' ' );
-               name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
-                              : strlen ( filename ) ) + 1 /* NUL */ );
-               memset ( &cpio, '0', sizeof ( cpio ) );
-               memcpy ( cpio.c_magic, CPIO_MAGIC, sizeof ( cpio.c_magic ) );
-               cpio_set_field ( cpio.c_mode, 0100644 );
-               cpio_set_field ( cpio.c_nlink, 1 );
-               cpio_set_field ( cpio.c_filesize, initrd->len );
-               cpio_set_field ( cpio.c_namesize, name_len );
-               if ( cmdline ) {
-                       bzimage_parse_cpio_cmdline ( image, &cpio,
-                                                    ( cmdline + 1 /* ' ' */ ));
-               }
-               offset = ( ( sizeof ( cpio ) + name_len + 0x03 ) & ~0x03 );
-       } else {
-               offset = 0;
-               name_len = 0;
-       }
+       offset = cpio_header ( initrd, &cpio );
 
        /* Copy in initrd image body (and cpio header if applicable) */
        if ( address ) {
@@ -413,7 +367,7 @@ static size_t bzimage_load_initrd ( struct image *image,
                        memset_user ( address, 0, 0, offset );
                        copy_to_user ( address, 0, &cpio, sizeof ( cpio ) );
                        copy_to_user ( address, sizeof ( cpio ), filename,
-                                      ( name_len - 1 /* NUL (or space) */ ) );
+                                      cpio_name_len ( initrd ) );
                }
                DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
                       "%s%s\n", image, initrd, user_to_phys ( address, 0 ),
index 49b959a91b42f0251f01b3df6135b541f3876d8a..d7b1f57738de8e32a6b5e9b3394aeeeb7c997881 100644 (file)
@@ -29,6 +29,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #include <ipxe/uaccess.h>
 #include <ipxe/init.h>
 #include <ipxe/memblock.h>
+#include <ipxe/cpio.h>
 
 /** @file
  *
index ddb3e5a455882a528e11dcf559c6df6a06e77f13..2fb9d3d3affdc4f691047dff7651658b602ed998 100644 (file)
@@ -11,13 +11,6 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
 #include <ipxe/uaccess.h>
 
-/** Minimum alignment for initrds
- *
- * Some versions of Linux complain about initrds that are not
- * page-aligned.
- */
-#define INITRD_ALIGN 4096
-
 /** Minimum free space required to reshuffle initrds
  *
  * Chosen to avoid absurdly long reshuffling times
index 080c72daf7d1f9d5c2f4d8cb5902b857a194c6ac..27aee75817c4707ad1a9e77c1bb86742821565d8 100644 (file)
@@ -30,6 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <ipxe/cpio.h>
 
@@ -45,3 +46,87 @@ void cpio_set_field ( char *field, unsigned long value ) {
        snprintf ( buf, sizeof ( buf ), "%08lx", value );
        memcpy ( field, buf, 8 );
 }
+
+/**
+ * Get CPIO image filename
+ *
+ * @v image            Image
+ * @ret len            CPIO filename length (0 for no filename)
+ */
+size_t cpio_name_len ( struct image *image ) {
+       const char *name = cpio_name ( image );
+       char *sep;
+       size_t len;
+
+       /* Check for existence of CPIO filename */
+       if ( ! name )
+               return 0;
+
+       /* Locate separator (if any) */
+       sep = strchr ( name, ' ' );
+       len = ( sep ? ( ( size_t ) ( sep - name ) ) : strlen ( name ) );
+
+       return len;
+}
+
+/**
+ * Parse CPIO image parameters
+ *
+ * @v image            Image
+ * @v cpio             CPIO header to fill in
+ */
+static void cpio_parse_cmdline ( struct image *image,
+                                struct cpio_header *cpio ) {
+       const char *cmdline;
+       char *arg;
+       char *end;
+       unsigned int mode;
+
+       /* Skip image filename */
+       cmdline = ( cpio_name ( image ) + cpio_name_len ( image ) );
+
+       /* Look for "mode=" */
+       if ( ( arg = strstr ( cmdline, "mode=" ) ) ) {
+               arg += 5;
+               mode = strtoul ( arg, &end, 8 /* Octal for file mode */ );
+               if ( *end && ( *end != ' ' ) ) {
+                       DBGC ( image, "CPIO %p strange \"mode=\" "
+                              "terminator '%c'\n", image, *end );
+               }
+               cpio_set_field ( cpio->c_mode, ( 0100000 | mode ) );
+       }
+}
+
+/**
+ * Construct CPIO header for image, if applicable
+ *
+ * @v image            Image
+ * @v cpio             CPIO header to fill in
+ * @ret len            Length of magic CPIO header (including filename)
+ */
+size_t cpio_header ( struct image *image, struct cpio_header *cpio ) {
+       size_t name_len;
+       size_t len;
+
+       /* Get filename length */
+       name_len = cpio_name_len ( image );
+
+       /* Images with no filename are assumed to already be CPIO archives */
+       if ( ! name_len )
+               return 0;
+
+       /* Construct CPIO header */
+       memset ( cpio, '0', sizeof ( *cpio ) );
+       memcpy ( cpio->c_magic, CPIO_MAGIC, sizeof ( cpio->c_magic ) );
+       cpio_set_field ( cpio->c_mode, 0100644 );
+       cpio_set_field ( cpio->c_nlink, 1 );
+       cpio_set_field ( cpio->c_filesize, image->len );
+       cpio_set_field ( cpio->c_namesize, ( name_len + 1 /* NUL */ ) );
+       cpio_parse_cmdline ( image, cpio );
+
+       /* Calculate total length */
+       len = ( ( sizeof ( *cpio ) + name_len + 1 /* NUL */ + CPIO_ALIGN - 1 )
+               & ~( CPIO_ALIGN - 1 ) );
+
+       return len;
+}
index 0637c531d03c68257e6796b54a0db86676459649..9c5e22d5af687da92a034cae382b40b35f8f4a44 100644 (file)
@@ -9,6 +9,8 @@
 
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
+#include <ipxe/image.h>
+
 /** A CPIO archive header
  *
  * All field are hexadecimal ASCII numbers padded with '0' on the
@@ -48,6 +50,25 @@ struct cpio_header {
 /** CPIO magic */
 #define CPIO_MAGIC "070701"
 
+/** CPIO header length alignment */
+#define CPIO_ALIGN 4
+
+/** Alignment for CPIO archives within an initrd */
+#define INITRD_ALIGN 4096
+
+/**
+ * Get CPIO image name
+ *
+ * @v image            Image
+ * @ret name           Image name (not NUL terminated)
+ */
+static inline __attribute__ (( always_inline )) const char *
+cpio_name ( struct image *image ) {
+       return image->cmdline;
+}
+
 extern void cpio_set_field ( char *field, unsigned long value );
+extern size_t cpio_name_len ( struct image *image );
+extern size_t cpio_header ( struct image *image, struct cpio_header *cpio );
 
 #endif /* _IPXE_CPIO_H */