openMSX
DiskImageUtils.hh
Go to the documentation of this file.
1 #ifndef DISK_IMAGE_UTILS_HH
2 #define DISK_IMAGE_UTILS_HH
3 
4 #include "openmsx.hh"
5 #include "AlignedBuffer.hh"
6 #include "endian.hh"
7 #include <vector>
8 
9 namespace openmsx {
10 
11 class SectorAccessibleDisk;
12 
13 struct MSXBootSector {
14  byte jumpCode[3]; // + 0 0xE5 to bootprogram
15  byte name[8]; // + 3
16  Endian::UA_L16 bpSector; // +11 bytes per sector (always 512)
17  byte spCluster; // +13 sectors per cluster (always 2)
18  Endian::L16 resvSectors; // +14 nb of non-data sectors (ex bootsector)
19  byte nrFats; // +16 nb of fats
20  Endian::UA_L16 dirEntries; // +17 max nb of files in root directory
21  Endian::UA_L16 nrSectors; // +19 nb of sectors on this disk
22  byte descriptor; // +21 media descriptor
23  Endian::L16 sectorsFat; // +22 sectors per FAT
24  Endian::L16 sectorsTrack; // +24 sectors per track
25  Endian::L16 nrSides; // +26 number of side
26  Endian::L16 hiddenSectors; // +28 not used
27  byte pad1[9]; // +30
29  byte pad2[512-43]; // +43
30 };
31 static_assert(sizeof(MSXBootSector) == 512);
32 
33 struct MSXDirEntry {
34  static constexpr byte ATT_REGULAR = 0x00; // Normal file
35  static constexpr byte ATT_READONLY = 0x01; // Read-Only file
36  static constexpr byte ATT_HIDDEN = 0x02; // Hidden file
37  static constexpr byte ATT_SYSTEM = 0x04; // System file
38  static constexpr byte ATT_VOLUME = 0x08; // filename is Volume Label
39  static constexpr byte ATT_DIRECTORY = 0x10; // entry is a subdir
40  static constexpr byte ATT_ARCHIVE = 0x20; // Archive bit
41 
42  union {
43  struct {
44  char base[8];// + 0
45  char ext [3];// + 8
46  } name;
47  char filename[8 + 3];// + 0
48  };
49  byte attrib; // +11
50  byte reserved[10]; // +12 unused
51  Endian::L16 time; // +22
52  Endian::L16 date; // +24
54  Endian::L32 size; // +28
55 };
56 static_assert(sizeof(MSXDirEntry) == 32);
57 
58 // Note: can't use Endian::L32 for 'start' and 'size' because the Partition
59 // struct itself is not 4-bytes aligned.
60 struct Partition {
61  byte boot_ind; // + 0 0x80 - active
62  byte head; // + 1 starting head
63  byte sector; // + 2 tarting sector
64  byte cyl; // + 3 starting cylinder
65  byte sys_ind; // + 4 what partition type
66  byte end_head; // + 5 end head
67  byte end_sector; // + 6 end sector
68  byte end_cyl; // + 7 end cylinder
69  Endian::UA_L32 start; // + 8 starting sector counting from 0
70  Endian::UA_L32 size; // +12 nr of sectors in partition
71 };
72 static_assert(sizeof(Partition) == 16);
73 static_assert(alignof(Partition) == 1, "must not have alignment requirements");
74 
76  char header[11]; // + 0
77  char pad[3]; // + 3
78  Partition part[31]; // + 14,+30,..,+494 Not 4-byte aligned!!
79  Endian::L16 end; // +510
80 };
81 static_assert(sizeof(PartitionTable) == 512);
82 
83 
84 // Buffer that can hold a (512-byte) disk sector.
85 // The main advantages of this type over something like 'byte buf[512]' are:
86 // - No need for reinterpret_cast<> when interpreting the data in a
87 // specific way (this could in theory cause alignment problems).
88 // - This type has a stricter alignment, so memcpy() and memset() can work
89 // faster compared to using a raw byte array.
90 union SectorBuffer {
91  byte raw[512]; // raw byte data
92  MSXBootSector bootSector; // interpreted as bootSector
93  MSXDirEntry dirEntry[16]; // interpreted as 16 dir entries
94  PartitionTable pt; // interpreted as Sunrise-IDE partition table
95  AlignedBuffer aligned; // force big alignment (for faster memcpy)
96 };
97 static_assert(sizeof(SectorBuffer) == 512);
98 
99 
100 namespace DiskImageUtils {
101 
109  void checkValidPartition(SectorAccessibleDisk& disk, unsigned partition);
110 
113  void checkFAT12Partition(SectorAccessibleDisk& disk, unsigned partition);
114 
117  [[nodiscard]] bool hasPartitionTable(SectorAccessibleDisk& disk);
118 
124  void format(SectorAccessibleDisk& disk, bool dos1 = false);
125 
130  void partition(SectorAccessibleDisk& disk,
131  const std::vector<unsigned>& sizes);
132 };
133 
134 } // namespace openmsx
135 
136 #endif
openmsx::DiskImageUtils::checkFAT12Partition
void checkFAT12Partition(SectorAccessibleDisk &disk, unsigned partition)
Like above, but also check whether partition is of type FAT12.
Definition: DiskImageUtils.cc:57
openmsx.hh
openmsx::MSXDirEntry::ATT_HIDDEN
static constexpr byte ATT_HIDDEN
Definition: DiskImageUtils.hh:36
openmsx::PartitionTable::part
Partition part[31]
Definition: DiskImageUtils.hh:78
openmsx::Partition::start
Endian::UA_L32 start
Definition: DiskImageUtils.hh:69
openmsx::SectorAccessibleDisk
Definition: SectorAccessibleDisk.hh:16
openmsx::MSXBootSector::name
byte name[8]
Definition: DiskImageUtils.hh:15
openmsx::DiskImageUtils::format
void format(SectorAccessibleDisk &disk, bool dos1)
Format the given disk (= a single partition).
Definition: DiskImageUtils.cc:188
openmsx::SectorBuffer
Definition: DiskImageUtils.hh:90
openmsx::MSXDirEntry::reserved
byte reserved[10]
Definition: DiskImageUtils.hh:50
openmsx::MSXBootSector::dirEntries
Endian::UA_L16 dirEntries
Definition: DiskImageUtils.hh:20
openmsx::MSXBootSector::sectorsTrack
Endian::L16 sectorsTrack
Definition: DiskImageUtils.hh:24
openmsx::Partition::end_sector
byte end_sector
Definition: DiskImageUtils.hh:67
openmsx::MSXBootSector::descriptor
byte descriptor
Definition: DiskImageUtils.hh:22
openmsx::MSXDirEntry::name
struct openmsx::MSXDirEntry::@1::@3 name
openmsx::PartitionTable
Definition: DiskImageUtils.hh:75
openmsx::MSXDirEntry::base
char base[8]
Definition: DiskImageUtils.hh:44
openmsx::MSXDirEntry::filename
char filename[8+3]
Definition: DiskImageUtils.hh:47
openmsx::MSXDirEntry::size
Endian::L32 size
Definition: DiskImageUtils.hh:54
openmsx::MSXDirEntry::ATT_ARCHIVE
static constexpr byte ATT_ARCHIVE
Definition: DiskImageUtils.hh:40
openmsx::MSXBootSector::nrSides
Endian::L16 nrSides
Definition: DiskImageUtils.hh:25
openmsx::SectorBuffer::dirEntry
MSXDirEntry dirEntry[16]
Definition: DiskImageUtils.hh:93
openmsx::MSXBootSector::jumpCode
byte jumpCode[3]
Definition: DiskImageUtils.hh:14
openmsx::MSXBootSector::resvSectors
Endian::L16 resvSectors
Definition: DiskImageUtils.hh:18
openmsx::Partition::end_cyl
byte end_cyl
Definition: DiskImageUtils.hh:68
openmsx::MSXDirEntry::startCluster
Endian::L16 startCluster
Definition: DiskImageUtils.hh:53
openmsx::SectorBuffer::raw
byte raw[512]
Definition: DiskImageUtils.hh:91
openmsx::SectorBuffer::bootSector
MSXBootSector bootSector
Definition: DiskImageUtils.hh:92
openmsx::MSXDirEntry::attrib
byte attrib
Definition: DiskImageUtils.hh:49
openmsx::MSXBootSector::nrFats
byte nrFats
Definition: DiskImageUtils.hh:19
openmsx::Partition::cyl
byte cyl
Definition: DiskImageUtils.hh:64
openmsx::MSXBootSector::bpSector
Endian::UA_L16 bpSector
Definition: DiskImageUtils.hh:16
openmsx::MSXDirEntry::ATT_SYSTEM
static constexpr byte ATT_SYSTEM
Definition: DiskImageUtils.hh:37
openmsx::Partition::size
Endian::UA_L32 size
Definition: DiskImageUtils.hh:70
Endian::UA_L32
Definition: endian.hh:255
openmsx::MSXBootSector::pad1
byte pad1[9]
Definition: DiskImageUtils.hh:27
AlignedBuffer.hh
openmsx::DiskImageUtils::hasPartitionTable
bool hasPartitionTable(SectorAccessibleDisk &disk)
Check whether the given disk is partitioned.
Definition: DiskImageUtils.cc:21
openmsx::MSXBootSector::spCluster
byte spCluster
Definition: DiskImageUtils.hh:17
openmsx::MSXDirEntry::ATT_DIRECTORY
static constexpr byte ATT_DIRECTORY
Definition: DiskImageUtils.hh:39
openmsx::MSXDirEntry::date
Endian::L16 date
Definition: DiskImageUtils.hh:52
openmsx::AlignedBuffer
Definition: AlignedBuffer.hh:28
openmsx::MSXBootSector::nrSectors
Endian::UA_L16 nrSectors
Definition: DiskImageUtils.hh:21
endian.hh
openmsx::MSXDirEntry::ATT_VOLUME
static constexpr byte ATT_VOLUME
Definition: DiskImageUtils.hh:38
Endian::EndianT
Definition: endian.hh:71
openmsx::Partition::sys_ind
byte sys_ind
Definition: DiskImageUtils.hh:65
openmsx::Partition
Definition: DiskImageUtils.hh:60
openmsx::Partition::end_head
byte end_head
Definition: DiskImageUtils.hh:66
openmsx::MSXDirEntry::ATT_READONLY
static constexpr byte ATT_READONLY
Definition: DiskImageUtils.hh:35
openmsx::Partition::head
byte head
Definition: DiskImageUtils.hh:62
openmsx::MSXBootSector::sectorsFat
Endian::L16 sectorsFat
Definition: DiskImageUtils.hh:23
openmsx::SectorBuffer::pt
PartitionTable pt
Definition: DiskImageUtils.hh:94
openmsx::Partition::boot_ind
byte boot_ind
Definition: DiskImageUtils.hh:61
openmsx::PartitionTable::header
char header[11]
Definition: DiskImageUtils.hh:76
openmsx::DiskImageUtils::checkValidPartition
void checkValidPartition(SectorAccessibleDisk &disk, unsigned partition)
Checks whether the disk is partitioned the specified partition exists throws a CommandException if on...
Definition: DiskImageUtils.cc:51
openmsx::PartitionTable::end
Endian::L16 end
Definition: DiskImageUtils.hh:79
openmsx::DiskImageUtils::partition
void partition(SectorAccessibleDisk &disk, const std::vector< unsigned > &sizes)
Write a partition table to the given disk and format each partition.
Definition: DiskImageUtils.cc:234
Endian::UA_L16
Definition: endian.hh:239
openmsx::MSXDirEntry::ATT_REGULAR
static constexpr byte ATT_REGULAR
Definition: DiskImageUtils.hh:34
openmsx::Partition::sector
byte sector
Definition: DiskImageUtils.hh:63
openmsx::MSXBootSector::pad2
byte pad2[512-43]
Definition: DiskImageUtils.hh:29
openmsx::MSXBootSector
Definition: DiskImageUtils.hh:13
openmsx::MSXDirEntry
Definition: DiskImageUtils.hh:33
openmsx::PartitionTable::pad
char pad[3]
Definition: DiskImageUtils.hh:77
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::MSXBootSector::hiddenSectors
Endian::L16 hiddenSectors
Definition: DiskImageUtils.hh:26
openmsx::MSXDirEntry::time
Endian::L16 time
Definition: DiskImageUtils.hh:51
openmsx::MSXBootSector::vol_id
Endian::UA_L32 vol_id
Definition: DiskImageUtils.hh:28
openmsx::MSXDirEntry::ext
char ext[3]
Definition: DiskImageUtils.hh:45