Skip to content

Commit

Permalink
[bzimage] Align initrd images to page boundary
Browse files Browse the repository at this point in the history
Some versions of Linux apparently complain if initrds are not aligned
to a page boundary.  Fix by changing INITRD_ALIGN from 4 bytes to 4096
bytes.

The amount of padding at the end of each initrd will now often be
sufficient to allow the cpio header to be prepended without crossing
an alignment boundary.  The final location of the initrd may therefore
end up being slightly higher than the post-shuffle location.
bzimage_load_initrd() must therefore now copy the initrd body prior to
copying the cpio header, otherwise the start of the initrd body may be
overwritten by the cpio header.  (Note that the guarantee that an
initrd will never need to overwrite an initrd at a higher location
still holds, since the overall length of each initrd cannot decrease
as a result of adding a cpio header.)

Reported-by: Dave Hansen <dave@sr71.net>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Jun 5, 2013
1 parent 0036fdd commit c825a9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
30 changes: 13 additions & 17 deletions src/arch/i386/image/bzimage.c
Expand Up @@ -360,15 +360,15 @@ static size_t bzimage_load_initrd ( struct image *image,
char *filename = initrd->cmdline;
char *cmdline;
struct cpio_header cpio;
size_t offset = 0;
size_t offset;
size_t name_len;
size_t pad_len;

/* Do not include kernel image itself as an initrd */
if ( initrd == image )
return 0;

/* Create cpio header before non-prebuilt images */
/* Create cpio header for non-prebuilt images */
if ( filename && filename[0] ) {
cmdline = strchr ( filename, ' ' );
name_len = ( ( cmdline ? ( ( size_t ) ( cmdline - filename ) )
Expand All @@ -383,24 +383,20 @@ static size_t bzimage_load_initrd ( struct image *image,
bzimage_parse_cpio_cmdline ( image, &cpio,
( cmdline + 1 /* ' ' */ ));
}
if ( address ) {
copy_to_user ( address, offset, &cpio,
sizeof ( cpio ) );
}
offset += sizeof ( cpio );
if ( address ) {
memset_user ( address, offset, 0, name_len );
copy_to_user ( address, offset, filename,
( name_len - 1 /* NUL (or space) */ ) );
}
offset += name_len;
offset = ( ( offset + 0x03 ) & ~0x03 );
offset = ( ( sizeof ( cpio ) + name_len + 0x03 ) & ~0x03 );
} else {
offset = 0;
}

/* Copy in initrd image body */
if ( address )
memmove_user ( address, offset, initrd->data, 0, initrd->len );
/* Copy in initrd image body (and cpio header if applicable) */
if ( address ) {
memmove_user ( address, offset, initrd->data, 0, initrd->len );
if ( offset ) {
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) */ ) );
}
DBGC ( image, "bzImage %p initrd %p [%#08lx,%#08lx,%#08lx)"
"%s%s\n", image, initrd, user_to_phys ( address, 0 ),
user_to_phys ( address, offset ),
Expand Down
5 changes: 3 additions & 2 deletions src/arch/i386/include/initrd.h
Expand Up @@ -13,9 +13,10 @@ FILE_LICENCE ( GPL2_OR_LATER );

/** Minimum alignment for initrds
*
* Chosen to maximise memcpy() speeds
* Some versions of Linux complain about initrds that are not
* page-aligned.
*/
#define INITRD_ALIGN 4
#define INITRD_ALIGN 4096

/** Minimum free space required to reshuffle initrds
*
Expand Down

0 comments on commit c825a9b

Please sign in to comment.