2013-11-09 20:29:11 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2013 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/xen_file.h>
|
|
|
|
#include <grub/i386/linux.h>
|
fix include loop on MinGW due to libintl.h pulling stdio.h
In file included from ./include/grub/dl.h:23:0,
from grub-core/lib/libgcrypt-grub/cipher/rfc2268.c:3:
./include/grub/list.h:34:18: warning: conflicting types for 'grub_list_push' [en
abled by default]
void EXPORT_FUNC(grub_list_push) (grub_list_t *head, grub_list_t item);
^
./include/grub/symbol.h:68:25: note: in definition of macro 'EXPORT_FUNC'
# define EXPORT_FUNC(x) x
^
In file included from ./include/grub/fs.h:30:0,
from ./include/grub/file.h:25,
from ./grub-core/lib/posix_wrap/stdio.h:23,
from c:\mingw\include\libintl.h:314,
from ./include/grub/i18n.h:33,
from ./include/grub/misc.h:27,
from ./include/grub/list.h:25,
from ./include/grub/dl.h:28,
from grub-core/lib/libgcrypt-grub/cipher/rfc2268.c:3:
./include/grub/partition.h:106:3: note: previous implicit declaration of 'grub_l
ist_push' was here
grub_list_push (GRUB_AS_LIST_P (&grub_partition_map_list),
^
list.h needs just ATTRIBUTE_ERROR from misc.h; split compiler features
into separate file grub/compiler.h and include it instead.
2014-01-18 17:22:57 +00:00
|
|
|
#include <grub/misc.h>
|
2017-01-25 22:54:46 +00:00
|
|
|
#include <grub/verity-hash.h>
|
2016-10-19 21:48:50 +00:00
|
|
|
|
2017-02-06 23:47:23 +00:00
|
|
|
#define XZ_MAGIC "\3757zXZ\0"
|
|
|
|
|
2013-11-09 20:29:11 +00:00
|
|
|
grub_elf_t
|
|
|
|
grub_xen_file (grub_file_t file)
|
2016-10-19 21:48:50 +00:00
|
|
|
{
|
|
|
|
return grub_xen_file_and_cmdline (file, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_elf_t
|
|
|
|
grub_xen_file_and_cmdline (grub_file_t file,
|
|
|
|
char *cmdline,
|
|
|
|
grub_size_t cmdline_max_len)
|
2013-11-09 20:29:11 +00:00
|
|
|
{
|
|
|
|
grub_elf_t elf;
|
2018-02-01 18:18:52 +00:00
|
|
|
struct linux_i386_kernel_header lh;
|
2013-11-09 20:29:11 +00:00
|
|
|
grub_file_t off_file;
|
2017-02-06 23:47:23 +00:00
|
|
|
grub_uint32_t payload_offset, payload_length;
|
|
|
|
grub_uint8_t magic[6];
|
2013-11-09 20:29:11 +00:00
|
|
|
|
|
|
|
elf = grub_elf_file (file, file->name);
|
|
|
|
if (elf)
|
|
|
|
return elf;
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
|
|
|
|
if (grub_file_seek (file, 0) == (grub_off_t) -1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
|
|
|
|
goto fail;
|
|
|
|
|
2014-09-22 16:47:10 +00:00
|
|
|
if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55)
|
2018-02-01 18:18:51 +00:00
|
|
|
|| lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
|
2013-11-09 20:29:11 +00:00
|
|
|
|| grub_le_to_cpu16 (lh.version) < 0x0208)
|
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_OS, "version too old for xen boot");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:47:23 +00:00
|
|
|
payload_length = lh.payload_length;
|
|
|
|
payload_offset = (lh.setup_sects + 1) * 512
|
|
|
|
+ lh.payload_offset;
|
|
|
|
|
|
|
|
if (payload_length < sizeof (magic))
|
2013-11-09 20:29:11 +00:00
|
|
|
{
|
|
|
|
grub_error (GRUB_ERR_BAD_OS, "payload too short");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_dprintf ("xen", "found bzimage payload 0x%llx-0x%llx\n",
|
2017-02-06 23:47:23 +00:00
|
|
|
(unsigned long long) payload_offset,
|
2016-03-11 09:26:51 +00:00
|
|
|
(unsigned long long) lh.payload_length);
|
2013-11-09 20:29:11 +00:00
|
|
|
|
2016-10-19 21:48:50 +00:00
|
|
|
if (cmdline)
|
|
|
|
grub_pass_verity_hash (&lh, cmdline, cmdline_max_len);
|
|
|
|
|
2017-02-06 23:47:23 +00:00
|
|
|
grub_file_seek (file, payload_offset);
|
|
|
|
|
|
|
|
if (grub_file_read (file, &magic, sizeof (magic)) != sizeof (magic))
|
|
|
|
{
|
|
|
|
if (!grub_errno)
|
|
|
|
grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
|
|
|
|
file->name);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Kernel suffixes xz payload with their uncompressed size.
|
|
|
|
Trim it. */
|
|
|
|
if (grub_memcmp (magic, XZ_MAGIC, sizeof (XZ_MAGIC) - 1) == 0)
|
|
|
|
payload_length -= 4;
|
|
|
|
off_file = grub_file_offset_open (file, payload_offset,
|
|
|
|
payload_length);
|
2013-11-09 20:29:11 +00:00
|
|
|
if (!off_file)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
elf = grub_elf_file (off_file, file->name);
|
|
|
|
if (elf)
|
|
|
|
return elf;
|
|
|
|
grub_file_offset_close (off_file);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
grub_error (GRUB_ERR_BAD_OS, "not xen image");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
grub_xen_get_info (grub_elf_t elf, struct grub_xen_file_info * xi)
|
|
|
|
{
|
|
|
|
grub_memset (xi, 0, sizeof (*xi));
|
|
|
|
|
2013-12-17 13:39:48 +00:00
|
|
|
if (grub_elf_is_elf64 (elf)
|
|
|
|
&& elf->ehdr.ehdr64.e_machine
|
|
|
|
== grub_cpu_to_le16_compile_time (EM_X86_64)
|
|
|
|
&& elf->ehdr.ehdr64.e_ident[EI_DATA] == ELFDATA2LSB)
|
2013-11-09 20:29:11 +00:00
|
|
|
{
|
|
|
|
xi->arch = GRUB_XEN_FILE_X86_64;
|
|
|
|
return grub_xen_get_info64 (elf, xi);
|
|
|
|
}
|
2013-12-17 13:39:48 +00:00
|
|
|
if (grub_elf_is_elf32 (elf)
|
|
|
|
&& elf->ehdr.ehdr32.e_machine == grub_cpu_to_le16_compile_time (EM_386)
|
|
|
|
&& elf->ehdr.ehdr32.e_ident[EI_DATA] == ELFDATA2LSB)
|
2013-11-09 20:29:11 +00:00
|
|
|
{
|
|
|
|
xi->arch = GRUB_XEN_FILE_I386;
|
|
|
|
return grub_xen_get_info32 (elf, xi);
|
|
|
|
}
|
|
|
|
return grub_error (GRUB_ERR_BAD_OS, "unknown ELF type");
|
|
|
|
}
|