aboutsummaryrefslogtreecommitdiff
path: root/atr2xfd.c
blob: 900ec865585d16363e0819524416d965afab66b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#ifndef VERSION
#define VERSION "???"
#endif

#define SELF "atr2xfd"
#define CHECK "atrcheck"

char *self = SELF;

void classify_boot_sector(FILE *);

int main(int argc, char **argv) {
	char *type;
	struct stat st;
	char infile[4096], outfile[4096];
	unsigned char buf[16];
	FILE *in, *out;
	int i, paras, hparas, secsize, seccount;
	int checkonly = 0, bytes = 0;

	if(strstr(argv[0], CHECK)) {
		self = CHECK;
		checkonly = 1;
	}

	if(argc < 2 || argc > 3) {
		fprintf(stderr,
				"%s v" VERSION " by B. Watson (WTFPL)\n", self);
		fprintf(stderr,
				"Usage: %s input.xfd%s\n",
				self,
				(checkonly ? "" : " [output.atr]"));
		exit(1);
	}

	strcpy(infile, argv[1]);
	if(argc == 3) {
		strcpy(outfile, argv[2]);
		if(checkonly)
			fprintf(stderr,
					"%s: output file not used with %s (ignoring it).\n",
					self, self);
	} else if(strcmp(infile, "-") == 0) {
		strcpy(outfile, "-");
	} else {
		char *p;
		strcpy(outfile, argv[1]);

		p = strstr(outfile, ".atr");
		if(!p) p = strstr(outfile, ".ATR");
		if(!p) p = outfile + strlen(outfile);
		strcpy(p, ".xfd");
	}

	if(!checkonly)
		fprintf(stderr, "%s: input '%s', output '%s'\n", self, infile, outfile);

	if(strcmp(infile, "-") == 0) {
		in = stdin;
	} else {
		if( !(in = fopen(infile, "rb")) ) {
			fprintf(stderr, "%s: (fatal) can't read %s: %s\n",
					self, infile, strerror(errno));
			exit(1);
		}
	}

	if(fstat(fileno(in), &st)) {
		fprintf(stderr, "%s: (fatal) can't stat %s: %s\n",
				self, infile, strerror(errno));
		exit(1);
	}

	/* A few sanity checks... */
	if(st.st_size < 400) {
		fprintf(stderr,
				"%s: (fatal) %s too small to be an ATR image (<400 bytes)\n",
				self, infile);
		exit(2);
	}

	if(st.st_size % 128 == 0 ) {
		fprintf(stderr,
				"%s: (fatal) %s looks like an XFD image, not an ATR\n",
				self, infile);
		exit(2);
	}

	if( (st.st_size - 16) % 128 != 0 ) {
		fprintf(stderr,
				"%s: (fatal) %s not a valid ATR image (not an even number "
				"of sectors)\n",
				self, infile);
		exit(2);
	}

	if(st.st_size > (65535 * 256)) {
		fprintf(stderr,
				"%s: (fatal) %s too large to be an ATR image (>16M)\n",
				self, infile);
		exit(2);
	}

	paras = st.st_size / 16 - 1;
	fprintf(stderr, "%s: size is %d 16-byte paragraphs\n", self, paras);

	if(fread(buf, 1, 16, in) != 16) {
		fprintf(stderr,
				"%s: (fatal) can't read ATR header from %s: %s\n",
				self, infile, strerror(errno));
		exit(1);
	}

	if( !(buf[0] == 0x96 && buf[1] == 0x02) ) {
		fprintf(stderr,
				"%s: (fatal) %s not an ATR file (no NICKATARI signature)!\n",
				self, infile);
		exit(2);
	}

	secsize = buf[4] + (buf[5] << 8);

	if( !(secsize == 128 || secsize == 256) ) {
		fprintf(stderr,
				"%s: (fatal) %s has invalid sector size %d\n",
				self, infile, secsize);
		exit(2);
	}

	if(secsize == 128)
		seccount = (st.st_size - 16) / secsize;
	else {
		seccount = (st.st_size - 400) / secsize + 3;
		if((st.st_size - 16) % 256 != 128)
			fprintf(stderr, "%s: partial sector at end of DD image, might "
					"be a truncated or bogus ATR.\n", self);
	}

	fprintf(stderr,
			"%s: sectors: %d, sector size: %d bytes",
			self, seccount, secsize);

	if(secsize == 256)
		fprintf(stderr, " (first 3 sectors are 128 bytes)");
	fputc('\n', stderr);

	if(secsize == 128) {
		if(seccount < 720)
			type = "short SS/SD image, <90K";
		else if(seccount == 720)
			type = "standard SS/SD image, 90K";
		else if(seccount == 1040)
			type = "1050 SS/ED image, 130K";
		else if(seccount == 1440)
			type = "XF551 DS/SD image, 180K";
		else
			type = "high-capacity floppy or hard disk image, SD";
	} else {
		if(seccount < 720)
			type = "short SS/DD image, <180K";
		else if(seccount == 720)
			type = "standard SS/DD image, 180K";
		else if(seccount == 1440)
			type = "XF551 DS/DD or ATR8000 SS/QD image, 360K";
		else if(seccount == 2880)
			type = "ATR8000 DS/QD or SS/PC image, 720K";
		else if(seccount == 5760)
			type = "ATR8000 PC 1.44M image, 1440K";
		else
			type = "high-capacity floppy or hard disk image, DD";
	}

	fprintf(stderr, "%s: %s is a %s\n", self, infile, type);

	if(checkonly) {
		classify_boot_sector(in);
	}

	hparas = buf[2] + (buf[3] << 8) + (buf[6] << 16);
	if(hparas != paras) {
		/* this is only a fatal error if checkonly is true */
		fprintf(stderr,
				"%s: (%s) %s file size (%d paragraphs) doesn't agree with "
				"ATR header (%d paragraphs). File may be truncated or corrupt.\n",
				self,
				(checkonly ? "fatal" : "warning"),
				infile, paras, hparas);

		if(checkonly)
			exit(1);

		fprintf(stderr,
				"%s: Using actual file size for XFD image; expect trouble.\n",
				self);
	}

	if(checkonly) {
		exit(0);
	}

	fprintf(stderr, "%s: ATR image OK (no fatal errors).\n", self);

	/* Only open the output file after the ATR is known to be good */
	if(strcmp(outfile, "-") == 0) {
		out = stdout;
	} else {
		if( !(out = fopen(outfile, "wb")) ) {
			fprintf(stderr,
					"%s: (fatal) can't write %s: %s\n",
					self, outfile, strerror(errno));
			exit(1);
		}
	}

	/* copy the data */
	while( (i = fgetc(in)) != EOF ) {
		fputc(i, out);
		bytes++;
	}

	/* fputc() returns EOF on error *or* EOF;
		check for I/O errors, return 1 if so */
	i = 0;

	if(ferror(in)) {
		i = 1;
		fprintf(stderr,
				"%s: error reading %s: %s\n", self, infile, strerror(errno));
	}

	if(ferror(out)) {
		i = 1;
		fprintf(stderr,
				"%s: error writing %s: %s\n", self, outfile, strerror(errno));
	}

	fclose(in);
	fclose(out);

	if(!i)
		fprintf(stderr, "%s: XFD image OK, wrote %d bytes\n", self, bytes);

	return i;
}

