glue-efi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2012,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 <config.h>
  19. #include <grub/util/misc.h>
  20. #include <grub/util/install.h>
  21. #include <grub/i18n.h>
  22. #include <grub/term.h>
  23. #include <grub/macho.h>
  24. #define _GNU_SOURCE 1
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. static void
  31. write_fat (FILE *in32, FILE *in64, FILE *out, const char *out_filename,
  32. const char *name32, const char *name64)
  33. {
  34. struct grub_macho_fat_header head;
  35. struct grub_macho_fat_arch arch32, arch64;
  36. grub_uint32_t size32, size64;
  37. long size;
  38. char *buf;
  39. fseek (in32, 0, SEEK_END);
  40. size = ftell (in32);
  41. if (size < 0)
  42. grub_util_error ("cannot get end of input file '%s': %s",
  43. name32, strerror (errno));
  44. size32 = (grub_uint32_t) size;
  45. fseek (in32, 0, SEEK_SET);
  46. fseek (in64, 0, SEEK_END);
  47. size = ftell (in64);
  48. if (size < 0)
  49. grub_util_error ("cannot get end of input file '%s': %s",
  50. name64, strerror (errno));
  51. size64 = (grub_uint64_t) size;
  52. fseek (in64, 0, SEEK_SET);
  53. head.magic = grub_cpu_to_le32_compile_time (GRUB_MACHO_FAT_EFI_MAGIC);
  54. head.nfat_arch = grub_cpu_to_le32_compile_time (2);
  55. arch32.cputype = grub_cpu_to_le32_compile_time (GRUB_MACHO_CPUTYPE_IA32);
  56. arch32.cpusubtype = grub_cpu_to_le32_compile_time (3);
  57. arch32.offset = grub_cpu_to_le32_compile_time (sizeof (head)
  58. + sizeof (arch32)
  59. + sizeof (arch64));
  60. arch32.size = grub_cpu_to_le32 (size32);
  61. arch32.align = 0;
  62. arch64.cputype = grub_cpu_to_le32_compile_time (GRUB_MACHO_CPUTYPE_AMD64);
  63. arch64.cpusubtype = grub_cpu_to_le32_compile_time (3);
  64. arch64.offset = grub_cpu_to_le32 (sizeof (head) + sizeof (arch32)
  65. + sizeof (arch64) + size32);
  66. arch64.size = grub_cpu_to_le32 (size64);
  67. arch64.align = 0;
  68. if (fwrite (&head, 1, sizeof (head), out) != sizeof (head)
  69. || fwrite (&arch32, 1, sizeof (arch32), out) != sizeof (arch32)
  70. || fwrite (&arch64, 1, sizeof (arch64), out) != sizeof (arch64))
  71. {
  72. if (out_filename)
  73. grub_util_error ("cannot write to `%s': %s",
  74. out_filename, strerror (errno));
  75. else
  76. grub_util_error ("cannot write to the stdout: %s", strerror (errno));
  77. }
  78. buf = xmalloc (size32);
  79. if (fread (buf, 1, size32, in32) != size32)
  80. grub_util_error (_("cannot read `%s': %s"), name32,
  81. strerror (errno));
  82. if (fwrite (buf, 1, size32, out) != size32)
  83. {
  84. if (out_filename)
  85. grub_util_error ("cannot write to `%s': %s",
  86. out_filename, strerror (errno));
  87. else
  88. grub_util_error ("cannot write to the stdout: %s", strerror (errno));
  89. }
  90. free (buf);
  91. buf = xmalloc (size64);
  92. if (fread (buf, 1, size64, in64) != size64)
  93. grub_util_error (_("cannot read `%s': %s"), name64,
  94. strerror (errno));
  95. if (fwrite (buf, 1, size64, out) != size64)
  96. {
  97. if (out_filename)
  98. grub_util_error ("cannot write to `%s': %s",
  99. out_filename, strerror (errno));
  100. else
  101. grub_util_error ("cannot write to the stdout: %s", strerror (errno));
  102. }
  103. free (buf);
  104. }
  105. void
  106. grub_util_glue_efi (const char *file32, const char *file64, const char *outname)
  107. {
  108. FILE *in32, *in64, *out;
  109. in32 = grub_util_fopen (file32, "r");
  110. if (!in32)
  111. grub_util_error (_("cannot open `%s': %s"), file32,
  112. strerror (errno));
  113. in64 = grub_util_fopen (file64, "r");
  114. if (!in64)
  115. grub_util_error (_("cannot open `%s': %s"), file64,
  116. strerror (errno));
  117. if (outname)
  118. out = grub_util_fopen (outname, "wb");
  119. else
  120. out = stdout;
  121. if (!out)
  122. {
  123. grub_util_error (_("cannot open `%s': %s"), outname ? : "stdout",
  124. strerror (errno));
  125. }
  126. write_fat (in32, in64, out, outname,
  127. file32, file64);
  128. fclose (in32);
  129. fclose (in64);
  130. if (out != stdout)
  131. fclose (out);
  132. }