/* 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.is_read_only()); 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) { chd_error chderr; imgtoolerr_t err = IMGTOOLERR_SUCCESS; hard_disk->hard_disk = NULL; hard_disk->chd = NULL; 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_write() Write sector(s) from MAME HD image */ imgtoolerr_t imghd_write(struct mess_hard_disk_file *disk, UINT32 lbasector, const void *buffer) { chd_interface interface_save; UINT32 reply; chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); reply = hard_disk_write(disk->hard_disk, lbasector, buffer); chd_set_interface(&interface_save); return reply ? IMGTOOLERR_SUCCESS : map_chd_error(reply); }
/* 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.is_read_only()) { 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(), 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()); 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_write() Write sector(s) from MAME HD image */ imgtoolerr_t imghd_write(struct mess_hard_disk_file *disk, UINT32 lbasector, const void *buffer) { UINT32 reply; reply = hard_disk_write(disk->hard_disk, lbasector, buffer); return (imgtoolerr_t)(reply ? IMGTOOLERR_SUCCESS : map_chd_error((chd_error)reply)); }
/* 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; }