loader/linux: do not pad initrd with zeroes at the end

Syslinux memdisk is using initrd image and needs to know uncompressed
size in advance. For gzip uncompressed size is at the end of compressed
stream. Grub padded each input file to 4 bytes at the end, which means
syslinux got wrong size.

Linux initramfs loader apparently does not care about trailing alignment.
So change code to align beginning of each file instead which atomatically
gives us the correct size for single file.

Reported-By: David Shaw <dshaw@jabberwocky.com>
This commit is contained in:
Andrei Borzenkov 2015-05-07 20:24:24 +03:00
parent 71783dc978
commit a8c473288d

View file

@ -161,6 +161,9 @@ grub_initrd_init (int argc, char *argv[],
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
{ {
const char *fname = argv[i]; const char *fname = argv[i];
initrd_ctx->size = ALIGN_UP (initrd_ctx->size, 4);
if (grub_memcmp (argv[i], "newc:", 5) == 0) if (grub_memcmp (argv[i], "newc:", 5) == 0)
{ {
const char *ptr, *eptr; const char *ptr, *eptr;
@ -205,7 +208,7 @@ grub_initrd_init (int argc, char *argv[],
initrd_ctx->nfiles++; initrd_ctx->nfiles++;
initrd_ctx->components[i].size initrd_ctx->components[i].size
= grub_file_size (initrd_ctx->components[i].file); = grub_file_size (initrd_ctx->components[i].file);
initrd_ctx->size += ALIGN_UP (initrd_ctx->components[i].size, 4); initrd_ctx->size += initrd_ctx->components[i].size;
} }
if (newc) if (newc)
@ -248,10 +251,12 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
int i; int i;
int newc = 0; int newc = 0;
struct dir *root = 0; struct dir *root = 0;
grub_ssize_t cursize = 0;
for (i = 0; i < initrd_ctx->nfiles; i++) for (i = 0; i < initrd_ctx->nfiles; i++)
{ {
grub_ssize_t cursize; grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
ptr += ALIGN_UP_OVERHEAD (cursize, 4);
if (initrd_ctx->components[i].newc_name) if (initrd_ctx->components[i].newc_name)
{ {
@ -283,8 +288,6 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx,
return grub_errno; return grub_errno;
} }
ptr += cursize; ptr += cursize;
grub_memset (ptr, 0, ALIGN_UP_OVERHEAD (cursize, 4));
ptr += ALIGN_UP_OVERHEAD (cursize, 4);
} }
if (newc) if (newc)
ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!") - 1, 0, 0); ptr = make_header (ptr, "TRAILER!!!", sizeof ("TRAILER!!!") - 1, 0, 0);