Debian Codegraph @

atari.c

raw symbols SCIP
1 /*
2 * atari.c
3 * Detection of ATARI ST partition map
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 * ATARI ST partition map
32 */
33
34 static char * get_name_for_type(char *type)
35 {
36 if (memcmp(type, "GEM", 3) == 0)
37 return "Standard GEMDOS";
38 else if (memcmp(type, "BGM", 3) == 0)
39 return "Big GEMDOS";
40 else if (memcmp(type, "XGM", 3) == 0)
41 return "Extended";
42 return "Unknown";
43 }
44
45 static void detect_atari_partmap_ext(SECTION *section, u8 extbase, int level);
46
47 void detect_atari_partmap(SECTION *section, int level)
48 {
49 unsigned char *buf;
50 int i, off, used, flag, flags[4];
51 u4 start, size, starts[4], sizes[4];
52 char types[4][4], *type;
53 u2 atari_csum;
54 char s[256], append[64];
55
56 /* partition maps only occur at the start of a device */
57 if (section->pos != 0)
58 return;
59
60 if (get_buffer(section, 0, 512, (void **)&buf) < 512)
61 return;
62
63 /* check checksum */
64 atari_csum = 0;
65 for (i = 0; i < 512; i += 2)
66 atari_csum += get_be_short(buf + i);
67 if (atari_csum != 0x1234)
68 return;
69
70 /* get data and check if it's actually used */
71 used = 0;
72 for (off = 0x1c6, i = 0; i < 4; i++, off += 12) {
73 flags[i] = buf[off];
74 get_string(buf + off + 1, 3, types[i]);
75 starts[i] = get_be_long(buf + off + 4);
76 sizes[i] = get_be_long(buf + off + 8);
77 if ((flags[i] & 1) && starts[i] != 0 && sizes[i] != 0)
78 used = 1;
79 }
80 if (!used)
81 return;
82
83 /* print data and handle each partition */
84 print_line(level, "ATARI ST partition map");
85 for (i = 0; i < 4; i++) {
86 start = starts[i];
87 size = sizes[i];
88 flag = flags[i];
89 type = types[i];
90 if ((flag & 1) == 0)
91 continue;
92
93 sprintf(append, " from %lu", start);
94 if (flag & 0x80)
95 strcat(append, ", bootable");
96 format_blocky_size(s, size, 512, "sectors", append);
97 print_line(level, "Partition %d: %s",
98 i+1, s);
99
100 print_line(level + 1, "Type \"%s\" (%s)", type,
101 get_name_for_type(type));
102
103 if (memcmp(type, "XGM", 3) == 0) {
104 /* extended partition */
105 detect_atari_partmap_ext(section, start, level + 1);
106 } else {
107 /* recurse for content detection */
108 analyze_recursive(section, level + 1,
109 (u8)start * 512, (u8)size * 512, 0);
110 }
111 }
112 }
113
114 static void detect_atari_partmap_ext(SECTION *section, u8 extbase, int level)
115 {
116 unsigned char *buf;
117 int extpartnum = 5;
118 u8 tablebase, nexttablebase;
119 int i, off, flags[4];
120 u4 start, size, starts[4], sizes[4];
121 char types[4][4], *type;
122 char s[256], append[64];
123
124 for (tablebase = extbase; tablebase; tablebase = nexttablebase) {
125 /* read sector from linked list */
126 if (get_buffer(section, tablebase << 9, 512, (void **)&buf) < 512)
127 return;
128
129 /* get data */
130 for (off = 0x1c6, i = 0; i < 4; i++, off += 12) {
131 flags[i] = buf[off];
132 get_string(buf + off + 1, 3, types[i]);
133 starts[i] = get_be_long(buf + off + 4);
134 sizes[i] = get_be_long(buf + off + 8);
135 }
136
137 /* print data and handle each partition */
138 nexttablebase = 0;
139 for (i = 0; i < 4; i++) {
140 start = starts[i];
141 size = sizes[i];
142 type = types[i];
143 if ((flags[i] & 1) == 0)
144 continue;
145
146 if (memcmp(type, "XGM", 3) == 0) {
147 /* link to next table */
148
149 nexttablebase = extbase + start;
150
151 } else {
152 /* real partition */
153
154 sprintf(append, " from %lu", start);
155 format_blocky_size(s, size, 512, "sectors", append);
156 print_line(level, "Partition %d: %s",
157 extpartnum, s);
158 extpartnum++;
159
160 print_line(level + 1, "Type \"%s\" (%s)", type,
161 get_name_for_type(type));
162
163 /* recurse for content detection */
164 analyze_recursive(section, level + 1,
165 (tablebase + start) * 512, (u8)size * 512, 0);
166 }
167 }
168 }
169 }
170
171 /* EOF */