blockstoragedevice: add support for raw images.

Raw images contain supplementary information like synchronization fields,
correction codes etc. that needs to be ignored/discarded when
reading/writing user data.
This commit establish a basic architecture for exchanging user data
between a virtual device and a raw image.
This commit is contained in:
Maxim Poliakovski 2024-07-15 01:38:26 +02:00
parent 55f91c5d3f
commit fd5498e16b
2 changed files with 67 additions and 28 deletions

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
Copyright (C) 2018-24 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -23,15 +23,20 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <devices/storage/blockstoragedevice.h>
#include <cstring>
using namespace std;
BlockStorageDevice::BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size, const uint64_t max_blocks) {
this->block_size = block_size;
this->cache_size = cache_blocks * this->block_size;
this->max_blocks = max_blocks;
// allocate device cache and fill it with zeroes
this->data_cache = std::unique_ptr<char[]>(new char[this->cache_size] ());
BlockStorageDevice::BlockStorageDevice(const uint32_t cache_blocks,
const uint32_t block_size,
const uint64_t max_blocks) {
this->block_size = block_size;
this->raw_blk_size = block_size;
this->cache_blocks = cache_blocks;
this->max_blocks = max_blocks;
this->is_ready = false;
this->cache_size = 0;
this->data_offset = 0;
}
BlockStorageDevice::~BlockStorageDevice() {
@ -41,29 +46,61 @@ BlockStorageDevice::~BlockStorageDevice() {
int BlockStorageDevice::set_host_file(std::string file_path) {
this->is_ready = false;
if (!this->img_file.open(file_path)) {
if (!this->img_file.open(file_path))
return -1;
}
this->size_bytes = this->img_file.size();
this->size_blocks = this->size_bytes / this->block_size;
if (this->size_blocks > this->max_blocks)
return -1;
this->set_fpos(0);
this->is_ready = true;
return this->set_block_size(this->block_size);
}
int BlockStorageDevice::set_block_size(const int blk_size) {
this->raw_blk_size = blk_size;
if (this->is_ready) {
this->size_blocks = this->size_bytes / this->raw_blk_size;
if (this->size_blocks > this->max_blocks)
return -1;
}
this->cache_size = this->cache_blocks * this->raw_blk_size;
// allocate data cache and fill it with zeroes
this->data_cache = std::unique_ptr<char[]>(new char[this->cache_size] ());
return 0;
}
int BlockStorageDevice::set_fpos(const uint32_t lba) {
this->cur_fpos = lba * this->block_size;
this->cur_fpos = lba * this->raw_blk_size;
return 0;
}
void BlockStorageDevice::fill_cache(const int nblocks) {
uint32_t read_size = nblocks * this->raw_blk_size;
this->img_file.read(this->data_cache.get(), this->cur_fpos, read_size);
this->cur_fpos += read_size;
// extract block data from raw images while discarding anything else
if (this->raw_blk_size > this->block_size) {
char *cache_ptr = this->data_cache.get();
for (int blk = 0; blk < nblocks; blk++) {
memcpy(cache_ptr + blk * this->block_size,
cache_ptr + blk * this->raw_blk_size + this->data_offset,
this->block_size
);
}
}
}
int BlockStorageDevice::read_begin(int nblocks, uint32_t max_len) {
uint32_t xfer_len = std::min(this->cache_size, max_len);
uint32_t xfer_len = std::min(this->cache_blocks * this->block_size, max_len);
uint32_t read_size = nblocks * this->block_size;
if (read_size > xfer_len) {
this->remain_size = read_size - xfer_len;
@ -72,28 +109,25 @@ int BlockStorageDevice::read_begin(int nblocks, uint32_t max_len) {
this->remain_size = 0;
}
this->img_file.read(this->data_cache.get(), this->cur_fpos, read_size);
this->cur_fpos += read_size;
this->fill_cache(read_size / this->block_size);
return read_size;
}
int BlockStorageDevice::read_more() {
uint32_t read_size;
if (!this->remain_size)
return 0;
if (this->remain_size > this->cache_size) {
this->remain_size -= this->cache_size;
read_size = this->cache_size;
uint32_t read_size = this->cache_blocks * this->block_size;
if (this->remain_size > read_size) {
this->remain_size -= read_size;
} else {
read_size = this->remain_size;
this->remain_size = 0;
}
this->img_file.read(this->data_cache.get(), this->cur_fpos, read_size);
this->cur_fpos += read_size;
this->fill_cache(read_size / this->block_size);
return read_size;
}

View File

@ -1,6 +1,6 @@
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
Copyright (C) 2018-24 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
@ -32,12 +32,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
class BlockStorageDevice {
public:
BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size=512, const uint64_t max_blocks=0xffffffffffffffff);
BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size=512,
const uint64_t max_blocks=0xffffffffffffffff);
~BlockStorageDevice();
void set_block_size(const int blk_size) { this->block_size = blk_size; };
int set_host_file(std::string file_path);
int set_block_size(const int blk_size);
int set_fpos(const uint32_t lba);
int read_begin(int nblocks, uint32_t max_len);
@ -46,12 +46,17 @@ public:
int write_begin(char *buf, int nblocks);
protected:
void fill_cache(const int nblocks);
ImgFile img_file;
uint64_t size_bytes = 0; // image file size in bytes
uint64_t size_blocks = 0; // image file size in blocks
uint64_t max_blocks = 0; // maximum number of blocks supported
uint64_t cur_fpos = 0; // current image file pointer position
uint32_t block_size = 512; // physical block size
uint32_t raw_blk_size = 512; // block size for raw images (CD-ROM)
uint32_t data_offset = 0; // offset to block data for raw images
uint32_t cache_blocks = 0;
uint32_t cache_size = 0; // cache size
uint32_t remain_size = 0;
bool is_writeable = false;