| 1 |
#ifndef DISK_IMAGE_UTILS_HH |
| 2 |
#define DISK_IMAGE_UTILS_HH |
| 3 |
|
| 4 |
#include "AlignedBuffer.hh" |
| 5 |
#include "endian.hh" |
| 6 |
#include "ranges.hh" |
| 7 |
|
| 8 |
#include <array> |
| 9 |
#include <cstdint> |
| 10 |
#include <ctime> |
| 11 |
#include <span> |
| 12 |
#include <string> |
| 13 |
#include <utility> |
| 14 |
|
| 15 |
namespace openmsx { |
| 16 |
|
| 17 |
class SectorAccessibleDisk; |
| 18 |
|
| 19 |
enum class MSXBootSectorType : int { |
| 20 |
DOS1 = 0, |
| 21 |
DOS2 = 1, |
| 22 |
NEXTOR = 2, |
| 23 |
}; |
| 24 |
|
| 25 |
struct MSXBootSector { |
| 26 |
std::array<uint8_t, 3> jumpCode; |
| 27 |
std::array<uint8_t, 8> name; |
| 28 |
Endian::UA_L16 bpSector; |
| 29 |
uint8_t spCluster; |
| 30 |
Endian::L16 resvSectors; |
| 31 |
uint8_t nrFats; |
| 32 |
Endian::UA_L16 dirEntries; |
| 33 |
Endian::UA_L16 nrSectors; |
| 34 |
uint8_t descriptor; |
| 35 |
Endian::L16 sectorsFat; |
| 36 |
Endian::L16 sectorsTrack; |
| 37 |
Endian::L16 nrSides; |
| 38 |
union { |
| 39 |
struct { |
| 40 |
Endian::L16 hiddenSectors; |
| 41 |
std::array<uint8_t, 512-30> pad; |
| 42 |
} dos1; |
| 43 |
struct { |
| 44 |
Endian::L16 hiddenSectors; |
| 45 |
std::array<uint8_t, 9> pad1; |
| 46 |
Endian::UA_L32 vol_id; |
| 47 |
std::array<uint8_t, 512-43> pad2; |
| 48 |
} dos2; |
| 49 |
struct { |
| 50 |
Endian::L32 hiddenSectors; |
| 51 |
Endian::L32 nrSectors; |
| 52 |
uint8_t physicalDriveNum; |
| 53 |
uint8_t reserved; |
| 54 |
uint8_t extendedBootSignature; |
| 55 |
Endian::UA_L32 vol_id; |
| 56 |
std::array<uint8_t, 11> volumeLabel; |
| 57 |
std::array<uint8_t, 8> fileSystemType; |
| 58 |
std::array<uint8_t, 512-62> padding; |
| 59 |
} extended; |
| 60 |
} params; |
| 61 |
}; |
| 62 |
static_assert(sizeof(MSXBootSector) == 512); |
| 63 |
|
| 64 |
struct MSXDirEntry { |
| 65 |
enum class Attrib : uint8_t { |
| 66 |
REGULAR = 0x00, |
| 67 |
READONLY = 0x01, |
| 68 |
HIDDEN = 0x02, |
| 69 |
SYSTEM = 0x04, |
| 70 |
VOLUME = 0x08, |
| 71 |
DIRECTORY = 0x10, |
| 72 |
ARCHIVE = 0x20, |
| 73 |
}; |
| 74 |
struct AttribValue { |
| 75 |
uint8_t value; |
| 76 |
|
| 77 |
constexpr AttribValue() = default; |
| 78 |
constexpr explicit(false) AttribValue(Attrib v) : value(std::to_underlying(v)) {} |
| 79 |
constexpr explicit AttribValue(uint8_t v) : value(v) {} |
| 80 |
constexpr explicit operator bool() const { return value != 0; } |
| 81 |
constexpr auto operator<=>(const AttribValue&) const = default; |
| 82 |
friend constexpr AttribValue operator|(AttribValue x, AttribValue y) { return AttribValue(x.value | y.value); } |
| 83 |
friend constexpr AttribValue operator&(AttribValue x, AttribValue y) { return AttribValue(x.value & y.value); } |
| 84 |
}; |
| 85 |
|
| 86 |
std::array<char, 8 + 3> filename; |
| 87 |
AttribValue attrib; |
| 88 |
std::array<uint8_t, 10> reserved; |
| 89 |
Endian::L16 time; |
| 90 |
Endian::L16 date; |
| 91 |
Endian::L16 startCluster; |
| 92 |
Endian::L32 size; |
| 93 |
|
| 94 |
[[nodiscard]] auto base() const { return subspan<8>(filename, 0); } |
| 95 |
[[nodiscard]] auto ext () const { return subspan<3>(filename, 8); } |
| 96 |
|
| 97 |
bool operator==(const MSXDirEntry& other) const = default; |
| 98 |
}; |
| 99 |
static_assert(sizeof(MSXDirEntry) == 32); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
struct Partition { |
| 104 |
uint8_t boot_ind; |
| 105 |
uint8_t head; |
| 106 |
uint8_t sector; |
| 107 |
uint8_t cyl; |
| 108 |
uint8_t sys_ind; |
| 109 |
uint8_t end_head; |
| 110 |
uint8_t end_sector; |
| 111 |
uint8_t end_cyl; |
| 112 |
Endian::UA_L32 start; |
| 113 |
Endian::UA_L32 size; |
| 114 |
}; |
| 115 |
static_assert(sizeof(Partition) == 16); |
| 116 |
static_assert(alignof(Partition) == 1, "must not have alignment requirements"); |
| 117 |
|
| 118 |
struct PartitionTableSunrise { |
| 119 |
std::array<char, 11> header; |
| 120 |
std::array<char, 3> pad; |
| 121 |
std::array<Partition, 31> part; |
| 122 |
Endian::L16 end; |
| 123 |
}; |
| 124 |
static_assert(sizeof(PartitionTableSunrise) == 512); |
| 125 |
|
| 126 |
struct PartitionTableNextor { |
| 127 |
std::array<char, 11> header; |
| 128 |
std::array<char, 435> pad; |
| 129 |
std::array<Partition, 4> part; |
| 130 |
Endian::L16 end; |
| 131 |
}; |
| 132 |
static_assert(sizeof(PartitionTableNextor) == 512); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
union SectorBuffer { |
| 142 |
std::array<uint8_t, 512> raw; |
| 143 |
MSXBootSector bootSector; |
| 144 |
std::array<MSXDirEntry, 16> dirEntry; |
| 145 |
PartitionTableSunrise ptSunrise; |
| 146 |
PartitionTableNextor ptNextor; |
| 147 |
AlignedBuffer aligned; |
| 148 |
}; |
| 149 |
static_assert(sizeof(SectorBuffer) == 512); |
| 150 |
|
| 151 |
|
| 152 |
namespace DiskImageUtils { |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
Partition& getPartition(SectorAccessibleDisk& disk, unsigned partition, SectorBuffer& buf); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
void checkSupportedPartition(SectorAccessibleDisk& disk, unsigned partition); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
[[nodiscard]] bool hasPartitionTable(SectorAccessibleDisk& disk); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
void format(SectorAccessibleDisk& disk, MSXBootSectorType bootType); |
| 179 |
void format(SectorAccessibleDisk& disk, MSXBootSectorType bootType, size_t nbSectors); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
unsigned partition(SectorAccessibleDisk& disk, |
| 187 |
std::span<const unsigned> sizes, MSXBootSectorType bootType); |
| 188 |
|
| 189 |
struct FatTimeDate { |
| 190 |
uint16_t time, date; |
| 191 |
}; |
| 192 |
FatTimeDate toTimeDate(time_t totalSeconds); |
| 193 |
time_t fromTimeDate(FatTimeDate timeDate); |
| 194 |
|
| 195 |
[[nodiscard]] std::string formatAttrib(MSXDirEntry::AttribValue attrib); |
| 196 |
}; |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
#endif |