-
Notifications
You must be signed in to change notification settings - Fork 0
/
dd.cpp
193 lines (145 loc) · 5.81 KB
/
dd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
@file dd.cpp
PUNY-80
TRS-80 disk image tool for Linux and other *nix operating systems
Copyright (c) 2024 Stefan Vogt and Shawn Sijnstra
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
See file LICENSE for details.
Based on TRS-80 Virtual Disk Kit by Miguel Dutra and Mike Gore.
*/
//---------------------------------------------------------------------------------
// Operating System Interface for DBLDOS
//---------------------------------------------------------------------------------
#include "windows.h"
#include <stdio.h>
#include "p80.h"
#include "vdi.h"
#include "osi.h"
#include "nd.h"
#include "dd.h"
//---------------------------------------------------------------------------------
// Validate DOS version and define operating parameters
//---------------------------------------------------------------------------------
DWORD CDD::Load(CVDI* pVDI, DWORD dwFlags)
{
BYTE nFT;
DWORD dwBytes;
DWORD dwError = NO_ERROR;
// Copy VDI pointer and user flags to member variables
m_pVDI = pVDI;
m_dwFlags = dwFlags;
// Get disk geometry
m_pVDI->GetDG(m_DG);
// Calculate the number of disk sides
if (m_dwFlags & V80_FLAG_SS)
m_nSides = 1;
else if (m_dwFlags & V80_FLAG_DS)
m_nSides = 2;
else
m_nSides = m_DG.LT.nLastSide - m_DG.LT.nFirstSide + 1;
// Check whether number of sector per tracks or disk density is different of the other tracks
m_nDensity = ((m_DG.FT.nLastSector - m_DG.FT.nFirstSector + 1) != (m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1) || m_DG.FT.nDensity != m_DG.LT.nDensity ? VDI_DENSITY_MIXED : m_DG.LT.nDensity);
// Calculate de first track number depending on disk density
nFT = m_DG.FT.nTrack + (m_nDensity == VDI_DENSITY_MIXED ? 1 : 0);
// Check internal buffer size (just in case)
if (sizeof(m_Buffer) < m_DG.FT.wSectorSize)
{
dwError = ERROR_INVALID_USER_BUFFER;
goto Done;
}
// Load PDRIVE table from first track, third sector
if ((dwError = m_pVDI->Read(nFT, m_DG.LT.nFirstSide, m_DG.LT.nFirstSector + 2, m_Buffer, m_DG.LT.wSectorSize)) != NO_ERROR)
goto Done;
// Assume initial disk parameters
m_nDDSL = 17;
m_nDDGA = 2;
m_nSPG = 5;
m_nGPL = 2;
m_nSPC = (m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1) * m_nSides;
m_nLumps = ((m_DG.FT.nLastSector - m_DG.FT.nFirstSector + 1) + (m_DG.LT.nLastSector - m_DG.LT.nFirstSector + 1) * (m_DG.LT.nTrack - m_DG.FT.nTrack)) / m_nSPG / m_nGPL;
// Calculate directory relative sector
m_wDirSector = m_nDDSL * m_nGPL * m_nSPG;
// Calculate number of directory sectors
m_nDirSectors = m_nDDGA * m_nSPG;
// If not first Load, release the previously allocated memory
if (m_pDir != NULL)
free(m_pDir);
// Calculate needed memory to hold the entire directory
dwBytes = m_nDirSectors * m_DG.LT.wSectorSize + (V80_MEM - (m_nDirSectors * m_DG.LT.wSectorSize) % V80_MEM);
// Allocate memory for the entire directory
if ((m_pDir = (BYTE*)calloc(dwBytes,1)) == NULL)
{
dwError = ERROR_OUTOFMEMORY;
goto Done;
}
// Load directory into memory
if ((dwError = DirRW(ND_DIR_READ)) != NO_ERROR)
goto Done;
// Check directory structure
if (!(m_dwFlags & V80_FLAG_CHKDIR))
if ((dwError = CheckDir()) == NO_ERROR)
goto Done;
dwError = ERROR_NOT_DOS_DISK;
Done:
return dwError;
}
//---------------------------------------------------------------------------------
// Check the directory structure
//---------------------------------------------------------------------------------
DWORD CDD::CheckDir(void)
{
int x;
void* pFile = NULL;
int nFiles = 0;
DWORD dwError = NO_ERROR;
// If dir check has not been disabled
if (!(m_dwFlags & V80_FLAG_CHKDIR))
{
// Validate disk name
for (x = 0; x < 8; x++)
{
if (((ND_GAT*)m_pDir)->cDiskName[x] < ' ' || ((ND_GAT*)m_pDir)->cDiskName[x] > 'z')
goto Error;
}
// Validate disk date
for (x = 0; x < 8; x++)
{
if (((ND_GAT*)m_pDir)->cDiskDate[x] < ' ' || ((ND_GAT*)m_pDir)->cDiskDate[x] > 'z')
goto Error;
}
}
// Validate each file name in the directory (while counting the number of files)
for (nFiles = 0; (dwError = Dir(&pFile, (pFile == NULL ? OSI_DIR_FIND_FIRST : OSI_DIR_FIND_NEXT))) == NO_ERROR; nFiles++)
{
// First 8 characters can be non-blanks
for (x = 0; x < 8 && ((ND_FPDE*)pFile)->cName[x] >= '0' && ((ND_FPDE*)pFile)->cName[x] <= 'z'; x++);
// But first one must be non-blank to be valid
if (x == 0)
goto Error;
// If a blank is found, then only blanks can be used up to the end of the name
for ( ; x < 8 && ((ND_FPDE*)pFile)->cName[x] == ' '; x++);
// If not, then this name is invalid
if (x < 8)
goto Error;
// The extension can have up to 3 non-blank characters
for (x = 0; x < 3 && ((ND_FPDE*)pFile)->cType[x] >= '0' && ((ND_FPDE*)pFile)->cType[x] <= 'z'; x++);
// If a blank is found, then only blanks can be used up to the end of the extension
for ( ; x < 3 && ((ND_FPDE*)pFile)->cType[x] == ' '; x++);
// If not, then this extension is invalid
if (x < 3)
goto Error;
}
// Ok if Dir() exited on this error // but found files
if (dwError == ERROR_NO_MORE_FILES) // && nFiles > 0)
{
dwError = NO_ERROR;
goto Done;
}
Error:
dwError = ERROR_FILE_CORRUPT;
Done:
return dwError;
}