KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
vmufs.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/vmufs.h
4 Copyright (C) 2003 Megan Potter
5
6*/
7
8/** \file dc/vmufs.h
9 \brief Low-level VMU filesystem driver.
10 \ingroup vfs_vmu
11
12 The VMU filesystem driver mounts itself on /vmu of the VFS. Each memory card
13 has its own subdirectory off of that directory (i.e, /vmu/a1 for slot 1 of
14 the first controller). VMUs themselves have no subdirectories, so the driver
15 itself is fairly simple.
16
17 Files on a VMU must be multiples of 512 bytes in size, and should have a
18 header attached so that they show up in the BIOS menu.
19
20 \author Megan Potter
21 \see dc/vmu_pkg.h
22 \see dc/fs_vmu.h
23*/
24
25#ifndef __DC_VMUFS_H
26#define __DC_VMUFS_H
27
28#include <sys/cdefs.h>
29__BEGIN_DECLS
30
31#include <dc/maple.h>
32
33/** \addtogroup vfs_vmu
34 @{
35*/
36
37/** \brief BCD timestamp, used several places in the vmufs.
38 \headerfile dc/vmufs.h
39*/
40typedef struct {
41 uint8 cent; /**< \brief Century */
42 uint8 year; /**< \brief Year, within century */
43 uint8 month; /**< \brief Month of the year */
44 uint8 day; /**< \brief Day of the month */
45 uint8 hour; /**< \brief Hour of the day */
46 uint8 min; /**< \brief Minutes */
47 uint8 sec; /**< \brief Seconds */
48 uint8 dow; /**< \brief Day of week (0 = monday, etc) */
50
51/** \brief VMU FS Root block layout.
52 \headerfile dc/vmufs.h
53*/
54typedef struct {
55 uint8 magic[16]; /**< \brief All should contain 0x55 */
56 uint8 use_custom; /**< \brief 0 = standard, 1 = custom */
57 uint8 custom_color[4];/**< \brief blue, green, red, alpha */
58 uint8 pad1[27]; /**< \brief All zeros */
59 vmu_timestamp_t timestamp; /**< \brief BCD timestamp */
60 uint8 pad2[8]; /**< \brief All zeros */
61 uint8 unk1[6]; /**< \brief ??? */
62 uint16 fat_loc; /**< \brief FAT location */
63 uint16 fat_size; /**< \brief FAT size in blocks */
64 uint16 dir_loc; /**< \brief Directory location */
65 uint16 dir_size; /**< \brief Directory size in blocks */
66 uint16 icon_shape; /**< \brief Icon shape for this VMS */
67 uint16 blk_cnt; /**< \brief Number of user blocks */
68 uint8 unk2[430]; /**< \brief ??? */
70
71/** \brief VMU FS Directory entries, 32 bytes each.
72 \headerfile dc/vmufs.h
73*/
74typedef struct {
75 uint8 filetype; /**< \brief 0x00 = no file; 0x33 = data; 0xcc = a game */
76 uint8 copyprotect; /**< \brief 0x00 = copyable; 0xff = copy protected */
77 uint16 firstblk; /**< \brief Location of the first block in the file */
78 char filename[12]; /**< \brief Note: there is no null terminator */
79 vmu_timestamp_t timestamp; /**< \brief File time */
80 uint16 filesize; /**< \brief Size of the file in blocks */
81 uint16 hdroff; /**< \brief Offset of header, in blocks from start of file */
82 uint8 dirty; /**< \brief See header notes */
83 uint8 pad1[3]; /**< \brief All zeros */
84} vmu_dir_t;
85
86/* Notes about the "dirty" field on vmu_dir_t :)
87
88 This byte should always be zero when written out to the VMU. What this
89 lets us do, though, is conserve on flash writes. If you only want to
90 modify one single file (which is the standard case) then re-writing all
91 of the dir blocks is a big waste. Instead, you should set the dirty flag
92 on the in-mem copy of the directory, and writing it back out will only
93 flush the containing block back to the VMU, setting it back to zero
94 in the process. Loaded blocks should always have zero here (though we
95 enforce that in the code to make sure) so it will be non-dirty by
96 default.
97 */
98
99
100/* ****************** Low level functions ******************** */
101
102/** \brief Fill in the date on a vmu_dir_t for writing.
103
104 \param d The directory to fill in the date on.
105*/
107
108/** \brief Reads a selected VMU's root block.
109
110 This function assumes the mutex is held.
111
112 \param dev The VMU to read from.
113 \param root_buf A buffer to hold the root block. You must allocate
114 this yourself before calling.
115 \retval -1 On failure.
116 \retval 0 On success.
117*/
119
120/** \brief Writes a selected VMU's root block.
121
122 This function assumes the mutex is held.
123
124 \param dev The VMU to write to.
125 \param root_buf The root block to write.
126 \retval -1 On failure.
127 \retval 0 On success.
128*/
130
131/** \brief Given a VMU's root block, return the amount of space in bytes
132 required to hold its directory.
133
134 \param root_buf The root block to check.
135 \return The amount of space, in bytes, needed.
136*/
138
139/** \brief Given a VMU's root block, return the amount of space in bytes
140 required to hold its FAT.
141
142 \param root_buf The root block to check.
143 \return The amount of space, in bytes, needed.
144*/
146
147/** \brief Given a selected VMU's root block, read its directory.
148
149 This function reads the directory of a given VMU root block. It assumes the
150 mutex is held. There must be at least the number of bytes returned by
151 vmufs_dir_blocks() available in the buffer for this to succeed.
152
153 \param dev The VMU to read.
154 \param root_buf The VMU's root block.
155 \param dir_buf The buffer to hold the directory. You must have
156 allocated this yourself.
157 \return 0 on success, <0 on failure.
158*/
160 vmu_dir_t * dir_buf);
161
162/** \brief Given a selected VMU's root block and dir blocks, write the dirty
163 dir blocks back to the VMU. Assumes the mutex is held.
164
165 \param dev The VMU to write to.
166 \param root The VMU's root block.
167 \param dir_buf The VMU's directory structure.
168 \return 0 on success, <0 on failure.
169*/
171 vmu_dir_t * dir_buf);
172
173/** \brief Given a selected VMU's root block, read its FAT.
174
175 This function reads the FAT of a VMU, given its root block. It assumes the
176 mutex is held. There must be at least the number of bytes returned by
177 vmufs_fat_blocks() available in the buffer for this to succeed.
178
179 \param dev The VMU to read from.
180 \param root The VMU's root block.
181 \param fat_buf The buffer to store the FAT into. You must
182 pre-allocate this.
183 \return 0 on success, <0 on failure.
184*/
185int vmufs_fat_read(maple_device_t * dev, vmu_root_t * root, uint16 * fat_buf);
186
187/** \brief Given a selected VMU's root block and its FAT, write the FAT blocks
188 back to the VMU.
189
190 This function assumes the mutex is held.
191
192 \param dev The VMU to write to.
193 \param root The VMU's root block.
194 \param fat_buf The buffer to write to the FAT.
195 \return 0 on success, <0 on failure.
196*/
197int vmufs_fat_write(maple_device_t * dev, vmu_root_t * root, uint16 * fat_buf);
198
199/** \brief Given a previously-read directory, locate a file by filename.
200
201 \param root The VMU root block.
202 \param dir The VMU directory.
203 \param fn The file to find (only checked up to 12 chars).
204 \return The index into the directory array on success, or
205 <0 on failure.
206*/
207int vmufs_dir_find(vmu_root_t * root, vmu_dir_t * dir, const char * fn);
208
209/** \brief Given a previously-read directory, add a new dirent to the dir.
210
211 Another file with the same name should not exist (delete it first if it
212 does). This function will not check for dupes!
213
214 \param root The VMU root block.
215 \param dir The VMU directory.
216 \param newdirent The new entry to add.
217 \return 0 on success, or <0 on failure. */
218int vmufs_dir_add(vmu_root_t * root, vmu_dir_t * dir, vmu_dir_t * newdirent);
219
220/** \brief Given a pointer to a directory struct and a previously loaded FAT,
221 load the indicated file from the VMU.
222
223 An appropriate amount of space must have been allocated previously in the
224 buffer. Assumes the mutex is held.
225
226 \param dev The VMU to read from.
227 \param fat The FAT of the VMU.
228 \param dirent The entry to read.
229 \param outbuf A buffer to write the data into. You must allocate
230 this yourself with the appropriate amount of space.
231 \return 0 on success, <0 on failure.
232*/
233int vmufs_file_read(maple_device_t * dev, uint16 * fat, vmu_dir_t * dirent, void * outbuf);
234
235/** \brief Given a pointer to a mostly-filled directory struct and a previously
236 loaded directory and FAT, write the indicated file to the VMU.
237
238 The named file should not exist in the directory already. The directory and
239 FAT will _not_ be sync'd back to the VMU, this must be done manually.
240 Assumes the mutex is held.
241
242 \param dev The VMU to write to.
243 \param root The VMU root block.
244 \param fat The FAT of the VMU.
245 \param dir The directory of the VMU.
246 \param newdirent The new entry to write.
247 \param filebuf The new file data.
248 \param size The size of the file in blocks (512-bytes each).
249 \return 0 on success, <0 on failure.
250*/
252 vmu_dir_t * dir, vmu_dir_t * newdirent, void * filebuf, int size);
253
254/** \brief Given a previously-read FAT and directory, delete the named file.
255
256 No changes are made to the VMU itself, just the in-memory structs.
257
258 \param root The VMU root block.
259 \param fat The FAT to be modified.
260 \param dir The directory to be modified.
261 \param fn The file name to be deleted.
262 \retval 0 On success.
263 \retval -1 If fn is not found.
264*/
265int vmufs_file_delete(vmu_root_t * root, uint16 * fat, vmu_dir_t * dir, const char *fn);
266
267/** \brief Given a previously-read FAT, return the number of blocks available
268 to write out new file data.
269
270 \param root The VMU root block.
271 \param fat The FAT to be examined.
272 \return The number of blocks available.
273*/
275
276/** \brief Given a previously-read directory, return the number of dirents
277 available for new files.
278
279 \param root The VMU root block.
280 \param dir The directory in question.
281 \return The number of entries available.
282*/
284
285/** \brief Lock the vmufs mutex.
286
287 This should be done before you attempt any low-level ops.
288
289 \retval 0 On success (no error conditions defined).
290*/
292
293/** \brief Unlock the vmufs mutex.
294
295 This should be done once you're done with any low-level ops.
296
297 \retval 0 On success (no error conditions defined).
298*/
300
301
302/* ****************** Higher level functions ******************** */
303
304/** \brief Read the directory from a VMU.
305
306 The output buffer will be allocated for you using malloc(), and the number
307 of entries will be returned. On failure, outbuf will not contain a dangling
308 buffer that needs to be freed (no further action required).
309
310 \param dev The VMU to read from.
311 \param outbuf A buffer that will be allocated where the directory
312 data will be placed.
313 \param outcnt The number of entries in outbuf.
314 \return 0 on success, or <0 on failure. */
315int vmufs_readdir(maple_device_t * dev, vmu_dir_t ** outbuf, int * outcnt);
316
317/** \brief Read a file from the VMU.
318
319 The output buffer will be allocated for you using malloc(), and the size of
320 the file will be returned. On failure, outbuf will not contain a dangling
321 buffer that needs to be freed (no further action required).
322
323 \param dev The VMU to read from.
324 \param fn The name of the file to read.
325 \param outbuf A buffer that will be allocated where the file data
326 will be placed.
327 \param outsize Storage for the size of the file, in bytes.
328 \return 0 on success, or <0 on failure.
329*/
330int vmufs_read(maple_device_t * dev, const char * fn, void ** outbuf, int * outsize);
331
332/** \brief Read a file from the VMU, using a pre-read dirent.
333
334 This function is faster to use than vmufs_read() if you already have done
335 the lookup, since it won't need to do that.
336
337 \param dev The VMU to read from.
338 \param dirent The entry to read.
339 \param outbuf A buffer that will be allocated where the file data
340 will be placed.
341 \param outsize Storage for the size of the file, in bytes.
342 \return 0 on success, <0 on failure.
343*/
344int vmufs_read_dirent(maple_device_t * dev, vmu_dir_t * dirent, void ** outbuf, int * outsize);
345
346/* Flags for vmufs_write */
347#define VMUFS_OVERWRITE 1 /**< \brief Overwrite existing files */
348#define VMUFS_VMUGAME 2 /**< \brief This file is a VMU game */
349#define VMUFS_NOCOPY 4 /**< \brief Set the no-copy flag */
350
351/** \brief Write a file to the VMU.
352
353 If the named file already exists, then the function checks 'flags'. If
354 VMUFS_OVERWRITE is set, then the old file is deleted first before the new
355 one is written (this all happens atomically). On partial failure, some data
356 blocks may have been written, but in general the card should not be damaged.
357
358 \param dev The VMU to write to.
359 \param fn The filename to write.
360 \param inbuf The data to write to the file.
361 \param insize The size of the file in bytes.
362 \param flags Flags for the write (i.e, VMUFS_OVERWRITE,
363 VMUFS_VMUGAME, VMUFS_NOCOPY).
364 \return 0 on success, or <0 for failure.
365*/
366int vmufs_write(maple_device_t * dev, const char * fn, void * inbuf, int insize, int flags);
367
368/** \brief Delete a file from the VMU.
369
370 \retval 0 On success.
371 \retval -1 If the file is not found.
372 \retval -2 On other failure.
373*/
374int vmufs_delete(maple_device_t * dev, const char * fn);
375
376/** \brief Return the number of user blocks free for file writing.
377
378 You should check this number before attempting to write.
379
380 \return The number of blocks free for writing.
381*/
383
384
385/** \brief Initialize vmufs.
386
387 Must be called before anything else is useful.
388
389 \retval 0 On success (no error conditions defined).
390*/
391int vmufs_init(void);
392
393/** \brief Shutdown vmufs.
394
395 Must be called after everything is finished.
396*/
398
399/** @} */
400
401__END_DECLS
402
403#endif /* __DC_VMUFS_H */
unsigned short uint16
16-bit unsigned integer
Definition types.h:34
unsigned char uint8
8-bit unsigned integer
Definition types.h:35
int vmufs_fat_write(maple_device_t *dev, vmu_root_t *root, uint16 *fat_buf)
Given a selected VMU's root block and its FAT, write the FAT blocks back to the VMU.
int vmufs_root_read(maple_device_t *dev, vmu_root_t *root_buf)
Reads a selected VMU's root block.
int vmufs_dir_blocks(vmu_root_t *root_buf)
Given a VMU's root block, return the amount of space in bytes required to hold its directory.
int vmufs_shutdown(void)
Shutdown vmufs.
int vmufs_dir_add(vmu_root_t *root, vmu_dir_t *dir, vmu_dir_t *newdirent)
Given a previously-read directory, add a new dirent to the dir.
int vmufs_dir_write(maple_device_t *dev, vmu_root_t *root, vmu_dir_t *dir_buf)
Given a selected VMU's root block and dir blocks, write the dirty dir blocks back to the VMU.
int vmufs_file_write(maple_device_t *dev, vmu_root_t *root, uint16 *fat, vmu_dir_t *dir, vmu_dir_t *newdirent, void *filebuf, int size)
Given a pointer to a mostly-filled directory struct and a previously loaded directory and FAT,...
int vmufs_fat_read(maple_device_t *dev, vmu_root_t *root, uint16 *fat_buf)
Given a selected VMU's root block, read its FAT.
int vmufs_mutex_lock(void)
Lock the vmufs mutex.
int vmufs_write(maple_device_t *dev, const char *fn, void *inbuf, int insize, int flags)
Write a file to the VMU.
int vmufs_delete(maple_device_t *dev, const char *fn)
Delete a file from the VMU.
int vmufs_readdir(maple_device_t *dev, vmu_dir_t **outbuf, int *outcnt)
Read the directory from a VMU.
int vmufs_free_blocks(maple_device_t *dev)
Return the number of user blocks free for file writing.
int vmufs_root_write(maple_device_t *dev, vmu_root_t *root_buf)
Writes a selected VMU's root block.
int vmufs_read_dirent(maple_device_t *dev, vmu_dir_t *dirent, void **outbuf, int *outsize)
Read a file from the VMU, using a pre-read dirent.
int vmufs_read(maple_device_t *dev, const char *fn, void **outbuf, int *outsize)
Read a file from the VMU.
int vmufs_file_delete(vmu_root_t *root, uint16 *fat, vmu_dir_t *dir, const char *fn)
Given a previously-read FAT and directory, delete the named file.
void vmufs_dir_fill_time(vmu_dir_t *d)
Fill in the date on a vmu_dir_t for writing.
int vmufs_dir_read(maple_device_t *dev, vmu_root_t *root_buf, vmu_dir_t *dir_buf)
Given a selected VMU's root block, read its directory.
int vmufs_init(void)
Initialize vmufs.
int vmufs_dir_free(vmu_root_t *root, vmu_dir_t *dir)
Given a previously-read directory, return the number of dirents available for new files.
int vmufs_file_read(maple_device_t *dev, uint16 *fat, vmu_dir_t *dirent, void *outbuf)
Given a pointer to a directory struct and a previously loaded FAT, load the indicated file from the V...
int vmufs_mutex_unlock(void)
Unlock the vmufs mutex.
int vmufs_fat_free(vmu_root_t *root, uint16 *fat)
Given a previously-read FAT, return the number of blocks available to write out new file data.
int vmufs_fat_blocks(vmu_root_t *root_buf)
Given a VMU's root block, return the amount of space in bytes required to hold its FAT.
int vmufs_dir_find(vmu_root_t *root, vmu_dir_t *dir, const char *fn)
Given a previously-read directory, locate a file by filename.
Maple Bus driver interface.
POSIX directory entry structure.
Definition dirent.h:62
One maple device.
Definition maple.h:271
VMU FS Directory entries, 32 bytes each.
Definition vmufs.h:74
uint8 dirty
See header notes.
Definition vmufs.h:82
uint16 filesize
Size of the file in blocks.
Definition vmufs.h:80
uint16 hdroff
Offset of header, in blocks from start of file.
Definition vmufs.h:81
uint8 copyprotect
0x00 = copyable; 0xff = copy protected
Definition vmufs.h:76
vmu_timestamp_t timestamp
File time.
Definition vmufs.h:79
uint16 firstblk
Location of the first block in the file.
Definition vmufs.h:77
uint8 filetype
0x00 = no file; 0x33 = data; 0xcc = a game
Definition vmufs.h:75
VMU FS Root block layout.
Definition vmufs.h:54
uint16 dir_size
Directory size in blocks.
Definition vmufs.h:65
uint16 fat_loc
FAT location.
Definition vmufs.h:62
uint8 use_custom
0 = standard, 1 = custom
Definition vmufs.h:56
uint16 dir_loc
Directory location.
Definition vmufs.h:64
uint16 fat_size
FAT size in blocks.
Definition vmufs.h:63
vmu_timestamp_t timestamp
BCD timestamp.
Definition vmufs.h:59
uint16 blk_cnt
Number of user blocks.
Definition vmufs.h:67
uint16 icon_shape
Icon shape for this VMS.
Definition vmufs.h:66
BCD timestamp, used several places in the vmufs.
Definition vmufs.h:40
uint8 dow
Day of week (0 = monday, etc)
Definition vmufs.h:48
uint8 sec
Seconds.
Definition vmufs.h:47
uint8 year
Year, within century.
Definition vmufs.h:42
uint8 month
Month of the year.
Definition vmufs.h:43
uint8 hour
Hour of the day.
Definition vmufs.h:45
uint8 cent
Century.
Definition vmufs.h:41
uint8 min
Minutes.
Definition vmufs.h:46
uint8 day
Day of the month.
Definition vmufs.h:44