libdragon
Loading...
Searching...
No Matches
Files | Data Structures | Macros | Functions
Controller Pak Filesystem Routines

Managed Controller Pak interface. More...

Files

file  mempak.c
 Controller Pak Filesystem Routine.
 
file  mempak.h
 Controller Pak Filesystem Routines.
 

Data Structures

struct  entry_structure_t
 Structure representing a save entry on a Controller Pak. More...
 

Macros

#define MEMPAK_BLOCK_SIZE   256
 Size in bytes of a Controller Pak block.
 

Functions

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

Detailed Description

Managed Controller Pak interface.

The Controller Pak system is a subsystem of the Controller Subsystem. Before attempting to read from or write to a Controller Pak, 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 Controller Pak in an organized way compatible with official software, first check that the Controller Pak is valid using validate_mempak. If the Controller Pak is invalid, it will need to be formatted using format_mempak. Once the Controller Pak 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 Controller Pak, 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 on a Controller Pak.

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 Controller Pak.

Function Documentation

◆ read_mempak_sector()

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

Read a sector from a Controller Pak.

This will read a sector from a Controller Pak. Sectors on Controller Paks 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 Controller Pak.

This will write a sector to a Controller Pak. Sectors on Controller Paks 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 Controller Pak is valid.

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

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

◆ get_mempak_free_space()

int get_mempak_free_space ( int  controller)

Return the number of free blocks on a Controller Pak.

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

◆ get_mempak_entry()

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

Read an entry on a Controller Pak.

Given an entry index (0-15), return the entry as found on the Controller Pak. 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 Controller Pak is bad or not present

◆ format_mempak()

int format_mempak ( int  controller)

Format a Controller Pak.

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

Parameters
[in]controllerThe controller (0-3) to format the Controller Pak on
Return values
0if the Controller Pak was formatted successfully
-2if the Controller Pak 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 Controller Pak.

Given a valid Controller Pak 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 Controller Pak 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 Controller Pak entry.

Given a Controller Pak entry structure with a valid region, name and block count, writes the entry and associated data to the Controller Pak. 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 Controller Pak wasn't present or was bad
-3if there was an error writing to the Controller Pak
-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 Controller Pak entry and associated data.

Given a valid Controller Pak 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 Controller Pak
Return values
0if the entry was deleted successfully
-1if the entry was invalid
-2if the Controller Pak was bad or not present