forked from CorshamTech/SD-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisk.cpp
281 lines (218 loc) · 7.53 KB
/
Disk.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//=============================================================================
// FILE: Disk.cpp
//
// This class represents one virtual disk, using a DSK format file. This class
// provides methods to read and write raw sectors, mount and umount DSK format
// files and other low-level functions.
//
// Note that there is no caching here. Writes immediately get written to the
// disk, and reads are always read directly from the disk. This might be
// optimized a bit by delaying writes and doing them during idle polls, but
// I'll leave that to someone else.
//
// Bob Applegate, K2UT - bob@corshamtech.com
#include <SD.h>
#include "Disk.h"
#include "Errors.h"
extern void hexdump(unsigned char *, unsigned int);
// Define to dump sectors
#undef DUMP_SECTORS
//=============================================================================
// This creates an instance of the Disk but does not do any initialization.
Disk::Disk(void)
{
mountedFlag = false;
isOpenF = false;;
}
//=============================================================================
// Destructor.
Disk::~Disk(void)
{
unmount(); // make sure it's unmounted
}
//=============================================================================
// Unmounts the disk currently mounted. Does not produce an error if there is
// nothing mounted, but it does set the next error to be NOT MOUNTED for
// future status requests.
void Disk::unmount(void)
{
if (mountedFlag) // no sense unmounting if not mounted
{
file.close();
isOpenF = false;;
setError(ERR_NOT_MOUNTED);
mountedFlag = false;
}
}
//=============================================================================
// Given a pathname to a file, attempt to open it. Returns true if mounted,
// false if not and the error flag is set with the reason.
bool Disk::mount(char *afilename, bool readOnly)
{
goodFlag = false; // assume it is not good
//byte buffer[SECTOR_SIZE];
Serial.print("Disk::Disk ");
Serial.println(afilename);
// Make sure the file exists!
if (SD.exists(afilename))
{
// Set the right open flag depending on whether it's read-only
// or not.
if (readOnly)
{
openFlag = FILE_READ;
Serial.println("opening read only");
}
else
{
openFlag = O_RDWR;
}
// Open the file!
file = SD.open(afilename, openFlag);
readOnlyFlag = readOnly;
if (!file)
{
Serial.println("Error opening file!");
// should set an error code
}
else
{
goodFlag = true;
mountedFlag = true;
isOpenF = true;
strcpy(filename, afilename); // save name for later
}
}
else
{
setError(ERR_FILE_NOT_FOUND);
}
}
//=============================================================================
// Close a file
void Disk::close(void)
{
file.close();
isOpenF = false;
}
//=============================================================================
// Reads a sector of data. On entry this is given the offset into the DSK
// file and a pointer to where to place the data. This always reads exactly
// SECTOR_SIZE bytes.
//
// Returns true on success, false on error
bool Disk::read(unsigned long offset, byte *buf)
{
bool ret = true; // assume a good return code
errorCode = ERR_NONE;
// Serial.print("Disk::read at offset ");
// Serial.println(offset, HEX);
// Serial.flush();
#ifdef DUMP_SECTORS
Serial.println("Sector dump:");
byte *orig = buf;
#endif
file.seek(offset);
if ((file.available() < SECTOR_SIZE) || (offset + SECTOR_SIZE > file.size()))
{
Serial.print("Not enough bytes: ");
Serial.println(file.available());
ret = false;
errorCode = ERR_READ_ERROR;
}
for (int i = 0; i < SECTOR_SIZE; i++)
{
*buf = file.read();
buf++;
}
#ifdef DUMP_SECTORS
hexdump(orig, SECTOR_SIZE);
#endif
return ret;
}
//=============================================================================
// This writes a single sector to the specified offset. On entry, this is
// given the long offset (must be a multiple of the sector size) and a pointer
// to exactly one sector's worth of data. Does the write, then rReturns true
// on success or false on error.
bool Disk::write(unsigned long offset, byte *buf)
{
bool ret = false; // assume failure
errorCode = ERR_NONE;
// Serial.print("Disk::write(");
// Serial.print(offset >> 16, HEX);
// Serial.println(offset & 0xffff, HEX);
if (readOnlyFlag)
{
errorCode = ERR_READ_ONLY;
}
else
{
if (file.seek(offset) == false)
{
Serial.print("Failed seeing to offset ");
Serial.println(offset);
}
if (file.available() < SECTOR_SIZE)
{
Serial.print("Not enough bytes: ");
Serial.println(file.available());
errorCode = ERR_WRITE_ERROR;
}
else
{
// Write the data and then flush it to be sure the
// data gets written.
int wrote = file.write(buf, SECTOR_SIZE);
file.flush();
if (wrote != SECTOR_SIZE)
{
Serial.print("Didn't write enough bytes: ");
Serial.println(wrote);
errorCode = ERR_WRITE_ERROR;
}
else
{
ret = true; // success!
#if 0
Serial.print("Just wrote ");
Serial.print(wrote);
Serial.print(" bytes at offset ");
Serial.println(offset);
#endif
#ifdef DUMP_SECTORS
hexdump(buf, SECTOR_SIZE);
#endif
}
}
}
return ret;
}
//=============================================================================
// This returns (for now) a single byte indicating the disk status via a
// bitmap:
//
// Bit
// 0: 0 = disk not present, 1 = mounted
// 1: 0 = read only, 1 = R/W
// 2: 0 = sector readable, 1 = sector unreadable
// 3:
// 4:
// 5:
// 6:
// 7:
//
// Generally speaking, a value of 0 means no problems.
byte Disk::getStatus(void)
{
byte ret = 0;
if (mountedFlag)
{
ret |= 0x01;
if (readOnlyFlag)
{
ret |= 0x02;
}
}
return ret;
}