harddisk.cpp: Allow specifying the desired block size for loose files / verifying a CHD's block size. [R. Belmont]

This commit is contained in:
arbee 2021-09-18 13:28:14 -04:00
parent 40c382bdbc
commit 13201c72ec
2 changed files with 47 additions and 7 deletions

View File

@ -147,7 +147,7 @@ chd_file *hard_disk_get_chd(hard_disk_file *file)
*
* @brief Hard disk get information.
*
* @param [in,out] file If non-null, the file.
* @param [in,out] file The hard disk file object to operate on.
*
* @return null if it fails, else a hard_disk_info*.
*/
@ -168,9 +168,9 @@ hard_disk_info *hard_disk_get_info(hard_disk_file *file)
*
* @brief Hard disk read.
*
* @param [in,out] file If non-null, the file.
* @param lbasector The lbasector.
* @param [in,out] buffer If non-null, the buffer.
* @param [in,out] file The hard disk file object to operate on.
* @param lbasector The sector number (Linear Block Address) to read.
* @param buffer The buffer where the hard disk data will be placed.
*
* @return An uint32_t.
*/
@ -202,9 +202,9 @@ uint32_t hard_disk_read(hard_disk_file *file, uint32_t lbasector, void *buffer)
*
* @brief Hard disk write.
*
* @param [in,out] file If non-null, the file.
* @param lbasector The lbasector.
* @param buffer The buffer.
* @param [in,out] file The hard disk file object to operate on.
* @param lbasector The sector number (Linear Block Address) to write.
* @param buffer The buffer containing the data to write.
*
* @return An uint32_t.
*/
@ -224,3 +224,41 @@ uint32_t hard_disk_write(hard_disk_file *file, uint32_t lbasector, const void *b
return (actual == file->info.sectorbytes);
}
}
/*-------------------------------------------------
hard_disk_set_block_size - sets the block size
for a non-CHD-backed hard disk (a bare file).
-------------------------------------------------*/
/**
* @fn bool hard_disk_set_block_size(hard_disk_file *file, uint32_t blocksize)
*
* @brief Hard disk set block size (works only for non-CHD-files)
*
* @param [in,out] file The hard_disk_file object to operate on.
* @param blocksize The block size of this hard disk, in bytes.
*
* @return true on success, false on failure. Failure means a CHD is in use and the CHD
* block size does not match the passed in size. If a CHD is in use and the block
* sizes match, success is returned).
*/
bool hard_disk_set_block_size(hard_disk_file *file, uint32_t blocksize)
{
if (file->chd)
{
// if the CHD block size matches our block size, we're OK.
if (file->chd->unit_bytes() == blocksize)
{
return true;
}
// indicate failure, since we can't change the block size of a CHD.
return false;
}
else
{
file->info.sectorbytes = blocksize;
}
return true;
}

View File

@ -48,6 +48,8 @@ void hard_disk_close(hard_disk_file *file);
chd_file *hard_disk_get_chd(hard_disk_file *file);
hard_disk_info *hard_disk_get_info(hard_disk_file *file);
bool hard_disk_set_block_size(hard_disk_file *file, uint32_t blocksize);
uint32_t hard_disk_read(hard_disk_file *file, uint32_t lbasector, void *buffer);
uint32_t hard_disk_write(hard_disk_file *file, uint32_t lbasector, const void *buffer);