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