libdragon
Files | Data Structures | Macros | Functions
Mempak Filesystem Routines

Managed mempak interface. More...

Files

file  mempak.c
 Mempak Filesystem Routine.
 
file  mempak.h
 Mempak Filesystem Routines.
 

Data Structures

struct  entry_structure_t
 Structure representing a save entry in a mempak. More...
 

Macros

#define MEMPAK_BLOCK_SIZE   256
 Size in bytes of a Mempak block.
 

Functions

int read_mempak_sector (int controller, int sector, uint8_t *sector_data)
 Read a sector from a mempak. More...
 
int write_mempak_sector (int controller, int sector, uint8_t *sector_data)
 Write a sector to a mempak. More...
 
int validate_mempak (int controller)
 Return whether a mempak is valid. More...
 
int get_mempak_entry (int controller, int entry, entry_structure_t *entry_data)
 Read an entry on a mempak. More...
 
int get_mempak_free_space (int controller)
 Return the number of free blocks on a mempak. More...
 
int format_mempak (int controller)
 Format a mempak. More...
 
int read_mempak_entry_data (int controller, entry_structure_t *entry, uint8_t *data)
 Read the data associated with an entry on a mempak. More...
 
int write_mempak_entry_data (int controller, entry_structure_t *entry, uint8_t *data)
 Write associated data to a mempak entry. More...
 
int delete_mempak_entry (int controller, entry_structure_t *entry)
 Delete a mempak entry and associated data. More...
 

Inode values

#define BLOCK_EMPTY   0x03
 This block is empty.
 
#define BLOCK_LAST   0x01
 This is the last block in the note.
 
#define BLOCK_VALID_FIRST   0x05
 First valid block that can contain user data.
 
#define BLOCK_VALID_LAST   0x7F
 Last valid block that can contain user data.
 

Detailed Description

Managed mempak interface.

The mempak system is a subsystem of the Controller Subsystem. Before attempting to read from or write to a mempak, be sure you have initialized the controller system with controller_init and verified that you have a mempak in the correct controller using identify_accessory.

To read and write to the mempak in an organized way compatible with official software, first check that the mempak is valid using validate_mempak. If the mempack is invalid, it will need to be formatted using format_mempak. Once the mempak is considered valid, existing notes can be enumerated using get_mempak_entry. To read the data associated with a note, use read_mempak_entry_data. To write a new note to the mempak, use write_mempak_entry_data. Note that there is no append functionality so if a note is being updated, ensure you have deleted the old note first using delete_mempak_entry. Code should be careful to check how many blocks are free before writing using get_mempak_free_space.


Data Structure Documentation

◆ entry_structure_t

struct entry_structure_t

Structure representing a save entry in a mempak.

Data Fields
uint32_t vendor Vendor ID.
uint16_t game_id Game ID.
uint16_t inode Inode pointer.
uint8_t region Intended region.
uint8_t blocks Number of blocks used by this entry.
See also
MEMPAK_BLOCK_SIZE
uint8_t valid Validity of this entry.
uint8_t entry_id ID of this entry.
char name[19] Name of this entry.

The complete list of valid ASCII characters in a note name is:

ABCDEFGHIJKLMNOPQRSTUVWXYZ!"#`*+,-./:=?@

The space character is also allowed. Any other character will be converted to a space before writing to the mempak.

Function Documentation

◆ read_mempak_sector()

int read_mempak_sector ( int  controller,
int  sector,
uint8_t *  sector_data 
)

Read a sector from a mempak.

This will read a sector from a mempak. Sectors on mempaks are always 256 bytes in size.

Parameters
[in]controllerThe controller (0-3) to read a sector from
[in]sectorThe sector (0-127) to read from
[out]sector_dataBuffer to place 256 read bytes of data
Return values
0if reading was successful
-1if the sector was out of bounds or sector_data was null
-2if there was an error reading part of a sector

◆ write_mempak_sector()

int write_mempak_sector ( int  controller,
int  sector,
uint8_t *  sector_data 
)

Write a sector to a mempak.

This will write a sector to a mempak. Sectors on mempaks are always 256 bytes in size.

