N64FlashcartMenu/src/menu/cpak_handler.c

78 lines
1.8 KiB
C
Raw Normal View History

2024-03-18 01:15:47 +01:00
#include <libdragon.h>
#include <fatfs/ff.h>
#include "../utils/fs.h"
#include "cpak_handler.h"
2024-03-18 02:14:09 +01:00
#define CPAK_BLOCKS 128
#define CPAK_BLOCK_SIZE MEMPAK_BLOCK_SIZE
2024-03-18 01:15:47 +01:00
2024-03-19 17:42:06 +01:00
cpak_err_t cpak_info_load(uint8_t port, cpak_info_t *cpak_info)
{
int res = validate_mempak(port);
if (res != CONTROLLER_PAK_OK) {
return CONTROLLER_PAK_ERR_INVALID;
}
2024-03-19 20:14:04 +01:00
for (int j = 0; j < 16; j++)
{
entry_structure_t entry;
get_mempak_entry(port, j, &entry);
cpak_info->entries[j] = entry;
}
cpak_info->free_space = get_mempak_free_space(port);
2024-03-19 17:42:06 +01:00
return CONTROLLER_PAK_OK;
}
int cpak_clone_contents_to_file(char *path, uint8_t port) {
2024-03-18 02:14:09 +01:00
uint8_t cpak_data[CPAK_BLOCKS * CPAK_BLOCK_SIZE];
2024-03-18 01:15:47 +01:00
int err;
2024-03-18 02:14:09 +01:00
for (int i = 0; i < CPAK_BLOCKS; i++) {
err = read_mempak_sector(port, i, &cpak_data[i * CPAK_BLOCK_SIZE]);
2024-03-18 01:15:47 +01:00
if (err) {
return err;
}
}
FIL fil;
UINT bytes_written;
if (f_open(&fil, strip_sd_prefix(path), FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
2024-03-19 17:42:06 +01:00
return CONTROLLER_PAK_ERR_IO;
2024-03-18 01:15:47 +01:00
}
2024-03-19 17:42:06 +01:00
FRESULT fwrite_err = f_write(&fil, &cpak_data, sizeof(cpak_data), &bytes_written);
2024-03-18 01:15:47 +01:00
f_close(&fil);
2024-03-19 17:42:06 +01:00
if (fwrite_err) {
return fwrite_err;
2024-03-18 01:15:47 +01:00
}
else {
2024-03-19 17:42:06 +01:00
return CONTROLLER_PAK_OK;
2024-03-18 01:15:47 +01:00
}
}
2024-03-18 02:14:09 +01:00
2024-03-19 17:42:06 +01:00
cpak_err_t cpak_overwrite_contents_from_file(char *path, uint8_t port) {
// TODO: implementation.
return CONTROLLER_PAK_ERR_IO;
}
2024-03-18 02:14:09 +01:00
2024-03-20 00:00:32 +01:00
cpak_err_t cpak_attempt_repair(uint8_t port, bool allow_formatting) {
int result = validate_mempak(port);
if (result == CONTROLLER_PAK_OK) {
return CONTROLLER_PAK_OK;
}
// TODO: repair implementation.
// last resort would be to format it completely!
if (result == -3 && allow_formatting) {
result = format_mempak(port);
}
return result;
2024-03-19 17:42:06 +01:00
}