Debian Codegraph @

apple.c

raw symbols SCIP
1 /*
2 * apple.c
3 * Detection of Apple partition maps and file systems
4 *
5 * Copyright (c) 2003 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 /*
31 * Apple partition map detection
32 */
33
34 void detect_apple_partmap(SECTION *section, int level)
35 {
36 int i, magic, count;
37 char s[256], append[64];
38 unsigned char *buf;
39 u8 start, size;
40
41 /* partition maps only occur at the start of a device */
42 /* disabled to allow for APM maps in El Torito entries
43 if (section->pos != 0)
44 return;
45 */
46
47 /*
48 if (buf[off] == 0x45 && buf[off+1] == 0x52) {
49 print_line(level, "HFS driver description map at sector %d", i);
50 }
51 */
52
53 if (get_buffer(section, 512, 512, (void **)&buf) < 512)
54 return;
55
56 magic = get_be_short(buf);
57 if (magic == 0x5453) {
58 print_line(level, "Old-style Apple partition map");
59 return;
60 }
61 if (magic != 0x504D)
62 return;
63
64 /* get partition count and print info */
65 count = get_be_long(buf + 4);
66 print_line(level, "Apple partition map, %d entries", count);
67
68 for (i = 1; i <= count; i++) {
69 /* read the right sector */
70 /* NOTE: the previous run through the loop might have called
71 * get_buffer indirectly, invalidating our old pointer */
72 if (i > 1 && get_buffer(section, i * 512, 512, (void **)&buf) < 512)
73 return;
74
75 /* check signature */
76 if (get_be_short(buf) != 0x504D) {
77 print_line(level, "Partition %d: invalid signature, skipping", i);
78 continue;
79 }
80
81 /* get position and size */
82 start = get_be_long(buf + 8);
83 size = get_be_long(buf + 12);
84 sprintf(append, " from %llu", start);
85 format_blocky_size(s, size, 512, "sectors", append);
86 print_line(level, "Partition %d: %s",
87 i, s);
88
89 /* get type */
90 get_string(buf + 48, 32, s);
91 print_line(level+1, "Type \"%s\"", s);
92
93 /* recurse for content detection */
94 if (start > count && size > 0) { /* avoid recursion on self */
95 analyze_recursive(section, level + 1,
96 start * 512, size * 512, 0);
97 }
98 }
99 }
100
101 /*
102 * Apple volume formats: MFS, HFS, HFS Plus
103 */
104
105 void detect_apple_volume(SECTION *section, int level)
106 {
107 char s[256], t[514];
108 unsigned char *buf;
109 u2 magic, version, volnamelen;
110 u4 blocksize, blockstart;
111 u8 blockcount, offset;
112 u8 catalogstart, cataloglength;
113 u4 firstleafnode, nodesize;
114
115 if (get_buffer(section, 1024, 512, (void **)&buf) < 512)
116 return;
117
118 magic = get_be_short(buf);
119 version = get_be_short(buf + 2);
120
121 if (magic == 0xD2D7) {
122 print_line(level, "MFS file system");
123
124 } else if (magic == 0x4244) {
125 print_line(level, "HFS file system");
126 blockcount = get_be_short(buf + 18);
127 blocksize = get_be_long(buf + 20);
128 blockstart = get_be_short(buf + 28);
129
130 get_pstring(buf + 36, s);
131 format_ascii(s, t);
132 print_line(level + 1, "Volume name \"%s\"", t);
133
134 format_blocky_size(s, blockcount, blocksize, "blocks", NULL);
135 print_line(level + 1, "Volume size %s", s);
136
137 if (get_be_short(buf + 0x7c) == 0x482B) {
138 print_line(level, "HFS wrapper for HFS Plus");
139
140 offset = (u8)get_be_short(buf + 0x7e) * blocksize +
141 (u8)blockstart * 512;
142 /* TODO: size */
143
144 analyze_recursive(section, level + 1,
145 offset, 0, 0);
146 }
147
148 } else if (magic == 0x482B) {
149 print_line(level, "HFS Plus file system");
150
151 blocksize = get_be_long(buf + 40);
152 blockcount = get_be_long(buf + 44);
153
154 format_blocky_size(s, blockcount, blocksize, "blocks", NULL);
155 print_line(level + 1, "Volume size %s", s);
156
157 /* To read the volume name, we have to parse some structures...
158 This code makes many assumptions which are usually true,
159 but don't have to be. */
160
161 /* get catalog file location on disk */
162 /* ASSUMPTION: This reads the location of the first extent
163 of the catalog file. If the catalog file is fragmented, we'll
164 be working with only the first fragment, which may not include
165 the node we're looking for. */
166 catalogstart = get_be_long(buf + 288) * blocksize;
167 cataloglength = get_be_long(buf + 292) * blocksize;
168 /* limit to actual length (byte count instead of block count) */
169 if (cataloglength > get_be_quad(buf + 272))
170 cataloglength = get_be_quad(buf + 272);
171
172 /* read header node of B-tree (4096 is the minimum node size) */
173 if (get_buffer(section, catalogstart, 4096, (void **)&buf) < 4096)
174 return;
175 firstleafnode = get_be_long(buf + 24);
176 nodesize = get_be_short(buf + 32);
177 if (nodesize < 4096)
178 return; /* illegal value */
179
180 /* read first lead node */
181 if ((firstleafnode + 1) * nodesize > cataloglength)
182 return; /* the location is beyond the end of the catalog */
183 if (get_buffer(section, catalogstart + firstleafnode * nodesize,
184 nodesize, (void **)&buf) < nodesize)
185 return;
186
187 /* the first record in this leaf node should be for parent id 1 */
188 if (buf[8] != 0xff)
189 return; /* not a leaf node */
190 if (get_be_short(buf + 14) <= 6)
191 return; /* key of first record is too short to contain a name */
192 if (get_be_long(buf + 16) != 1)
193 return; /* parent folder id is not "root parent" */
194 volnamelen = get_be_short(buf + 20);
195 format_utf16_be(buf + 22, volnamelen * 2, t);
196 print_line(level + 1, "Volume name \"%s\"", t);
197 }
198 }
199
200 /*
201 * Apple UDIF disk images
202 */
203
204 void detect_udif(SECTION *section, int level)
205 {
206 u8 pos;
207 unsigned char *buf;
208
209 if (section->size < 1024 || section->source->sequential)
210 return;
211
212 pos = section->size - 512;
213 if (get_buffer(section, pos, 512, (void **)&buf) < 512)
214 return;
215
216 if (memcmp(buf, "koly", 4) == 0) {
217 print_line(level, "Apple UDIF disk image, content detection may or may not work...");
218 }
219 }
220
221 /* EOF */