xen_file.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/xen_file.h>
  19. #include <grub/i386/linux.h>
  20. #include <grub/misc.h>
  21. #define XZ_MAGIC "\3757zXZ\0"
  22. grub_elf_t
  23. grub_xen_file (grub_file_t file)
  24. {
  25. grub_elf_t elf;
  26. struct linux_i386_kernel_header lh;
  27. grub_file_t off_file;
  28. grub_uint32_t payload_offset, payload_length;
  29. grub_uint8_t magic[6];
  30. elf = grub_elf_file (file, file->name);
  31. if (elf)
  32. return elf;
  33. grub_errno = GRUB_ERR_NONE;
  34. if (grub_file_seek (file, 0) == (grub_off_t) -1)
  35. goto fail;
  36. if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
  37. goto fail;
  38. if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55)
  39. || lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
  40. || grub_le_to_cpu16 (lh.version) < 0x0208)
  41. {
  42. grub_error (GRUB_ERR_BAD_OS, "version too old for xen boot");
  43. return NULL;
  44. }
  45. payload_length = lh.payload_length;
  46. payload_offset = (lh.setup_sects + 1) * 512
  47. + lh.payload_offset;
  48. if (payload_length < sizeof (magic))
  49. {
  50. grub_error (GRUB_ERR_BAD_OS, "payload too short");
  51. return NULL;
  52. }
  53. grub_dprintf ("xen", "found bzimage payload 0x%llx-0x%llx\n",
  54. (unsigned long long) payload_offset,
  55. (unsigned long long) lh.payload_length);
  56. grub_file_seek (file, payload_offset);
  57. if (grub_file_read (file, &magic, sizeof (magic)) != sizeof (magic))
  58. {
  59. if (!grub_errno)
  60. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  61. file->name);
  62. goto fail;
  63. }
  64. /* Kernel suffixes xz payload with their uncompressed size.
  65. Trim it. */
  66. if (grub_memcmp (magic, XZ_MAGIC, sizeof (XZ_MAGIC) - 1) == 0)
  67. payload_length -= 4;
  68. off_file = grub_file_offset_open (file, GRUB_FILE_TYPE_LINUX_KERNEL, payload_offset,
  69. payload_length);
  70. if (!off_file)
  71. goto fail;
  72. elf = grub_elf_file (off_file, file->name);
  73. if (elf)
  74. return elf;
  75. grub_file_offset_close (off_file);
  76. fail:
  77. grub_error (GRUB_ERR_BAD_OS, "not xen image");
  78. return NULL;
  79. }
  80. grub_err_t
  81. grub_xen_get_info (grub_elf_t elf, struct grub_xen_file_info * xi)
  82. {
  83. grub_memset (xi, 0, sizeof (*xi));
  84. if (grub_elf_is_elf64 (elf)
  85. && elf->ehdr.ehdr64.e_machine
  86. == grub_cpu_to_le16_compile_time (EM_X86_64)
  87. && elf->ehdr.ehdr64.e_ident[EI_DATA] == ELFDATA2LSB)
  88. {
  89. xi->arch = GRUB_XEN_FILE_X86_64;
  90. return grub_xen_get_info64 (elf, xi);
  91. }
  92. if (grub_elf_is_elf32 (elf)
  93. && elf->ehdr.ehdr32.e_machine == grub_cpu_to_le16_compile_time (EM_386)
  94. && elf->ehdr.ehdr32.e_ident[EI_DATA] == ELFDATA2LSB)
  95. {
  96. xi->arch = GRUB_XEN_FILE_I386;
  97. return grub_xen_get_info32 (elf, xi);
  98. }
  99. return grub_error (GRUB_ERR_BAD_OS, "unknown ELF type");
  100. }