[initrd] Rename bzimage_align() to initrd_align()

Alignment of initrd lengths is applicable to all Linux kernels, not
just those in the x86 bzImage format.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2025-05-22 13:41:21 +01:00
parent 9231d8c952
commit 029c7c4178
5 changed files with 24 additions and 30 deletions

View File

@@ -316,17 +316,6 @@ static void bzimage_set_cmdline ( struct image *image,
image->name, rm_cmdline ); image->name, rm_cmdline );
} }
/**
* Align initrd length
*
* @v len Length
* @ret len Length rounded up to INITRD_ALIGN
*/
static inline size_t bzimage_align ( size_t len ) {
return ( ( len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) );
}
/** /**
* Load initrd * Load initrd
* *
@@ -407,7 +396,7 @@ static int bzimage_check_initrds ( struct image *image,
/* Calculate length */ /* Calculate length */
len += bzimage_load_initrd ( image, initrd, NULL ); len += bzimage_load_initrd ( image, initrd, NULL );
len = bzimage_align ( len ); len = initrd_align ( len );
DBGC ( image, "bzImage %s initrd %s from [%#08lx,%#08lx)%s%s\n", DBGC ( image, "bzImage %s initrd %s from [%#08lx,%#08lx)%s%s\n",
image->name, initrd->name, virt_to_phys ( initrd->data ), image->name, initrd->name, virt_to_phys ( initrd->data ),
@@ -467,7 +456,7 @@ static void bzimage_load_initrds ( struct image *image,
for_each_image ( initrd ) { for_each_image ( initrd ) {
if ( virt_to_phys ( initrd->data ) >= top ) { if ( virt_to_phys ( initrd->data ) >= top ) {
top = ( virt_to_phys ( initrd->data ) + top = ( virt_to_phys ( initrd->data ) +
bzimage_align ( initrd->len ) ); initrd_align ( initrd->len ) );
} }
} }
@@ -491,7 +480,7 @@ static void bzimage_load_initrds ( struct image *image,
if ( other == initrd ) if ( other == initrd )
offset = 0; offset = 0;
offset += bzimage_load_initrd ( image, other, NULL ); offset += bzimage_load_initrd ( image, other, NULL );
offset = bzimage_align ( offset ); offset = initrd_align ( offset );
} }
/* Load initrd at this address */ /* Load initrd at this address */

View File

@@ -51,7 +51,6 @@ static void initrd_squash_high ( physaddr_t top ) {
struct image *initrd; struct image *initrd;
struct image *highest; struct image *highest;
void *data; void *data;
size_t len;
/* Squash up any initrds already within or below the region */ /* Squash up any initrds already within or below the region */
while ( 1 ) { while ( 1 ) {
@@ -70,9 +69,7 @@ static void initrd_squash_high ( physaddr_t top ) {
break; break;
/* Move this image to its final position */ /* Move this image to its final position */
len = ( ( highest->len + INITRD_ALIGN - 1 ) & current -= initrd_align ( highest->len );
~( INITRD_ALIGN - 1 ) );
current -= len;
DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->" DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->"
"[%#08lx,%#08lx)\n", highest->name, "[%#08lx,%#08lx)\n", highest->name,
virt_to_phys ( highest->data ), virt_to_phys ( highest->data ),
@@ -86,9 +83,7 @@ static void initrd_squash_high ( physaddr_t top ) {
/* Copy any remaining initrds (e.g. embedded images) to the region */ /* Copy any remaining initrds (e.g. embedded images) to the region */
for_each_image ( initrd ) { for_each_image ( initrd ) {
if ( virt_to_phys ( initrd->data ) >= top ) { if ( virt_to_phys ( initrd->data ) >= top ) {
len = ( ( initrd->len + INITRD_ALIGN - 1 ) & current -= initrd_align ( initrd->len );
~( INITRD_ALIGN - 1 ) );
current -= len;
DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->" DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->"
"[%#08lx,%#08lx)\n", initrd->name, "[%#08lx,%#08lx)\n", initrd->name,
virt_to_phys ( initrd->data ), virt_to_phys ( initrd->data ),
@@ -139,8 +134,8 @@ static void initrd_swap ( struct image *low, struct image *high ) {
( virt_to_phys ( high->data ) + high->len ), high->name ); ( virt_to_phys ( high->data ) + high->len ), high->name );
/* Calculate padded lengths */ /* Calculate padded lengths */
low_len = ( ( low->len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) ); low_len = initrd_align ( low->len );
high_len = ( ( high->len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 )); high_len = initrd_align ( high->len );
len = ( low_len + high_len ); len = ( low_len + high_len );
data = low->rwdata; data = low->rwdata;
assert ( high->data == ( data + low_len ) ); assert ( high->data == ( data + low_len ) );
@@ -165,15 +160,12 @@ static int initrd_swap_any ( void ) {
struct image *low; struct image *low;
struct image *high; struct image *high;
const void *adjacent; const void *adjacent;
size_t padded_len;
/* Find any pair of initrds that can be swapped */ /* Find any pair of initrds that can be swapped */
for_each_image ( low ) { for_each_image ( low ) {
/* Calculate location of adjacent image (if any) */ /* Calculate location of adjacent image (if any) */
padded_len = ( ( low->len + INITRD_ALIGN - 1 ) & adjacent = ( low->data + initrd_align ( low->len ) );
~( INITRD_ALIGN - 1 ) );
adjacent = ( low->data + padded_len );
/* Search for adjacent image */ /* Search for adjacent image */
for_each_image ( high ) { for_each_image ( high ) {

View File

@@ -60,9 +60,6 @@ struct cpio_header {
/** CPIO header length alignment */ /** CPIO header length alignment */
#define CPIO_ALIGN 4 #define CPIO_ALIGN 4
/** Alignment for CPIO archives within an initrd */
#define INITRD_ALIGN 4096
/** /**
* Get CPIO image name * Get CPIO image name
* *

View File

@@ -14,4 +14,19 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
extern void initrd_reshuffle ( physaddr_t bottom ); extern void initrd_reshuffle ( physaddr_t bottom );
extern int initrd_reshuffle_check ( size_t len, physaddr_t bottom ); extern int initrd_reshuffle_check ( size_t len, physaddr_t bottom );
/** Initial ramdisk chunk alignment */
#define INITRD_ALIGN 4096
/**
* Align initrd length
*
* @v len Length
* @ret len Aligned length
*/
static inline __attribute__ (( always_inline )) size_t
initrd_align ( size_t len ) {
return ( ( len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) );
}
#endif /* _IPXE_INITRD_H */ #endif /* _IPXE_INITRD_H */

View File

@@ -39,6 +39,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <wchar.h> #include <wchar.h>
#include <ipxe/image.h> #include <ipxe/image.h>
#include <ipxe/cpio.h> #include <ipxe/cpio.h>
#include <ipxe/initrd.h>
#include <ipxe/efi/efi.h> #include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/SimpleFileSystem.h> #include <ipxe/efi/Protocol/SimpleFileSystem.h>
#include <ipxe/efi/Protocol/BlockIo.h> #include <ipxe/efi/Protocol/BlockIo.h>