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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/* Copyright (c) 1996-98, Timothy Mann */
/* $Id: mkdisk.c,v 1.12 2008/06/26 04:39:56 mann Exp $ */
/* This software may be copied, modified, and used for any purpose
* without fee, provided that (1) the above copyright notice is
* retained, and (2) modified versions are clearly marked as having
* been modified, with the modifier's name and the date included. */
/*
* mkdisk.c
* Make a blank (unformatted) emulated floppy or hard drive in a file,
* or write protect/unprotect an existing one.
*/
/* If available, use C11 fopen()'s exclusive open flag. Option -f overrides. */
#define _ISOC11_SOURCE 1
#define _XOPEN_SOURCE 500 /* unistd.h: getopt(), ...; sys/stat.h: fchmod() */
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
static const char *safe_write_mode = "wx";
static const char *unsafe_write_mode = "w";
typedef unsigned char Uchar;
#include "reed.h"
ReedHardHeader rhh;
void Usage(char *progname)
{
fprintf(stderr,
"Usage:\t%s -1 [-f] file\n"
"\t%s [-3] [-f] file\n"
"\t%s -k [-s sides] [-d density] [-8] [-i] [-f] file\n"
"\t%s -h [-c cyl] [-s sec] [-g gran] [-d dcyl] [-S] [-f] file\n"
"\t%s {-p|-u} {-1|-3|-k|-h} file\n",
progname, progname, progname, progname, progname);
exit(2);
}
int
main(int argc, char *argv[])
{
int jv1 = 0, jv3 = 0, dmk = 0, hard = 0;
int cyl = -1, sec = -1, gran = -1, dir = -1, eight = 0, ignden = 0, spill = 0;
int writeprot = 0, unprot = 0;
int i, c, oumask;
char *fname;
char const *write_mode = safe_write_mode;
FILE *f;
opterr = 0;
for (;;) {
c = getopt(argc, argv, "13khc:s:g:d:8ipuSf");
if (c == -1) break;
switch (c) {
case '1':
jv1 = 1;
break;
case '3':
jv3 = 1;
break;
case 'k':
dmk = 1;
break;
case 'h':
hard = 1;
break;
case 'c':
cyl = atoi(optarg);
break;
case 's':
sec = atoi(optarg);
break;
case 'g':
gran = atoi(optarg);
break;
case 'd':
dir = atoi(optarg);
break;
case '8':
eight = 1;
break;
case 'i':
ignden = 1;
break;
case 'p':
writeprot = 1;
break;
case 'u':
unprot = 1;
break;
case 'S':
spill = 1;
break;
case 'f':
write_mode = unsafe_write_mode;
break;
case '?':
default:
Usage(argv[0]);
break;
}
}
if (argc - optind != 1) Usage(argv[0]);
fname = argv[optind];
if (writeprot || unprot) {
/* Completely different functionality here */
struct stat st;
int newmode;
if (writeprot && unprot) {
fprintf(stderr,
"%s: -p and -u are mutually exclusive\n", argv[0]);
exit(2);
}
if (jv1 + jv3 + dmk + hard != 1) {
fprintf(stderr,
"%s: %s requires exactly one of -1, -3, -k, or -h\n",
argv[0], writeprot ? "-p" : "-u");
exit(2);
}
if (stat(fname, &st) < 0) {
perror(fname);
exit(1);
}
/* Make writable so we can poke inside if need be */
if (chmod(fname, st.st_mode | (S_IWUSR|S_IWGRP|S_IWOTH)) < 0) {
perror(fname);
exit(1);
}
f = fopen(fname, "r+");
if (f == NULL) {
perror(fname);
exit(1);
}
/* Poke inside */
if (jv1) {
/* No indication inside; nothing to do here */
} else if (jv3) {
/* Set the magic byte */
fseek(f, 256*34-1, 0);
putc(writeprot ? 0 : 0xff, f);
} else if (dmk) {
/* Set the magic byte */
putc(writeprot ? 0xff : 0, f);
} else if (hard) {
/* Set the magic bit */
fseek(f, 7, 0);
newmode = getc(f);
if (newmode == EOF) {
perror(fname);
exit(1);
}
newmode = (newmode & 0x7f) | (writeprot ? 0x80 : 0);
fseek(f, 7, 0);
putc(newmode, f);
}
/* Finish by chmoding the file appropriately */
if (writeprot) {
newmode = st.st_mode & ~(S_IWUSR|S_IWGRP|S_IWOTH);
} else {
oumask = umask(0);
umask(oumask);
newmode = st.st_mode |
(( (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)) >> 1 ) & ~oumask);
}
if (fchmod(fileno(f), newmode)) {
perror(fname);
exit(1);
}
fclose(f);
exit(0);
}
switch (jv1 + jv3 + dmk + hard) {
case 0:
jv3 = 1;
break;
case 1:
break;
default:
fprintf(stderr,
"%s: -1, -3, -k, and -h are mutually exclusive\n", argv[0]);
exit(2);
}
if ((jv1 || jv3) && (cyl >= 0 || sec >= 0 || gran >= 0 || dir >= 0)) {
fprintf(stderr,
"%s: -c, -s, -g, -d are not meaningful with -1 or -3\n", argv[0]);
exit(2);
}
if (dmk && (cyl >= 0 || gran >= 0)) {
fprintf(stderr, "%s: -c and -g are not meaningful with -k\n", argv[0]);
exit(2);
}
if (!dmk && (eight || ignden)) {
fprintf(stderr, "%s: -8 and -i are only meaningful with -k\n", argv[0]);
exit(2);
}
if (!hard && spill) {
fprintf(stderr, "%s: -S is only meaningful with -h\n", argv[0]);
exit(2);
}
if (jv1) {
/* Unformatted JV1 disk - just an empty file! */
f = fopen(fname, write_mode);
if (f == NULL) {
perror(fname);
exit(1);
}
} else if (jv3) {
/* Unformatted JV3 disk. */
f = fopen(fname, write_mode);
if (f == NULL) {
perror(fname);
exit(1);
}
for (i=0; i<(256*34); i++) {
putc(0xff, f);
}
} else if (dmk) {
/* Unformatted DMK disk */
/* Reuse flag letters s, d */
#define sides sec
#define density dir
if (sides == -1) sides = 2;
if (density == -1) density = 2;
if (sides != 1 && sides != 2) {
fprintf(stderr, "%s error: sides must be 1 or 2\n", argv[0]);
exit(2);
}
if (density < 1 || density > 2) {
fprintf(stderr, "%s error: density must be 1 or 2\n", argv[0]);
exit(2);
}
f = fopen(fname, write_mode);
if (f == NULL) {
perror(fname);
exit(1);
}
putc(0, f); /* 0: not write protected */
putc(0, f); /* 1: initially zero tracks */
if (eight) {
if (density == 1)
i = 0x14e0;
else
i = 0x2940;
} else {
if (density == 1)
i = 0x0cc0;
else
i = 0x1900;
}
putc(i & 0xff, f); /* 2: LSB of track length */
putc(i >> 8, f); /* 3: MSB of track length */
i = 0;
if (sides == 1) i |= 0x10;
if (density == 1) i |= 0x40;
if (ignden) i |= 0x80;
putc(i, f); /* 4: options */
putc(0, f); /* 5: reserved */
putc(0, f); /* 6: reserved */
putc(0, f); /* 7: reserved */
putc(0, f); /* 8: reserved */
putc(0, f); /* 9: reserved */
putc(0, f); /* a: reserved */
putc(0, f); /* b: reserved */
putc(0, f); /* c: MBZ */
putc(0, f); /* d: MBZ */
putc(0, f); /* e: MBZ */
putc(0, f); /* f: MBZ */
} else /* hard */ {
/* Unformatted hard disk */
/* We don't care about most of this header, but we generate
it just in case some user wants to exchange hard drives with
Matthew Reed's emulator or with Pete Cervasio's port of
xtrshard/dct to Jeff Vavasour's Model III/4 emulator.
*/
time_t tt = time(0);
struct tm *lt = localtime(&tt);
Uchar *rhhp;
int cksum;
if (cyl == -1) cyl = 202;
if (sec == -1) sec = 256;
if (gran == -1) gran = 8;
if (dir == -1) dir = 1;
if (cyl < 3) {
fprintf(stderr, "%s error: cyl < 3\n", argv[0]);
exit(2);
}
if (cyl > 256) {
fprintf(stderr, "%s error: cyl > 256\n", argv[0]);
exit(2);
}
if (cyl > 203) {
fprintf(stderr,
"%s warning: cyl > 203 is incompatible with XTRSHARD/DCT\n",
argv[0]);
}
if (sec < 4) {
fprintf(stderr, "%s error: sec < 4\n", argv[0]);
exit(2);
}
if (sec > 256) {
fprintf(stderr, "%s error: sec > 256\n", argv[0]);
exit(2);
}
if ((sec % 32) != 0) {
fprintf(stderr, "%s warning: %s\n", argv[0],
"(sec % 32) != 0 is incompatible with WD1000/1010 emulation");
if (sec > 32) {
fprintf(stderr, "%s warning: %s\n", argv[0],
"(sec % 32) != 0 and sec > 32 "
"is incompatible with Matthew Reed's emulators");
}
}
if (gran < 1) {
fprintf(stderr, "%s error: gran < 1\n", argv[0]);
exit(2);
}
if (gran > 8) {
fprintf(stderr, "%s error: gran > 8\n", argv[0]);
exit(2);
}
if (sec < gran) {
fprintf(stderr, "%s error: sec < gran\n", argv[0]);
exit(2);
}
if (sec % gran != 0) {
fprintf(stderr, "%s error: sec %% gran != 0\n", argv[0]);
exit(2);
}
if (sec / gran > 32) {
fprintf(stderr, "%s error: sec / gran > 32\n", argv[0]);
exit(2);
}
if (dir < 1) {
fprintf(stderr, "%s error: dir < 1\n", argv[0]);
exit(2);
}
if (dir >= cyl) {
fprintf(stderr, "%s error: dir >= cyl\n", argv[0]);
exit(2);
}
/* Determine how much space we have for the filename field in the
rhh struct. xtrs and mkdisk are designed to let this filename
overflow into the reserved field afterward, but we must still
do some sanity checking. */
size_t rhh_filename_len = sizeof(rhh.filename) / sizeof(rhh.filename[0]);
size_t rhh_res2_len = sizeof(rhh.res2) / sizeof(rhh.res2[0]);
size_t fname_len = strlen(fname);
size_t fname_max = rhh_filename_len + rhh_res2_len;
static const char *rhh_res2_str = "reserved part of hard disk image header";
rhh.id1 = 0x56;
rhh.id2 = 0xcb;
rhh.ver = 0x10;
rhh.cksum = 0; /* init for cksum computation */
rhh.blks = 1;
rhh.mb4 = 4;
rhh.media = 0;
rhh.flag1 = 0;
rhh.flag2 = rhh.flag3 = 0;
rhh.crtr = 0x42;
rhh.dfmt = 0;
rhh.mm = lt->tm_mon + 1;
rhh.dd = lt->tm_mday;
rhh.yy = lt->tm_year;
rhh.dparm = 0;
rhh.cyl = cyl;
rhh.sec = sec;
rhh.gran = gran;
rhh.dcyl = dir;
strcpy(rhh.label, "xtrshard");
if (fname_len > rhh_filename_len) {
if (spill) {
if (strlen(fname) > fname_max) {
fprintf(stderr, "%s error: spilled filename must be at most %zd"
" bytes\n", argv[0], fname_max);
exit(2);
} else {
fprintf(stderr, "%s warning: filename %s spilled into %s\n", argv[0],
fname, rhh_res2_str);
/* Copy filename string starting where we left off. Why memcpy? The
chars used for the reserved area and the filename may differ in
signedness, depending on the host platform. Caveat user. Also,
don't overread past the \0 string terminator in fname if it is
present. (On my system, that reads into main's envp.) */
size_t remaining_len = fname_len - rhh_filename_len;
memcpy(rhh.res2, &fname[rhh_filename_len],
(rhh_res2_len > remaining_len) ? remaining_len : rhh_res2_len);
}
} else {
fprintf(stderr, "%s warning: truncating filename inside hard disk image"
" header to %zd bytes\n", argv[0], rhh_filename_len);
}
}
/* Tell GCC 8 we know what we're doing. rhh.filename is a static buffer.
We truncate fname on purpose. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
strncpy(rhh.filename, fname, rhh_filename_len);
#pragma GCC diagnostic pop
cksum = 0;
rhhp = (Uchar *) &rhh;
for (i=0; i<=31; i++) {
cksum += rhhp[i];
}
rhh.cksum = ((Uchar) cksum) ^ 0x4c;
f = fopen(fname, write_mode);
if (f == NULL) {
perror(fname);
exit(1);
}
fwrite(&rhh, sizeof(rhh), 1, f);
}
fclose(f);
return 0;
}
// vim:set cin et sw=2 ts=2 tw=80:
|