grub-macho2img.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* macho2img.c - tool to convert Mach-O to raw imagw. */
  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. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/macho.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. /* Please don't internationalise this file. It's pointless. */
  26. /* XXX: this file assumes particular Mach-O layout and does no checks. */
  27. /* However as build system ensures correct usage of this tool this
  28. shouldn't be a problem. */
  29. int
  30. main (int argc, char **argv)
  31. {
  32. FILE *in, *out;
  33. int do_bss = 0;
  34. char *buf;
  35. int bufsize;
  36. struct grub_macho_header32 *head;
  37. struct grub_macho_segment32 *curcmd;
  38. unsigned i;
  39. unsigned bssstart = 0;
  40. unsigned bssend = 0;
  41. if (argc && strcmp (argv[1], "--bss") == 0)
  42. do_bss = 1;
  43. if (argc < 2 + do_bss)
  44. {
  45. printf ("Usage: %s [--bss] filename.exec filename.img\n"
  46. "Convert Mach-O into raw image\n", argv[0]);
  47. return 0;
  48. }
  49. in = fopen (argv[1 + do_bss], "rb");
  50. if (! in)
  51. {
  52. printf ("Couldn't open %s\n", argv[1 + do_bss]);
  53. return 1;
  54. }
  55. out = fopen (argv[2 + do_bss], "wb");
  56. if (! out)
  57. {
  58. fclose (in);
  59. printf ("Couldn't open %s\n", argv[2 + do_bss]);
  60. return 2;
  61. }
  62. fseek (in, 0, SEEK_END);
  63. bufsize = ftell (in);
  64. fseek (in, 0, SEEK_SET);
  65. buf = malloc (bufsize);
  66. if (! buf)
  67. {
  68. fclose (in);
  69. fclose (out);
  70. printf ("Couldn't allocate buffer\n");
  71. return 3;
  72. }
  73. fread (buf, 1, bufsize, in);
  74. head = (struct grub_macho_header32 *) buf;
  75. if (grub_le_to_cpu32 (head->magic) != GRUB_MACHO_MAGIC32)
  76. {
  77. fclose (in);
  78. fclose (out);
  79. free (buf);
  80. printf ("Invalid Mach-O file\n");
  81. return 4;
  82. }
  83. curcmd = (struct grub_macho_segment32 *) (buf + sizeof (*head));
  84. for (i = 0; i < grub_le_to_cpu32 (head->ncmds); i++,
  85. curcmd = (struct grub_macho_segment32 *)
  86. (((char *) curcmd) + curcmd->cmdsize))
  87. {
  88. if (curcmd->cmd != GRUB_MACHO_CMD_SEGMENT32)
  89. continue;
  90. fwrite (buf + grub_le_to_cpu32 (curcmd->fileoff), 1,
  91. grub_le_to_cpu32 (curcmd->filesize), out);
  92. if (grub_le_to_cpu32 (curcmd->vmsize)
  93. > grub_le_to_cpu32 (curcmd->filesize))
  94. {
  95. bssstart = grub_le_to_cpu32 (curcmd->vmaddr)
  96. + grub_le_to_cpu32 (curcmd->filesize) ;
  97. bssend = grub_le_to_cpu32 (curcmd->vmaddr)
  98. + grub_le_to_cpu32 (curcmd->vmsize) ;
  99. }
  100. }
  101. if (do_bss)
  102. {
  103. grub_uint32_t tmp;
  104. fseek (out, 0x5c, SEEK_SET);
  105. tmp = grub_cpu_to_le32 (bssstart);
  106. fwrite (&tmp, 4, 1, out);
  107. tmp = grub_cpu_to_le32 (bssend);
  108. fwrite (&tmp, 4, 1, out);
  109. }
  110. fclose (in);
  111. fclose (out);
  112. printf("macho2img complete\n");
  113. return 0;
  114. }