Rework linux command

We want a single buffer that contains the entire kernel image in order to
perform a TPM measurement. Allocate one and copy the entire kernel into it
before pulling out the individual blocks later on.
This commit is contained in:
Matthew Garrett 2015-08-09 16:12:39 -07:00
parent 1e32d63145
commit a2599ab047

View file

@ -681,12 +681,13 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_file_t file = 0; grub_file_t file = 0;
struct linux_kernel_header lh; struct linux_kernel_header lh;
grub_uint8_t setup_sects; grub_uint8_t setup_sects;
grub_size_t real_size, prot_size, prot_file_size; grub_size_t real_size, prot_size, prot_file_size, kernel_offset;
grub_ssize_t len; grub_ssize_t len;
int i; int i;
grub_size_t align, min_align; grub_size_t align, min_align;
int relocatable; int relocatable;
grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR; grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
grub_uint8_t *kernel = NULL;
grub_dl_ref (my_mod); grub_dl_ref (my_mod);
@ -700,7 +701,15 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
if (! file) if (! file)
goto fail; goto fail;
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) len = grub_file_size (file);
kernel = grub_malloc (len);
if (!kernel)
{
grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("cannot allocate kernel buffer"));
goto fail;
}
if (grub_file_read (file, kernel, len) != len)
{ {
if (!grub_errno) if (!grub_errno)
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
@ -708,6 +717,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
goto fail; goto fail;
} }
grub_memcpy (&lh, kernel, sizeof (lh));
kernel_offset = sizeof (lh);
if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55)) if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
{ {
grub_error (GRUB_ERR_BAD_OS, "invalid magic number"); grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
@ -807,13 +819,9 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
linux_params.ps_mouse = linux_params.padding10 = 0; linux_params.ps_mouse = linux_params.padding10 = 0;
len = sizeof (linux_params) - sizeof (lh); len = sizeof (linux_params) - sizeof (lh);
if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
{ grub_memcpy (&linux_params + sizeof (lh), kernel + kernel_offset, len);
if (!grub_errno) kernel_offset += len;
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
goto fail;
}
linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE; linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
@ -872,7 +880,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
/* The other parameters are filled when booting. */ /* The other parameters are filled when booting. */
grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE); kernel_offset = real_size + GRUB_DISK_SECTOR_SIZE;
grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n", grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
(unsigned) real_size, (unsigned) prot_size); (unsigned) real_size, (unsigned) prot_size);
@ -1018,9 +1026,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
grub_pass_verity_hash(&lh, linux_cmdline); grub_pass_verity_hash(&lh, linux_cmdline);
len = prot_file_size; len = prot_file_size;
if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno) grub_memcpy (prot_mode_mem, kernel + kernel_offset, len);
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
argv[0]);
if (grub_errno == GRUB_ERR_NONE) if (grub_errno == GRUB_ERR_NONE)
{ {
@ -1031,6 +1037,8 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
fail: fail:
grub_free (kernel);
if (file) if (file)
grub_file_close (file); grub_file_close (file);