macho.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/cpu/macho.h>
  25. #include <grub/machoload.h>
  26. #include <grub/file.h>
  27. #include <grub/gzio.h>
  28. #include <grub/misc.h>
  29. #include <grub/mm.h>
  30. grub_err_t
  31. grub_macho_close (grub_macho_t macho)
  32. {
  33. grub_file_t file = macho->file;
  34. grub_free (macho->cmds32);
  35. grub_free (macho->cmds64);
  36. grub_free (macho);
  37. if (file)
  38. grub_file_close (file);
  39. return grub_errno;
  40. }
  41. grub_macho_t
  42. grub_macho_file (grub_file_t file)
  43. {
  44. grub_macho_t macho;
  45. union grub_macho_filestart filestart;
  46. macho = grub_malloc (sizeof (*macho));
  47. if (! macho)
  48. return 0;
  49. macho->file = file;
  50. macho->offset32 = -1;
  51. macho->offset64 = -1;
  52. macho->end32 = -1;
  53. macho->end64 = -1;
  54. macho->cmds32 = 0;
  55. macho->cmds64 = 0;
  56. if (grub_file_seek (macho->file, 0) == (grub_off_t) -1)
  57. goto fail;
  58. if (grub_file_read (macho->file, &filestart, sizeof (filestart))
  59. != sizeof (filestart))
  60. {
  61. grub_error_push ();
  62. grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
  63. goto fail;
  64. }
  65. /* Is it a fat file? */
  66. if (filestart.fat.magic == grub_be_to_cpu32 (GRUB_MACHO_FAT_MAGIC))
  67. {
  68. struct grub_macho_fat_arch *archs;
  69. int i, narchs;
  70. /* Load architecture description. */
  71. narchs = grub_be_to_cpu32 (filestart.fat.nfat_arch);
  72. if (grub_file_seek (macho->file, sizeof (struct grub_macho_fat_header))
  73. == (grub_off_t) -1)
  74. goto fail;
  75. archs = grub_malloc (sizeof (struct grub_macho_fat_arch) * narchs);
  76. if (!archs)
  77. goto fail;
  78. if (grub_file_read (macho->file, archs,
  79. sizeof (struct grub_macho_fat_arch) * narchs)
  80. != (grub_ssize_t)sizeof(struct grub_macho_fat_arch) * narchs)
  81. {
  82. grub_free (archs);
  83. grub_error_push ();
  84. grub_error (GRUB_ERR_READ_ERROR, "cannot read Mach-O header");
  85. goto fail;
  86. }
  87. for (i = 0; i < narchs; i++)
  88. {
  89. if (GRUB_MACHO_CPUTYPE_IS_HOST32
  90. (grub_be_to_cpu32 (archs[i].cputype)))
  91. {
  92. macho->offset32 = grub_be_to_cpu32 (archs[i].offset);
  93. macho->end32 = grub_be_to_cpu32 (archs[i].offset)
  94. + grub_be_to_cpu32 (archs[i].size);
  95. }
  96. if (GRUB_MACHO_CPUTYPE_IS_HOST64
  97. (grub_be_to_cpu32 (archs[i].cputype)))
  98. {
  99. macho->offset64 = grub_be_to_cpu32 (archs[i].offset);
  100. macho->end64 = grub_be_to_cpu32 (archs[i].offset)
  101. + grub_be_to_cpu32 (archs[i].size);
  102. }
  103. }
  104. grub_free (archs);
  105. }
  106. /* Is it a thin 32-bit file? */
  107. if (filestart.thin32.magic == GRUB_MACHO_MAGIC32)
  108. {
  109. macho->offset32 = 0;
  110. macho->end32 = grub_file_size (file);
  111. }
  112. /* Is it a thin 64-bit file? */
  113. if (filestart.thin64.magic == GRUB_MACHO_MAGIC64)
  114. {
  115. macho->offset64 = 0;
  116. macho->end64 = grub_file_size (file);
  117. }
  118. grub_macho_parse32 (macho);
  119. grub_macho_parse64 (macho);
  120. return macho;
  121. fail:
  122. grub_macho_close (macho);
  123. return 0;
  124. }
  125. grub_macho_t
  126. grub_macho_open (const char *name)
  127. {
  128. grub_file_t file;
  129. grub_macho_t macho;
  130. file = grub_gzfile_open (name, 1);
  131. if (! file)
  132. return 0;
  133. macho = grub_macho_file (file);
  134. if (! macho)
  135. grub_file_close (file);
  136. return macho;
  137. }