Files
RASCSI/cpp/devices/disk_track.cpp
Chris Hooper 25718e0887 Add support for drives larger than 2 TB (Closes: #1551)
Mode Sense (6) supports a 32-bit number for the maximum block count, where Mode Sense (10) supports a 64-bit number for the count. The Seagate SCSI Commands Reference Manual from 2016, has this to say about a "Short LBA mode parameter block descriptor" as returned by Mode Sense (6):

>  On a MODE SENSE command, if the number of logical blocks on the
>  medium exceeds the maximum value that is able to be specified in
>  the NUMBER OF LOGICAL BLOCKS field, the device server shall return
>  a value of FFFFFFFh.

Similarly, the Read Capacity (10) command should return FFFFFFFFh if the capacity of the drive exceeds 2^32 sectors, and that a driver should then know to use Read Capacity (16) if it supports that command.

There may be an unexpected side-effect if presenting a drive of more than 2^32 sectors to a legacy host that does not support devices that large. The Read Capacity commands specify the value returned as the last addressable sector on the device. This means that typically, an application which uses the value returned by that command is going to add 1 to it to get the actual number of blocks on a given device. If the program is not aware of Read Capacity (16), and is not using greater than 32-bit math, then it might report to the user that the total number of sectors on the drive as 0. I don't view this as a huge problem, however. In that case, the legacy driver wouldn't correctly support the capacity of the drive anyway, so providing that driver with a device that is 2 ^ 32 sectors or larger wouldn't make sense from the user perspective.
2025-11-18 21:03:26 +01:00

282 lines
5.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//---------------------------------------------------------------------------
//
// X68000 EMULATOR "XM6"
//
// Copyright (C) 2001-2006 (ytanaka@ipc-tokai.or.jp)
// Copyright (C) 2014-2020 GIMONS
//
// XM6i
// Copyright (C) 2010-2015 isaki@NetBSD.org
// Copyright (C) 2010 Y.Sugahara
//
// Imported sava's Anex86/T98Next image and MO format support patch.
// Comments translated to english by akuker.
//
//---------------------------------------------------------------------------
#include "disk_track.h"
#include <spdlog/spdlog.h>
#include <cassert>
#include <cstdlib>
#include <fstream>
DiskTrack::~DiskTrack()
{
// Release memory, but do not save automatically
free(dt.buffer);
}
void DiskTrack::Init(int track, int size, int sectors, bool raw, off_t imgoff)
{
assert(track >= 0);
assert((sectors > 0) && (sectors <= 0x100));
assert(imgoff >= 0);
// Set Parameters
dt.track = track;
dt.size = size;
dt.sectors = sectors;
dt.raw = raw;
// Not initialized (needs to be loaded)
dt.init = false;
// Not Changed
dt.changed = false;
// Offset to actual data
dt.imgoffset = imgoff;
}
bool DiskTrack::Load(const string& path, uint64_t& cache_miss_read_count)
{
// Not needed if already loaded
if (dt.init) {
assert(dt.buffer);
return true;
}
++cache_miss_read_count;
// Calculate offset (previous tracks are considered to hold 256 sectors)
off_t offset = ((off_t)dt.track << 8);
if (dt.raw) {
assert(dt.size == 11);
offset *= 0x930;
offset += 0x10;
} else {
offset <<= dt.size;
}
// Add offset to real image
offset += dt.imgoffset;
// Calculate length (data size of this track)
const int64_t length = dt.sectors << dt.size;
// Allocate buffer memory
assert((dt.sectors > 0) && (dt.sectors <= 0x100));
if (dt.buffer == nullptr) {
if (posix_memalign((void **)&dt.buffer, 512, ((length + 511) / 512) * 512)) {
spdlog::warn("posix_memalign failed");
}
dt.length = length;
}
if (dt.buffer == nullptr) {
return false;
}
// Reallocate if the buffer length is different
if (dt.length != static_cast<uint32_t>(length)) {
free(dt.buffer);
if (posix_memalign((void **)&dt.buffer, 512, ((length + 511) / 512) * 512)) {
spdlog::warn("posix_memalign failed");
}
dt.length = length;
}
// Resize and clear changemap
dt.changemap.resize(dt.sectors);
fill(dt.changemap.begin(), dt.changemap.end(), false); //NOSONAR ranges::fill() cannot be applied to vector<bool>
ifstream in(path, ios::binary);
if (in.fail()) {
return false;
}
if (dt.raw) {
// Split Reading
for (int i = 0; i < dt.sectors; i++) {
in.seekg(offset);
if (in.fail()) {
return false;
}
in.read((char *)&dt.buffer[i << dt.size], 1 << dt.size);
if (in.fail()) {
return false;
}
// Next offset
offset += 0x930;
}
} else {
// Continuous reading
in.seekg(offset);
if (in.fail()) {
return false;
}
in.read((char *)dt.buffer, length);
if (in.fail()) {
return false;
}
}
// Set a flag and end normally
dt.init = true;
dt.changed = false;
return true;
}
bool DiskTrack::Save(const string& path, uint64_t& cache_miss_write_count)
{
// Not needed if not initialized
if (!dt.init) {
return true;
}
// Not needed unless changed
if (!dt.changed) {
return true;
}
++cache_miss_write_count;
// Need to write
assert(dt.buffer);
assert((dt.sectors > 0) && (dt.sectors <= 0x100));
// Writing in RAW mode is not allowed
assert(!dt.raw);
// Calculate offset (previous tracks are considered to hold 256 sectors)
off_t offset = ((off_t)dt.track << 8);
offset <<= dt.size;
// Add offset to real image
offset += dt.imgoffset;
// Calculate length per sector
const int length = 1 << dt.size;
ofstream out(path, ios::in | ios::out | ios::binary);
if (out.fail()) {
return false;
}
// Partial write loop
int total;
for (int i = 0; i < dt.sectors;) {
// If changed
if (dt.changemap[i]) {
// Initialize write size
total = 0;
out.seekp(offset + ((off_t)i << dt.size));
if (out.fail()) {
return false;
}
// Consectutive sector length
int j;
for (j = i; j < dt.sectors; j++) {
// end when interrupted
if (!dt.changemap[j]) {
break;
}
// Add one sector
total += length;
}
out.write((const char *)&dt.buffer[i << dt.size], total);
if (out.fail()) {
return false;
}
// To unmodified sector
i = j;
} else {
// Next Sector
i++;
}
}
// Drop the change flag and exit
fill(dt.changemap.begin(), dt.changemap.end(), false); //NOSONAR ranges::fill() cannot be applied to vector<bool>
dt.changed = false;
return true;
}
bool DiskTrack::ReadSector(span<uint8_t> buf, int sec) const
{
assert(sec >= 0 && sec < 0x100);
// Error if not initialized
if (!dt.init) {
return false;
}
// // Error if the number of sectors exceeds the valid number
if (sec >= dt.sectors) {
return false;
}
// Copy
assert(dt.buffer);
assert((dt.sectors > 0) && (dt.sectors <= 0x100));
memcpy(buf.data(), &dt.buffer[(off_t)sec << dt.size], (off_t)1 << dt.size);
// Success
return true;
}
bool DiskTrack::WriteSector(span<const uint8_t> buf, int sec)
{
assert((sec >= 0) && (sec < 0x100));
assert(!dt.raw);
// Error if not initialized
if (!dt.init) {
return false;
}
// // Error if the number of sectors exceeds the valid number
if (sec >= dt.sectors) {
return false;
}
// Calculate offset and length
const int offset = sec << dt.size;
const int length = 1 << dt.size;
// Compare
assert(dt.buffer);
assert((dt.sectors > 0) && (dt.sectors <= 0x100));
if (memcmp(buf.data(), &dt.buffer[offset], length) == 0) {
// Exit normally since it's attempting to write the same thing
return true;
}
// Copy, change
memcpy(&dt.buffer[offset], buf.data(), length);
dt.changemap[sec] = true;
dt.changed = true;
// Success
return true;
}