Root/
Source at commit 2605 created 9 years 4 months ago. By ifabio, Temp disabled feature introduced in commit 2562 (Added ability to auto-select last booted partition as the boot volume), cause AllocateKernelMemory error. | |
---|---|
1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | #include <stdio.h>␊ |
25 | #include <stdlib.h>␊ |
26 | #include <stdbool.h>␊ |
27 | #include <mach/mach.h>␊ |
28 | #include <mach/mach_error.h>␊ |
29 | #include <sys/file.h>␊ |
30 | #include <mach-o/loader.h>␊ |
31 | #include <libkern/OSByteOrder.h>␊ |
32 | #include <unistd.h>␊ |
33 | ␊ |
34 | int␉infile, outfile;␊ |
35 | ␊ |
36 | struct mach_header␉mh;␊ |
37 | void *␉␉cmds;␊ |
38 | ␊ |
39 | static bool␉swap_ends;␊ |
40 | ␊ |
41 | static unsigned long swap(unsigned long x)␊ |
42 | {␊ |
43 | if (swap_ends)␉return OSSwapInt32(x);␊ |
44 | else␉␉␉return x;␊ |
45 | }␊ |
46 | ␊ |
47 | int␊ |
48 | main(int argc, char *argv[])␊ |
49 | {␊ |
50 | kern_return_t␉result;␊ |
51 | vm_address_t␉data;␊ |
52 | int␉␉␉nc, ncmds;␊ |
53 | char *␉␉cp;␊ |
54 | ␊ |
55 | if (argc == 2)␊ |
56 | ␉{␊ |
57 | ␉␉infile = open(argv[1], O_RDONLY);␊ |
58 | ␉␉if (infile < 0)␉␉goto usage;␊ |
59 | ␉␉outfile = fileno(stdout);␊ |
60 | }␊ |
61 | else if (argc == 3)␊ |
62 | ␉{␊ |
63 | ␉infile = open(argv[1], O_RDONLY);␊ |
64 | ␉␉if (infile < 0)␉␉goto usage;␊ |
65 | ␉␉outfile = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);␊ |
66 | ␉␉if (outfile < 0)␉goto usage;␊ |
67 | }␊ |
68 | else ␊ |
69 | ␉{␊ |
70 | ␉usage:␊ |
71 | ␉fprintf(stderr, "usage: machOconv inputfile [outputfile]\n");␊ |
72 | ␉␉exit(1);␊ |
73 | }␊ |
74 | ␊ |
75 | nc = read(infile, &mh, sizeof (mh));␊ |
76 | if (nc < 0)␊ |
77 | ␉{␊ |
78 | ␉␉perror("read mach header");␊ |
79 | ␉␉exit(1);␊ |
80 | }␊ |
81 | ␉␊ |
82 | if (nc < (int)sizeof (mh))␊ |
83 | ␉{␊ |
84 | ␉␉fprintf(stderr, "read mach header: premature EOF %d\n", nc);␊ |
85 | ␉␉exit(1);␊ |
86 | }␊ |
87 | ␉␊ |
88 | ␉␊ |
89 | if (mh.magic == MH_MAGIC)␉␉swap_ends = false;␊ |
90 | else if (mh.magic == MH_CIGAM)␉swap_ends = true;␊ |
91 | else␊ |
92 | ␉{␊ |
93 | ␉fprintf(stderr, "bad magic number %lx\n", (unsigned long)mh.magic);␊ |
94 | ␉␉exit(1);␊ |
95 | }␊ |
96 | ␉␊ |
97 | cmds = calloc(swap(mh.sizeofcmds), sizeof (char));␊ |
98 | if (cmds == 0)␊ |
99 | ␉{␊ |
100 | ␉␉fprintf(stderr, "alloc load commands: no memory\n");␊ |
101 | ␉␉exit(1);␊ |
102 | }␊ |
103 | nc = read(infile, cmds, swap(mh.sizeofcmds));␊ |
104 | if (nc < 0) ␊ |
105 | ␉{␊ |
106 | ␉␉perror("read load commands");␊ |
107 | ␉␉exit(1);␊ |
108 | }␊ |
109 | if (nc < (int)swap(mh.sizeofcmds))␊ |
110 | ␉{␊ |
111 | ␉␉fprintf(stderr, "read load commands: premature EOF %d\n", nc);␊ |
112 | ␉␉exit(1);␊ |
113 | }␊ |
114 | ␊ |
115 | ␉unsigned long vmstart = (unsigned long)-1;␊ |
116 | ␊ |
117 | ␉// First pass: determine actual load address␊ |
118 | ␉for (ncmds = swap(mh.ncmds), cp = cmds;␊ |
119 | ␉␉ ncmds > 0; ncmds--)␊ |
120 | ␉{␊ |
121 | #define lcp␉((struct load_command *)cp) ␊ |
122 | #define scp␉((struct segment_command *)cp)␊ |
123 | ␉␉␊ |
124 | ␉␉switch(swap(lcp->cmd))␊ |
125 | ␉␉{␊ |
126 | ␉␉␉case LC_SEGMENT:␊ |
127 | ␉␉␉␉if(vmstart > swap(scp->vmaddr)) ␊ |
128 | ␉␉␉␉{␊ |
129 | ␉␉␉␉␉vmstart = swap(scp->vmaddr);␊ |
130 | ␉␉␉␉}␊ |
131 | ␉␉}␊ |
132 | ␉␉cp += swap(lcp->cmdsize);␊ |
133 | ␊ |
134 | ␉}␊ |
135 | ␉␊ |
136 | ␉// Second pass: output to file.␊ |
137 | for (ncmds = swap(mh.ncmds), cp = cmds;␊ |
138 | ␉␉ ncmds > 0; ncmds--)␊ |
139 | ␉{␊ |
140 | #define lcp␉((struct load_command *)cp) ␊ |
141 | #define scp␉((struct segment_command *)cp)␊ |
142 | ␊ |
143 | ␉ bool␉isDATA;␊ |
144 | ␉ unsigned␉vmsize;␊ |
145 | ␉␉␊ |
146 | ␉␉switch(swap(lcp->cmd))␊ |
147 | ␉␉{␊ |
148 | ␉␉␉case LC_SEGMENT:␊ |
149 | ␉␉␉␉isDATA = (strcmp(scp->segname, "__DATA") == 0);␊ |
150 | ␉␉␉␉if (isDATA)␊ |
151 | ␉␉␉␉{␊ |
152 | ␉␉␉␉␉vmsize = swap(scp->filesize);␊ |
153 | ␉␉␉␉}␊ |
154 | ␉␉␉␉else␊ |
155 | ␉␉␉␉{␊ |
156 | ␉␉␉␉␉vmsize = swap(scp->vmsize);␊ |
157 | ␉␉␉␉}␊ |
158 | ␉␉␉␉␊ |
159 | ␉␉␉␉result = vm_allocate(mach_task_self(), &data, vmsize, true);␊ |
160 | ␉␉␉␉if (result != KERN_SUCCESS) {␊ |
161 | ␉␉␉␉␉mach_error("vm_allocate segment data", result);␊ |
162 | ␉␉␉␉␉exit(1);␊ |
163 | ␉␉␉␉}␊ |
164 | ␉␉␉␉␊ |
165 | ␉␉␉␉lseek(infile, swap(scp->fileoff), L_SET);␊ |
166 | ␉␉␉␉nc = read(infile, (void *)data, swap(scp->filesize));␊ |
167 | ␉␉␉␉if (nc < 0) {␊ |
168 | ␉␉␉␉␉perror("read segment data");␊ |
169 | ␉␉␉␉␉exit(1);␊ |
170 | ␉␉␉␉}␊ |
171 | ␉␉␉␉if (nc < (int)swap(scp->filesize)) {␊ |
172 | ␉␉␉␉␉fprintf(stderr, "read segment data: premature EOF %d\n", nc);␊ |
173 | ␉␉␉␉␉exit(1);␊ |
174 | ␉␉␉␉}␊ |
175 | ␉␉␉␉␊ |
176 | ␉␉␉␉lseek(outfile, swap(scp->vmaddr) - vmstart, L_SET);␊ |
177 | ␉␉␉␉nc = write(outfile, (void *)data, vmsize);␊ |
178 | ␉␉␉␉if (nc < (int)vmsize) {␊ |
179 | ␉␉␉␉␉perror("write segment data");␊ |
180 | ␉␉␉␉␉exit(1);␊ |
181 | ␉␉␉␉}␊ |
182 | ␉␉␉␉␊ |
183 | ␉␉␉␉vm_deallocate(mach_task_self(), data, vmsize);␊ |
184 | ␉␉␉␉break;␊ |
185 | ␉␉}␊ |
186 | ␉␉␊ |
187 | ␉␉cp += swap(lcp->cmdsize);␊ |
188 | }␊ |
189 | ␉␊ |
190 | exit(0);␊ |
191 | }␊ |
192 |