macho.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* macho.c - load Mach-O files. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* This Mach-O loader is incomplete and can load only non-relocatable segments.
  20. This is however enough to boot xnu (otool -l and Mach-O specs for more info).
  21. */
  22. #include <grub/err.h>
  23. #include <grub/macho.h>
  24. #include <grub/machoload.h>
  25. #include <grub/file.h>
  26. #include <grub/misc.h>
  27. #include <grub/mm.h>
  28. #include <grub/i18n.h>
  29. #include <grub/dl.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. grub_err_t
  32. grub_macho_close (grub_macho_t macho)
  33. {
  34. grub_file_t file = macho->file;
  35. if (!macho->uncompressed32)
  36. grub_free (macho->cmds32);
  37. grub_free (macho->uncompressed32);
  38. if (!macho->uncompressed64)
  39. grub_free (macho->cmds64);
  40. grub_free (macho->uncompressed64);
  41. grub_free (macho);
  42. if (file)
  43. grub_file_close (file);
  44. return grub_errno;
  45. }
  46. grub_macho_t
  47. grub_macho_file (grub_file_t file, const char *filename, int is_64bit)
  48. {
  49. grub_macho_t macho;
  50. union grub_macho_filestart filestart;
  51. macho = grub_malloc (sizeof (*macho));
  52. if (! macho)
  53. return 0;
  54. macho->file = file;
  55. macho->offset32 = -1;
  56. macho->offset64 = -1;
  57. macho->end32 = -1;
  58. macho->end64 = -1;
  59. macho->cmds32 = 0;
  60. macho->cmds64 = 0;
  61. macho->uncompressed32 = 0;
  62. macho->uncompressed64 = 0;
  63. macho->compressed32 = 0;
  64. macho->compressed64 = 0;
  65. if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
  66. goto fail;
  67. if (grub_file_read (macho->file, &filestart, sizeof (filestart))
  68. != sizeof (filestart))
  69. {
  70. if (!grub_errno)
  71. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  72. filename);
  73. goto fail;
  74. }
  75. /* Is it a fat file? */
  76. if (filestart.fat.magic == grub_cpu_to_be32_compile_time (GRUB_MACHO_FAT_MAGIC))
  77. {
  78. struct grub_macho_fat_arch *archs;
  79. int i, narchs;
  80. /* Load architecture description. */
  81. narchs = grub_be_to_cpu32 (filestart.fat.nfat_arch);
  82. if (grub_file_seek (macho->file, sizeof (struct grub_macho_fat_header))
  83. == (grub_off_t) -1)
  84. goto fail;
  85. archs = grub_calloc (narchs, sizeof (struct grub_macho_fat_arch));
  86. if (!archs)
  87. goto fail;
  88. if (grub_file_read (macho->file, archs,
  89. sizeof (struct grub_macho_fat_arch) * narchs)
  90. != (grub_ssize_t) sizeof(struct grub_macho_fat_arch) * narchs)
  91. {
  92. grub_free (archs);
  93. if (!grub_errno)
  94. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  95. filename);
  96. goto fail;
  97. }
  98. for (i = 0; i < narchs; i++)
  99. {
  100. if ((archs[i].cputype
  101. == grub_cpu_to_be32_compile_time (GRUB_MACHO_CPUTYPE_IA32))
  102. && !is_64bit)
  103. {
  104. macho->offset32 = grub_be_to_cpu32 (archs[i].offset);
  105. macho->end32 = grub_be_to_cpu32 (archs[i].offset)
  106. + grub_be_to_cpu32 (archs[i].size);
  107. }
  108. if ((archs[i].cputype
  109. == grub_cpu_to_be32_compile_time (GRUB_MACHO_CPUTYPE_AMD64))
  110. && is_64bit)
  111. {
  112. macho->offset64 = grub_be_to_cpu32 (archs[i].offset);
  113. macho->end64 = grub_be_to_cpu32 (archs[i].offset)
  114. + grub_be_to_cpu32 (archs[i].size);
  115. }
  116. }
  117. grub_free (archs);
  118. }
  119. /* Is it a thin 32-bit file? */
  120. if (filestart.thin32.magic == GRUB_MACHO_MAGIC32 && !is_64bit)
  121. {
  122. macho->offset32 = 0;
  123. macho->end32 = grub_file_size (file);
  124. }
  125. /* Is it a thin 64-bit file? */
  126. if (filestart.thin64.magic == GRUB_MACHO_MAGIC64 && is_64bit)
  127. {
  128. macho->offset64 = 0;
  129. macho->end64 = grub_file_size (file);
  130. }
  131. if (grub_memcmp (filestart.lzss.magic, GRUB_MACHO_LZSS_MAGIC,
  132. sizeof (filestart.lzss.magic)) == 0 && !is_64bit)
  133. {
  134. macho->offset32 = 0;
  135. macho->end32 = grub_file_size (file);
  136. }
  137. /* Is it a thin 64-bit file? */
  138. if (grub_memcmp (filestart.lzss.magic, GRUB_MACHO_LZSS_MAGIC,
  139. sizeof (filestart.lzss.magic)) == 0 && is_64bit)
  140. {
  141. macho->offset64 = 0;
  142. macho->end64 = grub_file_size (file);
  143. }
  144. grub_macho_parse32 (macho, filename);
  145. grub_macho_parse64 (macho, filename);
  146. if (macho->offset32 == -1 && !is_64bit)
  147. {
  148. grub_error (GRUB_ERR_BAD_OS,
  149. "Mach-O doesn't contain suitable 32-bit architecture");
  150. goto fail;
  151. }
  152. if (macho->offset64 == -1 && is_64bit)
  153. {
  154. grub_error (GRUB_ERR_BAD_OS,
  155. "Mach-O doesn't contain suitable 64-bit architecture");
  156. goto fail;
  157. }
  158. return macho;
  159. fail:
  160. macho->file = 0;
  161. grub_macho_close (macho);
  162. return 0;
  163. }
  164. grub_macho_t
  165. grub_macho_open (const char *name, enum grub_file_type type, int is_64bit)
  166. {
  167. grub_file_t file;
  168. grub_macho_t macho;
  169. file = grub_file_open (name, type);
  170. if (! file)
  171. return 0;
  172. macho = grub_macho_file (file, name, is_64bit);
  173. if (! macho)
  174. grub_file_close (file);
  175. return macho;
  176. }