vaseboot/VasEBoot-core/loader/i386/xen_file.c

118 lines
3.6 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2013 Free Software Foundation, Inc.
*
* VAS_EBOOT 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.
*
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/xen_file.h>
#include <VasEBoot/i386/linux.h>
#include <VasEBoot/misc.h>
#define XZ_MAGIC "\3757zXZ\0"
VasEBoot_elf_t
VasEBoot_xen_file (VasEBoot_file_t file)
{
VasEBoot_elf_t elf;
struct linux_i386_kernel_header lh;
VasEBoot_file_t off_file;
VasEBoot_uint32_t payload_offset, payload_length;
VasEBoot_uint8_t magic[6];
elf = VasEBoot_elf_file (file, file->name);
if (elf)
return elf;
VasEBoot_errno = VAS_EBOOT_ERR_NONE;
if (VasEBoot_file_seek (file, 0) == (VasEBoot_off_t) -1)
goto fail;
if (VasEBoot_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
goto fail;
if (lh.boot_flag != VasEBoot_cpu_to_le16_compile_time (0xaa55)
|| lh.header != VasEBoot_cpu_to_le32_compile_time (VAS_EBOOT_LINUX_I386_MAGIC_SIGNATURE)
|| VasEBoot_le_to_cpu16 (lh.version) < 0x0208)
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "version too old for xen boot");
return NULL;
}
payload_length = lh.payload_length;
payload_offset = (lh.setup_sects + 1) * 512
+ lh.payload_offset;
if (payload_length < sizeof (magic))
{
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "payload too short");
return NULL;
}
VasEBoot_dprintf ("xen", "found bzimage payload 0x%llx-0x%llx\n",
(unsigned long long) payload_offset,
(unsigned long long) lh.payload_length);
VasEBoot_file_seek (file, payload_offset);
if (VasEBoot_file_read (file, &magic, sizeof (magic)) != sizeof (magic))
{
if (!VasEBoot_errno)
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"),
file->name);
goto fail;
}
/* Kernel suffixes xz payload with their uncompressed size.
Trim it. */
if (VasEBoot_memcmp (magic, XZ_MAGIC, sizeof (XZ_MAGIC) - 1) == 0)
payload_length -= 4;
off_file = VasEBoot_file_offset_open (file, VAS_EBOOT_FILE_TYPE_LINUX_KERNEL, payload_offset,
payload_length);
if (!off_file)
goto fail;
elf = VasEBoot_elf_file (off_file, file->name);
if (elf)
return elf;
VasEBoot_file_offset_close (off_file);
fail:
VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "not xen image");
return NULL;
}
VasEBoot_err_t
VasEBoot_xen_get_info (VasEBoot_elf_t elf, struct VasEBoot_xen_file_info * xi)
{
VasEBoot_memset (xi, 0, sizeof (*xi));
if (VasEBoot_elf_is_elf64 (elf)
&& elf->ehdr.ehdr64.e_machine
== VasEBoot_cpu_to_le16_compile_time (EM_X86_64)
&& elf->ehdr.ehdr64.e_ident[EI_DATA] == ELFDATA2LSB)
{
xi->arch = VAS_EBOOT_XEN_FILE_X86_64;
return VasEBoot_xen_get_info64 (elf, xi);
}
if (VasEBoot_elf_is_elf32 (elf)
&& elf->ehdr.ehdr32.e_machine == VasEBoot_cpu_to_le16_compile_time (EM_386)
&& elf->ehdr.ehdr32.e_ident[EI_DATA] == ELFDATA2LSB)
{
xi->arch = VAS_EBOOT_XEN_FILE_I386;
return VasEBoot_xen_get_info32 (elf, xi);
}
return VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, "unknown ELF type");
}