Previous 15994 Revisions Next

r6197 Tuesday 27th October, 2009 at 21:42:03 UTC by R. Belmont
CHDMAN: -createuncomphd option creates an uncompressed writeable harddisk CHD
[src/tools]chdman.c

src/tools/chdman.c
r6196r6197
152152   printf("usage: chdman -info input.chd\n");
153153   printf("   or: chdman -createraw inputhd.raw output.chd [inputoffs [hunksize]]\n");
154154   printf("   or: chdman -createhd inputhd.raw output.chd [ident.bin] [inputoffs [cylinders heads sectors [sectorsize [hunksize]]]]\n");
155   printf("   or: chdman -createuncomphd inputhd.raw output.chd [ident.bin] [inputoffs [cylinders heads sectors [sectorsize [hunksize]]]]\n");
155156   printf("   or: chdman -createblankhd output.chd cylinders heads sectors [sectorsize [hunksize]]\n");
156157   printf("   or: chdman -createcd input.toc output.chd\n");
157158   printf("   or: chdman -createav input.avi output.chd [firstframe [numframes]]\n");
r6196r6197
424425   return (err != CHDERR_NONE);
425426}
426427
428/*-------------------------------------------------
429    do_createhd_uncomp - create a new uncompressed hard
430    disk image from a raw file
431-------------------------------------------------*/
427432
433static int do_createhd_uncomp(int argc, char *argv[], int param)
434{
435   UINT32 guess_cylinders = 0, guess_heads = 0, guess_sectors = 0, guess_sectorsize = 0;
436   UINT32 cylinders, heads, sectors, sectorsize, hunksize, totalsectors, offset;
437   const char *inputfile, *outputfile = NULL;
438   chd_error err = CHDERR_NONE;
439   UINT32 identdatasize = 0;
440   UINT8 *identdata = NULL;
441   chd_file *chd = NULL;
442   char metadata[256];
443   chd_header header;
444
445   /* if a file is provided for argument 4 (ident filename), then shift the remaining arguments down */
446   if (argc >= 5)
447   {
448      char *scan;
449
450      /* if there are any non-digits in the 'offset', then treat it as a ident file */
451      for (scan = argv[4]; *scan != 0; scan++)
452         if (!isdigit((UINT8)*scan))
453            break;
454      if (*scan != 0)
455      {
456         /* attempt to load the file */
457         file_error filerr = core_fload(argv[4], (void **)&identdata, &identdatasize);
458         if (filerr != FILERR_NONE)
459         {
460            fprintf(stderr, "Error opening ident file '%s'\n", argv[4]);
461            return 1;
462         }
463
464         /* shift the remaining arguments down */
465         if (argc > 5)
466            memmove(&argv[4], &argv[5], (argc - 5) * sizeof(argv[0]));
467         argc--;
468      }
469   }
470
471   /* require 4-5, or 8-10 args total */
472   if (argc != 4 && argc != 5 && argc != 8 && argc != 9 && argc != 10)
473      return usage();
474
475   /* extract the first few parameters */
476   inputfile = argv[2];
477   outputfile = argv[3];
478   offset = (argc >= 5) ? atoi(argv[4]) : (get_file_size(inputfile) % IDE_SECTOR_SIZE);
479
480   /* if less than 8 parameters, we need to guess the CHS values */
481   if (argc < 8)
482   {
483      if (identdata != NULL)
484         err = get_chs_from_ident(inputfile, offset, identdata, identdatasize, &guess_cylinders, &guess_heads, &guess_sectors, &guess_sectorsize);
485      else
486         err = guess_chs(inputfile, offset, IDE_SECTOR_SIZE, &guess_cylinders, &guess_heads, &guess_sectors, &guess_sectorsize);
487      if (err != CHDERR_NONE)
488         goto cleanup;
489   }
490
491   /* parse the remaining parameters */
492   cylinders = (argc >= 6) ? atoi(argv[5]) : guess_cylinders;
493   heads = (argc >= 7) ? atoi(argv[6]) : guess_heads;
494   sectors = (argc >= 8) ? atoi(argv[7]) : guess_sectors;
495   sectorsize = (argc >= 9) ? atoi(argv[8]) : guess_sectorsize;
496   if (sectorsize == 0) sectorsize = IDE_SECTOR_SIZE;
497   hunksize = (argc >= 10) ? atoi(argv[9]) : (sectorsize > 4096) ? sectorsize : ((4096 / sectorsize) * sectorsize);
498   totalsectors = cylinders * heads * sectors;
499
500   /* print some info */
501   printf("Input file:   %s\n", inputfile);
502   printf("Output file:  %s\n", outputfile);
503   printf("Input offset: %d\n", offset);
504   printf("Cylinders:    %d\n", cylinders);
505   printf("Heads:        %d\n", heads);
506   printf("Sectors:      %d\n", sectors);
507   printf("Bytes/sector: %d\n", sectorsize);
508   printf("Sectors/hunk: %d\n", hunksize / sectorsize);
509   printf("Logical size: %s\n", big_int_string((UINT64)totalsectors * (UINT64)sectorsize));
510
511   /* create the new hard drive */
512   err = chd_create(outputfile, (UINT64)totalsectors * (UINT64)sectorsize, hunksize, CHDCOMPRESSION_NONE, NULL);
513   if (err != CHDERR_NONE)
514   {
515      fprintf(stderr, "Error creating CHD file: %s\n", chd_error_string(err));
516      goto cleanup;
517   }
518
519   /* open the new hard drive */
520   err = chd_open(outputfile, CHD_OPEN_READWRITE, NULL, &chd);
521   if (err != CHDERR_NONE)
522   {
523      fprintf(stderr, "Error opening new CHD file: %s\n", chd_error_string(err));
524      goto cleanup;
525   }
526
527   /* write the metadata */
528   sprintf(metadata, HARD_DISK_METADATA_FORMAT, cylinders, heads, sectors, sectorsize);
529   err = chd_set_metadata(chd, HARD_DISK_METADATA_TAG, 0, metadata, strlen(metadata) + 1, CHD_MDFLAGS_CHECKSUM);
530   if (err != CHDERR_NONE)
531   {
532      fprintf(stderr, "Error adding hard disk metadata: %s\n", chd_error_string(err));
533      goto cleanup;
534   }
535
536   /* write the ident if present */
537   if (identdata != NULL)
538   {
539      err = chd_set_metadata(chd, HARD_DISK_IDENT_METADATA_TAG, 0, identdata, identdatasize, CHD_MDFLAGS_CHECKSUM);
540      if (err != CHDERR_NONE)
541      {
542         fprintf(stderr, "Error adding hard disk metadata: %s\n", chd_error_string(err));
543         goto cleanup;
544      }
545   }
546
547   /* compress the hard drive */
548   err = chdman_compress_file(chd, inputfile, offset);
549   if (err != CHDERR_NONE)
550      fprintf(stderr, "Error during compression: %s\n", chd_error_string(err));
551
552   /* make it writeable */
553   header = *chd_get_header(chd);
554   header.flags |= CHDFLAGS_IS_WRITEABLE;
555   err = chd_set_header_file(chd_core_file(chd), &header);
556   if (err != CHDERR_NONE)
557      fprintf(stderr, "Error writing new header: %s\n", chd_error_string(err));
558
559cleanup:
560   /* close everything down */
561   if (chd != NULL)
562      chd_close(chd);
563   if (err != CHDERR_NONE)
564      osd_rmfile(outputfile);
565   if (identdata != NULL)
566      free(identdata);
567   return (err != CHDERR_NONE);
568}
569
428570/*-------------------------------------------------
429571    do_createraw - create a new compressed raw
430572    image from a raw file
r6196r6197
31463288   } option_list[] =
31473289   {
31483290      { "-createhd",      do_createhd, 0 },
3291      { "-createuncomphd",    do_createhd_uncomp, 0 },
31493292      { "-createraw",      do_createraw, 0 },
31503293      { "-createcd",      do_createcd, 0 },
31513294      { "-createblankhd",   do_createblankhd, 0 },

Previous 15994 Revisions Next


© 1998-2018 The MESS Team