/* imghd_open() Open stream as a MAME HD image */ imgtoolerr_t imghd_open(imgtool_stream *stream, struct mess_hard_disk_file *hard_disk) { chd_error chderr; imgtoolerr_t err = IMGTOOLERR_SUCCESS; hard_disk->hard_disk = nullptr; hard_disk->chd = nullptr; chderr = hard_disk->chd->open(*stream_core_file(stream), stream_isreadonly(stream)); if (chderr) { err = map_chd_error(chderr); goto done; } hard_disk->hard_disk = hard_disk_open(hard_disk->chd); if (!hard_disk->hard_disk) { err = IMGTOOLERR_UNEXPECTED; goto done; } hard_disk->stream = stream; done: if (err) imghd_close(hard_disk); return err; }
/* imghd_open() Open stream as a MAME HD image */ imgtoolerr_t imghd_open(imgtool_stream *stream, struct mess_hard_disk_file *hard_disk) { imgtoolerr_t err = IMGTOOLERR_SUCCESS; char encoded_image_ref[encoded_image_ref_max_len]; chd_interface interface_save; hard_disk->hard_disk = NULL; hard_disk->chd = NULL; encode_image_ref(stream, encoded_image_ref); /* use our CHD interface */ chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); hard_disk->chd = chd_open(encoded_image_ref, !stream_isreadonly(stream), NULL); if (!hard_disk->chd) { err = imghd_chd_getlasterror(); goto done; } hard_disk->hard_disk = hard_disk_open(hard_disk->chd); if (!hard_disk->hard_disk) { err = IMGTOOLERR_UNEXPECTED; goto done; } hard_disk->stream = stream; done: if (err) imghd_close(hard_disk); chd_set_interface(&interface_save); return err; }
/* imgtool_chd_open - interface for opening a hard disk image */ static chd_interface_file *imgtool_chd_open(const char *filename, const char *mode) { imgtool_stream *img = decode_image_ref(filename); /* invalid "file name"? */ if (img == NULL) return NULL; /* read-only fp? */ if ((stream_isreadonly(img)) && ! (mode[0] == 'r' && !strchr(mode, '+'))) return NULL; /* otherwise return file pointer */ return (chd_interface_file *) img; }
/* imghd_open() Open stream as a MAME HD image */ imgtoolerr_t imghd_open(imgtool_stream *stream, struct mess_hard_disk_file *hard_disk) { chd_error chderr; imgtoolerr_t err = IMGTOOLERR_SUCCESS; char encoded_image_ref[encoded_image_ref_max_len]; chd_interface interface_save; hard_disk->hard_disk = NULL; hard_disk->chd = NULL; encode_image_ref(stream, encoded_image_ref); /* use our CHD interface */ chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); chderr = chd_open(encoded_image_ref, stream_isreadonly(stream) ? CHD_OPEN_READ : CHD_OPEN_READWRITE, NULL, &hard_disk->chd); if (chderr) { err = map_chd_error(chderr); goto done; } hard_disk->hard_disk = hard_disk_open(hard_disk->chd); if (!hard_disk->hard_disk) { err = IMGTOOLERR_UNEXPECTED; goto done; } hard_disk->stream = stream; done: if (err) imghd_close(hard_disk); chd_set_interface(&interface_save); return err; }
/* imghd_create() Create a MAME HD image */ imgtoolerr_t imghd_create(imgtool_stream *stream, UINT32 hunksize, UINT32 cylinders, UINT32 heads, UINT32 sectors, UINT32 seclen) { imgtoolerr_t err = IMGTOOLERR_SUCCESS; chd_file chd; chd_error rc; chd_codec_type compression[4] = { CHD_CODEC_NONE }; /* sanity check args */ if (hunksize >= 2048) { err = IMGTOOLERR_PARAMCORRUPT; return err; } if (hunksize <= 0) hunksize = 1024; /* default value */ /* bail if we are read only */ if (stream_isreadonly(stream)) { err = IMGTOOLERR_READONLY; return err; } /* calculations */ const UINT64 logicalbytes = (UINT64)cylinders * heads * sectors * seclen; /* create the new hard drive */ rc = chd.create(*stream_core_file(stream), logicalbytes, hunksize, seclen, compression); if (rc != CHDERR_NONE) { err = map_chd_error(rc); return err; } /* open the new hard drive */ rc = chd.open(*stream_core_file(stream)); if (rc != CHDERR_NONE) { err = map_chd_error(rc); return err; } /* write the metadata */ const std::string metadata = string_format(HARD_DISK_METADATA_FORMAT, cylinders, heads, sectors, seclen); err = (imgtoolerr_t)chd.write_metadata(HARD_DISK_METADATA_TAG, 0, metadata); if (rc != CHDERR_NONE) { err = map_chd_error(rc); return err; } /* alloc and zero buffer */ dynamic_buffer cache; cache.resize(hunksize); memset(&cache[0], 0, hunksize); /* zero out every hunk */ const int totalhunks = (logicalbytes + hunksize - 1) / hunksize; for (int hunknum = 0; hunknum < totalhunks; hunknum++) { rc = chd.write_units(hunknum, &cache[0]); if (rc) { err = IMGTOOLERR_WRITEERROR; return err; } } return err; }
/* imghd_create() Create a MAME HD image */ imgtoolerr_t imghd_create(imgtool_stream *stream, UINT32 hunksize, UINT32 cylinders, UINT32 heads, UINT32 sectors, UINT32 seclen) { imgtoolerr_t err = IMGTOOLERR_SUCCESS; char encoded_image_ref[encoded_image_ref_max_len]; chd_interface interface_save; UINT8 *cache = NULL; chd_file *chd = NULL; int rc; UINT64 logicalbytes; int hunknum, totalhunks; char metadata[256]; /* jump through the hoops as required by the CHD system */ encode_image_ref(stream, encoded_image_ref); chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); /* sanity check args */ if (hunksize >= 2048) { err = IMGTOOLERR_PARAMCORRUPT; goto done; } if (hunksize <= 0) hunksize = 1024; /* default value */ /* bail if we are read only */ if (stream_isreadonly(stream)) { err = IMGTOOLERR_READONLY; goto done; } /* calculations */ logicalbytes = (UINT64)cylinders * heads * sectors * seclen; /* create the new hard drive */ rc = chd_create(encoded_image_ref, logicalbytes, hunksize, CHDCOMPRESSION_NONE, NULL); if (rc != CHDERR_NONE) { err = map_chd_error(rc); goto done; } /* open the new hard drive */ rc = chd_open(encoded_image_ref, CHD_OPEN_READWRITE, NULL, &chd); if (rc != CHDERR_NONE) { err = map_chd_error(rc); goto done; } /* write the metadata */ sprintf(metadata, HARD_DISK_METADATA_FORMAT, cylinders, heads, sectors, seclen); err = chd_set_metadata(chd, HARD_DISK_METADATA_TAG, 0, metadata, strlen(metadata) + 1); if (rc != CHDERR_NONE) { err = map_chd_error(rc); goto done; } /* alloc and zero buffer */ cache = malloc(hunksize); if (!cache) { err = IMGTOOLERR_OUTOFMEMORY; goto done; } memset(cache, '\0', hunksize); /* zero out every hunk */ totalhunks = (logicalbytes + hunksize - 1) / hunksize; for (hunknum = 0; hunknum < totalhunks; hunknum++) { rc = chd_write(chd, hunknum, cache); if (rc) { err = IMGTOOLERR_WRITEERROR; goto done; } } done: if (cache) free(cache); if (chd) chd_close(chd); chd_set_interface(&interface_save); return err; }