diff options
Diffstat (limited to 'src/devices/imagedev/harddriv.cpp')
-rw-r--r-- | src/devices/imagedev/harddriv.cpp | 280 |
1 files changed, 165 insertions, 115 deletions
diff --git a/src/devices/imagedev/harddriv.cpp b/src/devices/imagedev/harddriv.cpp index cdb4f72e098..078131fca6b 100644 --- a/src/devices/imagedev/harddriv.cpp +++ b/src/devices/imagedev/harddriv.cpp @@ -17,9 +17,13 @@ #include "harddriv.h" #include "emuopts.h" +#include "fileio.h" #include "harddisk.h" #include "romload.h" +#include "multibyte.h" +#include "opresolv.h" + OPTION_GUIDE_START(hd_option_guide) OPTION_INT('C', "cylinders", "Cylinders") @@ -34,7 +38,17 @@ static char const *const hd_option_spec = // device type definition -DEFINE_DEVICE_TYPE(HARDDISK, harddisk_image_device, "harddisk_image", "Harddisk") +DEFINE_DEVICE_TYPE(HARDDISK, harddisk_image_device, "harddisk_image", "Hard disk") + +//------------------------------------------------- +// harddisk_image_base_device - constructor +//------------------------------------------------- + +harddisk_image_base_device::harddisk_image_base_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) + : device_t(mconfig, type, tag, owner, clock) + , device_image_interface(mconfig, *this) +{ +} //------------------------------------------------- // harddisk_image_device - constructor @@ -48,14 +62,14 @@ harddisk_image_device::harddisk_image_device(const machine_config &mconfig, cons //------------------------------------------------- // harddisk_image_device - constructor for subclasses //------------------------------------------------- + harddisk_image_device::harddisk_image_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) - : device_t(mconfig, type, tag, owner, clock), - device_image_interface(mconfig, *this), - m_chd(nullptr), - m_hard_disk_handle(nullptr), - m_device_image_load(load_delegate()), - m_device_image_unload(unload_delegate()), - m_interface(nullptr) + : harddisk_image_base_device(mconfig, type, tag, owner, clock) + , m_chd(nullptr) + , m_hard_disk_handle() + , m_device_image_load(*this) + , m_device_image_unload(*this) + , m_interface(nullptr) { } @@ -89,49 +103,38 @@ const util::option_guide &harddisk_image_device::create_option_guide() const void harddisk_image_device::device_start() { + m_device_image_load.resolve(); + m_device_image_unload.resolve(); + m_chd = nullptr; - // try to locate the CHD from a DISK_REGION - chd_file *handle = machine().rom_load().get_disk_handle(tag()); - if (handle != nullptr) - { - m_hard_disk_handle = hard_disk_open(handle); - } + if (has_preset_images()) + setup_current_preset_image(); else - { - m_hard_disk_handle = nullptr; - } + m_hard_disk_handle.reset(); } void harddisk_image_device::device_stop() { - if (m_hard_disk_handle != nullptr) - { - hard_disk_close(m_hard_disk_handle); - m_hard_disk_handle = nullptr; - } + m_hard_disk_handle.reset(); } -image_init_result harddisk_image_device::call_load() +std::pair<std::error_condition, std::string> harddisk_image_device::call_load() { - image_init_result our_result; - - our_result = internal_load_hd(); + std::error_condition our_result = internal_load_hd(); - /* Check if there is an image_load callback defined */ - if (!m_device_image_load.isnull()) + // Check if there is an image_load callback defined + if (!our_result && !m_device_image_load.isnull()) { - /* Let the override do some additional work/checks */ + // Let the override do some additional work/checks our_result = m_device_image_load(*this); } - return our_result; + return std::make_pair(our_result, std::string()); } -image_init_result harddisk_image_device::call_create(int create_format, util::option_resolution *create_args) +std::pair<std::error_condition, std::string> harddisk_image_device::call_create(int create_format, util::option_resolution *create_args) { - int err; - if (!create_args) throw emu_fatalerror("harddisk_image_device::call_create: Expected create_args to not be nullptr"); @@ -143,33 +146,38 @@ image_init_result harddisk_image_device::call_create(int create_format, util::op const uint32_t totalsectors = cylinders * heads * sectors; - /* create the CHD file */ + // create the CHD file chd_codec_type compression[4] = { CHD_CODEC_NONE }; - err = m_origchd.create(image_core_file(), (uint64_t)totalsectors * (uint64_t)sectorsize, hunksize, sectorsize, compression); - if (err != CHDERR_NONE) - return image_init_result::FAIL; - - /* if we created the image and hence, have metadata to set, set the metadata */ + util::core_file::ptr proxy; + std::error_condition err = util::core_file::open_proxy(image_core_file(), proxy); + if (!err) + err = m_origchd.create(std::move(proxy), uint64_t(totalsectors) * uint64_t(sectorsize), hunksize, sectorsize, compression); + if (err) + return std::make_pair(err, std::string()); + + // if we created the image and hence, have metadata to set, set the metadata err = m_origchd.write_metadata(HARD_DISK_METADATA_TAG, 0, string_format(HARD_DISK_METADATA_FORMAT, cylinders, heads, sectors, sectorsize)); m_origchd.close(); - if (err != CHDERR_NONE) - return image_init_result::FAIL; + if (err) + return std::make_pair(err, std::string()); - return internal_load_hd(); + return std::make_pair(internal_load_hd(), std::string()); +} + +void harddisk_image_device::setup_current_preset_image() +{ + chd_file *chd = current_preset_image_chd(); + m_hard_disk_handle.reset(new hard_disk_file(chd)); } void harddisk_image_device::call_unload() { - /* Check if there is an image_unload callback defined */ + // Check if there is an image_unload callback defined if (!m_device_image_unload.isnull()) m_device_image_unload(*this); - if (m_hard_disk_handle != nullptr) - { - hard_disk_close(m_hard_disk_handle); - m_hard_disk_handle = nullptr; - } + m_hard_disk_handle.reset(); if (m_chd) { @@ -183,28 +191,28 @@ void harddisk_image_device::call_unload() open_disk_diff - open a DISK diff file -------------------------------------------------*/ -static chd_error open_disk_diff(emu_options &options, const char *name, chd_file &source, chd_file &diff_chd) +static std::error_condition open_disk_diff(emu_options &options, const char *name, chd_file &source, chd_file &diff_chd) { std::string fname = std::string(name).append(".dif"); /* try to open the diff */ //printf("Opening differencing image file: %s\n", fname.c_str()); emu_file diff_file(options.diff_directory(), OPEN_FLAG_READ | OPEN_FLAG_WRITE); - osd_file::error filerr = diff_file.open(fname.c_str()); - if (filerr == osd_file::error::NONE) + std::error_condition filerr = diff_file.open(fname); + if (!filerr) { std::string fullpath(diff_file.fullpath()); diff_file.close(); //printf("Opening differencing image file: %s\n", fullpath.c_str()); - return diff_chd.open(fullpath.c_str(), true, &source); + return diff_chd.open(fullpath, true, &source); } /* didn't work; try creating it instead */ //printf("Creating differencing image: %s\n", fname.c_str()); diff_file.set_openflags(OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS); - filerr = diff_file.open(fname.c_str()); - if (filerr == osd_file::error::NONE) + filerr = diff_file.open(fname); + if (!filerr) { std::string fullpath(diff_file.fullpath()); diff_file.close(); @@ -212,29 +220,31 @@ static chd_error open_disk_diff(emu_options &options, const char *name, chd_file /* create the CHD */ //printf("Creating differencing image file: %s\n", fupointllpath.c_str()); chd_codec_type compression[4] = { CHD_CODEC_NONE }; - chd_error err = diff_chd.create(fullpath.c_str(), source.logical_bytes(), source.hunk_bytes(), compression, source); - if (err != CHDERR_NONE) + std::error_condition err = diff_chd.create(fullpath, source.logical_bytes(), source.hunk_bytes(), compression, source); + if (err) return err; return diff_chd.clone_all_metadata(source); } - return CHDERR_FILE_NOT_FOUND; + return std::errc::no_such_file_or_directory; } -image_init_result harddisk_image_device::internal_load_hd() +std::error_condition harddisk_image_device::internal_load_hd() { - chd_error err = CHDERR_NONE; + if (has_preset_images()) + { + setup_current_preset_image(); + return std::error_condition(); + } + + std::error_condition err; m_chd = nullptr; uint8_t header[64]; - if (m_hard_disk_handle != nullptr) - { - hard_disk_close(m_hard_disk_handle); - m_hard_disk_handle = nullptr; - } + m_hard_disk_handle.reset(); - /* open the CHD file */ + // open the CHD file if (loaded_through_softlist()) { m_chd = machine().rom_load().get_disk_handle(device().subtag("harddriv").c_str()); @@ -247,19 +257,25 @@ image_init_result harddisk_image_device::internal_load_hd() if (!memcmp("MComprHD", header, 8)) { - err = m_origchd.open(image_core_file(), true); + util::core_file::ptr proxy; + err = util::core_file::open_proxy(image_core_file(), proxy); + if (!err) + err = m_origchd.open(std::move(proxy), true); - if (err == CHDERR_NONE) + if (!err) { m_chd = &m_origchd; } - else if (err == CHDERR_FILE_NOT_WRITEABLE) + else if (err == chd_file::error::FILE_NOT_WRITEABLE) { - err = m_origchd.open(image_core_file(), false); - if (err == CHDERR_NONE) + err = util::core_file::open_proxy(image_core_file(), proxy); + if (!err) + err = m_origchd.open(std::move(proxy), false); + + if (!err) { err = open_disk_diff(device().machine().options(), basename_noext(), m_origchd, m_diffchd); - if (err == CHDERR_NONE) + if (!err) { m_chd = &m_diffchd; } @@ -268,69 +284,103 @@ image_init_result harddisk_image_device::internal_load_hd() } } - if (m_chd != nullptr) + if (m_chd) + { + // open the hard disk file + try + { + m_hard_disk_handle.reset(new hard_disk_file(m_chd)); + if (m_hard_disk_handle) + return std::error_condition(); + } + catch (...) + { + err = image_error::INVALIDIMAGE; + } + } + else if (!is_open()) { - /* open the hard disk file */ - m_hard_disk_handle = hard_disk_open(m_chd); - if (m_hard_disk_handle != nullptr) - return image_init_result::PASS; + err = image_error::UNSPECIFIED; } else { - if (is_open()) - { - uint32_t skip = 0; + uint32_t skip = 0; - // check for 2MG format - if (!memcmp(header, "2IMG", 4)) + if (!memcmp(header, "2IMG", 4)) // check for 2MG format + { + skip = get_u32le(&header[0x18]); + osd_printf_verbose("harddriv: detected 2MG, creator is %c%c%c%c, data at %08x\n", header[4], header[5], header[6], header[7], skip); + } + else if (is_filetype("hdi")) // check for HDI format + { + skip = get_u32le(&header[0x8]); + uint32_t data_size = get_u32le(&header[0xc]); + if (data_size == length() - skip) { - skip = header[0x18] | (header[0x19] << 8) | (header[0x1a] << 16) | (header[0x1b] << 24); - osd_printf_verbose("harddriv: detected 2MG, creator is %c%c%c%c, data at %08x\n", header[4], header[5], header[6], header[7], skip); + osd_printf_verbose("harddriv: detected Anex86 HDI, data at %08x\n", skip); } - // check for HDI format - else if (is_filetype("hdi")) + else { - skip = header[0x8] | (header[0x9] << 8) | (header[0xa] << 16) | (header[0xb] << 24); - uint32_t data_size = header[0xc] | (header[0xd] << 8) | (header[0xe] << 16) | (header[0xf] << 24); - if (data_size == image_core_file().size() - skip) - { - osd_printf_verbose("harddriv: detected Anex86 HDI, data at %08x\n", skip); - } - else - { - skip = 0; - } + skip = 0; } - - m_hard_disk_handle = hard_disk_open(image_core_file(), skip); - if (m_hard_disk_handle != nullptr) - return image_init_result::PASS; } - return image_init_result::FAIL; + try + { + m_hard_disk_handle.reset(new hard_disk_file(image_core_file(), skip)); + if (m_hard_disk_handle) + return std::error_condition(); + } + catch (...) + { + err = image_error::INVALIDIMAGE; + } } /* if we had an error, close out the CHD */ - m_origchd.close(); m_diffchd.close(); + m_origchd.close(); m_chd = nullptr; - seterror(IMAGE_ERROR_UNSPECIFIED, chd_file::error_string(err)); - return image_init_result::FAIL; + if (err) + return err; + else + return image_error::UNSPECIFIED; } -/************************************* - * - * Get the CHD file (from the src/chd.c core) - * after an image has been opened with the hd core - * - *************************************/ +const hard_disk_file::info &harddisk_image_device::get_info() const +{ + return m_hard_disk_handle->get_info(); +} -chd_file *harddisk_image_device::get_chd_file() +bool harddisk_image_device::read(uint32_t lbasector, void *buffer) { - chd_file *result = nullptr; - hard_disk_file *hd_file = get_hard_disk_file(); - if (hd_file) - result = hard_disk_get_chd(hd_file); - return result; + return m_hard_disk_handle->read(lbasector, buffer); } + +bool harddisk_image_device::write(uint32_t lbasector, const void *buffer) +{ + return m_hard_disk_handle->write(lbasector, buffer); +} + + +bool harddisk_image_device::set_block_size(uint32_t blocksize) +{ + return m_hard_disk_handle->set_block_size(blocksize); +} + +std::error_condition harddisk_image_device::get_inquiry_data(std::vector<uint8_t> &data) const +{ + return m_hard_disk_handle->get_inquiry_data(data); +} + +std::error_condition harddisk_image_device::get_cis_data(std::vector<uint8_t> &data) const +{ + return m_hard_disk_handle->get_cis_data(data); +} + +std::error_condition harddisk_image_device::get_disk_key_data(std::vector<uint8_t> &data) const +{ + return m_hard_disk_handle->get_disk_key_data(data); +} + |