CD-ROM: Add max blocks check.

The code does not support more than 2^32 - 2 blocks because of this expression: static_cast<uint32_t>(this->size_blocks + 1)
This commit is contained in:
joevt 2023-07-14 16:54:26 -07:00 committed by dingusdev
parent 655b9a17e1
commit 3978d0754d
3 changed files with 9 additions and 4 deletions

View File

@ -25,9 +25,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
using namespace std;
BlockStorageDevice::BlockStorageDevice(const uint32_t cache_blocks, const uint32_t block_size) {
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] ());
@ -46,6 +47,9 @@ int BlockStorageDevice::set_host_file(std::string file_path) {
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;

View File

@ -32,7 +32,7 @@ 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);
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; };
@ -49,6 +49,7 @@ protected:
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 cache_size = 0; // cache size

View File

@ -30,7 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstring>
#include <string>
CdromDrive::CdromDrive() : BlockStorageDevice(31, 2048) {
CdromDrive::CdromDrive() : BlockStorageDevice(31, 2048, 0xfffffffe) {
this->is_writeable = false;
}
@ -133,7 +133,7 @@ uint32_t CdromDrive::request_sense(uint8_t *data_ptr, uint8_t sense_key,
}
uint32_t CdromDrive::report_capacity(uint8_t *data_ptr) {
WRITE_DWORD_BE_A(data_ptr, this->size_blocks);
WRITE_DWORD_BE_A(data_ptr, static_cast<uint32_t>(this->size_blocks));
WRITE_DWORD_BE_A(&data_ptr[4], this->block_size);
return 8;
}