/* 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; }
/* imghd_get_header() Return pointer to the header of MAME HD image */ const hard_disk_info *imghd_get_header(struct mess_hard_disk_file *disk) { chd_interface interface_save; const hard_disk_info *reply; chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); reply = hard_disk_get_info(disk->hard_disk); chd_set_interface(&interface_save); return reply; }
/* 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_read() Read sector(s) from MAME HD image */ imgtoolerr_t imghd_read(struct mess_hard_disk_file *disk, UINT32 lbasector, UINT32 numsectors, void *buffer) { chd_interface interface_save; UINT32 reply; chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); reply = hard_disk_read(disk->hard_disk, lbasector, numsectors, buffer); chd_set_interface(&interface_save); return reply ? IMGTOOLERR_SUCCESS : imghd_chd_getlasterror(); }
/* imghd_close() Close MAME HD image */ void imghd_close(struct mess_hard_disk_file *disk) { chd_interface interface_save; chd_save_interface(&interface_save); chd_set_interface(&imgtool_chd_interface); if (disk->hard_disk) { hard_disk_close(disk->hard_disk); disk->hard_disk = NULL; } if (disk->chd) { chd_close(disk->chd); disk->chd = NULL; } chd_set_interface(&interface_save); }
/* 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; 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; }