KallistiOS  ##version##
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Data Structures | Macros | Functions
vmufs.h File Reference

Low-level VMU filesystem driver. More...

#include <sys/cdefs.h>
#include <dc/maple.h>

Go to the source code of this file.

Data Structures

struct  vmu_timestamp_t
 BCD timestamp, used several places in the vmufs. More...
 
struct  vmu_root_t
 VMU FS Root block layout. More...
 
struct  vmu_dir_t
 VMU FS Directory entries, 32 bytes each. More...
 

Macros

#define VMUFS_OVERWRITE   1
 Overwrite existing files. More...
 
#define VMUFS_VMUGAME   2
 This file is a VMU game. More...
 
#define VMUFS_NOCOPY   4
 Set the no-copy flag. More...
 

Functions

void vmufs_dir_fill_time (vmu_dir_t *d)
 Fill in the date on a vmu_dir_t for writing. More...
 
int vmufs_root_read (maple_device_t *dev, vmu_root_t *root_buf)
 Reads a selected VMU's root block. More...
 
int vmufs_root_write (maple_device_t *dev, vmu_root_t *root_buf)
 Writes a selected VMU's root block. More...
 
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. More...
 
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. More...
 
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. More...
 
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. Assumes the mutex is held. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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 VMU. More...
 
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, write the indicated file to the VMU. More...
 
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. More...
 
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. More...
 
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. More...
 
int vmufs_mutex_lock ()
 Lock the vmufs mutex. More...
 
int vmufs_mutex_unlock ()
 Unlock the vmufs mutex. More...
 
int vmufs_readdir (maple_device_t *dev, vmu_dir_t **outbuf, int *outcnt)
 Read the directory from a VMU. More...
 
int vmufs_read (maple_device_t *dev, const char *fn, void **outbuf, int *outsize)
 Read a file from the VMU. More...
 
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. More...
 
int vmufs_write (maple_device_t *dev, const char *fn, void *inbuf, int insize, int flags)
 Write a file to the VMU. More...
 
int vmufs_delete (maple_device_t *dev, const char *fn)
 Delete a file from the VMU. More...
 
int vmufs_free_blocks (maple_device_t *dev)
 Return the number of user blocks free for file writing. More...
 
int vmufs_init ()
 Initialize vmufs. More...
 
int vmufs_shutdown ()
 Shutdown vmufs. More...
 

Detailed Description

Low-level VMU filesystem driver.

The VMU filesystem driver mounts itself on /vmu of the VFS. Each memory card has its own subdirectory off of that directory (i.e, /vmu/a1 for slot 1 of the first controller). VMUs themselves have no subdirectories, so the driver itself is fairly simple.

Files on a VMU must be multiples of 512 bytes in size, and should have a header attached so that they show up in the BIOS menu.

Author
Dan Potter
See also
dc/vmu_pkg.h
dc/fs_vmu.h

Macro Definition Documentation

#define VMUFS_NOCOPY   4

Set the no-copy flag.

#define VMUFS_OVERWRITE   1

Overwrite existing files.

#define VMUFS_VMUGAME   2

This file is a VMU game.

Function Documentation

int vmufs_delete ( maple_device_t dev,
const char *  fn 
)

Delete a file from the VMU.

Return values
0On success.
-1If the file is not found.
-2On other failure.
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.

Another file with the same name should not exist (delete it first if it does). This function will not check for dupes!

Parameters
rootThe VMU root block.
dirThe VMU directory.
newdirentThe new entry to add.
Returns
0 on success, or <0 on failure.
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.

Parameters
root_bufThe root block to check.
Returns
The amount of space, in bytes, needed.
void vmufs_dir_fill_time ( vmu_dir_t d)

Fill in the date on a vmu_dir_t for writing.

Parameters
dThe directory to fill in the date on.
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.

Parameters
rootThe VMU root block.
dirThe VMU directory.
fnThe file to find (only checked up to 12 chars).
Returns
The index into the directory array on success, or <0 on failure.
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.

Parameters
rootThe VMU root block.
dirThe directory in question.
Returns
The number of entries available.
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.

This function reads the directory of a given VMU root block. It assumes the mutex is held. There must be at least the number of bytes returned by vmufs_dir_blocks() available in the buffer for this to succeed.

Parameters
devThe VMU to read.
root_bufThe VMU's root block.
dir_bufThe buffer to hold the directory. You must have allocated this yourself.
Returns
0 on success, <0 on failure.
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. Assumes the mutex is held.

Parameters
devThe VMU to write to.
rootThe VMU's root block.
dir_bufThe VMU's directory structure.
Returns
0 on success, <0 on failure.
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.

Parameters
root_bufThe root block to check.
Returns
The amount of space, in bytes, needed.
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.

Parameters
rootThe VMU root block.
fatThe FAT to be examined.
Returns
The number of blocks available.
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.

