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