/* these signatures are for the first N bytes of sector 1. */
unsigned char dos1_sig[] = "\x00\x01\x00\x07\x00\x13\x4c\x12";
unsigned char dos2_sig[] = "\x00\x03\x00\x07\x40\x15\x4c\x14";
unsigned char dos3_sig[] = "\x01\x09\x00\x32\x06\x32";
unsigned char dos4_sig[] = "\x00\x21\x01\x07\xf8\x07\x4c\xd9";
unsigned char dos4_noboot_sig[] = "\x00\x00\x00\x01\x00\x01\x01\x01";
unsigned char mydos30_sig[] = "\x00\x03\x00\x07\xd8\x17\x4c\x14";
unsigned char mydos31_sig[] = "\x00\x03\x00\x07\xe0\x07\x4c\x14";
unsigned char mydos4_sig[] = "\x4d\x03\x00\x07\xe0\x07\x4c\x14";
unsigned char topdos11_sig[] = "\xc0\x03\x00\x07\x40\x15\x4c\x24";
unsigned char topdos15_sig[] = "\x80\x03\x00\x07\x40\x15\x4c\x24";
unsigned char blank_sig[] = "\x00\x00\x00\x00\x00\x00\x00\x00";
unsigned char pico_sig[] = "\x00\x03\x00\x10\x10\x4c";
unsigned char superdos2_sig[] = "\x29\x03\x00\x07\x40\x15\xa0";
unsigned char superdos5_sig[] = "\x64\x03\x00\x07\x40\x15\xa9\x07";
unsigned char smartdos_sig[] = "\x00\x03\x00\x07\xd4\x15\x4c\x14";
unsigned char dosxe_sig[] = "\x58\x03\x00\x07\xca\x0c\x4c\x30";
unsigned char fenders_sig[] = "\x00\x03\x00\x07\x77\xe4\xa0\x00\x8c\x44";
unsigned char fenders2_sig[] = "\x00\x03\x00\x07\x77\xe4\xa0\x01\x8c\x44";

/* both of these claim to be "Turbo DOS XE version 2.1" */
unsigned char turbodos_sig[] = "\x04\x03\x00\x07\x40\x15\x4c\x16";
unsigned char turbodosxe_sig[] = "\x01\x03\x00\x07\x40\x15\x4c\x16";

