2009-06-04 21:01:11 +00:00
|
|
|
/* macho2img.c - tool to convert Mach-O to raw imagw. */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-09-24 07:19:57 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2009-06-04 21:01:11 +00:00
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/macho.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-11-11 19:52:41 +00:00
|
|
|
/* Please don't internationalise this file. It's pointless. */
|
|
|
|
|
2009-06-04 21:01:11 +00:00
|
|
|
/* XXX: this file assumes particular Mach-O layout and does no checks. */
|
2009-06-10 21:04:23 +00:00
|
|
|
/* However as build system ensures correct usage of this tool this
|
2009-06-04 21:01:11 +00:00
|
|
|
shouldn't be a problem. */
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
FILE *in, *out;
|
|
|
|
int do_bss = 0;
|
|
|
|
char *buf;
|
|
|
|
int bufsize;
|
|
|
|
struct grub_macho_header32 *head;
|
|
|
|
struct grub_macho_segment32 *curcmd;
|
|
|
|
unsigned i;
|
|
|
|
unsigned bssstart = 0;
|
|
|
|
unsigned bssend = 0;
|
|
|
|
|
|
|
|
if (argc && strcmp (argv[1], "--bss") == 0)
|
|
|
|
do_bss = 1;
|
|
|
|
if (argc < 2 + do_bss)
|
|
|
|
{
|
|
|
|
printf ("Usage: %s [--bss] filename.exec filename.img\n"
|
|
|
|
"Convert Mach-O into raw image\n", argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-11 21:48:43 +00:00
|
|
|
in = fopen (argv[1 + do_bss], "rb");
|
2009-06-04 21:01:11 +00:00
|
|
|
if (! in)
|
|
|
|
{
|
|
|
|
printf ("Couldn't open %s\n", argv[1 + do_bss]);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-11 21:48:43 +00:00
|
|
|
out = fopen (argv[2 + do_bss], "wb");
|
2009-06-04 21:01:11 +00:00
|
|
|
if (! out)
|
|
|
|
{
|
|
|
|
fclose (in);
|
|
|
|
printf ("Couldn't open %s\n", argv[2 + do_bss]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
fseek (in, 0, SEEK_END);
|
|
|
|
bufsize = ftell (in);
|
|
|
|
fseek (in, 0, SEEK_SET);
|
|
|
|
buf = malloc (bufsize);
|
|
|
|
if (! buf)
|
|
|
|
{
|
|
|
|
fclose (in);
|
|
|
|
fclose (out);
|
|
|
|
printf ("Couldn't allocate buffer\n");
|
|
|
|
return 3;
|
2009-06-10 21:04:23 +00:00
|
|
|
}
|
2009-06-04 21:01:11 +00:00
|
|
|
fread (buf, 1, bufsize, in);
|
|
|
|
head = (struct grub_macho_header32 *) buf;
|
|
|
|
if (grub_le_to_cpu32 (head->magic) != GRUB_MACHO_MAGIC32)
|
|
|
|
{
|
|
|
|
fclose (in);
|
|
|
|
fclose (out);
|
|
|
|
free (buf);
|
2010-08-10 14:32:48 +00:00
|
|
|
printf ("Invalid Mach-O file\n");
|
2009-06-04 21:01:11 +00:00
|
|
|
return 4;
|
2009-06-10 21:04:23 +00:00
|
|
|
}
|
2009-06-04 21:01:11 +00:00
|
|
|
curcmd = (struct grub_macho_segment32 *) (buf + sizeof (*head));
|
2009-06-10 21:04:23 +00:00
|
|
|
for (i = 0; i < grub_le_to_cpu32 (head->ncmds); i++,
|
|
|
|
curcmd = (struct grub_macho_segment32 *)
|
2009-06-04 21:01:11 +00:00
|
|
|
(((char *) curcmd) + curcmd->cmdsize))
|
|
|
|
{
|
|
|
|
if (curcmd->cmd != GRUB_MACHO_CMD_SEGMENT32)
|
|
|
|
continue;
|
2009-06-10 21:04:23 +00:00
|
|
|
fwrite (buf + grub_le_to_cpu32 (curcmd->fileoff), 1,
|
2009-06-04 21:01:11 +00:00
|
|
|
grub_le_to_cpu32 (curcmd->filesize), out);
|
2009-06-10 21:04:23 +00:00
|
|
|
if (grub_le_to_cpu32 (curcmd->vmsize)
|
2009-06-04 21:01:11 +00:00
|
|
|
> grub_le_to_cpu32 (curcmd->filesize))
|
|
|
|
{
|
2009-06-10 21:04:23 +00:00
|
|
|
bssstart = grub_le_to_cpu32 (curcmd->vmaddr)
|
2009-06-04 21:01:11 +00:00
|
|
|
+ grub_le_to_cpu32 (curcmd->filesize) ;
|
2009-06-10 21:04:23 +00:00
|
|
|
bssend = grub_le_to_cpu32 (curcmd->vmaddr)
|
2009-06-04 21:01:11 +00:00
|
|
|
+ grub_le_to_cpu32 (curcmd->vmsize) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (do_bss)
|
|
|
|
{
|
|
|
|
grub_uint32_t tmp;
|
|
|
|
fseek (out, 0x5c, SEEK_SET);
|
|
|
|
tmp = grub_cpu_to_le32 (bssstart);
|
|
|
|
fwrite (&tmp, 4, 1, out);
|
|
|
|
tmp = grub_cpu_to_le32 (bssend);
|
|
|
|
fwrite (&tmp, 4, 1, out);
|
|
|
|
}
|
|
|
|
fclose (in);
|
|
|
|
fclose (out);
|
|
|
|
printf("macho2img complete\n");
|
|
|
|
return 0;
|
|
|
|
}
|