fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pfdc: Add a program to manipulate PFDC image files

Hampa Hug f0a82946 0f47f9f9

+2671
+17
Makefile.dep
··· 2282 2282 src/devices/block/block.h \ 2283 2283 src/devices/block/pfdc.h \ 2284 2284 src/utils/pceimg/pce-img.h 2285 + 2286 + src/utils/pfdc/main.o: src/utils/pfdc/main.c \ 2287 + src/config.h \ 2288 + src/devices/block/pfdc-img-raw.h \ 2289 + src/devices/block/pfdc.h \ 2290 + src/lib/getopt.h \ 2291 + src/utils/pfdc/main.h \ 2292 + src/utils/pfdc/pfdc-img-io.h 2293 + 2294 + src/utils/pfdc/pfdc-img-io.o: src/utils/pfdc/pfdc-img-io.c \ 2295 + src/devices/block/pfdc-img-ana.h \ 2296 + src/devices/block/pfdc-img-imd.h \ 2297 + src/devices/block/pfdc-img-pfdc.h \ 2298 + src/devices/block/pfdc-img-raw.h \ 2299 + src/devices/block/pfdc-img-td0.h \ 2300 + src/devices/block/pfdc.h \ 2301 + src/utils/pfdc/pfdc-img-io.h
+1
Makefile.inc.in
··· 143 143 include $(srcdir)/src/arch/sims32/Makefile.inc 144 144 include $(srcdir)/src/utils/Makefile.inc 145 145 include $(srcdir)/src/utils/pceimg/Makefile.inc 146 + include $(srcdir)/src/utils/pfdc/Makefile.inc 146 147 147 148 TARGETS += $(BIN) $(ETC) $(SHARE) 148 149
+27
src/utils/pfdc/Makefile.inc
··· 1 + # src/utils/pfdc/Makefile.inc 2 + 3 + rel := src/utils/pfdc 4 + 5 + DIRS += $(rel) 6 + DIST += $(rel)/Makefile.inc 7 + 8 + PFDC_BAS := main pfdc-img-io 9 + PFDC_SRC := $(foreach f,$(PFDC_BAS),$(rel)/$(f).c) 10 + PFDC_OBJ := $(foreach f,$(PFDC_BAS),$(rel)/$(f).o) 11 + PFDC_HDR := $(foreach f,$(PFDC_BAS),$(rel)/$(f).h) 12 + PFDC_MAN1 := $(rel)/pfdc.1 13 + PFDC_BIN := $(rel)/pfdc$(EXEEXT) 14 + 15 + PFDC_OBJ_EXT := $(DEV_BLK_PFDC_OBJ) src/lib/getopt.o 16 + 17 + BIN += $(PFDC_BIN) 18 + MAN1 += $(PFDC_MAN1) 19 + CLN += $(PFDC_BIN) $(PFDC_OBJ) 20 + DIST += $(PFDC_SRC) $(PFDC_HDR) $(PFDC_MAN1) 21 + 22 + $(rel)/main.o: $(rel)/main.c 23 + $(rel)/pfdc-img-io.o: $(rel)/pfdc-img-io.c 24 + 25 + $(rel)/pfdc$(EXEEXT): $(PFDC_OBJ_EXT) $(PFDC_OBJ) 26 + $(QP)echo " LD $@" 27 + $(QR)$(LD) $(LDFLAGS_DEFAULT) -o $@ $(PFDC_OBJ) $(PFDC_OBJ_EXT)
+2099
src/utils/pfdc/main.c
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/utils/pfdc/main.c * 7 + * Created: 2010-08-13 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2010 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #include <config.h> 24 + 25 + #include <stdlib.h> 26 + #include <stdio.h> 27 + #include <string.h> 28 + 29 + #include <lib/getopt.h> 30 + 31 + #include "main.h" 32 + #include <devices/block/pfdc.h> 33 + #include <devices/block/pfdc-img-raw.h> 34 + #include "pfdc-img-io.h" 35 + 36 + 37 + #define PFDC_TRK_ALTERNATE (1 << 0) 38 + #define PFDC_TRK_BAD_ID (1 << 1) 39 + #define PFDC_TRK_RANGE (1 << 2) 40 + #define PFDC_TRK_SIZE (1 << 3) 41 + #define PFDC_TRK_ENCODING (1 << 4) 42 + 43 + 44 + typedef int (*pfdc_trk_cb) (pfdc_img_t *img, pfdc_trk_t *trk, 45 + unsigned c, unsigned h, void *opaque 46 + ); 47 + 48 + typedef int (*pfdc_sct_cb) (pfdc_img_t *img, pfdc_sct_t *sct, 49 + unsigned c, unsigned h, unsigned s, unsigned a, void *opaque 50 + ); 51 + 52 + 53 + static const char *arg0 = NULL; 54 + 55 + static int par_verbose = 0; 56 + 57 + static int par_list_tracks = 0; 58 + static int par_list_sectors = 0; 59 + static int par_print_info = 0; 60 + 61 + static unsigned par_fmt_inp = PFDC_FORMAT_NONE; 62 + static unsigned par_fmt_out = PFDC_FORMAT_NONE; 63 + 64 + static unsigned par_filler = 0xf6; 65 + 66 + static unsigned long par_cnt; 67 + 68 + static int par_cyl_all = 1; 69 + static unsigned par_cyl[2]; 70 + 71 + static int par_trk_all = 1; 72 + static unsigned par_trk[2]; 73 + 74 + static int par_sct_all = 1; 75 + static unsigned par_sct[2]; 76 + 77 + static int par_rsc_all = 1; 78 + static unsigned par_rsc[2]; 79 + 80 + static int par_alt_all = 1; 81 + static unsigned par_alt[2]; 82 + 83 + static unsigned par_order_cnt = 0; 84 + static unsigned char par_order[256]; 85 + 86 + 87 + static pce_option_t opts[] = { 88 + { '?', 0, "help", NULL, "Print usage information" }, 89 + { 'a', 1, "alternate", "a", "Select alternate sectors" }, 90 + { 'c', 1, "cylinder", "c", "Select cylinders [all]" }, 91 + { 'e', 2, "edit", "what val", "Edit selected sector attributes" }, 92 + { 'f', 0, "info", NULL, "Print image information [no]" }, 93 + { 'F', 1, "filler", "val", "Set the sector fill byte [0xf6]" }, 94 + { 'h', 1, "head", "h", "Select heads [all]" }, 95 + { 'i', 1, "input", "filename", "Load an input file" }, 96 + { 'I', 1, "input-format", "format", "Set the input format [auto]" }, 97 + { 'l', 0, "list-tracks", NULL, "List tracks [no]" }, 98 + { 'L', 0, "list-sectors", NULL, "List sectors [no]" }, 99 + { 'm', 1, "merge", "filename", "Merge an image" }, 100 + { 'n', 1, "new", "size", "Create a standard image of <size> KiB" }, 101 + { 'o', 1, "output", "filename", "Set the output file name [none]" }, 102 + { 'O', 1, "output-format", "format", "Set the output format [auto]" }, 103 + { 'p', 1, "operation", "name [...]", "Perform an operation" }, 104 + { 'r', 3, "record", "c h s", "Select sectors [all all all]" }, 105 + { 's', 1, "sectors", "s", "Select logical sectors [all]" }, 106 + { 'S', 1, "real-sectors", "s", "Select real sectors [all]" }, 107 + { 'v', 0, "verbose", NULL, "Verbose operation [no]" }, 108 + { 'V', 0, "version", NULL, "Print version information" }, 109 + { -1, 0, NULL, NULL, NULL } 110 + }; 111 + 112 + 113 + static 114 + void print_help (void) 115 + { 116 + pce_getopt_help ( 117 + "pfdc: convert and modify PCE FDC image files", 118 + "usage: pfdc [options] [input] [options] [output]", 119 + opts 120 + ); 121 + 122 + fputs ( 123 + "\n" 124 + "operations are:\n" 125 + " comment-add text Add to the image comment\n" 126 + " comment-load filename Load the image comment from a file\n" 127 + " comment-print Print the image comment\n" 128 + " comment-save filename Save the image comment to a file\n" 129 + " comment-set text Set the image comment\n" 130 + " delete Delete sectors\n" 131 + " info Print image information\n" 132 + " load filename Load individual sectors\n" 133 + " new Create new sectors\n" 134 + " reorder s1,s2,... Reorder sectors in a track\n" 135 + " rotate first Rotate tracks\n" 136 + " save filename Save individual sectors\n" 137 + "\n" 138 + "file formats are:\n" 139 + " pfdc, ana, imd, raw, td0\n" 140 + "\n" 141 + "sector attributes are:\n" 142 + " crc-id, crc-data, del-dam, data-rate, fm, mfm, size, c, h, s\n", 143 + stdout 144 + ); 145 + 146 + fflush (stdout); 147 + } 148 + 149 + static 150 + void print_version (void) 151 + { 152 + fputs ( 153 + "pfdc version " PCE_VERSION_STR 154 + "\n\n" 155 + "Copyright (C) 2010 Hampa Hug <hampa@hampa.ch>\n", 156 + stdout 157 + ); 158 + 159 + fflush (stdout); 160 + } 161 + 162 + 163 + static 164 + int pfdc_parse_range (const char *str, unsigned *v1, unsigned *v2, int *all) 165 + { 166 + *v1 = 0; 167 + *v2 = 0; 168 + *all = 0; 169 + 170 + if (strcmp (str, "all") == 0) { 171 + *all = 1; 172 + return (0); 173 + } 174 + 175 + while ((*str >= '0') && (*str <= '9')) { 176 + *v1 = 10 * *v1 + (*str - '0'); 177 + str += 1; 178 + } 179 + 180 + if (*str == '-') { 181 + str += 1; 182 + 183 + if (*str == 0) { 184 + *v2 = ~(unsigned) 0; 185 + return (0); 186 + } 187 + 188 + while ((*str >= '0') && (*str <= '9')) { 189 + *v2 = 10 * *v2 + (*str - '0'); 190 + str += 1; 191 + } 192 + } 193 + else { 194 + *v2 = *v1; 195 + } 196 + 197 + 198 + if (*str != 0) { 199 + return (1); 200 + } 201 + 202 + return (0); 203 + } 204 + 205 + static 206 + int pfdc_parse_int_list (const char **str, unsigned *val) 207 + { 208 + const char *s; 209 + 210 + s = *str; 211 + 212 + if ((*s < '0') || (*s > '9')) { 213 + return (1); 214 + } 215 + 216 + *val = 0; 217 + 218 + while ((*s >= '0') && (*s <= '9')) { 219 + *val = 10 * *val + (*s - '0'); 220 + s += 1; 221 + } 222 + 223 + if (*s == ',') { 224 + s += 1; 225 + } 226 + 227 + *str = s; 228 + 229 + return (0); 230 + } 231 + 232 + static 233 + int pfdc_sel_match_track (unsigned c, unsigned h) 234 + { 235 + if (par_cyl_all == 0) { 236 + if ((c < par_cyl[0]) || (c > par_cyl[1])) { 237 + return (0); 238 + } 239 + } 240 + 241 + if (par_trk_all == 0) { 242 + if ((h < par_trk[0]) || (h > par_trk[1])) { 243 + return (0); 244 + } 245 + } 246 + 247 + return (1); 248 + } 249 + 250 + static 251 + int pfdc_sel_match (unsigned c, unsigned h, unsigned s, unsigned r, unsigned a) 252 + { 253 + if (pfdc_sel_match_track (c, h) == 0) { 254 + return (0); 255 + } 256 + 257 + if (par_sct_all == 0) { 258 + if ((s < par_sct[0]) || (s > par_sct[1])) { 259 + return (0); 260 + } 261 + } 262 + 263 + if (par_rsc_all == 0) { 264 + if ((r < par_rsc[0]) || (r > par_rsc[1])) { 265 + return (0); 266 + } 267 + } 268 + 269 + if (par_alt_all == 0) { 270 + if ((a < par_alt[0]) || (a > par_alt[1])) { 271 + return (0); 272 + } 273 + } 274 + 275 + return (1); 276 + } 277 + 278 + static 279 + int pfdc_for_all_sectors (pfdc_img_t *img, pfdc_sct_cb fct, void *opaque) 280 + { 281 + unsigned c, h, s, a; 282 + pfdc_cyl_t *cyl; 283 + pfdc_trk_t *trk; 284 + pfdc_sct_t *sct; 285 + 286 + for (c = 0; c < img->cyl_cnt; c++) { 287 + cyl = img->cyl[c]; 288 + 289 + for (h = 0; h < cyl->trk_cnt; h++) { 290 + trk = cyl->trk[h]; 291 + 292 + for (s = 0; s < trk->sct_cnt; s++) { 293 + sct = trk->sct[s]; 294 + 295 + a = 0; 296 + while (sct != 0) { 297 + if (pfdc_sel_match (c, h, sct->s, s, a)) { 298 + if (fct (img, sct, c, h, s, a, opaque)) { 299 + return (1); 300 + } 301 + } 302 + 303 + sct = sct->next; 304 + a += 1; 305 + } 306 + } 307 + } 308 + } 309 + 310 + return (0); 311 + } 312 + 313 + static 314 + int pfdc_for_all_tracks (pfdc_img_t *img, pfdc_trk_cb fct, void *opaque) 315 + { 316 + unsigned c, h; 317 + pfdc_cyl_t *cyl; 318 + pfdc_trk_t *trk; 319 + 320 + for (c = 0; c < img->cyl_cnt; c++) { 321 + cyl = img->cyl[c]; 322 + 323 + for (h = 0; h < cyl->trk_cnt; h++) { 324 + trk = cyl->trk[h]; 325 + 326 + if (pfdc_sel_match_track (c, h) == 0) { 327 + continue; 328 + } 329 + 330 + if (fct (img, trk, c, h, opaque)) { 331 + return (1); 332 + } 333 + } 334 + } 335 + 336 + return (0); 337 + } 338 + 339 + static 340 + unsigned count_digits (unsigned long val) 341 + { 342 + unsigned n; 343 + 344 + n = 0; 345 + 346 + do { 347 + val = val / 10; 348 + n += 1; 349 + } while (val > 0); 350 + 351 + return (n); 352 + } 353 + 354 + static 355 + void print_ulong (FILE *fp, unsigned long val, unsigned w) 356 + { 357 + unsigned i; 358 + char buf[128]; 359 + 360 + i = 0; 361 + 362 + do { 363 + buf[i++] = (val % 10) + '0'; 364 + val = val / 10; 365 + 366 + if (w > 0) { 367 + w -= 1; 368 + } 369 + } while ((val > 0) && (i < 128)); 370 + 371 + while (w > 0) { 372 + fputc (' ', fp); 373 + w -= 1; 374 + } 375 + 376 + while (i > 0) { 377 + i -= 1; 378 + fputc (buf[i], fp); 379 + } 380 + } 381 + 382 + static 383 + const char *pfdc_enc_to_string (unsigned encoding) 384 + { 385 + switch (encoding) { 386 + case PFDC_ENC_FM: 387 + return ("FM"); 388 + 389 + case PFDC_ENC_MFM: 390 + return ("MFM"); 391 + 392 + default: 393 + return ("UNKNOWN"); 394 + } 395 + } 396 + 397 + static 398 + int pfdc_list_sectors_cb (pfdc_img_t *img, pfdc_trk_t *trk, 399 + unsigned c, unsigned h, void *opaque) 400 + { 401 + int alt; 402 + unsigned s; 403 + unsigned pcmax, phmax, psmax; 404 + unsigned lcmax, lhmax, lsmax; 405 + unsigned ssmax; 406 + unsigned long drmax; 407 + pfdc_sct_t *sct; 408 + 409 + if ((c > 0) || (h > 0)) { 410 + fputs ("\n", stdout); 411 + } 412 + 413 + pcmax = c; 414 + phmax = h; 415 + psmax = 0; 416 + 417 + lcmax = 0; 418 + lhmax = 0; 419 + lsmax = 0; 420 + 421 + ssmax = 0; 422 + 423 + drmax = 0; 424 + 425 + for (s = 0; s < trk->sct_cnt; s++) { 426 + psmax = (s > psmax) ? s : psmax; 427 + 428 + sct = trk->sct[s]; 429 + 430 + while (sct != NULL) { 431 + lcmax = (sct->c > lcmax) ? sct->c : lcmax; 432 + lhmax = (sct->h > lhmax) ? sct->h : lhmax; 433 + lsmax = (sct->s > lsmax) ? sct->s : lsmax; 434 + ssmax = (sct->n > ssmax) ? sct->n : ssmax; 435 + drmax = (sct->data_rate > drmax) ? sct->data_rate : drmax; 436 + sct = sct->next; 437 + } 438 + } 439 + 440 + pcmax = count_digits (pcmax); 441 + phmax = count_digits (phmax); 442 + psmax = count_digits (psmax); 443 + 444 + lcmax = count_digits (lcmax); 445 + lhmax = count_digits (lhmax); 446 + lsmax = count_digits (lsmax); 447 + 448 + ssmax = count_digits (ssmax); 449 + 450 + drmax = count_digits (drmax); 451 + 452 + for (s = 0; s < trk->sct_cnt; s++) { 453 + sct = trk->sct[s]; 454 + 455 + alt = (sct->next != NULL); 456 + 457 + while (sct != NULL) { 458 + print_ulong (stdout, c, pcmax); 459 + print_ulong (stdout, h, phmax + 1); 460 + print_ulong (stdout, s, psmax + 1); 461 + 462 + print_ulong (stdout, sct->c, lcmax + 3); 463 + print_ulong (stdout, sct->h, lhmax + 1); 464 + print_ulong (stdout, sct->s, lsmax + 1); 465 + 466 + print_ulong (stdout, sct->n, ssmax + 3); 467 + 468 + printf ("%6s", pfdc_enc_to_string (sct->encoding)); 469 + 470 + print_ulong (stdout, sct->data_rate, drmax + 1); 471 + 472 + if (sct->flags || alt) { 473 + fputs (" ", stdout); 474 + } 475 + 476 + if (alt) { 477 + fputs (" ALT", stdout); 478 + } 479 + 480 + if (sct->flags & PFDC_FLAG_CRC_ID) { 481 + fputs (" CRC-ID", stdout); 482 + } 483 + 484 + if (sct->flags & PFDC_FLAG_CRC_DATA) { 485 + fputs (" CRC-DATA", stdout); 486 + } 487 + 488 + if (sct->flags & PFDC_FLAG_DEL_DAM) { 489 + fputs (" DEL-DAM", stdout); 490 + } 491 + 492 + fputs ("\n", stdout); 493 + 494 + sct = sct->next; 495 + } 496 + } 497 + 498 + return (0); 499 + } 500 + 501 + static 502 + int pfdc_list_sectors (pfdc_img_t *img) 503 + { 504 + return (pfdc_for_all_tracks (img, pfdc_list_sectors_cb, NULL)); 505 + } 506 + 507 + 508 + static 509 + int pfdc_list_track_cb (pfdc_img_t *img, pfdc_trk_t *trk, 510 + unsigned c, unsigned h, void *opaque) 511 + { 512 + unsigned long sct_flg, trk_flg; 513 + unsigned s; 514 + pfdc_sct_t *sct; 515 + 516 + sct_flg = 0; 517 + trk_flg = 0; 518 + 519 + for (s = 0; s < trk->sct_cnt; s++) { 520 + sct = trk->sct[s]; 521 + 522 + if (sct->next != NULL) { 523 + trk_flg |= PFDC_TRK_ALTERNATE; 524 + } 525 + 526 + while (sct != NULL) { 527 + sct_flg |= sct->flags; 528 + 529 + if ((sct->c != c) || (sct->h != h)) { 530 + trk_flg |= PFDC_TRK_BAD_ID; 531 + } 532 + 533 + if ((sct->s < 1) || (sct->s > trk->sct_cnt)) { 534 + trk_flg |= PFDC_TRK_RANGE; 535 + } 536 + 537 + if (sct->n != 512) { 538 + trk_flg |= PFDC_TRK_SIZE; 539 + } 540 + 541 + if (sct->encoding != PFDC_ENC_MFM) { 542 + trk_flg |= PFDC_TRK_ENCODING; 543 + } 544 + 545 + sct = sct->next; 546 + } 547 + } 548 + 549 + printf ("%u %u %u", 550 + c, h, trk->sct_cnt 551 + ); 552 + 553 + if (trk_flg || sct_flg) { 554 + fputs (" ", stdout); 555 + } 556 + 557 + if (trk_flg & PFDC_TRK_BAD_ID) { 558 + fputs (" BAD-ID", stdout); 559 + } 560 + 561 + if (trk_flg & PFDC_TRK_SIZE) { 562 + fputs (" SIZE", stdout); 563 + } 564 + 565 + if (trk_flg & PFDC_TRK_ENCODING) { 566 + fputs (" ENCODING", stdout); 567 + } 568 + 569 + if (trk_flg & PFDC_TRK_RANGE) { 570 + fputs (" RANGE", stdout); 571 + } 572 + 573 + if (sct_flg & PFDC_FLAG_CRC_ID) { 574 + fputs (" CRC-ID", stdout); 575 + } 576 + 577 + if (sct_flg & PFDC_FLAG_CRC_DATA) { 578 + fputs (" CRC-DATA", stdout); 579 + } 580 + 581 + if (sct_flg & PFDC_FLAG_DEL_DAM) { 582 + fputs (" DEL-DAM", stdout); 583 + } 584 + 585 + fputs ("\n", stdout); 586 + 587 + return (0); 588 + } 589 + 590 + static 591 + int pfdc_list_tracks (pfdc_img_t *img) 592 + { 593 + return (pfdc_for_all_tracks (img, pfdc_list_track_cb, NULL)); 594 + } 595 + 596 + 597 + static 598 + int pfdc_delete_sectors_cb (pfdc_img_t *img, pfdc_trk_t *trk, 599 + unsigned c, unsigned h, void *opaque) 600 + { 601 + unsigned i, j, a; 602 + pfdc_sct_t *sct, *tmp; 603 + 604 + j = 0; 605 + 606 + for (i = 0; i < trk->sct_cnt; i++) { 607 + a = 0; 608 + 609 + sct = trk->sct[i]; 610 + 611 + while ((sct != NULL) && pfdc_sel_match (c, h, sct->s, i, a)) { 612 + tmp = sct; 613 + sct = tmp->next; 614 + tmp->next = NULL; 615 + 616 + pfdc_sct_del (tmp); 617 + 618 + par_cnt += 1; 619 + 620 + a += 1; 621 + } 622 + 623 + trk->sct[i] = sct; 624 + 625 + if (sct != NULL) { 626 + while (sct->next != NULL) { 627 + if (pfdc_sel_match (c, h, sct->s, i, a + 1) == 0) { 628 + tmp = sct->next; 629 + sct->next = tmp->next; 630 + tmp->next = NULL; 631 + 632 + pfdc_sct_del (tmp); 633 + 634 + par_cnt += 1; 635 + } 636 + else { 637 + sct = sct->next; 638 + } 639 + 640 + a += 1; 641 + } 642 + } 643 + 644 + if (trk->sct[i] != NULL) { 645 + trk->sct[j++] = trk->sct[i]; 646 + } 647 + } 648 + 649 + trk->sct_cnt = j; 650 + 651 + return (0); 652 + } 653 + 654 + static 655 + int pfdc_delete_sectors (pfdc_img_t *img) 656 + { 657 + int r; 658 + 659 + par_cnt = 0; 660 + 661 + r = pfdc_for_all_tracks (img, pfdc_delete_sectors_cb, NULL); 662 + 663 + if (par_verbose) { 664 + fprintf (stderr, "%s: delete %lu sectors\n", arg0, par_cnt); 665 + } 666 + 667 + if (r) { 668 + fprintf (stderr, "%s: deleting failed\n", arg0); 669 + } 670 + 671 + return (r); 672 + } 673 + 674 + 675 + static 676 + int pfdc_new_alternates (pfdc_img_t *img, pfdc_sct_t *sct, 677 + unsigned c, unsigned h) 678 + { 679 + unsigned a; 680 + 681 + if (par_alt_all) { 682 + return (0); 683 + } 684 + 685 + if (par_alt[1] == 0) { 686 + return (0); 687 + } 688 + 689 + for (a = 1; a <= par_alt[1]; a++) { 690 + if (sct->next == NULL) { 691 + sct->next = pfdc_sct_clone (sct, 0); 692 + 693 + if (sct->next == NULL) { 694 + return (1); 695 + } 696 + 697 + par_cnt += 1; 698 + } 699 + 700 + sct = sct->next; 701 + } 702 + 703 + return (0); 704 + } 705 + 706 + static 707 + int pfdc_new_sectors (pfdc_img_t *img, pfdc_trk_t *trk, unsigned c, unsigned h) 708 + { 709 + unsigned s; 710 + pfdc_sct_t *sct; 711 + 712 + if (par_sct_all) { 713 + for (s = 0; s < trk->sct_cnt; s++) { 714 + if (pfdc_new_alternates (img, trk->sct[s], c, h)) { 715 + return (1); 716 + } 717 + } 718 + } 719 + else { 720 + for (s = par_sct[0]; s <= par_sct[1]; s++) { 721 + sct = pfdc_img_get_sector (img, c, h, s, 0); 722 + 723 + if (sct == NULL) { 724 + sct = pfdc_sct_new (c, h, s, 512); 725 + 726 + if (sct == NULL) { 727 + return (1); 728 + } 729 + 730 + if (pfdc_trk_add_sector (trk, sct)) { 731 + pfdc_sct_del (sct); 732 + return (1); 733 + } 734 + 735 + pfdc_sct_fill (sct, par_filler); 736 + 737 + par_cnt += 1; 738 + } 739 + 740 + if (pfdc_new_alternates (img, sct, c, h)) { 741 + return (1); 742 + } 743 + } 744 + } 745 + 746 + return (0); 747 + } 748 + 749 + static 750 + int pfdc_new_tracks (pfdc_img_t *img, pfdc_cyl_t *cyl, unsigned c) 751 + { 752 + unsigned h, h0, h1; 753 + pfdc_trk_t *trk; 754 + 755 + if (par_trk_all) { 756 + h0 = 0; 757 + h1 = cyl->trk_cnt; 758 + } 759 + else { 760 + h0 = par_trk[0]; 761 + h1 = par_trk[1] + 1; 762 + } 763 + 764 + for (h = h0; h < h1; h++) { 765 + trk = pfdc_img_get_track (img, c, h, 1); 766 + 767 + if (trk == NULL) { 768 + return (1); 769 + } 770 + 771 + if (pfdc_new_sectors (img, trk, c, h)) { 772 + return (1); 773 + } 774 + } 775 + 776 + return (0); 777 + } 778 + 779 + static 780 + int pfdc_new_cylinders (pfdc_img_t *img) 781 + { 782 + unsigned c, c0, c1; 783 + pfdc_cyl_t *cyl; 784 + 785 + if (par_cyl_all) { 786 + c0 = 0; 787 + c1 = img->cyl_cnt; 788 + } 789 + else { 790 + c0 = par_cyl[0]; 791 + c1 = par_cyl[1] + 1; 792 + } 793 + 794 + for (c = c0; c < c1; c++) { 795 + cyl = pfdc_img_get_cylinder (img, c, 1); 796 + 797 + if (cyl == NULL) { 798 + return (1); 799 + } 800 + 801 + if (pfdc_new_tracks (img, cyl, c)) { 802 + return (1); 803 + } 804 + } 805 + 806 + return (0); 807 + } 808 + 809 + static 810 + int pfdc_new (pfdc_img_t **img) 811 + { 812 + int r; 813 + 814 + if (*img == NULL) { 815 + *img = pfdc_img_new(); 816 + 817 + if (*img == NULL) { 818 + return (1); 819 + } 820 + } 821 + 822 + par_cnt = 0; 823 + 824 + r = pfdc_new_cylinders (*img); 825 + 826 + if (par_verbose) { 827 + fprintf (stderr, "%s: create %lu sectors\n", arg0, par_cnt); 828 + } 829 + 830 + if (r) { 831 + fprintf (stderr, "%s: creating failed\n", arg0); 832 + } 833 + 834 + return (0); 835 + } 836 + 837 + int pfdc_new_standard (pfdc_img_t **img, unsigned long size) 838 + { 839 + unsigned c, h, s; 840 + 841 + if (size == 0) { 842 + *img = pfdc_img_new(); 843 + 844 + return (*img == NULL); 845 + } 846 + 847 + if (pfdc_get_geometry_from_size (1024 * size, &c, &h, &s)) { 848 + return (1); 849 + } 850 + 851 + par_cyl_all = 0; 852 + par_cyl[0] = 0; 853 + par_cyl[1] = c - 1; 854 + 855 + par_trk_all = 0; 856 + par_trk[0] = 0; 857 + par_trk[1] = h - 1; 858 + 859 + par_sct_all = 0; 860 + par_sct[0] = 1; 861 + par_sct[1] = s; 862 + 863 + par_rsc_all = 1; 864 + par_alt_all = 1; 865 + 866 + return (pfdc_new (img)); 867 + } 868 + 869 + 870 + static 871 + int pfdc_reorder_track_cb (pfdc_img_t *img, pfdc_trk_t *trk, 872 + unsigned c, unsigned h, void *opaque) 873 + { 874 + unsigned i, j; 875 + unsigned s1, s2; 876 + pfdc_sct_t *tmp; 877 + 878 + par_cnt += 1; 879 + 880 + for (i = 0; i < par_order_cnt; i++) { 881 + s1 = i; 882 + s2 = i; 883 + 884 + if (i >= trk->sct_cnt) { 885 + return (0); 886 + } 887 + 888 + for (j = i; j < trk->sct_cnt; j++) { 889 + if (trk->sct[j]->s == par_order[i]) { 890 + s2 = j; 891 + break; 892 + } 893 + } 894 + 895 + if ((j < trk->sct_cnt) && (i != j)) { 896 + tmp = trk->sct[i]; 897 + trk->sct[i] = trk->sct[j]; 898 + trk->sct[j] = tmp; 899 + } 900 + } 901 + 902 + return (0); 903 + } 904 + 905 + static 906 + int pfdc_reorder_tracks (pfdc_img_t *img, const char *order) 907 + { 908 + int r; 909 + unsigned i; 910 + unsigned val; 911 + 912 + i = 0; 913 + 914 + while (i < 256) { 915 + if (*order == 0) { 916 + break; 917 + } 918 + 919 + if (pfdc_parse_int_list (&order, &val)) { 920 + return (1); 921 + } 922 + 923 + par_order[i] = val; 924 + 925 + i += 1; 926 + } 927 + 928 + par_order_cnt = i; 929 + 930 + par_cnt = 0; 931 + 932 + r = pfdc_for_all_tracks (img, pfdc_reorder_track_cb, NULL); 933 + 934 + if (par_verbose) { 935 + fprintf (stderr, "%s: reorder %lu tracks\n", arg0, par_cnt); 936 + } 937 + 938 + if (r) { 939 + fprintf (stderr, "%s: reordering failed\n", arg0); 940 + } 941 + 942 + return (r); 943 + } 944 + 945 + 946 + static 947 + int pfdc_rotate_track_cb (pfdc_img_t *img, pfdc_trk_t *trk, 948 + unsigned c, unsigned h, void *opaque) 949 + { 950 + unsigned i, i0; 951 + unsigned first; 952 + pfdc_sct_t *sct; 953 + 954 + if (trk->sct_cnt == 0) { 955 + return (0); 956 + } 957 + 958 + par_cnt += 1; 959 + 960 + first = *(unsigned *) opaque; 961 + 962 + i0 = 0; 963 + 964 + for (i = 0; i < trk->sct_cnt; i++) { 965 + sct = trk->sct[i]; 966 + 967 + if (sct->s == first) { 968 + i0 = i; 969 + break; 970 + } 971 + 972 + if ((sct->s >= first) && (sct->s < trk->sct[i0]->s)) { 973 + i0 = i; 974 + } 975 + } 976 + 977 + while (i0 > 0) { 978 + sct = trk->sct[0]; 979 + 980 + for (i = 1; i < trk->sct_cnt; i++) { 981 + trk->sct[i - 1] = trk->sct[i]; 982 + } 983 + 984 + trk->sct[trk->sct_cnt - 1] = sct; 985 + 986 + i0 -= 1; 987 + } 988 + 989 + return (0); 990 + } 991 + 992 + static 993 + int pfdc_rotate_tracks (pfdc_img_t *img, unsigned first) 994 + { 995 + int r; 996 + 997 + par_cnt = 0; 998 + 999 + r = pfdc_for_all_tracks (img, pfdc_rotate_track_cb, &first); 1000 + 1001 + if (par_verbose) { 1002 + fprintf (stderr, "%s: rotate %lu tracks\n", arg0, par_cnt); 1003 + } 1004 + 1005 + if (r) { 1006 + fprintf (stderr, "%s: rotating failed\n", arg0); 1007 + } 1008 + 1009 + return (r); 1010 + } 1011 + 1012 + 1013 + static 1014 + int pfdc_load_sectors_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1015 + unsigned c, unsigned h, unsigned s, unsigned a, void *opaque) 1016 + { 1017 + FILE *fp; 1018 + 1019 + fp = opaque; 1020 + 1021 + pfdc_sct_fill (sct, 0); 1022 + 1023 + if (fread (sct->data, 1, sct->n, fp) != sct->n) { 1024 + ; 1025 + } 1026 + 1027 + par_cnt += 1; 1028 + 1029 + return (0); 1030 + } 1031 + 1032 + static 1033 + int pfdc_load_sectors (pfdc_img_t *img, const char *fname) 1034 + { 1035 + int r; 1036 + FILE *fp; 1037 + 1038 + fp = fopen (fname, "rb"); 1039 + 1040 + if (fp == NULL) { 1041 + fprintf (stderr, "%s: can't open file (%s)\n", arg0, fname); 1042 + return (1); 1043 + } 1044 + 1045 + par_cnt = 0; 1046 + 1047 + r = pfdc_for_all_sectors (img, pfdc_load_sectors_cb, fp); 1048 + 1049 + fclose (fp); 1050 + 1051 + if (par_verbose) { 1052 + fprintf (stderr, "%s: load %lu sectors\n", arg0, par_cnt); 1053 + } 1054 + 1055 + if (r) { 1056 + fprintf (stderr, "%s: loading sectors failed\n", arg0); 1057 + } 1058 + 1059 + return (r); 1060 + } 1061 + 1062 + 1063 + static 1064 + int pfdc_save_sectors_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1065 + unsigned c, unsigned h, unsigned s, unsigned a, void *opaque) 1066 + { 1067 + FILE *fp; 1068 + 1069 + fp = opaque; 1070 + 1071 + if (fwrite (sct->data, 1, sct->n, fp) != sct->n) { 1072 + return (1); 1073 + } 1074 + 1075 + par_cnt += 1; 1076 + 1077 + return (0); 1078 + } 1079 + 1080 + static 1081 + int pfdc_save_sectors (pfdc_img_t *img, const char *fname) 1082 + { 1083 + int r; 1084 + FILE *fp; 1085 + 1086 + fp = fopen (fname, "wb"); 1087 + 1088 + if (fp == NULL) { 1089 + fprintf (stderr, "%s: can't create file (%s)\n", arg0, fname); 1090 + return (1); 1091 + } 1092 + 1093 + par_cnt = 0; 1094 + 1095 + r = pfdc_for_all_sectors (img, pfdc_save_sectors_cb, fp); 1096 + 1097 + fclose (fp); 1098 + 1099 + if (par_verbose) { 1100 + fprintf (stderr, "%s: save %lu sectors to %s\n", 1101 + arg0, par_cnt, fname 1102 + ); 1103 + } 1104 + 1105 + if (r) { 1106 + fprintf (stderr, "%s: saving sectors failed\n", arg0); 1107 + } 1108 + 1109 + return (r); 1110 + } 1111 + 1112 + 1113 + static 1114 + int pfdc_merge_sectors_equal (const pfdc_sct_t *s1, const pfdc_sct_t *s2) 1115 + { 1116 + if ((s1->c != s2->c) || (s1->h != s2->h) || (s1->s != s2->s)) { 1117 + return (0); 1118 + } 1119 + 1120 + if (s1->n != s2->n) { 1121 + return (0); 1122 + } 1123 + 1124 + if (memcmp (s1->data, s2->data, s1->n) != 0) { 1125 + return (0); 1126 + } 1127 + 1128 + return (1); 1129 + } 1130 + 1131 + static 1132 + int pfdc_merge_sectors_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1133 + unsigned c, unsigned h, unsigned s, unsigned a, void *opaque) 1134 + { 1135 + pfdc_img_t *dimg; 1136 + pfdc_sct_t *dsct, *dtmp; 1137 + 1138 + dimg = opaque; 1139 + 1140 + dsct = pfdc_img_get_sector (dimg, c, h, sct->s, 0); 1141 + 1142 + if (dsct == NULL) { 1143 + dsct = pfdc_sct_clone (sct, 1); 1144 + 1145 + if (dsct == NULL) { 1146 + return (1); 1147 + } 1148 + 1149 + if (pfdc_img_add_sector (dimg, dsct, c, h)) { 1150 + pfdc_sct_del (dsct); 1151 + return (1); 1152 + } 1153 + 1154 + while (dsct != NULL) { 1155 + par_cnt += 1; 1156 + dsct = dsct->next; 1157 + } 1158 + 1159 + return (0); 1160 + } 1161 + 1162 + dtmp = dsct; 1163 + 1164 + while (dtmp != NULL) { 1165 + if (pfdc_merge_sectors_equal (dtmp, sct)) { 1166 + break; 1167 + } 1168 + 1169 + dtmp = dtmp->next; 1170 + } 1171 + 1172 + if (dtmp == NULL) { 1173 + dtmp = pfdc_sct_clone (sct, 0); 1174 + 1175 + if (dtmp == NULL) { 1176 + return (1); 1177 + } 1178 + 1179 + pfdc_sct_add_alternate (dsct, dtmp); 1180 + 1181 + par_cnt += 1; 1182 + } 1183 + 1184 + return (0); 1185 + } 1186 + 1187 + static 1188 + int pfdc_merge_image (pfdc_img_t *img, const char *fname) 1189 + { 1190 + int r; 1191 + pfdc_img_t *src; 1192 + 1193 + src = pfdc_img_load (fname, par_fmt_inp); 1194 + 1195 + if (src == NULL) { 1196 + fprintf (stderr, "%s: loading image failed (%s)\n", 1197 + arg0, fname 1198 + ); 1199 + return (1); 1200 + } 1201 + 1202 + par_cnt = 0; 1203 + 1204 + r = pfdc_for_all_sectors (src, pfdc_merge_sectors_cb, img); 1205 + 1206 + pfdc_img_del (src); 1207 + 1208 + if (par_verbose) { 1209 + fprintf (stderr, "%s: merge %lu sectors from %s\n", 1210 + arg0, par_cnt, fname 1211 + ); 1212 + } 1213 + 1214 + if (r) { 1215 + fprintf (stderr, "%s: merging failed\n", arg0); 1216 + } 1217 + 1218 + return (r); 1219 + } 1220 + 1221 + static 1222 + int pfdc_show_comment (pfdc_img_t *img) 1223 + { 1224 + unsigned i; 1225 + 1226 + fputs ("comments:\n", stdout); 1227 + 1228 + for (i = 0; i < img->comment_size; i++) { 1229 + fputc (img->comment[i], stdout); 1230 + } 1231 + 1232 + fputs ("\n", stdout); 1233 + 1234 + return (0); 1235 + } 1236 + 1237 + static 1238 + int pfdc_set_comment (pfdc_img_t *img, const char *str) 1239 + { 1240 + const unsigned char *tmp; 1241 + 1242 + if ((str == NULL) || (*str == 0)) { 1243 + pfdc_img_set_comment (img, NULL, 0); 1244 + return (0); 1245 + } 1246 + 1247 + tmp = (const unsigned char *) str; 1248 + 1249 + if (pfdc_img_set_comment (img, tmp, strlen (str))) { 1250 + return (1); 1251 + } 1252 + 1253 + return (0); 1254 + } 1255 + 1256 + static 1257 + int pfdc_add_comment (pfdc_img_t *img, const char *str) 1258 + { 1259 + unsigned char c; 1260 + const unsigned char *tmp; 1261 + 1262 + if (img->comment_size > 0) { 1263 + c = 0x0a; 1264 + 1265 + if (pfdc_img_add_comment (img, &c, 1)) { 1266 + return (1); 1267 + } 1268 + } 1269 + 1270 + tmp = (const unsigned char *) str; 1271 + 1272 + if (pfdc_img_add_comment (img, tmp, strlen (str))) { 1273 + return (1); 1274 + } 1275 + 1276 + return (0); 1277 + } 1278 + 1279 + static 1280 + int pfdc_save_comment (pfdc_img_t *img, const char *fname) 1281 + { 1282 + unsigned cnt; 1283 + FILE *fp; 1284 + 1285 + fp = fopen (fname, "w"); 1286 + 1287 + if (fp == NULL) { 1288 + return (1); 1289 + } 1290 + 1291 + cnt = img->comment_size; 1292 + 1293 + if (cnt > 0) { 1294 + if (fwrite (img->comment, 1, cnt, fp) != cnt) { 1295 + fclose (fp); 1296 + return (1); 1297 + } 1298 + 1299 + fputc (0x0a, fp); 1300 + } 1301 + 1302 + fclose (fp); 1303 + 1304 + if (par_verbose) { 1305 + fprintf (stderr, "%s: save comments to %s\n", arg0, fname); 1306 + } 1307 + 1308 + return (0); 1309 + } 1310 + 1311 + static 1312 + int pfdc_load_comment (pfdc_img_t *img, const char *fname) 1313 + { 1314 + int c, cr; 1315 + unsigned i, nl; 1316 + FILE *fp; 1317 + unsigned char buf[256]; 1318 + 1319 + fp = fopen (fname, "r"); 1320 + 1321 + if (fp == NULL) { 1322 + return (1); 1323 + } 1324 + 1325 + pfdc_img_set_comment (img, NULL, 0); 1326 + 1327 + cr = 0; 1328 + nl = 0; 1329 + i = 0; 1330 + 1331 + while (1) { 1332 + c = fgetc (fp); 1333 + 1334 + if (c == EOF) { 1335 + break; 1336 + } 1337 + 1338 + if (c == 0x0d) { 1339 + if (cr) { 1340 + nl += 1; 1341 + } 1342 + 1343 + cr = 1; 1344 + } 1345 + else if (c == 0x0a) { 1346 + nl += 1; 1347 + cr = 0; 1348 + } 1349 + else { 1350 + if (cr) { 1351 + nl += 1; 1352 + } 1353 + 1354 + if (i > 0) { 1355 + while (nl > 0) { 1356 + buf[i++] = 0x0a; 1357 + nl -= 1; 1358 + 1359 + if (i >= 256) { 1360 + pfdc_img_add_comment (img, buf, i); 1361 + i = 0; 1362 + } 1363 + } 1364 + } 1365 + 1366 + nl = 0; 1367 + cr = 0; 1368 + 1369 + buf[i++] = c; 1370 + 1371 + if (i >= 256) { 1372 + pfdc_img_add_comment (img, buf, i); 1373 + i = 0; 1374 + } 1375 + } 1376 + } 1377 + 1378 + if (i > 0) { 1379 + pfdc_img_add_comment (img, buf, i); 1380 + i = 0; 1381 + } 1382 + 1383 + fclose (fp); 1384 + 1385 + if (par_verbose) { 1386 + fprintf (stderr, "%s: load comments from %s\n", arg0, fname); 1387 + } 1388 + 1389 + return (0); 1390 + } 1391 + 1392 + static 1393 + void pfdc_print_range (const char *str1, unsigned v1, unsigned v2, const char *str2) 1394 + { 1395 + fputs (str1, stdout); 1396 + 1397 + if (v1 == v2) { 1398 + printf ("%u", v1); 1399 + } 1400 + else { 1401 + printf ("%u-%u", v1, v2); 1402 + } 1403 + 1404 + fputs (str2, stdout); 1405 + } 1406 + 1407 + static 1408 + int pfdc_print_info (pfdc_img_t *img) 1409 + { 1410 + int fc, fh, fs, ff; 1411 + unsigned c, h, s; 1412 + unsigned tcnt[2], scnt[2], ssize[2], srng[2]; 1413 + unsigned long stotal, atotal; 1414 + unsigned long dsize; 1415 + unsigned long flags, tflags; 1416 + const pfdc_cyl_t *cyl; 1417 + const pfdc_trk_t *trk; 1418 + const pfdc_sct_t *sct, *alt; 1419 + 1420 + fc = 1; 1421 + fh = 1; 1422 + fs = 1; 1423 + 1424 + tcnt[0] = 0; 1425 + tcnt[1] = 0; 1426 + 1427 + scnt[0] = 0; 1428 + scnt[1] = 0; 1429 + 1430 + srng[0] = 0; 1431 + srng[1] = 0; 1432 + 1433 + ssize[0] = 0; 1434 + ssize[1] = 0; 1435 + 1436 + stotal = 0; 1437 + atotal = 0; 1438 + dsize = 0; 1439 + 1440 + flags = 0; 1441 + tflags = 0; 1442 + 1443 + for (c = 0; c < img->cyl_cnt; c++) { 1444 + cyl = img->cyl[c]; 1445 + 1446 + if (fc || (cyl->trk_cnt < tcnt[0])) { 1447 + tcnt[0] = cyl->trk_cnt; 1448 + } 1449 + 1450 + if (fc || (cyl->trk_cnt > tcnt[1])) { 1451 + tcnt[1] = cyl->trk_cnt; 1452 + } 1453 + 1454 + fc = 0; 1455 + 1456 + for (h = 0; h < cyl->trk_cnt; h++) { 1457 + trk = cyl->trk[h]; 1458 + 1459 + if (fh || (trk->sct_cnt < scnt[0])) { 1460 + scnt[0] = trk->sct_cnt; 1461 + } 1462 + 1463 + if (fh || (trk->sct_cnt > scnt[1])) { 1464 + scnt[1] = trk->sct_cnt; 1465 + } 1466 + 1467 + fh = 0; 1468 + 1469 + for (s = 0; s < trk->sct_cnt; s++) { 1470 + sct = trk->sct[s]; 1471 + 1472 + if (fs || (sct->n < ssize[0])) { 1473 + ssize[0] = sct->n; 1474 + } 1475 + 1476 + if (fs || (sct->n > ssize[1])) { 1477 + ssize[1] = sct->n; 1478 + } 1479 + 1480 + if (fs || (sct->s < srng[0])) { 1481 + srng[0] = sct->s; 1482 + } 1483 + 1484 + if (fs || (sct->s > srng[1])) { 1485 + srng[1] = sct->s; 1486 + } 1487 + 1488 + if ((sct->c != c) || (sct->h != h)) { 1489 + tflags |= PFDC_TRK_BAD_ID; 1490 + } 1491 + 1492 + fs = 0; 1493 + 1494 + stotal += 1; 1495 + dsize += sct->n; 1496 + flags |= sct->flags; 1497 + 1498 + alt = sct->next; 1499 + 1500 + while (alt != NULL) { 1501 + atotal += 1; 1502 + flags |= sct->flags; 1503 + alt = alt->next; 1504 + } 1505 + } 1506 + } 1507 + } 1508 + 1509 + printf ("cylinders: %u\n", img->cyl_cnt); 1510 + pfdc_print_range ("heads: ", tcnt[0], tcnt[1], "\n"); 1511 + pfdc_print_range ("sectors: ", scnt[0], scnt[1], "\n"); 1512 + pfdc_print_range ("sector range: ", srng[0], srng[1], "\n"); 1513 + pfdc_print_range ("sector size: ", ssize[0], ssize[1], "\n"); 1514 + 1515 + printf ("flags: "); 1516 + 1517 + ff = 1; 1518 + 1519 + if (flags & PFDC_FLAG_CRC_ID) { 1520 + printf (" CRC-ID"); 1521 + ff = 0; 1522 + } 1523 + 1524 + if (flags & PFDC_FLAG_CRC_DATA) { 1525 + printf (" CRC-DATA"); 1526 + ff = 0; 1527 + } 1528 + 1529 + if (flags & PFDC_FLAG_DEL_DAM) { 1530 + printf (" DEL-DAM"); 1531 + ff = 0; 1532 + } 1533 + 1534 + if (tflags & PFDC_TRK_BAD_ID) { 1535 + printf (" BAD-ID"); 1536 + ff = 0; 1537 + } 1538 + 1539 + if (ff) { 1540 + printf (" -"); 1541 + } 1542 + 1543 + printf ("\n"); 1544 + 1545 + printf ("total sectors: %lu + %lu\n", stotal, atotal); 1546 + printf ("data size: %lu (%.2f KiB)\n", dsize, (double) dsize / 1024); 1547 + 1548 + if (img->comment_size > 0) { 1549 + fputs ("\n", stdout); 1550 + pfdc_show_comment (img); 1551 + } 1552 + 1553 + return (0); 1554 + } 1555 + 1556 + 1557 + static 1558 + int pfdc_edit_crcid_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1559 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1560 + { 1561 + pfdc_sct_set_flags (sct, PFDC_FLAG_CRC_ID, (*(unsigned long *) p) != 0); 1562 + par_cnt += 1; 1563 + return (0); 1564 + } 1565 + 1566 + static 1567 + int pfdc_edit_crcdata_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1568 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1569 + { 1570 + pfdc_sct_set_flags (sct, PFDC_FLAG_CRC_DATA, (*(unsigned long *) p) != 0); 1571 + par_cnt += 1; 1572 + return (0); 1573 + } 1574 + 1575 + static 1576 + int pfdc_edit_deldam_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1577 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1578 + { 1579 + pfdc_sct_set_flags (sct, PFDC_FLAG_DEL_DAM, (*(unsigned long *) p) != 0); 1580 + par_cnt += 1; 1581 + return (0); 1582 + } 1583 + 1584 + static 1585 + int pfdc_edit_fm_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1586 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1587 + { 1588 + pfdc_sct_set_encoding (sct, PFDC_ENC_FM, sct->data_rate); 1589 + par_cnt += 1; 1590 + return (0); 1591 + } 1592 + 1593 + static 1594 + int pfdc_edit_mfm_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1595 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1596 + { 1597 + pfdc_sct_set_encoding (sct, PFDC_ENC_MFM, sct->data_rate); 1598 + par_cnt += 1; 1599 + return (0); 1600 + } 1601 + 1602 + static 1603 + int pfdc_edit_c_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1604 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1605 + { 1606 + sct->c = (*(unsigned long *) p) & 0xff; 1607 + par_cnt += 1; 1608 + return (0); 1609 + } 1610 + 1611 + static 1612 + int pfdc_edit_h_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1613 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1614 + { 1615 + sct->h = (*(unsigned long *) p) & 0xff; 1616 + par_cnt += 1; 1617 + return (0); 1618 + } 1619 + 1620 + static 1621 + int pfdc_edit_s_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1622 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1623 + { 1624 + sct->s = (*(unsigned long *) p) & 0xff; 1625 + par_cnt += 1; 1626 + return (0); 1627 + } 1628 + 1629 + static 1630 + int pfdc_edit_size_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1631 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1632 + { 1633 + pfdc_sct_set_size (sct, *(unsigned long *) p, par_filler); 1634 + par_cnt += 1; 1635 + return (0); 1636 + } 1637 + 1638 + static 1639 + int pfdc_edit_data_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1640 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1641 + { 1642 + pfdc_sct_fill (sct, *(unsigned long *) p); 1643 + par_cnt += 1; 1644 + return (0); 1645 + } 1646 + 1647 + static 1648 + int pfdc_edit_datarate_cb (pfdc_img_t *img, pfdc_sct_t *sct, 1649 + unsigned c, unsigned h, unsigned s, unsigned a, void *p) 1650 + { 1651 + sct->data_rate = *(unsigned long *) p; 1652 + par_cnt += 1; 1653 + return (0); 1654 + } 1655 + 1656 + static 1657 + int pfdc_edit_sectors (pfdc_img_t *img, const char *what, const char *val) 1658 + { 1659 + int r; 1660 + unsigned long v; 1661 + pfdc_sct_cb fct; 1662 + 1663 + v = strtoul (val, NULL, 0); 1664 + 1665 + if (strcmp (what, "c") == 0) { 1666 + fct = pfdc_edit_c_cb; 1667 + } 1668 + else if (strcmp (what, "crc") == 0) { 1669 + fct = pfdc_edit_crcdata_cb; 1670 + } 1671 + else if (strcmp (what, "crc-data") == 0) { 1672 + fct = pfdc_edit_crcdata_cb; 1673 + } 1674 + else if (strcmp (what, "crc-id") == 0) { 1675 + fct = pfdc_edit_crcid_cb; 1676 + } 1677 + else if (strcmp (what, "data") == 0) { 1678 + fct = pfdc_edit_data_cb; 1679 + } 1680 + else if (strcmp (what, "del-dam") == 0) { 1681 + fct = pfdc_edit_deldam_cb; 1682 + } 1683 + else if (strcmp (what, "data-rate") == 0) { 1684 + fct = pfdc_edit_datarate_cb; 1685 + } 1686 + else if (strcmp (what, "fm") == 0) { 1687 + fct = pfdc_edit_fm_cb; 1688 + } 1689 + else if (strcmp (what, "h") == 0) { 1690 + fct = pfdc_edit_h_cb; 1691 + } 1692 + else if (strcmp (what, "mfm") == 0) { 1693 + fct = pfdc_edit_mfm_cb; 1694 + } 1695 + else if (strcmp (what, "s") == 0) { 1696 + fct = pfdc_edit_s_cb; 1697 + } 1698 + else if (strcmp (what, "size") == 0) { 1699 + fct = pfdc_edit_size_cb; 1700 + } 1701 + else { 1702 + fprintf (stderr, "%s: unknown field (%s)\n", arg0, what); 1703 + return (1); 1704 + } 1705 + 1706 + par_cnt = 0; 1707 + 1708 + r = pfdc_for_all_sectors (img, fct, &v); 1709 + 1710 + if (par_verbose) { 1711 + fprintf (stderr, "%s: edit %lu sectors (%s = %lu)\n", 1712 + arg0, par_cnt, what, v 1713 + ); 1714 + } 1715 + 1716 + if (r) { 1717 + fprintf (stderr, "%s: editing failed (%s = %lu)\n", 1718 + arg0, what, v 1719 + ); 1720 + } 1721 + 1722 + return (r); 1723 + } 1724 + 1725 + 1726 + static 1727 + int pfdc_operation (pfdc_img_t **img, const char *op, int argc, char **argv) 1728 + { 1729 + int r; 1730 + char **optarg; 1731 + 1732 + if (*img == NULL) { 1733 + *img = pfdc_img_new(); 1734 + 1735 + if (*img == NULL) { 1736 + return (1); 1737 + } 1738 + } 1739 + 1740 + r = -1; 1741 + 1742 + if (strcmp (op, "comment-print") == 0) { 1743 + r = pfdc_show_comment (*img); 1744 + } 1745 + else if (strcmp (op, "delete") == 0) { 1746 + r = pfdc_delete_sectors (*img); 1747 + } 1748 + else if (strcmp (op, "info") == 0) { 1749 + r = pfdc_print_info (*img); 1750 + } 1751 + else if (strcmp (op, "new") == 0) { 1752 + r = pfdc_new (img); 1753 + } 1754 + 1755 + if (r != -1) { 1756 + return (r); 1757 + } 1758 + 1759 + r = pce_getopt (argc, argv, &optarg, opts); 1760 + 1761 + if ((r != 0) && (r != 'p')) { 1762 + fprintf (stderr, "%s: bad operation argument (%s)\n", 1763 + arg0, op 1764 + ); 1765 + return (1); 1766 + } 1767 + 1768 + r = 1; 1769 + 1770 + if (strcmp (op, "comment-add") == 0) { 1771 + r = pfdc_add_comment (*img, optarg[0]); 1772 + } 1773 + else if (strcmp (op, "comment-load") == 0) { 1774 + r = pfdc_load_comment (*img, optarg[0]); 1775 + } 1776 + else if (strcmp (op, "comment-save") == 0) { 1777 + r = pfdc_save_comment (*img, optarg[0]); 1778 + } 1779 + else if (strcmp (op, "comment-set") == 0) { 1780 + r = pfdc_set_comment (*img, optarg[0]); 1781 + } 1782 + else if (strcmp (op, "load") == 0) { 1783 + r = pfdc_load_sectors (*img, optarg[0]); 1784 + } 1785 + else if (strcmp (op, "reorder") == 0) { 1786 + r = pfdc_reorder_tracks (*img, optarg[0]); 1787 + } 1788 + else if (strcmp (op, "rotate") == 0) { 1789 + unsigned long first; 1790 + 1791 + first = strtoul (optarg[0], NULL, 0); 1792 + r = pfdc_rotate_tracks (*img, first); 1793 + } 1794 + else if (strcmp (op, "save") == 0) { 1795 + r = pfdc_save_sectors (*img, optarg[0]); 1796 + } 1797 + else { 1798 + fprintf (stderr, "%s: unknown operation (%s)\n", arg0, op); 1799 + return (1); 1800 + } 1801 + 1802 + if (r) { 1803 + fprintf (stderr, "%s: operation failed (%s)\n", arg0, op); 1804 + } 1805 + 1806 + return (r); 1807 + } 1808 + 1809 + 1810 + static 1811 + pfdc_img_t *pfdc_load_image (const char *fname) 1812 + { 1813 + pfdc_img_t *img; 1814 + 1815 + if (par_verbose) { 1816 + fprintf (stderr, "%s: load image from %s\n", arg0, fname); 1817 + } 1818 + 1819 + img = pfdc_img_load (fname, par_fmt_inp); 1820 + 1821 + if (img == NULL) { 1822 + fprintf (stderr, "%s: loading failed (%s)\n", arg0, fname); 1823 + return (NULL); 1824 + } 1825 + 1826 + if (par_list_tracks) { 1827 + par_list_tracks = 0; 1828 + pfdc_list_tracks (img); 1829 + } 1830 + 1831 + if (par_list_sectors) { 1832 + par_list_sectors = 0; 1833 + pfdc_list_sectors (img); 1834 + } 1835 + 1836 + if (par_print_info) { 1837 + par_print_info = 0; 1838 + pfdc_print_info (img); 1839 + } 1840 + 1841 + return (img); 1842 + } 1843 + 1844 + static 1845 + int pfdc_set_format (const char *name, unsigned *val) 1846 + { 1847 + if (strcmp (name, "pfdc") == 0) { 1848 + *val = PFDC_FORMAT_PFDC; 1849 + } 1850 + else if (strcmp (name, "pfdc0") == 0) { 1851 + *val = PFDC_FORMAT_PFDC0; 1852 + } 1853 + else if (strcmp (name, "pfdc1") == 0) { 1854 + *val = PFDC_FORMAT_PFDC1; 1855 + } 1856 + else if (strcmp (name, "pfdc2") == 0) { 1857 + *val = PFDC_FORMAT_PFDC; 1858 + } 1859 + else if (strcmp (name, "ana") == 0) { 1860 + *val = PFDC_FORMAT_ANA; 1861 + } 1862 + else if (strcmp (name, "imd") == 0) { 1863 + *val = PFDC_FORMAT_IMD; 1864 + } 1865 + else if (strcmp (name, "raw") == 0) { 1866 + *val = PFDC_FORMAT_RAW; 1867 + } 1868 + else if (strcmp (name, "td0") == 0) { 1869 + *val = PFDC_FORMAT_TD0; 1870 + } 1871 + else { 1872 + fprintf (stderr, "%s: unknown format (%s)\n", arg0, name); 1873 + *val = PFDC_FORMAT_NONE; 1874 + return (1); 1875 + } 1876 + 1877 + return (0); 1878 + } 1879 + 1880 + int main (int argc, char **argv) 1881 + { 1882 + int r; 1883 + char **optarg; 1884 + unsigned long size; 1885 + pfdc_img_t *img; 1886 + const char *out; 1887 + 1888 + arg0 = argv[0]; 1889 + 1890 + img = NULL; 1891 + out = NULL; 1892 + 1893 + while (1) { 1894 + r = pce_getopt (argc, argv, &optarg, opts); 1895 + 1896 + if (r == GETOPT_DONE) { 1897 + break; 1898 + } 1899 + 1900 + if (r < 0) { 1901 + return (1); 1902 + } 1903 + 1904 + switch (r) { 1905 + case '?': 1906 + print_help(); 1907 + return (0); 1908 + 1909 + case 'V': 1910 + print_version(); 1911 + return (0); 1912 + 1913 + case 'a': 1914 + if (pfdc_parse_range (optarg[0], &par_alt[0], &par_alt[1], &par_alt_all)) { 1915 + return (1); 1916 + } 1917 + break; 1918 + 1919 + case 'c': 1920 + if (pfdc_parse_range (optarg[0], &par_cyl[0], &par_cyl[1], &par_cyl_all)) { 1921 + return (1); 1922 + } 1923 + break; 1924 + 1925 + case 'e': 1926 + if (img != NULL) { 1927 + if (pfdc_edit_sectors (img, optarg[0], optarg[1])) { 1928 + return (1); 1929 + } 1930 + } 1931 + break; 1932 + 1933 + case 'f': 1934 + if (img != NULL) { 1935 + pfdc_print_info (img); 1936 + } 1937 + else { 1938 + par_print_info = 1; 1939 + } 1940 + break; 1941 + 1942 + case 'F': 1943 + par_filler = strtoul (optarg[0], NULL, 0); 1944 + break; 1945 + 1946 + case 'h': 1947 + if (pfdc_parse_range (optarg[0], &par_trk[0], &par_trk[1], &par_trk_all)) { 1948 + return (1); 1949 + } 1950 + break; 1951 + 1952 + case 'i': 1953 + if (img != NULL) { 1954 + pfdc_img_del (img); 1955 + } 1956 + 1957 + img = pfdc_load_image (optarg[0]); 1958 + 1959 + if (img == NULL) { 1960 + return (1); 1961 + } 1962 + break; 1963 + 1964 + case 'I': 1965 + if (pfdc_set_format (optarg[0], &par_fmt_inp)) { 1966 + return (1); 1967 + } 1968 + break; 1969 + 1970 + case 'l': 1971 + if (img != NULL) { 1972 + pfdc_list_tracks (img); 1973 + } 1974 + else { 1975 + par_list_tracks = 1; 1976 + } 1977 + break; 1978 + 1979 + case 'L': 1980 + if (img != NULL) { 1981 + pfdc_list_sectors (img); 1982 + } 1983 + else { 1984 + par_list_sectors = 1; 1985 + } 1986 + break; 1987 + 1988 + case 'm': 1989 + if (img != NULL) { 1990 + if (pfdc_merge_image (img, optarg[0])) { 1991 + return (1); 1992 + } 1993 + } 1994 + else { 1995 + img = pfdc_load_image (optarg[0]); 1996 + 1997 + if (img == NULL) { 1998 + return (1); 1999 + } 2000 + } 2001 + break; 2002 + 2003 + case 'n': 2004 + size = strtoul (optarg[0], NULL, 0); 2005 + 2006 + if (pfdc_new_standard (&img, size)) { 2007 + pfdc_img_del (img); 2008 + return (1); 2009 + } 2010 + break; 2011 + 2012 + case 'o': 2013 + out = optarg[0]; 2014 + break; 2015 + 2016 + case 'O': 2017 + if (pfdc_set_format (optarg[0], &par_fmt_out)) { 2018 + return (1); 2019 + } 2020 + break; 2021 + 2022 + case 'p': 2023 + if (pfdc_operation (&img, optarg[0], argc, argv)) { 2024 + return (1); 2025 + } 2026 + break; 2027 + 2028 + case 'r': 2029 + if (pfdc_parse_range (optarg[0], &par_cyl[0], &par_cyl[1], &par_cyl_all)) { 2030 + return (1); 2031 + } 2032 + 2033 + if (pfdc_parse_range (optarg[1], &par_trk[0], &par_trk[1], &par_trk_all)) { 2034 + return (1); 2035 + } 2036 + 2037 + if (pfdc_parse_range (optarg[2], &par_sct[0], &par_sct[1], &par_sct_all)) { 2038 + return (1); 2039 + } 2040 + break; 2041 + 2042 + case 's': 2043 + if (pfdc_parse_range (optarg[0], &par_sct[0], &par_sct[1], &par_sct_all)) { 2044 + return (1); 2045 + } 2046 + break; 2047 + 2048 + case 'S': 2049 + if (pfdc_parse_range (optarg[0], &par_rsc[0], &par_rsc[1], &par_rsc_all)) { 2050 + return (1); 2051 + } 2052 + break; 2053 + 2054 + case 'v': 2055 + par_verbose = 1; 2056 + break; 2057 + 2058 + case 0: 2059 + if (img == NULL) { 2060 + img = pfdc_load_image (optarg[0]); 2061 + 2062 + if (img == NULL) { 2063 + return (1); 2064 + } 2065 + } 2066 + else if (out == NULL) { 2067 + out = optarg[0]; 2068 + } 2069 + else { 2070 + fprintf (stderr, "%s: unknown option (%s)\n", 2071 + arg0, optarg[0] 2072 + ); 2073 + 2074 + return (1); 2075 + } 2076 + break; 2077 + 2078 + default: 2079 + return (1); 2080 + } 2081 + } 2082 + 2083 + if ((img != NULL) && (out != NULL)) { 2084 + if (par_verbose) { 2085 + fprintf (stderr, "%s: save image to %s\n", arg0, out); 2086 + } 2087 + 2088 + r = pfdc_img_save (out, img, par_fmt_out); 2089 + 2090 + if (r) { 2091 + fprintf (stderr, "%s: saving failed (%s)\n", 2092 + argv[0], out 2093 + ); 2094 + return (1); 2095 + } 2096 + } 2097 + 2098 + return (0); 2099 + }
+27
src/utils/pfdc/main.h
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/utils/pfdc/main.h * 7 + * Created: 2010-08-13 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2010 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #ifndef PFDC_MAIN_H 24 + #define PFDC_MAIN_H 1 25 + 26 + 27 + #endif
+221
src/utils/pfdc/pfdc-img-io.c
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/utils/pfdc/pfdc-img-io.c * 7 + * Created: 2010-08-21 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2010 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #include <stdlib.h> 24 + #include <stdio.h> 25 + #include <string.h> 26 + 27 + #include <devices/block/pfdc-img-ana.h> 28 + #include <devices/block/pfdc-img-imd.h> 29 + #include <devices/block/pfdc-img-pfdc.h> 30 + #include <devices/block/pfdc-img-raw.h> 31 + #include <devices/block/pfdc-img-td0.h> 32 + 33 + #include "pfdc-img-io.h" 34 + 35 + 36 + static 37 + int pfdc_get_file_size (const char *fname, unsigned long *size) 38 + { 39 + FILE *fp; 40 + 41 + fp = fopen (fname, "rb"); 42 + 43 + if (fp == NULL) { 44 + return (1); 45 + } 46 + 47 + if (fseek (fp, 0, SEEK_END) != 0) { 48 + fclose (fp); 49 + return (1); 50 + } 51 + 52 + *size = ftell (fp); 53 + 54 + fclose (fp); 55 + 56 + return (0); 57 + } 58 + 59 + static 60 + int pfdc_is_raw (const char *fname) 61 + { 62 + unsigned long size; 63 + unsigned c, h, s; 64 + 65 + if (pfdc_get_file_size (fname, &size)) { 66 + return (0); 67 + } 68 + 69 + if (pfdc_get_geometry_from_size (size, &c, &h, &s)) { 70 + return (0); 71 + } 72 + 73 + return (1); 74 + } 75 + 76 + static 77 + unsigned pfdc_get_type (unsigned type, const char *fname) 78 + { 79 + unsigned i; 80 + const char *ext; 81 + 82 + if (type != PFDC_FORMAT_NONE) { 83 + return (type); 84 + } 85 + 86 + ext = ""; 87 + 88 + i = 0; 89 + while (fname[i] != 0) { 90 + if (fname[i] == '.') { 91 + ext = fname + i; 92 + } 93 + 94 + i += 1; 95 + } 96 + 97 + if (strcasecmp (ext, ".ana") == 0) { 98 + return (PFDC_FORMAT_ANA); 99 + } 100 + else if (strcasecmp (ext, ".ima") == 0) { 101 + return (PFDC_FORMAT_RAW); 102 + } 103 + else if (strcasecmp (ext, ".imd") == 0) { 104 + return (PFDC_FORMAT_IMD); 105 + } 106 + else if (strcasecmp (ext, ".raw") == 0) { 107 + return (PFDC_FORMAT_RAW); 108 + } 109 + else if (strcasecmp (ext, ".td0") == 0) { 110 + return (PFDC_FORMAT_TD0); 111 + } 112 + 113 + if (strcasecmp (ext, ".img") == 0) { 114 + if (pfdc_is_raw (fname)) { 115 + return (PFDC_FORMAT_RAW); 116 + } 117 + } 118 + 119 + return (PFDC_FORMAT_PFDC); 120 + } 121 + 122 + pfdc_img_t *pfdc_img_load_fp (FILE *fp, unsigned type) 123 + { 124 + pfdc_img_t *img; 125 + 126 + img = NULL; 127 + 128 + switch (type) { 129 + case PFDC_FORMAT_PFDC0: 130 + case PFDC_FORMAT_PFDC1: 131 + case PFDC_FORMAT_PFDC: 132 + img = pfdc_load_pfdc (fp); 133 + break; 134 + 135 + case PFDC_FORMAT_ANA: 136 + img = pfdc_load_anadisk (fp); 137 + break; 138 + 139 + case PFDC_FORMAT_IMD: 140 + img = pfdc_load_imd (fp); 141 + break; 142 + 143 + case PFDC_FORMAT_RAW: 144 + img = pfdc_load_raw (fp); 145 + break; 146 + 147 + case PFDC_FORMAT_TD0: 148 + img = pfdc_load_td0 (fp); 149 + break; 150 + } 151 + 152 + return (img); 153 + } 154 + 155 + pfdc_img_t *pfdc_img_load (const char *fname, unsigned type) 156 + { 157 + FILE *fp; 158 + pfdc_img_t *img; 159 + 160 + type = pfdc_get_type (type, fname); 161 + 162 + fp = fopen (fname, "rb"); 163 + 164 + if (fp == NULL) { 165 + return (NULL); 166 + } 167 + 168 + img = pfdc_img_load_fp (fp, type); 169 + 170 + fclose (fp); 171 + 172 + return (img); 173 + } 174 + 175 + int pfdc_img_save_fp (FILE *fp, const pfdc_img_t *img, unsigned type) 176 + { 177 + switch (type) { 178 + case PFDC_FORMAT_PFDC0: 179 + return (pfdc_save_pfdc (fp, img, 0)); 180 + 181 + case PFDC_FORMAT_PFDC1: 182 + return (pfdc_save_pfdc (fp, img, 1)); 183 + 184 + case PFDC_FORMAT_PFDC: 185 + return (pfdc_save_pfdc (fp, img, 2)); 186 + 187 + case PFDC_FORMAT_ANA: 188 + return (pfdc_save_anadisk (fp, img)); 189 + 190 + case PFDC_FORMAT_IMD: 191 + return (pfdc_save_imd (fp, img)); 192 + 193 + case PFDC_FORMAT_RAW: 194 + return (pfdc_save_raw (fp, img)); 195 + 196 + case PFDC_FORMAT_TD0: 197 + return (pfdc_save_td0 (fp, img)); 198 + } 199 + 200 + return (1); 201 + } 202 + 203 + int pfdc_img_save (const char *fname, const pfdc_img_t *img, unsigned type) 204 + { 205 + int r; 206 + FILE *fp; 207 + 208 + type = pfdc_get_type (type, fname); 209 + 210 + fp = fopen (fname, "wb"); 211 + 212 + if (fp == NULL) { 213 + return (1); 214 + } 215 + 216 + r = pfdc_img_save_fp (fp, img, type); 217 + 218 + fclose (fp); 219 + 220 + return (r); 221 + }
+49
src/utils/pfdc/pfdc-img-io.h
··· 1 + /***************************************************************************** 2 + * pce * 3 + *****************************************************************************/ 4 + 5 + /***************************************************************************** 6 + * File name: src/utils/pfdc/pfdc-img-io.h * 7 + * Created: 2010-08-21 by Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2010 Hampa Hug <hampa@hampa.ch> * 9 + *****************************************************************************/ 10 + 11 + /***************************************************************************** 12 + * This program is free software. You can redistribute it and / or modify it * 13 + * under the terms of the GNU General Public License version 2 as published * 14 + * by the Free Software Foundation. * 15 + * * 16 + * This program is distributed in the hope that it will be useful, but * 17 + * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 + * Public License for more details. * 20 + *****************************************************************************/ 21 + 22 + 23 + #ifndef PFDC_IMG_IO_H 24 + #define PFDC_IMG_IO_H 1 25 + 26 + 27 + #include <devices/block/pfdc.h> 28 + 29 + 30 + #define PFDC_FORMAT_NONE 0 31 + #define PFDC_FORMAT_PFDC 1 32 + #define PFDC_FORMAT_PFDC0 2 33 + #define PFDC_FORMAT_PFDC1 3 34 + #define PFDC_FORMAT_ANA 4 35 + #define PFDC_FORMAT_IMD 5 36 + #define PFDC_FORMAT_RAW 6 37 + #define PFDC_FORMAT_TD0 7 38 + 39 + 40 + pfdc_img_t *pfdc_img_load_fp (FILE *fp, unsigned type); 41 + 42 + pfdc_img_t *pfdc_img_load (const char *fname, unsigned type); 43 + 44 + int pfdc_img_save_fp (FILE *fp, const pfdc_img_t *img, unsigned type); 45 + 46 + int pfdc_img_save (const char *fname, const pfdc_img_t *img, unsigned type); 47 + 48 + 49 + #endif
+230
src/utils/pfdc/pfdc.1
··· 1 + .TH PFDC 1 "2010-08-22" "HH" "pce" 2 + \ 3 + .SH NAME 4 + pfdc \- convert and modify PFDC floppy disk image files 5 + 6 + .SH SYNOPSIS 7 + .BI pfdc " [options] [input-file] [options] [output-file]" 8 + 9 + .SH DESCRIPTION 10 + \fBpfdc\fR(1) is used to modify and convert PFDC floppy disk images 11 + files. 12 + 13 + .SH OPTIONS 14 + .TP 15 + .BI "-a, --alternate " "alt1[-alt2]" 16 + Select a range of alternate sectors. 17 + \ 18 + .TP 19 + .BI "-c, --cylinder " "cyl1[-cyl2]" 20 + Select a range of cylinders. 21 + \ 22 + .TP 23 + .BI "-e, --edit " "what val" 24 + For all selected sectors, set sector attribute \fIwhat\fR to \fIval\fR. 25 + For boolean attributes, a value of 0 disables the attribute and any other 26 + value enables it. 27 + Recognized attributes are: 28 + .RS 29 + .TP 30 + .B crc-id 31 + The ID field contains a CRC error. 32 + .TP 33 + .B crc-data 34 + The data field contains a CRC error. 35 + .TP 36 + .B del-dam 37 + The sector has a deleted data address mark. 38 + .TP 39 + .B data-rate 40 + The data rate in bits per second. 41 + .TP 42 + .B fm 43 + The sector uses FM encoding. 44 + .TP 45 + .B mfm 46 + The sector uses MFM encoding. 47 + .TP 48 + .B size 49 + The sector size in bytes. 50 + .TP 51 + .B c 52 + The cylinder number in the sector ID. 53 + .TP 54 + .B h 55 + The head number in the sector ID. 56 + .TP 57 + .B s 58 + The sector number in the sector ID. 59 + .RE 60 + \ 61 + .TP 62 + .B "-f, --info" 63 + Print information about the current image or the next image loaded. 64 + \ 65 + .TP 66 + .BI "-F, --filler " val 67 + Set the fill byte to \fIval\fR. The fill byte is used when sectors 68 + are created or enlarged. 69 + \ 70 + .TP 71 + .BI "-h, --head " "head1[-head2]" 72 + Select a range of heads. 73 + \ 74 + .TP 75 + .BI "-i, --input " filename 76 + Load an image from \fIfilename\fR. 77 + \ 78 + .TP 79 + .BI "-I, --input-format " format 80 + Set the input file format to \fIformat\fR. 81 + Valid formats are: 82 + .RS 83 + .TP 84 + .B pfdc 85 + The native PFDC file format. 86 + .TP 87 + .B ana 88 + The anadisk dump format. 89 + .TP 90 + .B imd 91 + The ImageDisk file format. 92 + .TP 93 + .B raw 94 + A raw sector dump. 95 + .TP 96 + .B td0 97 + The teledisk file format. Only files that don't use advanced compression 98 + are supported. 99 + .RE 100 + \ 101 + .TP 102 + .B "-l, --list-tracks" 103 + List all tracks in the current image or in the next image loaded. 104 + \ 105 + .TP 106 + .B "-L, --list-sectors" 107 + List all sectors in the current image or in the next image loaded. 108 + \ 109 + .TP 110 + .BI "-m, --merge " filename 111 + Load an image from \fIfilename\fR and merge it with the current 112 + image. Sectors that are identical are discarded. Sectors that 113 + exist in only one image are retained. Sectors that exist in both 114 + images, but differ, are added as alternate sectors. 115 + \ 116 + .TP 117 + .BI "-n, --new " size 118 + Create a new image of size \fIsize\fR KiB. 119 + \ 120 + .TP 121 + .BI "-o, --output " filename 122 + Set the output file name. Before exiting, the current image will 123 + be written to this file. 124 + \ 125 + .TP 126 + .BI "-O, --output-format " format 127 + Set the output file format to \fIformat\fR. See the \fI-I\fR option 128 + for a list of valid formats. 129 + \ 130 + .TP 131 + .BI "-p, --operation " "name [arg...]" 132 + Perform an operation on the current image. Valid operations are: 133 + .RS 134 + .TP 135 + .BI "comment-add " text 136 + Add \fItext\fR to the image comment. 137 + .TP 138 + .BI "comment-load " filename 139 + Load the image comment from file \fIfilename\fR. 140 + .TP 141 + .B comment-print 142 + Print the current image comment. 143 + .TP 144 + .BI "comment-save " filename 145 + Save the current image comment to \fIfilename\fR. 146 + .TP 147 + .BI "comment-set " text 148 + Set the image comment to \fItext\fR. 149 + .TP 150 + .B delete 151 + Delete all selected sectors. 152 + .TP 153 + .B info 154 + Print information about the current image (same as \fB-f\fR). 155 + .TP 156 + .BI "load "filename" 157 + Load the contents of all selected sectors from \fIfilename\fR. The 158 + contents of the sectors are read sequentially from the file. 159 + .TP 160 + .B new 161 + Create all selected sectors, if they do not already exist. 162 + .TP 163 + .BI "reorder " "s1,s2,s3,..." 164 + Reorder the sectors on all selected tracks. Sectors that are not 165 + mentioned in the parameter are moved to the end of the track. 166 + .TP 167 + .BI "rotate " first 168 + Rotate the sectors on all selected tracks such that \fIfirst\fR is 169 + the first sector on the track. If \fIfirst\fR does not exist on 170 + a track, the next higher sector will be rotated to the start of 171 + the track. 172 + .TP 173 + .BI "save " filename 174 + Save all selected sectors to \fIfilename\fR. The contents of the 175 + sectors are written sequentially to the file. 176 + .RE 177 + \ 178 + .TP 179 + .BI "-r, --record " "cyl1[-cyl2] head1[-head2] sect1[-sect2]" 180 + Select sectors. This is the same as using the \fB-c\fR, \fB-h\fR 181 + and \fB-s\fR options seperately. 182 + \ 183 + .TP 184 + .BI "-s, --sector " "sect1[-sect2]" 185 + Select a range of logical sectors. 186 + \ 187 + .TP 188 + .BI "-S, --real-sectors " "sect1[-sect2]" 189 + Select a range of physical sectors. 190 + \ 191 + .TP 192 + .B "-v, --verbose" 193 + Enable verbose operation. 194 + \ 195 + .TP 196 + .B --help 197 + Print usage information. 198 + \ 199 + .TP 200 + .B --version 201 + Print version information. 202 + 203 + .SH EXAMPLES 204 + Convert an ImageDisk file to a PFDC file: 205 + .IP "" 206 + $ pfdc source.imd dest.pfdc 207 + .PP 208 + Get image information: 209 + .IP 210 + $ pfdc -f image.pfdc 211 + .PP 212 + Add sectors 10 and 11 to all tracks on side 0: 213 + .IP 214 + $ pfdc -i source.pfdc -r all 0 10-11 -p new -o dest.pfdc 215 + .PP 216 + Mark the first sector in the image as having a bad data CRC: 217 + .IP 218 + $ pfdc -i source.pfdc -r 0 0 1 -e crc-data 1 -o dest.pfdc 219 + .PP 220 + Set the image comment: 221 + .IP 222 + $ pfdc -i source.pfdc -p comment-set "Test image" -o dest.pfdc 223 + 224 + .SH SEE ALSO 225 + .BR pce-ibmpc "(1)," 226 + .BR pce-macplus "(1)," 227 + .BR pce-img "(1)" 228 + 229 + .SH AUTHOR 230 + Hampa Hug <hampa@hampa.ch>