/* these signatures are for offset $18 of sector 1. */
unsigned char dos2_code_sig[] = "\x36\xad\x12\x07";
unsigned char dos25_code_sig[] = "\x35\x20\x5f\x07";

#define SIG_MATCH(x, y) (memcmp(x, y, sizeof(y) - 1) == 0)

void classify_boot_sector(FILE *f) {
	unsigned char buf[128];
	char *dos_type = "Unknown", *boot_text = "";
	int bootable = -1; /* -1 = unknown, 0 = no, 1 = yes */
	int dos2_bootable, i;

	/* read 1st sector... */
	if( (fread(buf, 1, 128, f) < 128) ) {
		fprintf(stderr, "%s: EOF reading sector 1, truncated image.\n", self);
		exit(2);
	}

#if 1
	for(i = 0; i < 8; i++) fprintf(stderr, "\\x%02x", buf[i]);
	fputc('\n', stderr);
#endif

	dos2_bootable = (buf[14] != 0);

	/* now figure out what kind of boot sector this is */
	if(SIG_MATCH(buf, dos1_sig)) {
		dos_type = "DOS 1.0";
		bootable = dos2_bootable;
	} else if(SIG_MATCH(buf, dos3_sig)) {
		dos_type = "DOS 3.0";
		/* can't tell if bootable from just the boot sector, would have
			to search for FMS.SYS */
	} else if(SIG_MATCH(buf, dos4_sig)) {
		dos_type = "DOS 4.0";
		bootable = 1;
	} else if(SIG_MATCH(buf, dos4_noboot_sig)) {
		dos_type = "DOS 4.0";
		bootable = 0;
	} else if(SIG_MATCH(buf, dos2_sig)) {
		if(SIG_MATCH(buf + 0x18, dos2_code_sig))
			dos_type = "DOS 2.0";
		else if(SIG_MATCH(buf + 0x18, dos25_code_sig))
			dos_type = "DOS 2.5";
		else
			dos_type = "DOS 2.0 compatible";
		bootable = dos2_bootable;
	} else if(buf[0] == 0x00 && buf[1] == 0x03 &&
			(buf[32] == 0x11 || buf[32] == 0x20) &&
			buf[6] == 0x4c && buf[7] == 0x80) {
		if(buf[32] == 0x11)
			dos_type = "SpartaDOS 1.1";
		else
			dos_type = "SpartaDOS >= 2";
	} else if(SIG_MATCH(buf, mydos30_sig)) {
		dos_type = "MyDOS 3.0x";
		bootable = dos2_bootable;
	} else if(SIG_MATCH(buf, mydos31_sig)) {
		dos_type = "MyDOS 3.1x";
		bootable = dos2_bootable;
	} else if(SIG_MATCH(buf, mydos4_sig)) {
		dos_type = "MyDOS 4.x";
		bootable = dos2_bootable;
	} else if(SIG_MATCH(buf, pico_sig)) {
		dos_type = "MyPicoDOS 4.x";
	} else if(SIG_MATCH(buf, topdos11_sig)) {
		dos_type = "Top DOS 1.1";
		bootable = !(buf[15] == 0 && buf[16] == 0);
	} else if(SIG_MATCH(buf, topdos15_sig)) {
		dos_type = "Top DOS 1.5";
		bootable = dos2_bootable;
	} else if(SIG_MATCH(buf, superdos2_sig)) {
		dos_type = "SuperDOS 2.9";
		bootable = (buf[7] != 0);
	} else if(SIG_MATCH(buf, superdos5_sig)) {
		dos_type = "SuperDOS 5.1";
	} else if(SIG_MATCH(buf, turbodos_sig)) {
		dos_type = "Turbo DOS XE";
	} else if(SIG_MATCH(buf, turbodosxe_sig)) {
		dos_type = "Turbo DOS XE";
	} else if(SIG_MATCH(buf, dosxe_sig)) {
		dos_type = "DOS XE";
	} else if(SIG_MATCH(buf, smartdos_sig)) {
		dos_type = "Smart DOS";
		bootable = dos2_bootable;
	} else if(SIG_MATCH(buf, fenders_sig) || SIG_MATCH(buf, fenders2_sig)) {
		dos_type = "Fenders 3-sector loader";
		bootable = 1;
	} else if(SIG_MATCH(buf, blank_sig)) {
		dos_type = "None (empty boot sector)";
		bootable = 0;
	}

	if(bootable == 0)
		boot_text = ", not bootable";
	else if(bootable == 1)
		boot_text = ", bootable";

	fprintf(stderr, "%s: DOS type is: %s%s\n", self, dos_type, boot_text);
}