This function reads the FAT of a VMU, given its root block. It assumes the mutex is held. There must be at least the number of bytes returned by vmufs_fat_blocks() available in the buffer for this to succeed.

Parameters
devThe VMU to read from.
rootThe VMU's root block.
fat_bufThe buffer to store the FAT into. You must pre-allocate this.
Returns
0 on success, <0 on failure.
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.

This function assumes the mutex is held.

Parameters
devThe VMU to write to.
rootThe VMU's root block.
fat_bufThe buffer to write to the FAT.
Returns
0 on success, <0 on failure.
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.

No changes are made to the VMU itself, just the in-memory structs.

Parameters
rootThe VMU root block.
fatThe FAT to be modified.
dirThe directory to be modified.
fnThe file name to be deleted.
Return values
0On success.
-1If fn is not found.
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 VMU.

An appropriate amount of space must have been allocated previously in the buffer. Assumes the mutex is held.

Parameters
devThe VMU to read from.
fatThe FAT of the VMU.
direntThe entry to read.
outbufA buffer to write the data into. You must allocate this yourself with the appropriate amount of space.
Returns
0 on success, <0 on failure.
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, write the indicated file to the VMU.

The named file should not exist in the directory already. The directory and FAT will not be sync'd back to the VMU, this must be done manually. Assumes the mutex is held.

Parameters
devThe VMU to write to.
rootThe VMU root block.
fatThe FAT of the VMU.
dirThe directory of the VMU.
newdirentThe new entry to write.
filebufThe new file data.
sizeThe size of the file in blocks (512-bytes each).
Returns
0 on success, <0 on failure.
int vmufs_free_blocks ( maple_device_t dev)

Return the number of user blocks free for file writing.

You should check this number before attempting to write.

Returns
The number of blocks free for writing.
int vmufs_init ( )

Initialize vmufs.

Must be called before anything else is useful.

Return values
0On success (no error conditions defined).
int vmufs_mutex_lock ( )

Lock the vmufs mutex.

This should be done before you attempt any low-level ops.

Return values
0On success (no error conditions defined).
int vmufs_mutex_unlock ( )

Unlock the vmufs mutex.

This should be done once you're done with any low-level ops.

Return values
0On success (no error conditions defined).
int vmufs_read ( maple_device_t dev,
const char *  fn,
void **  outbuf,
int *  outsize 
)

Read a file from the VMU.

The output buffer will be allocated for you using malloc(), and the size of the file will be returned. On failure, outbuf will not contain a dangling buffer that needs to be freed (no further action required).

Parameters
devThe VMU to read from.
fnThe name of the file to read.
outbufA buffer that will be allocated where the file data will be placed.
outsizeStorage for the size of the file, in bytes.
Returns
0 on success, or <0 on failure.
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.

This function is faster to use than vmufs_read() if you already have done the lookup, since it won't need to do that.

Parameters
devThe VMU to read from.
direntThe entry to read.
outbufA buffer that will be allocated where the file data will be placed.
outsizeStorage for the size of the file, in bytes.
Returns
0 on success, <0 on failure.
int vmufs_readdir ( maple_device_t dev,
vmu_dir_t **  outbuf,
int *  outcnt 
)

Read the directory from a VMU.

The output buffer will be allocated for you using malloc(), and the number of entries will be returned. On failure, outbuf will not contain a dangling buffer that needs to be freed (no further action required).

Parameters
devThe VMU to read from.
outbufA buffer that will be allocated where the directory data will be placed.
outcntThe number of entries in outbuf.
Returns
0 on success, or <0 on failure.
int vmufs_root_read ( maple_device_t dev,
vmu_root_t root_buf 
)

Reads a selected VMU's root block.

This function assumes the mutex is held.

Parameters
devThe VMU to read from.
root_bufA buffer to hold the root block. You must allocate this yourself before calling.
Return values
-1On failure.
0On success.
int vmufs_root_write ( maple_device_t dev,
vmu_root_t root_buf 
)

Writes a selected VMU's root block.

This function assumes the mutex is held.

Parameters
devThe VMU to write to.
root_bufThe root block to write.
Return values
-1On failure.
0On success.
int vmufs_shutdown ( )

Shutdown vmufs.

Must be called after everything is finished.

int vmufs_write ( maple_device_t dev,
const char *  fn,
void *  inbuf,
int  insize,
int  flags 
)

Write a file to the VMU.

If the named file already exists, then the function checks 'flags'. If VMUFS_OVERWRITE is set, then the old file is deleted first before the new one is written (this all happens atomically). On partial failure, some data blocks may have been written, but in general the card should not be damaged.

Parameters
devThe VMU to write to.
fnThe filename to write.
inbufThe data to write to the file.
insizeThe size of the file in bytes.
flagsFlags for the write (i.e, VMUFS_OVERWRITE, VMUFS_VMUGAME, VMUFS_NOCOPY).
Returns
0 on success, or <0 for failure.