Parameters
[in]controllerThe controller (0-3) to write a sector to
[in]sectorThe sector (0-127) to write to
[out]sector_dataBuffer containing 256 bytes of data to write to sector
Return values
0if writing was successful
-1if the sector was out of bounds or sector_data was null
-2if there was an error writing part of a sector

◆ validate_mempak()

int validate_mempak ( int  controller)

Return whether a mempak is valid.

This function will return whether the mempak in a particular controller is formatted and valid.

Parameters
[in]controllerThe controller (0-3) to validate
Return values
0if the mempak is valid and ready to be used
-2if the mempak is not present or couldn't be read
-3if the mempak is bad or unformatted

◆ get_mempak_entry()

int get_mempak_entry ( int  controller,
int  entry,
entry_structure_t entry_data 
)

Read an entry on a mempak.

Given an entry index (0-15), return the entry as found on the mempak. If the entry is blank or invalid, the valid flag is cleared.

Parameters
[in]controllerThe controller (0-3) from which the entry should be read
[in]entryThe entry index (0-15) to read
[out]entry_dataStructure containing information on the entry
Return values
0if the entry was read successfully
-1if the entry is out of bounds or entry_data is null
-2if the mempak is bad or not present

◆ get_mempak_free_space()

int get_mempak_free_space ( int  controller)

Return the number of free blocks on a mempak.

Note that a block is identical in size to a sector. To calculate the number of bytes free, multiply the return of this function by MEMPAK_BLOCK_SIZE.

Parameters
[in]controllerThe controller (0-3) to read the free space from
Returns
The number of blocks free on the memory card or a negative number on failure

◆ format_mempak()

int format_mempak ( int  controller)

Format a mempak.

Formats a mempak. Should only be done to wipe a mempak or to initialize the filesystem in case of a blank or corrupt mempak.

Parameters
[in]controllerThe controller (0-3) to format the mempak on
Return values
0if the mempak was formatted successfully
-2if the mempak was not present or couldn't be formatted

◆ read_mempak_entry_data()

int read_mempak_entry_data ( int  controller,
entry_structure_t entry,
uint8_t *  data 
)

Read the data associated with an entry on a mempak.

Given a valid mempak entry fetched by get_mempak_entry, retrieves the contents of the entry. The calling function must ensure that enough room is available in the passed in buffer for the entire entry. The entry structure itself contains the number of blocks used to store the data which can be multiplied by MEMPAK_BLOCK_SIZE to calculate the size of the buffer needed.

Parameters
[in]controllerThe controller (0-3) to read the entry data from
[in]entryThe entry structure associated with the data to be read. An entry structure can be fetched based on index using get_mempak_entry
[out]dataThe data associated with an entry
Return values
0if the entry was successfully read
-1if input parameters were out of bounds or the entry was corrupted somehow
-2if the mempak was not present or bad
-3if the data couldn't be read

◆ write_mempak_entry_data()

int write_mempak_entry_data ( int  controller,
entry_structure_t entry,
uint8_t *  data 
)

Write associated data to a mempak entry.

Given a mempak entry structure with a valid region, name and block count, writes the entry and associated data to the mempak. This function will not overwrite any existing user data. To update an existing entry, use delete_mempak_entry followed by write_mempak_entry_data with the same entry structure.

Parameters
[in]controllerThe controller (0-3) to write the entry and data to
[in]entryThe entry structure containing a region, name and block count
[in]dataThe associated data to write to to the created entry
Return values
0if the entry was created and written successfully
-1if the parameters were invalid or the note has no length
-2if the mempak wasn't present or was bad
-3if there was an error writing to the mempak
-4if there wasn't enough space to store the note
-5if there is no room in the TOC to add a new entry

◆ delete_mempak_entry()

int delete_mempak_entry ( int  controller,
entry_structure_t entry 
)

Delete a mempak entry and associated data.

Given a valid mempak entry fetched by get_mempak_entry, removes the entry and frees all associated blocks.

Parameters
[in]controllerThe controller (0-3) to delete the note from
[in]entryThe entry structure that is to be deleted from the mempak
Return values
0if the entry was deleted successfully
-1if the entry was invalid
-2if the mempak was bad or not present