mirror of
https://github.com/dingusdev/dingusppc.git
synced 2026-04-19 10:20:36 +00:00
blockstoragedevice: clean up and add write support.
This commit is contained in:
@@ -22,11 +22,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
/** @file Block storage device implementation. */
|
||||
|
||||
#include <devices/storage/blockstoragedevice.h>
|
||||
#include <loguru.hpp>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
using namespace std;
|
||||
|
||||
BlockStorageDevice::BlockStorageDevice(const uint32_t cache_blocks,
|
||||
const uint32_t block_size,
|
||||
const uint64_t max_blocks) {
|
||||
@@ -49,7 +48,7 @@ int BlockStorageDevice::set_host_file(std::string file_path) {
|
||||
if (!this->img_file.open(file_path))
|
||||
return -1;
|
||||
|
||||
this->size_bytes = this->img_file.size();
|
||||
this->size_bytes = this->img_file.size();
|
||||
|
||||
this->set_fpos(0);
|
||||
|
||||
@@ -105,9 +104,8 @@ int BlockStorageDevice::read_begin(int nblocks, uint32_t max_len) {
|
||||
if (read_size > xfer_len) {
|
||||
this->remain_size = read_size - xfer_len;
|
||||
read_size = xfer_len;
|
||||
} else {
|
||||
} else
|
||||
this->remain_size = 0;
|
||||
}
|
||||
|
||||
this->fill_cache(read_size / this->block_size);
|
||||
|
||||
@@ -132,9 +130,41 @@ int BlockStorageDevice::read_more() {
|
||||
return read_size;
|
||||
}
|
||||
|
||||
int BlockStorageDevice::write_begin(char *buf, int nblocks) {
|
||||
int BlockStorageDevice::write_begin(int nblocks, uint32_t max_len) {
|
||||
if (!this->is_writeable)
|
||||
return -1;
|
||||
ABORT_F("write attempt to read-only block storage device");
|
||||
|
||||
return 0;
|
||||
uint64_t xfer_len = std::min(this->cache_blocks * this->block_size, max_len);
|
||||
|
||||
this->write_size = nblocks * this->block_size;
|
||||
if (this->write_size > xfer_len) {
|
||||
this->remain_size = write_size - xfer_len;
|
||||
this->write_size = xfer_len;
|
||||
} else
|
||||
this->remain_size = 0;
|
||||
|
||||
return this->write_size;
|
||||
}
|
||||
|
||||
int BlockStorageDevice::write_more() {
|
||||
this->write_cache();
|
||||
|
||||
this->write_size = this->cache_blocks * this->block_size;
|
||||
|
||||
if (this->remain_size > this->write_size) {
|
||||
this->remain_size -= this->write_size;
|
||||
} else {
|
||||
this->write_size = this->remain_size;
|
||||
this->remain_size = 0;
|
||||
}
|
||||
|
||||
return this->write_size;
|
||||
}
|
||||
|
||||
void BlockStorageDevice::write_cache() {
|
||||
if (this->write_size) {
|
||||
this->img_file.write(this->data_cache.get(), this->cur_fpos, this->write_size);
|
||||
this->cur_fpos += this->write_size;
|
||||
this->write_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,17 +33,19 @@ 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);
|
||||
const uint64_t max_blocks=UINT64_MAX);
|
||||
~BlockStorageDevice();
|
||||
|
||||
int set_host_file(std::string file_path);
|
||||
int set_block_size(const int blk_size);
|
||||
|
||||
int set_fpos(const uint64_t lba);
|
||||
int read_begin(int nblocks, uint32_t max_len = UINT32_MAX);
|
||||
int data_left() { return this->remain_size; }
|
||||
int read_begin(int nblocks, uint32_t max_len = UINT32_MAX);
|
||||
int read_more();
|
||||
int write_begin(char *buf, int nblocks);
|
||||
int write_begin(int nblocks, uint32_t max_len = UINT32_MAX);
|
||||
int write_more();
|
||||
void write_cache();
|
||||
|
||||
protected:
|
||||
void fill_cache(const int nblocks);
|
||||
@@ -53,6 +55,7 @@ protected:
|
||||
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
|
||||
uint64_t write_size = 0; // number of bytes to write next
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user